mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
craft-pdf: an object nested past the depth limit is refused where it passes it (cos, content) (#1952)
This commit is contained in:
1 parent
5fd8db24b1
commit
8c3cd06633
3 files changed
+62
-6
No files matched your search
@@ -220,9 +220,10 @@ impl Iterator for Ops<'_> {
|
||||
#[cfg(test)]
|
||||
tests::RESCANNED.with(|c| c.set(c.get() + read));
|
||||
let number = matches!(b, b'+' | b'-' | b'.') || b.is_ascii_digit();
|
||||
if number && read > MAX_RETRIED_TOKEN {
|
||||
// A long unreadable number is skipped whole: retrying from each of its
|
||||
// bytes would read the rest of it again every time.
|
||||
if (number && read > MAX_RETRIED_TOKEN) || (lx.too_deep() && read > 0) {
|
||||
// A long unreadable number, or an operand nested deeper than the
|
||||
// parser follows, is skipped whole: retrying from each of its bytes
|
||||
// would read the rest of it again every time.
|
||||
*skipped += read;
|
||||
} else {
|
||||
// Skip the offending byte and carry on.
|
||||
|
||||
@@ -101,6 +101,29 @@ fn a_lone_minus_is_read_as_zero() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_long_run_of_unclosed_brackets_is_read_once() {
|
||||
// Every `[` opens an array nested inside the one before, so none of them parses: each is
|
||||
// refused once it passes the depth limit, not after reading to the end of the stream.
|
||||
for open in ["[", "<<"] {
|
||||
let n = 100_000;
|
||||
let src = format!("{} 1 0 0 1 0 0 cm", open.repeat(n));
|
||||
RESCANNED.with(|c| c.set(0));
|
||||
let p = parse(src.as_bytes());
|
||||
let rescanned = RESCANNED.with(|c| c.get());
|
||||
assert!(rescanned <= 2 * src.len(), "{rescanned} bytes read again for {open} x {n}");
|
||||
assert!(p.skipped >= n, "{} skipped", p.skipped);
|
||||
assert_eq!(p.ops.last().map(|o| o.op.as_slice()), Some(&b"cm"[..]), "{open}");
|
||||
}
|
||||
// A stray `[` before a whole stream of operators still loses only itself.
|
||||
let p = parse(b"[ 1 0 0 1 0 0 cm 0 0 m");
|
||||
assert_eq!(p.ops.last().map(|o| o.op.as_slice()), Some(&b"m"[..]));
|
||||
// An array nested within the limit is still one operand.
|
||||
let deep = format!("{}{} TJ", "[".repeat(100), "]".repeat(100));
|
||||
let p = parse(deep.as_bytes());
|
||||
assert_eq!((p.ops.len(), p.skipped), (1, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn operators_keep_operands_and_spans() {
|
||||
let src = b"q 1 0 0 1 72 720 cm BT /F1 12 Tf (Hello \\(world\\)) Tj [(A) -120 (B)] TJ ET Q";
|
||||
|
||||
@@ -33,11 +33,13 @@ pub struct Lexer<'a> {
|
||||
pub pos: usize,
|
||||
/// Objects built by the current top-level [`Lexer::object`] call.
|
||||
nodes: usize,
|
||||
/// Whether the current top-level [`Lexer::object`] call reached [`MAX_DEPTH`].
|
||||
too_deep: bool,
|
||||
}
|
||||
|
||||
impl<'a> Lexer<'a> {
|
||||
pub fn new(data: &'a [u8], pos: usize) -> Self {
|
||||
Self { data, pos: pos.min(data.len()), nodes: 0 }
|
||||
Self { data, pos: pos.min(data.len()), nodes: 0, too_deep: false }
|
||||
}
|
||||
|
||||
fn peek(&self) -> Option<u8> {
|
||||
@@ -90,6 +92,7 @@ impl<'a> Lexer<'a> {
|
||||
/// Parse one direct object. References `N G R` are recognised.
|
||||
pub fn object(&mut self) -> Result<Object, CosError> {
|
||||
self.nodes = 0;
|
||||
self.too_deep = false;
|
||||
self.object_depth(0)
|
||||
}
|
||||
|
||||
@@ -97,8 +100,16 @@ impl<'a> Lexer<'a> {
|
||||
self.nodes > MAX_NODES
|
||||
}
|
||||
|
||||
/// Whether the last [`Lexer::object`] call failed because the object nests deeper than the
|
||||
/// parser follows. Such an object is refused as soon as it passes the limit, not skipped byte
|
||||
/// by byte as junk, so reading it costs at most the limit's worth of bytes.
|
||||
pub fn too_deep(&self) -> bool {
|
||||
self.too_deep
|
||||
}
|
||||
|
||||
fn object_depth(&mut self, depth: usize) -> Result<Object, CosError> {
|
||||
if depth > MAX_DEPTH {
|
||||
self.too_deep = true;
|
||||
return Err(self.err("nesting too deep"));
|
||||
}
|
||||
self.nodes = self.nodes.saturating_add(1);
|
||||
@@ -139,7 +150,7 @@ impl<'a> Lexer<'a> {
|
||||
let before = self.pos;
|
||||
match self.object_depth(depth + 1) {
|
||||
Ok(o) => items.push(o),
|
||||
Err(e) if self.over_budget() => return Err(e),
|
||||
Err(e) if self.over_budget() || self.too_deep => return Err(e),
|
||||
// Tolerate junk inside arrays by skipping one byte.
|
||||
Err(_) if self.pos == before => self.pos += 1,
|
||||
Err(e) => return Err(e),
|
||||
@@ -297,10 +308,15 @@ impl<'a> Lexer<'a> {
|
||||
Some(_) => {
|
||||
// Tolerate garbage between entries (seen in damaged files): skip a token.
|
||||
let before = self.pos;
|
||||
let _ = self.object_depth(depth + 1);
|
||||
let skipped = self.object_depth(depth + 1);
|
||||
if self.over_budget() {
|
||||
return Err(self.err("object too large"));
|
||||
}
|
||||
if let Err(e) = skipped
|
||||
&& self.too_deep
|
||||
{
|
||||
return Err(e);
|
||||
}
|
||||
if self.pos == before {
|
||||
self.pos += 1;
|
||||
}
|
||||
@@ -859,6 +875,22 @@ mod tests {
|
||||
assert!(Lexer::new(deep.as_bytes(), 0).object().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_object_nested_past_the_limit_is_refused_where_it_passes_it() {
|
||||
// Not skipped byte by byte as junk to the end of the data: the bytes after it are unread.
|
||||
for (open, width) in [("[", 1), ("<<", 2), ("<< /A [", 7)] {
|
||||
let deep = open.repeat(10_000);
|
||||
let mut lx = Lexer::new(deep.as_bytes(), 0);
|
||||
assert!(lx.object().is_err());
|
||||
assert!(lx.too_deep(), "{open}");
|
||||
assert!(lx.pos <= (MAX_DEPTH + 2) * width, "{open}: read to {}", lx.pos);
|
||||
}
|
||||
// A new object starts with the flag clear.
|
||||
let mut lx = Lexer::new(b"[[1]]", 0);
|
||||
assert!(lx.object().is_ok());
|
||||
assert!(!lx.too_deep());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_object_has_a_node_budget() {
|
||||
// A wide array (from a tiny compressed object stream) must not grow without bound.
|
||||
|
||||
Reference in new issue
Block a user