laminapdf-syntax 0.2.0

PDF syntax in Rust: the object model, lexer and parser, stream filters, decryption, and resource limits for untrusted input.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Resource limits applied while reading untrusted input ([ADR 0003] §3).
//!
//! Every number read from a file is attacker-controlled, so everything that can grow with the
//! input (nesting, decoded bytes, reference chains, …) is bounded by a [`Limits`] value.
//! **Hard** limits fail the call that hits them with [`Error::LimitExceeded`]; **soft** limits
//! drop the item over the limit and carry on with a warning (the reader in `laminapdf-cos`
//! raises it).
//!
//! [ADR 0003]: https://github.com/laudebugs/laminapdf/blob/main/docs/decisions/0003-reader-tolerance-and-limits.md
//! [`Error::LimitExceeded`]: crate::Error::LimitExceeded

use core::fmt;

/// Which hard limit was exceeded (the `limit` of [`Error::LimitExceeded`](crate::Error::LimitExceeded)).
///
/// Soft limits never produce an error, so they have no variant here.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Limit {
    /// [`Limits::max_nesting_depth`].
    NestingDepth,
    /// [`Limits::max_stream_decoded`].
    StreamDecoded,
    /// [`Limits::max_decoded_total`].
    DecodedTotal,
    /// [`Limits::max_filter_chain`].
    FilterChain,
    /// [`Limits::max_resolve_depth`].
    ResolveDepth,
    /// [`Limits::max_page_tree_depth`]. The only reader of the page tree today is
    /// `Document::pages`, one of ADR 0003's tolerant paths: it skips a subtree past this
    /// limit with the `page-tree-node-skipped` warning (an `Error::Strict` in strict mode)
    /// instead of returning this error (ADR 0003 §3). No reader returns it yet:
    /// `laminapdf-model`'s lookup of a page through its `/Parent` chain stops at the limit and
    /// finds no page.
    PageTreeDepth,
    /// [`Limits::max_tree_depth`].
    TreeDepth,
    /// [`Limits::max_string_length`].
    StringLength,
    /// [`Limits::max_content_operators`].
    ContentOperators,
    /// [`Limits::max_xobject_depth`].
    XObjectDepth,
    /// [`Limits::max_image_pixels`].
    ImagePixels,
    /// [`Limits::max_read_window`].
    ReadWindow,
}

impl Limit {
    /// Every hard limit, in declaration order.
    ///
    /// Added in 0.2.0.
    pub const ALL: [Self; 12] = [
        Self::NestingDepth,
        Self::StreamDecoded,
        Self::DecodedTotal,
        Self::FilterChain,
        Self::ResolveDepth,
        Self::PageTreeDepth,
        Self::TreeDepth,
        Self::StringLength,
        Self::ContentOperators,
        Self::XObjectDepth,
        Self::ImagePixels,
        Self::ReadWindow,
    ];

    /// The name of the [`Limits`] field, e.g. `"max_nesting_depth"`. Stable: part of the
    /// public API in Rust, WASM and TypeScript (listed in `fixtures/codes.json`).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::NestingDepth => "max_nesting_depth",
            Self::StreamDecoded => "max_stream_decoded",
            Self::DecodedTotal => "max_decoded_total",
            Self::FilterChain => "max_filter_chain",
            Self::ResolveDepth => "max_resolve_depth",
            Self::PageTreeDepth => "max_page_tree_depth",
            Self::TreeDepth => "max_tree_depth",
            Self::StringLength => "max_string_length",
            Self::ContentOperators => "max_content_operators",
            Self::XObjectDepth => "max_xobject_depth",
            Self::ImagePixels => "max_image_pixels",
            Self::ReadWindow => "max_read_window",
        }
    }

    /// The configured value of this limit in `limits`.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn value_in(self, limits: &Limits) -> u64 {
        // `usize` is at most 64 bits on every supported target, so the casts are lossless.
        match self {
            Self::NestingDepth => limits.max_nesting_depth as u64,
            Self::StreamDecoded => limits.max_stream_decoded as u64,
            Self::DecodedTotal => limits.max_decoded_total as u64,
            Self::FilterChain => limits.max_filter_chain as u64,
            Self::ResolveDepth => limits.max_resolve_depth as u64,
            Self::PageTreeDepth => limits.max_page_tree_depth as u64,
            Self::TreeDepth => limits.max_tree_depth as u64,
            Self::StringLength => limits.max_string_length as u64,
            Self::ContentOperators => limits.max_content_operators,
            Self::XObjectDepth => limits.max_xobject_depth as u64,
            Self::ImagePixels => limits.max_image_pixels,
            Self::ReadWindow => limits.max_read_window as u64,
        }
    }
}

impl fmt::Display for Limit {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.name())
    }
}

/// Resource limits for reading untrusted PDF input ([ADR 0003] §3).
///
/// `Copy`, with private fields: read a limit with its getter and change it with the
/// matching `with_*` builder, starting from [`Limits::default`] (or [`Limits::new`] in a
/// `const` context):
///
/// ```
/// use laminapdf_syntax::Limits;
///
/// const STRICTER: Limits = Limits::new().with_max_nesting_depth(64).with_max_stream_decoded(1 << 20);
/// assert_eq!(STRICTER.max_nesting_depth(), 64);
/// assert_eq!(Limits::default().max_nesting_depth(), 256);
/// ```
///
/// | Field | Kind | Default |
/// | --- | --- | --- |
/// | [`max_nesting_depth`](Self::max_nesting_depth) | hard | 256 |
/// | [`max_stream_decoded`](Self::max_stream_decoded) | hard | 64 MiB |
/// | [`max_decoded_total`](Self::max_decoded_total) | hard | 1 GiB |
/// | [`max_filter_chain`](Self::max_filter_chain) | hard | 8 |
/// | [`max_resolve_depth`](Self::max_resolve_depth) | hard | 32 |
/// | [`max_page_tree_depth`](Self::max_page_tree_depth) | hard | 256 |
/// | [`max_tree_depth`](Self::max_tree_depth) | hard | 64 |
/// | [`max_string_length`](Self::max_string_length) | hard | 16 MiB |
/// | [`max_xref_sections`](Self::max_xref_sections) | soft | 4,096 |
/// | [`max_object_number`](Self::max_object_number) | soft | 8,388,607 |
/// | [`max_warnings`](Self::max_warnings) | soft | 1,000 |
/// | [`max_content_operators`](Self::max_content_operators) | hard | 10,000,000 |
/// | [`max_xobject_depth`](Self::max_xobject_depth) | hard | 32 |
/// | [`max_image_pixels`](Self::max_image_pixels) | hard (reserved) | 2^28 |
/// | [`max_read_window`](Self::max_read_window) | hard | 256 MiB |
///
/// Reserved limits are part of the API now so they can be configured once, but nothing
/// enforces them until the feature they bound lands (image decoding).
///
/// [ADR 0003]: https://github.com/laudebugs/laminapdf/blob/main/docs/decisions/0003-reader-tolerance-and-limits.md
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[expect(
    clippy::struct_field_names,
    reason = "the field names are the ADR's public limit names (`max_*`)"
)]
pub struct Limits {
    max_nesting_depth: usize,
    max_stream_decoded: usize,
    max_decoded_total: usize,
    max_filter_chain: usize,
    max_resolve_depth: usize,
    max_page_tree_depth: usize,
    max_tree_depth: usize,
    max_string_length: usize,
    max_xref_sections: usize,
    max_object_number: u32,
    max_warnings: usize,
    max_content_operators: u64,
    max_xobject_depth: usize,
    max_image_pixels: u64,
    max_read_window: usize,
}

impl Default for Limits {
    fn default() -> Self {
        Self::new()
    }
}

macro_rules! limit_accessors {
    ($( $(#[$doc:meta])* $field:ident, $with:ident: $ty:ty; )*) => {
        $(
            $(#[$doc])*
            #[doc = ""]
            #[doc = "Added in 0.2.0."]
            #[must_use]
            pub const fn $field(&self) -> $ty {
                self.$field
            }

            #[doc = concat!("Sets [`", stringify!($field), "`](Self::", stringify!($field), ").")]
            #[doc = ""]
            #[doc = "Added in 0.2.0."]
            #[must_use]
            pub const fn $with(mut self, value: $ty) -> Self {
                self.$field = value;
                self
            }
        )*
    };
}

impl Limits {
    /// The defaults from ADR 0003 (the same as [`Limits::default`], usable in `const`).
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            max_nesting_depth: 256,
            max_stream_decoded: 64 * 1024 * 1024,
            max_decoded_total: 1024 * 1024 * 1024,
            max_filter_chain: 8,
            max_resolve_depth: 32,
            max_page_tree_depth: 256,
            max_tree_depth: 64,
            max_string_length: 16 * 1024 * 1024,
            max_xref_sections: 4096,
            // ISO 32000-1 Annex C: the largest object number a conforming reader must support.
            max_object_number: 8_388_607,
            max_warnings: 1000,
            max_content_operators: 10_000_000,
            max_xobject_depth: 32,
            max_image_pixels: 1 << 28,
            max_read_window: 256 * 1024 * 1024,
        }
    }

    /// No limits at all, for trusted input only (Lamina PDF's own output in tests).
    ///
    /// Nesting is parsed recursively, so hostile input with unlimited nesting depth can
    /// exhaust the stack: never use this for files from outside.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn unlimited() -> Self {
        Self {
            max_nesting_depth: usize::MAX,
            max_stream_decoded: usize::MAX,
            max_decoded_total: usize::MAX,
            max_filter_chain: usize::MAX,
            max_resolve_depth: usize::MAX,
            max_page_tree_depth: usize::MAX,
            max_tree_depth: usize::MAX,
            max_string_length: usize::MAX,
            max_xref_sections: usize::MAX,
            max_object_number: u32::MAX,
            max_warnings: usize::MAX,
            max_content_operators: u64::MAX,
            max_xobject_depth: usize::MAX,
            max_image_pixels: u64::MAX,
            max_read_window: usize::MAX,
        }
    }

    limit_accessors! {
        /// Hard: array/dictionary nesting depth accepted by the parser, and the graphics states
        /// one content stream may save with `q` in `laminapdf-content` (default 256; ADR 0023 §4).
        max_nesting_depth, with_max_nesting_depth: usize;
        /// Hard: bytes one stream may decode to, per filter stage (default 64 MiB).
        max_stream_decoded, with_max_stream_decoded: usize;
        /// Hard: bytes decoded by one top-level call (`open`, one page's content, one
        /// `resolve` or `decode_stream`), across all streams and filter stages (default 1 GiB).
        max_decoded_total, with_max_decoded_total: usize;
        /// Hard: filters in one stream's `/Filter` chain (default 8).
        max_filter_chain, with_max_filter_chain: usize;
        /// Hard: indirect objects in flight at once while resolving a reference chain (default 32).
        max_resolve_depth, with_max_resolve_depth: usize;
        /// Hard: `/Kids` levels below the page-tree root (default 256). Hit inside the
        /// tolerant page walk, the subtree is skipped with a `page-tree-node-skipped` warning,
        /// as ADR 0003 §3 prescribes for hard limits on tolerant paths.
        max_page_tree_depth, with_max_page_tree_depth: usize;
        /// Hard: depth of name trees, number trees and outlines (default 64; enforced by the
        /// typed views).
        max_tree_depth, with_max_tree_depth: usize;
        /// Hard: bytes in one string or name token (default 16 MiB).
        max_string_length, with_max_string_length: usize;
        /// Soft: `/Prev` hops in the xref chain; past it the stored xref is abandoned and
        /// rebuilt by scanning (default 4,096).
        max_xref_sections, with_max_xref_sections: usize;
        /// Soft: highest object number kept; higher entries are dropped with a warning
        /// (default 8,388,607, ISO 32000-1 Annex C).
        max_object_number, with_max_object_number: u32;
        /// Soft: warnings kept per document; later ones are dropped after one
        /// `warnings-truncated` warning (default 1,000).
        max_warnings, with_max_warnings: usize;
        /// Hard: operators read by one content-stream interpretation (`laminapdf-content`), the
        /// page's and every Form XObject's each time it is drawn (default 10,000,000; ADR 0023).
        max_content_operators, with_max_content_operators: u64;
        /// Hard: Form `XObject`s drawn at once by `laminapdf-content`; patterns and Type 3 glyphs
        /// too once they are interpreted (default 32; ADR 0023).
        max_xobject_depth, with_max_xobject_depth: usize;
        /// Hard, reserved: pixels in one decoded image (default 2^28).
        max_image_pixels, with_max_image_pixels: u64;
        /// Hard: bytes one parse may read from a source without an in-memory slice
        /// (`laminapdf-cos` reads such sources in windows; default 256 MiB). It bounds how far a
        /// window grows for one object (its head, a stream's data, the search for a missing
        /// `endstream`) and the backward walk over an object header found by repair. Past
        /// it, the object fails with `LimitExceeded` where an in-memory parse would have read
        /// on; a tolerant path (repair, `/Length` recovery, the xref entry check) treats the
        /// item as unreadable instead. In-memory input is never limited by it.
        max_read_window, with_max_read_window: usize;
    }
}

/// The decoding budget of one top-level call: [`Limits::max_decoded_total`] bytes, spent by
/// every filter stage of every stream the call decodes.
///
/// Create one per call ([`DecodeBudget::new`]) and pass it to every
/// [`filters::decode`](crate::filters::decode) the call makes.
///
/// Added in 0.2.0.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DecodeBudget {
    remaining: usize,
}

impl DecodeBudget {
    /// A fresh budget of `limits.max_decoded_total()` bytes.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn new(limits: &Limits) -> Self {
        Self {
            remaining: limits.max_decoded_total,
        }
    }

    /// Bytes that may still be decoded.
    ///
    /// Added in 0.2.0.
    #[must_use]
    pub const fn remaining(&self) -> usize {
        self.remaining
    }

    /// Records `bytes` of decoded output.
    pub(crate) const fn spend(&mut self, bytes: usize) {
        self.remaining = self.remaining.saturating_sub(bytes);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn defaults_match_the_adr_table() {
        let l = Limits::default();
        assert_eq!(
            [
                l.max_nesting_depth(),
                l.max_stream_decoded(),
                l.max_decoded_total(),
                l.max_filter_chain(),
                l.max_resolve_depth(),
                l.max_page_tree_depth(),
                l.max_tree_depth(),
                l.max_string_length(),
                l.max_xref_sections(),
                l.max_warnings(),
                l.max_xobject_depth(),
                l.max_read_window(),
            ],
            [
                256,
                64 << 20,
                1 << 30,
                8,
                32,
                256,
                64,
                16 << 20,
                4096,
                1000,
                32,
                256 << 20
            ]
        );
    }

    #[test]
    fn defaults_for_wide_limits_match_the_adr_table() {
        let l = Limits::default();
        assert_eq!(
            (l.max_object_number(), l.max_content_operators(), l.max_image_pixels()),
            (8_388_607, 10_000_000, 1 << 28)
        );
    }

    #[test]
    fn with_builder_changes_only_its_field() {
        let l = Limits::default().with_max_filter_chain(2);
        assert_eq!(
            l,
            Limits {
                max_filter_chain: 2,
                ..Limits::default()
            }
        );
    }

    #[test]
    fn limit_value_in_reads_the_matching_field() {
        let l = Limits::default().with_max_string_length(7);
        assert_eq!(Limit::StringLength.value_in(&l), 7);
    }

    #[test]
    fn limit_names_are_distinct() {
        let mut names: Vec<_> = Limit::ALL.iter().map(|l| l.name()).collect();
        names.sort_unstable();
        names.dedup();
        assert_eq!(names.len(), Limit::ALL.len());
    }
}