PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
fix(cos): a decode budget shared by every stream one walk reads
aiongg733e38bfa718
diff --git a/crates/cos/src/budget.rs b/crates/cos/src/budget.rsnew file mode 100644index 000000000..b89d06001--- /dev/null+++ b/crates/cos/src/budget.rs@@ -0,0 +1,135 @@+//! A shared allowance for operations that decode many streams.++use std::collections::HashSet;++use pdfcraft_filters::FilterError;++use crate::{CosError, Document, ObjRef, Object, Stream};++/// Decoded bytes one page's content walk may produce in all: its `/Contents` streams and every+/// form XObject it enters, each occurrence counted again.+pub const MAX_PAGE_DECODED: usize = 256 << 20;+/// Form XObjects one page's content walk may enter, each entry counted again.+pub const MAX_PAGE_FORMS: usize = 50_000;++/// What one operation may still decode and enter across all the streams it reads.+///+/// [`MAX_DECODED`](crate::MAX_DECODED) bounds one decode. It does not bound a page whose+/// `/Contents` names the same compressed stream thousands of times, or a form drawn thousands of+/// times from other forms: each occurrence would get a fresh allowance. A `DecodeBudget` is+/// shared by every decode of one operation, so the total decoded bytes and form entries stay+/// under fixed limits however the file repeats its references. A decode that runs past the+/// remaining allowance spends all of it, so a bomb referenced many times is inflated once.+#[derive(Debug)]+pub struct DecodeBudget {+ bytes: usize,+ forms: usize,+ seen: HashSet<ObjRef>,+}++impl DecodeBudget {+ /// An allowance of `bytes` decoded bytes and `forms` form entries.+ pub fn new(bytes: usize, forms: usize) -> Self {+ Self { bytes, forms, seen: HashSet::new() }+ }++ /// The allowance for one page: [`MAX_PAGE_DECODED`] bytes and [`MAX_PAGE_FORMS`] forms.+ pub fn page() -> Self {+ Self::new(MAX_PAGE_DECODED, MAX_PAGE_FORMS)+ }++ /// Decoded bytes still allowed.+ pub fn remaining(&self) -> usize {+ self.bytes+ }++ /// Whether a decode was refused for lack of allowance or a form entry for lack of visits.+ pub fn exhausted(&self) -> bool {+ self.bytes == 0 || self.forms == 0+ }++ /// `stream` decoded like [`Stream::decoded`], charged to the allowance. Fails, without+ /// decoding, once the allowance is spent.+ pub fn decode(&mut self, stream: &Stream) -> Result<Vec<u8>, CosError> {+ if self.bytes == 0 && !stream.raw.is_empty() {+ return Err(CosError::Filter(FilterError::LimitExceeded(0).to_string()));+ }+ match stream.decoded_within_typed(self.bytes) {+ Ok(data) => {+ self.bytes = self.bytes.saturating_sub(data.len());+ Ok(data)+ }+ Err(e) => {+ if matches!(e, FilterError::LimitExceeded(_)) {+ self.bytes = 0;+ }+ Err(CosError::Filter(e.to_string()))+ }+ }+ }++ /// Counts one form entry. `false` once [`DecodeBudget::new`]'s `forms` entries are spent:+ /// the caller leaves that form out.+ pub fn enter_form(&mut self) -> bool {+ match self.forms.checked_sub(1) {+ Some(left) => {+ self.forms = left;+ true+ }+ None => false,+ }+ }++ /// `true` the first time `r` is passed to this budget, for callers that need each+ /// referenced stream only once.+ pub fn first_visit(&mut self, r: ObjRef) -> bool {+ self.seen.insert(r)+ }++ /// Each stream `contents` names, decoded against the allowance, in order. `contents` is a+ /// page's `/Contents`: one stream, or an array whose non-stream entries are left out.+ /// A stream that fails to decode, or arrives after the allowance is spent, is an `Err` in its+ /// place, so the caller chooses between skipping it and giving up.+ pub fn decode_contents(&mut self, doc: &Document, contents: &Object) -> Vec<Result<Vec<u8>, CosError>> {+ match &*doc.resolve(contents) {+ Object::Stream(s) => vec![self.decode(s)],+ Object::Array(list) => list+ .iter()+ .filter_map(|entry| match &*doc.resolve(entry) {+ Object::Stream(s) => Some(self.decode(s)),+ _ => None,+ })+ .collect(),+ _ => Vec::new(),+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::Dict;++ #[test]+ fn repeated_references_share_one_allowance() {+ let mut doc = Document::new_empty();+ let bomb = doc.add(Object::Stream(Stream::flate(Dict::new(), &vec![b' '; 1 << 20])));+ let contents = Object::Array(vec![Object::Ref(bomb); 64]);+ let mut budget = DecodeBudget::new(10 << 20, 1);+ let decoded = budget.decode_contents(&doc, &contents);+ assert_eq!(decoded.len(), 64);+ assert_eq!(decoded.iter().filter(|d| d.is_ok()).count(), 10);+ let total: usize = decoded.iter().flatten().map(Vec::len).sum();+ assert_eq!(total, 10 << 20);+ assert!(budget.exhausted());+ // A decode past the allowance spends it, so a bomb is inflated at most once more.+ let mut budget = DecodeBudget::new((1 << 20) - 1, 1);+ assert!(budget.decode_contents(&doc, &contents).iter().all(Result::is_err));+ assert_eq!(budget.remaining(), 0);++ assert!(budget.enter_form());+ assert!(!budget.enter_form());+ assert!(budget.first_visit(bomb));+ assert!(!budget.first_visit(bomb));+ }+}diff --git a/crates/cos/src/lib.rs b/crates/cos/src/lib.rsindex 152daed2d..f3be66b9a 100644--- a/crates/cos/src/lib.rs+++ b/crates/cos/src/lib.rs@@ -13,6 +13,7 @@#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]+mod budget;mod bytes;mod document;mod object;@@ -21,6 +22,7 @@ mod parser;mod security;mod writer;+pub use budget::{DecodeBudget, MAX_PAGE_DECODED, MAX_PAGE_FORMS};pub use bytes::Bytes;pub use document::{Document, ReadState, Revision, SourceIdentity, XrefEntry};pub use object::{Dict, MAX_DECODED, Name, ObjRef, Object, PdfString, Stream};diff --git a/crates/cos/src/object.rs b/crates/cos/src/object.rsindex 438b8d5f3..fe33388be 100644--- a/crates/cos/src/object.rs+++ b/crates/cos/src/object.rs@@ -214,11 +214,21 @@ impl Stream {/// Like [`Stream::decoded`], for a stream whose decoded size has a tighter bound than/// [`MAX_DECODED`]: decoding past `max` bytes is an error.pub fn decoded_within(&self, max: usize) -> Result<Vec<u8>, CosError> {+ self.decoded_within_typed(max).map_err(|e| CosError::Filter(e.to_string()))+ }++ /// [`Stream::decoded_within`] with the filter error kept typed, so a caller can tell a+ /// spent limit from corrupt data.+ pub(crate) fn decoded_within_typed(&self, max: usize) -> Result<Vec<u8>, pdfcraft_filters::FilterError> {+ let max = max.min(MAX_DECODED);let chain = self.filters();if chain.is_empty() {- return self.raw_within(max);+ if self.raw.len() > max {+ return Err(pdfcraft_filters::FilterError::LimitExceeded(max));+ }+ return Ok(self.raw.to_vec());}- pdfcraft_filters::decode_tolerant(&chain, &self.raw, max.min(MAX_DECODED)).map(|(v, _)| v).map_err(|e| CosError::Filter(e.to_string()))+ pdfcraft_filters::decode_tolerant(&chain, &self.raw, max).map(|(v, _)| v)}/// Decoded data, bounded by [`MAX_DECODED`]; any corruption is an error.