mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-15 23:32:37 +00:00
fix(content): an inline image only the renderer reads counts as ambiguous (#5874)
This commit is contained in:
1 parent
e78102c907
commit
743a772ee9
3 files changed
+29
-6
No files matched your search
@@ -67,8 +67,9 @@ pub struct Parsed {
|
||||
/// Bytes that could not be read as operands or operators.
|
||||
pub skipped: usize,
|
||||
/// Inline images the renderer's tokenizer reads no image for (it stops, or finds no `EI` it
|
||||
/// accepts). Their end here is a guess, so the operators after them may not be the ones the
|
||||
/// page draws.
|
||||
/// accepts), so their end here is a guess and the operators after them may not be the ones
|
||||
/// the page draws; and images it reads whose parameters this parser can't, which are missing
|
||||
/// from `ops` although the page draws them.
|
||||
pub ambiguous_images: usize,
|
||||
/// For [`Pieces::parse`]: per piece, whether a comment runs into it from the piece before
|
||||
/// and whether one runs out of it into the next. Only boundaries are recorded, so the size
|
||||
@@ -139,8 +140,8 @@ impl<'a> Ops<'a> {
|
||||
self.skipped
|
||||
}
|
||||
|
||||
/// Inline images read so far whose end is this parser's guess (as in
|
||||
/// [`Parsed::ambiguous_images`]).
|
||||
/// Inline images read so far that are ambiguous (as in [`Parsed::ambiguous_images`]): either
|
||||
/// their end is this parser's guess, or the renderer reads them and this parser cannot.
|
||||
pub fn ambiguous_images(&self) -> usize {
|
||||
self.ambiguous_images
|
||||
}
|
||||
@@ -291,7 +292,8 @@ struct Inline {
|
||||
image: Option<(Dict, Vec<u8>)>,
|
||||
/// The position after its `EI` (or the end of the stream).
|
||||
end: usize,
|
||||
/// The renderer's tokenizer reads no image here, so `end` is this parser's guess.
|
||||
/// The renderer's tokenizer reads no image here, so `end` is this parser's guess; or it reads
|
||||
/// one whose parameters this parser can't, so `image` is `None` for an image the page draws.
|
||||
ambiguous: bool,
|
||||
}
|
||||
|
||||
@@ -339,7 +341,9 @@ fn inline_image(data: &[u8], bi: usize, pos: usize) -> Inline {
|
||||
{
|
||||
samples = rest;
|
||||
}
|
||||
return Inline { image: readable.then(|| (dict, samples.to_vec())), end, ambiguous: false };
|
||||
// The renderer draws an image whose parameters this parser can't read: it is not
|
||||
// represented here, so a redaction can't inspect or remove it and must not trust the page.
|
||||
return Inline { image: readable.then(|| (dict, samples.to_vec())), end, ambiguous: !readable };
|
||||
}
|
||||
if !readable {
|
||||
// Without an `EI` after it, the image runs to the end of the stream.
|
||||
|
||||
@@ -209,6 +209,16 @@ fn filtered_unknown_and_damaged_inline_images_end_where_the_renderer_ends_them()
|
||||
}
|
||||
}
|
||||
|
||||
/// The renderer reads an image whose parameters this parser can't (a stray operand before `ID`):
|
||||
/// the page draws an image `ops` doesn't hold, so it is counted as ambiguous rather than dropped
|
||||
/// silently.
|
||||
#[test]
|
||||
fn an_inline_image_read_by_the_renderer_but_not_here_is_ambiguous() {
|
||||
let parsed = parse(b"q BI /W 1 /H 1 /BPC 8 /CS /G 7 ID \x80 EI Q");
|
||||
assert_eq!(parsed.ops.iter().map(|op| op.op.as_slice()).collect::<Vec<_>>(), [b"q".as_slice(), b"Q"]);
|
||||
assert_eq!(parsed.ambiguous_images, 1);
|
||||
}
|
||||
|
||||
/// A zlib stream (one stored block) for 64 DeviceGray samples whose bytes hold " EI\n" at offset 41,
|
||||
/// the way a stored deflate block copies incompressible samples verbatim (#3579).
|
||||
const FLATE_WITH_EI: &str = "789c014000bfffed746159d50788f7ea604984cd08e00b4c8983207f56fe82c77ef8dfb8b9a3a586382045490a3120\
|
||||
|
||||
@@ -32,3 +32,12 @@ fn a_page_whose_inline_image_the_renderer_cannot_end_is_refused() {
|
||||
mark(&mut doc, 0, &[[5.0, 95.0, 45.0, 110.0]], "");
|
||||
assert_eq!(apply(&mut doc, None), Err(RedactError::Unreadable(1)));
|
||||
}
|
||||
|
||||
/// The renderer draws an image whose parameters the content parser can't read (a stray operand
|
||||
/// before `ID`): it would stay under the mark unseen, so the page is refused.
|
||||
#[test]
|
||||
fn a_page_whose_inline_image_only_the_renderer_reads_is_refused() {
|
||||
let mut doc = one_page(b"q 10 0 0 10 50 50 cm BI /W 1 /H 1 /CS /G /BPC 8 7 ID \x80 EI Q", "", vec![]);
|
||||
mark(&mut doc, 0, &[[45.0, 45.0, 65.0, 65.0]], "");
|
||||
assert_eq!(apply(&mut doc, None), Err(RedactError::Unreadable(1)));
|
||||
}
|
||||
Reference in new issue
Block a user