Skip to content

Commit

Permalink
Add 'text' option to pdf_apply_redactions().
Browse files Browse the repository at this point in the history
This allows for the default (current) operation of removing
text that intersects with redaction regions, as well as the
new option of leaving text alone.

This is obviously insecure, but given the increasing use of
redaction operations as a mechanism for removing arbitrary
content, this should be useful.
  • Loading branch information
robinwatts authored and sebras committed Apr 3, 2024
1 parent 260e736 commit ef1771c
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 16 deletions.
12 changes: 12 additions & 0 deletions include/mupdf/pdf/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,23 @@ enum {
PDF_REDACT_LINE_ART_REMOVE_IF_TOUCHED
};

enum {
/* Remove any text that overlaps with the redaction region,
* however slightly. This is the default option, and is the
* correct option for secure behaviour. */
PDF_REDACT_TEXT_REMOVE,
/* Do not remove any text at all as part of this redaction
* operation. Using this option is INSECURE! Use at your own
* risk. */
PDF_REDACT_TEXT_NONE
};

typedef struct
{
int black_boxes;
int image_method;
int line_art;
int text;
} pdf_redact_options;

int pdf_redact_page(fz_context *ctx, pdf_document *doc, pdf_page *page, pdf_redact_options *opts);
Expand Down
4 changes: 2 additions & 2 deletions platform/java/jni/pdfannotation.c
Original file line number Diff line number Diff line change
Expand Up @@ -1583,11 +1583,11 @@ FUN(PDFAnnotation_getHiddenForEditing)(JNIEnv *env, jobject self)
}

JNIEXPORT jboolean JNICALL
FUN(PDFAnnotation_applyRedaction)(JNIEnv *env, jobject self, jboolean blackBoxes, jint imageMethod, jint lineArt)
FUN(PDFAnnotation_applyRedaction)(JNIEnv *env, jobject self, jboolean blackBoxes, jint imageMethod, jint lineArt, jint text)
{
fz_context *ctx = get_context(env);
pdf_annot *annot = from_PDFAnnotation_safe(env, self);
pdf_redact_options opts = { blackBoxes, imageMethod, lineArt };
pdf_redact_options opts = { blackBoxes, imageMethod, lineArt, text };
jboolean redacted = JNI_FALSE;

if (!ctx || !annot) return JNI_FALSE;
Expand Down
4 changes: 2 additions & 2 deletions platform/java/jni/pdfpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ FUN(PDFPage_update)(JNIEnv *env, jobject self)
}

JNIEXPORT jboolean JNICALL
FUN(PDFPage_applyRedactions)(JNIEnv *env, jobject self, jboolean blackBoxes, jint imageMethod, jint lineArt)
FUN(PDFPage_applyRedactions)(JNIEnv *env, jobject self, jboolean blackBoxes, jint imageMethod, jint lineArt, jint text)
{
fz_context *ctx = get_context(env);
pdf_page *page = from_PDFPage(env, self);
jboolean redacted = JNI_FALSE;
pdf_redact_options opts = { blackBoxes, imageMethod, lineArt };
pdf_redact_options opts = { blackBoxes, imageMethod, lineArt, text };

if (!ctx || !page) return JNI_FALSE;

Expand Down
3 changes: 3 additions & 0 deletions platform/java/mupdf_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,9 @@ static int check_enums()
valid &= com_artifex_mupdf_fitz_PDFPage_REDACT_LINE_ART_REMOVE_IF_TOUCHED == PDF_REDACT_LINE_ART_REMOVE_IF_TOUCHED;
valid &= com_artifex_mupdf_fitz_PDFPage_REDACT_LINE_ART_REMOVE_IF_COVERED == PDF_REDACT_LINE_ART_REMOVE_IF_COVERED;

valid &= com_artifex_mupdf_fitz_PDFPage_REDACT_TEXT_REMOVE == PDF_REDACT_TEXT_REMOVE;
valid &= com_artifex_mupdf_fitz_PDFPage_REDACT_TEXT_NONE == PDF_REDACT_TEXT_NONE;

return valid ? 1 : 0;
}

Expand Down
12 changes: 8 additions & 4 deletions platform/java/mupdf_native.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions platform/java/src/com/artifex/mupdf/fitz/PDFAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,20 @@ public void setAppearance(Image image) {

public boolean applyRedaction(boolean blackBoxes)
{
return applyRedaction(blackBoxes, PDFPage.REDACT_IMAGE_PIXELS, PDFPage.REDACT_LINE_ART_NONE);
return applyRedaction(blackBoxes, PDFPage.REDACT_IMAGE_PIXELS, PDFPage.REDACT_LINE_ART_NONE, PDFPage.REDACT_TEXT_REMOVE);
}

public boolean applyRedaction(boolean blackBoxes, int imageMethod)
{
return applyRedaction(blackBoxes, imageMethod, PDFPage.REDACT_LINE_ART_NONE);
return applyRedaction(blackBoxes, imageMethod, PDFPage.REDACT_LINE_ART_NONE, PDFPage.REDACT_TEXT_REMOVE);
}

public native boolean applyRedaction(boolean blackBoxes, int imageMethod, int lineArt);
public boolean applyRedaction(boolean blackBoxes, int imageMethod, int lineArt)
{
return applyRedaction(blackBoxes, imageMethod, lineArt, PDFPage.REDACT_TEXT_REMOVE);
}

public native boolean applyRedaction(boolean blackBoxes, int imageMethod, int lineArt, int text);

/** @deprecated use getBorderWidth instead. */
public float getBorder() { return getBorderWidth(); }
Expand Down
13 changes: 10 additions & 3 deletions platform/java/src/com/artifex/mupdf/fitz/PDFPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,21 @@ public class PDFPage extends Page
public static final int REDACT_LINE_ART_REMOVE_IF_COVERED = 1;
public static final int REDACT_LINE_ART_REMOVE_IF_TOUCHED = 2;

public native boolean applyRedactions(boolean blackBoxes, int imageMethod, int lineArt);
public static final int REDACT_TEXT_REMOVE = 0;
public static final int REDACT_TEXT_NONE = 1;

public native boolean applyRedactions(boolean blackBoxes, int imageMethod, int lineArt, int text);

public boolean applyRedactions() {
return applyRedactions(true, REDACT_IMAGE_PIXELS, REDACT_LINE_ART_NONE);
return applyRedactions(true, REDACT_IMAGE_PIXELS, REDACT_LINE_ART_NONE, REDACT_TEXT_REMOVE);
}

public boolean applyRedactions(boolean blackBoxes, int imageMethod) {
return applyRedactions(blackBoxes, imageMethod, REDACT_LINE_ART_NONE);
return applyRedactions(blackBoxes, imageMethod, REDACT_LINE_ART_NONE, REDACT_TEXT_REMOVE);
}

public boolean applyRedactions(boolean blackBoxes, int imageMethod, int lineArt) {
return applyRedactions(blackBoxes, imageMethod, lineArt, REDACT_TEXT_REMOVE);
}

public native boolean update();
Expand Down
6 changes: 5 additions & 1 deletion source/pdf/pdf-clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ struct redact_filter_state {
pdf_page *page;
pdf_annot *target; // NULL if all
int line_art;
int text;
};

static void
Expand Down Expand Up @@ -989,6 +990,7 @@ void init_redact_filter(fz_context *ctx, pdf_redact_options *redact_opts, struct
int black_boxes = redact_opts ? redact_opts->black_boxes : 0;
int image_method = redact_opts ? redact_opts->image_method : PDF_REDACT_IMAGE_PIXELS;
int line_art = redact_opts ? redact_opts->line_art : PDF_REDACT_LINE_ART_NONE;
int text = redact_opts ? redact_opts->text : PDF_REDACT_TEXT_REMOVE;

memset(&red->filter_opts, 0, sizeof red->filter_opts);
memset(&red->sanitize_opts, 0, sizeof red->sanitize_opts);
Expand All @@ -1001,9 +1003,11 @@ void init_redact_filter(fz_context *ctx, pdf_redact_options *redact_opts, struct
if (black_boxes)
red->filter_opts.complete = pdf_redact_end_page;
red->line_art = line_art;
red->text = text;

red->sanitize_opts.opaque = red;
red->sanitize_opts.text_filter = pdf_redact_text_filter;
if (text == PDF_REDACT_TEXT_REMOVE)
red->sanitize_opts.text_filter = pdf_redact_text_filter;
if (image_method == PDF_REDACT_IMAGE_PIXELS)
red->sanitize_opts.image_filter = pdf_redact_image_filter_pixels;
if (image_method == PDF_REDACT_IMAGE_REMOVE)
Expand Down
3 changes: 2 additions & 1 deletion source/tools/murun.c
Original file line number Diff line number Diff line change
Expand Up @@ -7899,6 +7899,7 @@ static void ffi_PDFPage_applyRedactions(js_State *J)
if (js_isdefined(J, 1)) opts.black_boxes = js_toboolean(J, 1);
if (js_isdefined(J, 2)) opts.image_method = js_tointeger(J, 2);
if (js_isdefined(J, 3)) opts.line_art = js_tointeger(J, 3);
if (js_isdefined(J, 4)) opts.text = js_tointeger(J, 4);
fz_try(ctx)
pdf_redact_page(ctx, page->doc, page, &opts);
fz_catch(ctx)
Expand Down Expand Up @@ -10444,7 +10445,7 @@ int murun_main(int argc, char **argv)
jsB_propfun(J, "PDFPage.createAnnotation", ffi_PDFPage_createAnnotation, 1);
jsB_propfun(J, "PDFPage.deleteAnnotation", ffi_PDFPage_deleteAnnotation, 1);
jsB_propfun(J, "PDFPage.update", ffi_PDFPage_update, 0);
jsB_propfun(J, "PDFPage.applyRedactions", ffi_PDFPage_applyRedactions, 3);
jsB_propfun(J, "PDFPage.applyRedactions", ffi_PDFPage_applyRedactions, 4);
jsB_propfun(J, "PDFPage.process", ffi_PDFPage_process, 1);
jsB_propfun(J, "PDFPage.toPixmap", ffi_PDFPage_toPixmap, 6);
jsB_propfun(J, "PDFPage.getTransform", ffi_PDFPage_getTransform, 0);
Expand Down

0 comments on commit ef1771c

Please sign in to comment.