From e79f3006c3543bc11d6c7f5f09d562c5951c29ab Mon Sep 17 00:00:00 2001 From: Bob Singor Date: Sun, 29 Nov 2026 22:45:45 +0200 Subject: [PATCH] =?UTF-8?q?Apply=20redactions=20through=20the=20page?= =?UTF-8?q?=E2=80=99s=20active=20layer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/embedpdf/fork-ledger.md | 4 +- fpdfsdk/epdf_redact.cpp | 3 +- fpdfsdk/epdf_redact_embeddertest.cpp | 46 ++++++++++++++++++++ testing/embedpdf_layer_mode_expectations.txt | 4 -- testing/embedpdf_layer_mode_writers.txt | 2 + 5 files changed, 53 insertions(+), 6 deletions(-) diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index 9a9868fd7d..4ff169a0a7 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -78,13 +78,15 @@ upstream accessor to the same state needs the same seam. ### view-scope **Rule.** A base document resolves references through the current `CPDF_DocumentViewScope`: the effective view (the base as a layer sees it) or the frozen base. Read entry points open a scope. +`EPDFPage_ApplyRedactions` holds the page's view while selecting marks and applying their regions. **Why.** One frozen base serves many layers, and a reference must resolve to the version this layer sees. **Where.** `cpdf_document_view_scope.{h,cpp}`, `CPDF_BaseDocument`, read entry points in `fpdfsdk/`. Every function in `epdf_annot.cpp` opens one before it walks the document's objects. **Guarded by.** `cpdf_base_document_unittest.cpp`, `cpdf_layer_document_unittest.cpp`, `EPDFAnnotEmbedderTest.ReadsResolveThroughTheLayer`, -`EPDFPageLabelsEmbedderTest.WritesInALayerTransaction` (`FPDF_GetPageLabel`). +`EPDFPageLabelsEmbedderTest.WritesInALayerTransaction` (`FPDF_GetPageLabel`), +`EPDFRedactEmbedderTest.MovedMarkRedactsItsNewPositionInTheSavedLayer`. **When upstream touches this.** A new entry point that walks objects beyond its own page opens a scope, whether it reads or writes: `FPDFPage_Flatten()` writes one page but reads every page's `/Annots`. Without a scope, a base page with layer `/Annots` crashes the view detector. diff --git a/fpdfsdk/epdf_redact.cpp b/fpdfsdk/epdf_redact.cpp index b8e00de732..f1f3a20e8e 100644 --- a/fpdfsdk/epdf_redact.cpp +++ b/fpdfsdk/epdf_redact.cpp @@ -499,7 +499,8 @@ EPDFPage_ApplyRedactions(FPDF_PAGE page, *out_removed_annot_count = 0; } - CPDF_Page* pPage = CPDFPageFromFPDFPage(page); + ScopedFPDFPageView page_view(page); + CPDF_Page* pPage = page_view.Get(); if (!pPage || count < 0 || (count > 0 && !marks) || (marks && count == 0)) { return false; } diff --git a/fpdfsdk/epdf_redact_embeddertest.cpp b/fpdfsdk/epdf_redact_embeddertest.cpp index c01f50ae18..3ee2908094 100644 --- a/fpdfsdk/epdf_redact_embeddertest.cpp +++ b/fpdfsdk/epdf_redact_embeddertest.cpp @@ -1766,6 +1766,52 @@ TEST_F(EPDFRedactEmbedderTest, AnImageThatCannotBeDecodedFailsTheApply) { EXPECT_FALSE(EPDFPage_ApplyRedactions(page.get(), nullptr, 0, nullptr)); } +TEST_F(EPDFRedactEmbedderTest, MovedMarkRedactsItsNewPositionInTheSavedLayer) { + const std::string input = PdfOf({ + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 120] /Contents 4 0 R " + "/Resources << /Font << /F1 5 0 R >> >> /Annots [6 0 R] >>", + StreamObject("", + "BT /F1 20 Tf 10 80 Td (KEEP) Tj " + "0 -50 Td (REMOVE) Tj ET"), + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", + "<< /Type /Annot /Subtype /Redact /Rect [5 75 150 105] >>", + }); + for (bool apply_all : {false, true}) { + SCOPED_TRACE(apply_all ? "all marks" : "selected mark"); + OpenedPdf opened = OpenPdf(input, /*layer=*/true); + ASSERT_TRUE(opened.doc); + ScopedFPDFPage page(FPDF_LoadPage(opened.doc.get(), 0)); + ASSERT_TRUE(page); + ASSERT_EQ(L"KEEP\r\nREMOVE", ReadText(page.get())); + ScopedFPDFAnnotation mark(FPDFPage_GetAnnot(page.get(), 0)); + ASSERT_TRUE(mark); + + ASSERT_TRUE(EPDFLayer_BeginTransaction(opened.doc.get())); + const FS_RECTF moved{5, 55, 150, 25}; + ASSERT_TRUE(EPDFAnnot_SetRect(mark.get(), &moved)); + ASSERT_TRUE(EPDFLayer_CommitTransaction(opened.doc.get())); + + ASSERT_TRUE(EPDFLayer_BeginTransaction(opened.doc.get())); + const FPDF_ANNOTATION selected[] = {mark.get()}; + ASSERT_TRUE(EPDFPage_ApplyRedactions(page.get(), + apply_all ? nullptr : selected, + apply_all ? 0 : 1, nullptr)); + ASSERT_TRUE(FPDFPage_GenerateContent(page.get())); + ASSERT_TRUE(EPDFLayer_CommitTransaction(opened.doc.get())); + + ClearString(); + ASSERT_TRUE(FPDF_SaveAsCopy(opened.doc.get(), this, FPDF_NO_INCREMENTAL)); + ScopedSavedDoc saved_doc = OpenScopedSavedDocument(); + ASSERT_TRUE(saved_doc); + ScopedSavedPage saved_page = LoadScopedSavedPage(0); + ASSERT_TRUE(saved_page); + EXPECT_EQ(L"KEEP", ReadText(saved_page.get())); + ExpectNoSentinel(CPDFDocumentFromFPDFDocument(saved_doc.get()), "REMOVE"); + } +} + // Marks passed to EPDFPage_ApplyRedactions are applied, in any order; the // page's other marks stay. TEST_F(EPDFRedactEmbedderTest, ApplyingSomeMarksLeavesTheOthers) { diff --git a/testing/embedpdf_layer_mode_expectations.txt b/testing/embedpdf_layer_mode_expectations.txt index 11f322a46c..657bb102d8 100644 --- a/testing/embedpdf_layer_mode_expectations.txt +++ b/testing/embedpdf_layer_mode_expectations.txt @@ -45,7 +45,3 @@ EPDFLayerEmbedderTest.LayerDiagnosticsRejectPlainDocuments The test checks a pla FPDFViewEmbedderTest.UnSupportedOperationsLoadCustomDocument EPDF_LoadBaseDocument doesn't call the unsupported-feature handler; the engine doesn't set one. FPDFViewEmbedderTest.DocumentHasValidCrossReferenceTable FPDF_DocumentHasValidCrossReferenceTable reports false on a layer; the engine doesn't call it. FPDFViewEmbedderTest.LoadDocumentWithEmptyXRefConsistently FPDF_DocumentHasValidCrossReferenceTable reports false on a layer; the engine doesn't call it. - -# --- Fork bugs found by layer mode. -EPDFRedactEmbedderTest.FullyRemovedSpanLeavesNoPropertyOrStructureResidue BUG (rule view-scope): EPDFPage_ApplyRedactions reads references without a CPDF_DocumentViewScope (ApplyAllRedactionsCore), so it can read the base's old version of an object the layer changed. -EPDFAnnotEmbedderTest.ApplyRedactionFallsBackToRectForNonFiniteQuad BUG (rule view-scope): applying a redaction (EPDFPage_ApplyRedactions) reads references without a CPDF_DocumentViewScope. diff --git a/testing/embedpdf_layer_mode_writers.txt b/testing/embedpdf_layer_mode_writers.txt index 474b5cf50b..d9deb329f2 100644 --- a/testing/embedpdf_layer_mode_writers.txt +++ b/testing/embedpdf_layer_mode_writers.txt @@ -66,6 +66,7 @@ EPDFAnnotEmbedderTest.ApplyRedactionCascadesPopupRemoval 5 EPDFAnnotEmbedderTest.ApplyRedactionClonesSharedFormBeforeVectorEdit 15 EPDFAnnotEmbedderTest.ApplyRedactionCountsIntersectingAnnotation 5 EPDFAnnotEmbedderTest.ApplyRedactionDoesNotRemoveTouchOnlyAnnotation 5 +EPDFAnnotEmbedderTest.ApplyRedactionFallsBackToRectForNonFiniteQuad 9 EPDFAnnotEmbedderTest.ApplyRedactionHonorsOverlayTextAlignment 8 EPDFAnnotEmbedderTest.ApplyRedactionLabelWithoutDaFontUsesHelvetica 8 EPDFAnnotEmbedderTest.ApplyRedactionOnNeverParsedPageRemovesText 9 @@ -330,6 +331,7 @@ EPDFPpoEmbedderTest.RenamesAFontWithOtherWidths 20 EPDFPpoEmbedderTest.RenamesTheFontOfAnAppearanceWithAComment 20 EPDFPpoEmbedderTest.SharesAFontDefinedTheSameWay 19 EPDFRedactEmbedderTest.DirectReplacementTextIsRemovedFromSiblingObjects 3 +EPDFRedactEmbedderTest.FullyRemovedSpanLeavesNoPropertyOrStructureResidue 8 EPDFRedactEmbedderTest.InheritedPropertiesLeaveNoUnusedOriginalOnPage 5 EPDFRedactEmbedderTest.NestedFormsUseTheRenderersTransformOrder 7 EPDFRedactEmbedderTest.RedactingAFlattenedStampLeavesAStampSharingItsDrawingAlone 22