malmal 0.2.0

Upload scanner for images, SVG, PDF and PostScript files
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
//! Checks that take more than a keyword: what a string says, what the numbers
//! in a dictionary add up to, and where in the file an object or a signature
//! of another format stands. They run once every object is known, object
//! streams included, and once the document has been opened if it is
//! encrypted. An object that will read the same then as when it is walked
//! is looked at during that walk, while its value is at hand: see [`Early`].

use std::ops::Range;

use memchr::memmem;

use super::crypt::StreamKind;
use super::filters::DecodeError;
use super::parser::{Dict, Name, Value, is_regular, is_whitespace};
use super::{Document, Object, Output, content, postscript};
use crate::limits::{Budget, LimitExceeded};
use crate::report::{Finding, Severity};

/// A page side longer than this, in points and after `/UserUnit`, is no
/// page: 400 inches, twice what the format allows without a user unit.
const MAX_PAGE_SIDE: f64 = 28_800.0;
/// The format's own limit on the length of a name.
const MAX_NAME: usize = 255;
/// A calculator function's program is a few hundred bytes.
const MAX_FUNCTION: usize = 64 << 10;

/// Annotation flags.
const HIDDEN: usize = 1 << 1;
const PRINT: usize = 1 << 2;
const NO_VIEW: usize = 1 << 5;

const PAGE_BOXES: [&[u8]; 5] = [b"MediaBox", b"CropBox", b"BleedBox", b"TrimBox", b"ArtBox"];

/// The operators a Type 4 function may use, sorted.
const CALCULATOR: [&[u8]; 42] = [
    b"abs",
    b"add",
    b"and",
    b"atan",
    b"bitshift",
    b"ceiling",
    b"copy",
    b"cos",
    b"cvi",
    b"cvr",
    b"div",
    b"dup",
    b"eq",
    b"exch",
    b"exp",
    b"false",
    b"floor",
    b"ge",
    b"gt",
    b"idiv",
    b"if",
    b"ifelse",
    b"index",
    b"le",
    b"ln",
    b"log",
    b"lt",
    b"mod",
    b"mul",
    b"ne",
    b"neg",
    b"not",
    b"or",
    b"pop",
    b"roll",
    b"round",
    b"sin",
    b"sqrt",
    b"sub",
    b"true",
    b"truncate",
    b"xor",
];

pub(super) struct Pass<'p, 'a> {
    pub doc: &'p Document<'a>,
    /// Strings and streams are plaintext: the document is not encrypted, or
    /// it has been opened.
    pub readable: bool,
    pub max_pixels: u64,
}

/// What the objects looked at ahead of their turn hold, to be reported when
/// their turn comes: the findings in the order they were made, and the
/// object each came from.
#[derive(Default)]
pub(super) struct Early {
    findings: Vec<Finding>,
    objects: Vec<usize>,
}

impl Early {
    /// Look at the object at `index`, whose value is `value`, in a document
    /// that can be read.
    pub fn visit(&mut self, doc: &Document<'_>, index: usize, value: &Value, max_pixels: u64) {
        let pass = Pass {
            doc,
            readable: true,
            max_pixels,
        };
        let mut out = Output {
            findings: &mut self.findings,
            embedded: Vec::new(),
            contents: Vec::new(),
            xfa: Vec::new(),
            file_streams: Vec::new(),
            encrypt_sources: Vec::new(),
        };
        let object = &doc.objects[index];
        pass.visit(object, value, object.is_stream(), &mut out);
        self.objects.resize(self.findings.len(), index);
    }
}

/// Look at every object, or at its findings in `early` for the objects from
/// `seen_from` on, which have been looked at.
pub(super) fn inspect(
    pass: &Pass<'_, '_>,
    early: Early,
    seen_from: usize,
    budget: &mut Budget,
    out: &mut Output<'_>,
) {
    let doc = pass.doc;
    let readable = pass.readable;
    let mut early = early.objects.into_iter().zip(early.findings).peekable();
    let mut streams = Vec::new();
    for (count, object) in doc.objects.iter().enumerate() {
        if count % 256 == 0
            && let Err(limit) = budget.check_time()
        {
            return out.limit(limit);
        }
        let value = if count < seen_from {
            let value = doc.value(object);
            pass.visit(object, &value, object.is_stream(), out);
            value
        } else {
            while let Some((_, finding)) = early.next_if(|(index, _)| *index <= count) {
                out.add(finding);
            }
            if !object.is_stream() {
                continue;
            }
            doc.value(object)
        };
        let (Some(start), Some(dict)) = (object.stream_start(), value.as_dict()) else {
            continue;
        };
        streams.extend(
            doc.stream_data(object, dict)
                .map(|data| start..start + data.len()),
        );
        if readable && let Err(limit) = pass.stream(object, dict, budget, out) {
            return out.limit(limit);
        }
    }

    let streams = merged(streams);
    conflicting_objects(doc, &streams, out);
    foreign_signatures(doc.data, &streams, out);
}

impl Pass<'_, '_> {
    fn visit(&self, object: &Object, value: &Value, stream_dict: bool, out: &mut Output<'_>) {
        match value {
            Value::Dict(dict) => {
                self.dict(object, dict, stream_dict, out);
                for (key, item) in &dict.0 {
                    check_length(key, object.offset, out);
                    self.visit(object, item, false, out);
                }
            }
            Value::Array(items) => {
                for item in items {
                    self.visit(object, item, false, out);
                }
            }
            Value::Name(name) => check_length(name, object.offset, out),
            _ => {}
        }
    }

    /// A string as the document's author wrote it, where it can be read.
    fn string(&self, object: &Object, value: &Value) -> Option<Vec<u8>> {
        if !self.readable {
            return None;
        }
        let raw = self.doc.resolve(Some(value)).as_string()?;
        // Strings in an object stream are covered by the stream's encryption.
        match (&self.doc.crypt, object.packed()) {
            (Some(crypt), false) => {
                crypt.decrypt(object.number, object.generation, StreamKind::Ordinary, raw)
            }
            _ => Some(raw.to_vec()),
        }
    }

    fn number(&self, dict: &Dict, key: &[u8]) -> Option<f64> {
        self.doc.resolve(dict.get(key)).as_number()
    }

    fn dict(&self, object: &Object, whole: &Dict, stream_dict: bool, out: &mut Output<'_>) {
        let doc = self.doc;
        let offset = object.offset;
        let dict = &Keys::of(whole);
        let is = |key: &[u8], name: &[u8]| doc.name_is(dict.get(key), name);
        let present = |key: &[u8]| !matches!(doc.resolve(dict.get(key)), Value::Null);

        let external = if stream_dict
            && [&b"F"[..], b"FFilter", b"FDecodeParms"]
                .into_iter()
                .any(present)
        {
            Some("stream data is read from a file outside the document")
        } else if is(b"Subtype", b"Form") && present(b"Ref") {
            Some("form draws a page of another document")
        } else if is(b"FS", b"URL") {
            Some("file specification names a URL to fetch")
        } else if present(b"OPI") || is(b"Type", b"OPI") {
            Some("OPI comments name a file to put in place of the content")
        } else {
            None
        };
        if let Some(message) = external {
            out.report("pdf.external_ref", Severity::Medium, message, offset);
        }
        if is(b"S", b"Thread") && present(b"F") {
            out.report(
                "pdf.remote_goto",
                Severity::Low,
                "action opens another document",
                offset,
            );
        }

        for value in dict.all(b"URI") {
            // A link's address, or the catalog's dictionary with the base
            // that relative addresses are resolved against.
            let base = doc.resolve(Some(value)).as_dict();
            let targets = base
                .into_iter()
                .flat_map(|base| base.all(b"Base"))
                .chain([value]);
            for target in targets {
                if let Some(address) = self.string(object, target)
                    && let Some((severity, message)) = dangerous_address(&address)
                {
                    out.report("pdf.dangerous_uri", severity, message, offset);
                }
            }
        }
        for value in dict.all(b"DA") {
            if let Some(appearance) = self.string(object, value)
                && content::scan(&appearance).postscript
            {
                postscript(
                    "default appearance string uses the PostScript operator",
                    offset,
                    out,
                );
            }
        }

        if is(b"Subtype", b"Image")
            && let (Some(width), Some(height)) =
                (self.number(whole, b"Width"), self.number(whole, b"Height"))
            && width * height > self.max_pixels as f64
        {
            out.report(
                "pdf.pixel_bomb",
                Severity::Medium,
                &format!("image declares {width} x {height} pixels"),
                offset,
            );
        }
        if is(b"Type", b"Page") || is(b"Type", b"Pages") {
            self.page_boxes(whole, offset, out);
        }

        let filters = match doc.resolve(dict.get(b"Filter")) {
            Value::Array(items) => items.iter().collect(),
            Value::Null => Vec::new(),
            other => vec![other],
        };
        for filter in filters {
            if is(b"Subtype", b"Image")
                && let Some(b"JBIG2Decode" | b"JPXDecode") = doc.resolve(Some(filter)).as_name()
            {
                out.report(
                    "pdf.risky_codec",
                    Severity::Info,
                    "image is compressed with JBIG2 or JPEG 2000, whose decoders have a long \
                     history of exploits",
                    offset,
                );
            }
        }
        for key in [&b"DecodeParms"[..], b"DP"] {
            let parameters = match doc.resolve(dict.get(key)) {
                Value::Array(items) => items.iter().collect(),
                other => vec![other],
            };
            let out_of_range = parameters
                .into_iter()
                .filter_map(|item| doc.resolve(Some(item)).as_dict())
                .any(|parameters| !self.decode_parameters_valid(parameters));
            if out_of_range {
                out.malformed("decode parameters outside what the format allows", offset);
            }
        }

        // An annotation, whose flags can keep it off the screen and on paper.
        if present(b"Rect")
            && present(b"Subtype")
            && let Some(flags) = doc.resolve(dict.get(b"F")).as_usize()
            && flags & PRINT != 0
            && flags & NO_VIEW != 0
            && flags & HIDDEN == 0
        {
            print_only("annotation is printed and not shown on screen", offset, out);
        }
        // The usage dictionary of a layer.
        let state = |usage: &[u8], key: &[u8], name: &[u8]| {
            let usage = doc.resolve(dict.get(usage)).as_dict();
            usage.is_some_and(|usage| doc.name_is(usage.get(key), name))
        };
        if state(b"Print", b"PrintState", b"ON") && state(b"View", b"ViewState", b"OFF") {
            print_only("layer is printed and not shown on screen", offset, out);
        }

        if let Some(ranges) = doc.resolve(dict.get(b"ByteRange")).as_array() {
            self.signed_ranges(ranges, offset, out);
        }
    }

    fn page_boxes(&self, dict: &Dict, offset: usize, out: &mut Output<'_>) {
        let unit = self.number(dict, b"UserUnit").unwrap_or(1.0).max(1.0);
        for key in PAGE_BOXES {
            let Some(corners) = self.doc.resolve(dict.get(key)).as_array() else {
                continue;
            };
            let corner = |index: usize| self.doc.resolve(corners.get(index)).as_number();
            let (Some(left), Some(bottom), Some(right), Some(top)) =
                (corner(0), corner(1), corner(2), corner(3))
            else {
                continue;
            };
            let side = (right - left).abs().max((top - bottom).abs()) * unit;
            if !side.is_finite() || side > MAX_PAGE_SIDE {
                return out.report(
                    "pdf.page_size",
                    Severity::Medium,
                    "page is larger than any real page; rendering it exhausts memory",
                    offset,
                );
            }
        }
    }

    /// Decoders size their buffers from these numbers.
    fn decode_parameters_valid(&self, parameters: &Dict) -> bool {
        let within =
            |key: &[u8], accepts: &dyn Fn(f64) -> bool| match self.doc.resolve(parameters.get(key))
            {
                Value::Null => true,
                Value::Number(value) => value.fract() == 0.0 && accepts(*value),
                _ => false,
            };
        within(b"Predictor", &|n| {
            n == 1.0 || n == 2.0 || (10.0..=15.0).contains(&n)
        }) && within(b"Colors", &|n| (1.0..=32.0).contains(&n))
            && within(b"BitsPerComponent", &|n| {
                [1.0, 2.0, 4.0, 8.0, 16.0].contains(&n)
            })
            && within(b"Columns", &|n| (1.0..=16_777_216.0).contains(&n))
    }

    /// `/ByteRange` of a signature: pairs of offset and length of what was
    /// signed, which is the whole file but the signature value.
    fn signed_ranges(&self, ranges: &[Value], offset: usize, out: &mut Output<'_>) {
        let numbers: Option<Vec<usize>> = ranges
            .iter()
            .map(|item| self.doc.resolve(Some(item)).as_usize())
            .collect();
        let Some(numbers) = numbers.filter(|n| n.len() >= 4 && n.len() % 2 == 0) else {
            return;
        };
        let end = numbers[numbers.len() - 2].saturating_add(numbers[numbers.len() - 1]);
        let length = self.doc.data.trim_ascii_end().len();
        if numbers[0] != 0 || end < length {
            out.report(
                "pdf.signature_modified",
                Severity::Info,
                "signature does not cover the whole file: bytes were added after signing, \
                 or left out of what was signed",
                offset,
            );
        }
    }

    /// Checks on what a stream holds.
    fn stream(
        &self,
        object: &Object,
        dict: &Dict,
        budget: &mut Budget,
        out: &mut Output<'_>,
    ) -> Result<(), LimitExceeded> {
        let doc = self.doc;
        let is = |key: &[u8], name: &[u8]| doc.name_is(dict.get(key), name);
        let is_xml = is(b"Type", b"Metadata") || is(b"Subtype", b"XML");
        let is_calculator = self.number(dict, b"FunctionType") == Some(4.0);
        if !is_xml && !is_calculator {
            return Ok(());
        }
        let content = match doc.decode(object, dict, budget) {
            Ok(content) => content,
            Err(DecodeError::Limit) => return Err(LimitExceeded::Decompression),
            // Nothing reads what cannot be decoded.
            Err(_) => return Ok(()),
        };
        if is_xml {
            xml(&content, object.offset, out);
        }
        if is_calculator && !is_calculator_program(&content) {
            postscript(
                "function holds PostScript beyond the operators of a calculator function",
                object.offset,
                out,
            );
        }
        Ok(())
    }
}

/// A dictionary, and which of the keys that [`Pass::dict`] asks for it has.
/// Going through the entries for each key in turn is most of what a
/// document of many small objects costs; nearly every answer is no.
struct Keys<'d> {
    dict: &'d Dict,
    seen: u32,
}

impl<'d> Keys<'d> {
    /// One bit for each key asked for, and one for all the others.
    fn bit(key: &[u8]) -> u32 {
        let index = match key {
            b"F" => 0,
            b"FFilter" => 1,
            b"FDecodeParms" => 2,
            b"Subtype" => 3,
            b"Ref" => 4,
            b"FS" => 5,
            b"OPI" => 6,
            b"Type" => 7,
            b"S" => 8,
            b"URI" => 9,
            b"DA" => 10,
            b"Filter" => 11,
            b"DecodeParms" => 12,
            b"DP" => 13,
            b"Rect" => 14,
            b"Print" => 15,
            b"View" => 16,
            b"ByteRange" => 17,
            _ => 31,
        };
        1 << index
    }

    fn of(dict: &'d Dict) -> Self {
        let seen = dict
            .0
            .iter()
            .fold(0, |seen, (key, _)| seen | Self::bit(key.text()));
        Self { dict, seen }
    }

    fn get(&self, key: &[u8]) -> Option<&'d Value> {
        if self.seen & Self::bit(key) == 0 {
            return None;
        }
        self.dict.get(key)
    }

    fn all(&self, key: &'d [u8]) -> impl Iterator<Item = &'d Value> + use<'d> {
        let present = self.seen & Self::bit(key) != 0;
        present.then(|| self.dict.all(key)).into_iter().flatten()
    }
}

fn check_length(name: &Name, offset: usize, out: &mut Output<'_>) {
    if name.text().len() > MAX_NAME {
        out.malformed("name longer than the format allows", offset);
    }
}

fn print_only(message: &str, offset: usize, out: &mut Output<'_>) {
    out.report("pdf.print_only", Severity::Info, message, offset);
}

/// What a file name ends with when opening the file runs it.
const PROGRAMS: [&[u8]; 24] = [
    b".app",
    b".bat",
    b".cmd",
    b".com",
    b".command",
    b".cpl",
    b".dll",
    b".exe",
    b".hta",
    b".jar",
    b".js",
    b".jse",
    b".lnk",
    b".msi",
    b".pif",
    b".ps1",
    b".reg",
    b".scr",
    b".sh",
    b".url",
    b".vbe",
    b".vbs",
    b".wsf",
    b".wsh",
];

/// Whether a link's address runs a script, reaches for a network share or
/// opens a program on the reader's own machine, read the way a browser
/// reads it: tabs and line breaks dropped, leading spaces and control
/// characters ignored, letters in either case, UTF-16 accepted.
///
/// A link to any other file on the reader's machine is what converting a
/// saved web page leaves behind (`file:///C|/book/index.html`), and is as
/// common in documents that mean no harm as in the others.
fn dangerous_address(raw: &[u8]) -> Option<(Severity, &'static str)> {
    let text: Vec<u8> = match raw {
        [0xFE, 0xFF, rest @ ..] => rest
            .chunks(2)
            .map(|unit| match unit {
                [0, low] if low.is_ascii() => *low,
                _ => 0xFF,
            })
            .collect(),
        _ => raw.to_vec(),
    };
    let text: Vec<u8> = text
        .into_iter()
        .filter(|byte| !matches!(byte, b'\t' | b'\n' | b'\r'))
        .skip_while(|&byte| byte <= b' ')
        .map(|byte| byte.to_ascii_lowercase())
        .collect();

    let share = (
        Severity::Medium,
        "link opens a network share, which is sent the reader's Windows credentials",
    );
    let program = (
        Severity::Medium,
        "link opens a program or script on the reader's machine",
    );
    // Windows takes no notice of dots and spaces at the end of a name.
    let runs = |path: &[u8]| {
        let end = path.iter().position(|&byte| matches!(byte, b'?' | b'#'));
        let name = path[..end.unwrap_or(path.len())].trim_ascii_end();
        let name = name.strip_suffix(b".").unwrap_or(name).trim_ascii_end();
        PROGRAMS.iter().any(|ending| name.ends_with(ending))
    };
    if text.starts_with(b"\\\\") {
        return Some(share);
    }
    let length = text.iter().position(|&byte| byte == b':')?;
    let scheme = &text[..length];
    if scheme.is_empty()
        || !scheme
            .iter()
            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.'))
    {
        return None;
    }
    match scheme {
        b"javascript" | b"vbscript" | b"livescript" | b"data" => {
            Some((Severity::High, "link runs a script"))
        }
        b"mhtml" | b"jar" | b"search" | b"shell" | b"its" | b"mk" => Some((
            Severity::High,
            "link starts a protocol handler with a history of exploits",
        )),
        _ if scheme.starts_with(b"ms-") || scheme.starts_with(b"search-") => Some((
            Severity::High,
            "link starts a protocol handler with a history of exploits",
        )),
        b"smb" | b"cifs" => Some(share),
        b"file" => {
            // Two slashes are followed by a host, four or more by the host
            // of a share; a host that is a drive letter is none.
            let path = &text[length + 1..];
            let is_slash = |byte: &u8| matches!(byte, b'/' | b'\\');
            let slashes = path.iter().take_while(|byte| is_slash(byte)).count();
            let host = path[slashes..].split(is_slash).next().unwrap_or_default();
            let local = matches!(host, b"" | b"localhost" | [_, b':' | b'|']);
            if slashes >= 4 || (slashes == 2 && !local) {
                Some(share)
            } else {
                runs(path).then_some(program)
            }
        }
        // A drive letter.
        [letter] if letter.is_ascii_alphabetic() => runs(&text).then_some(program),
        _ => None,
    }
}

/// XML carried in the document: metadata and form packets. The parsers that
/// read it, in readers and in the tools of a server, expand what a DTD
/// declares and fetch what the text names.
pub(super) fn xml(packet: &[u8], offset: usize, out: &mut Output<'_>) {
    // UTF-16 and UTF-32 text reads as ASCII once the zero bytes are gone.
    let text: Vec<u8> = packet
        .iter()
        .filter(|&&byte| byte != 0)
        .map(u8::to_ascii_lowercase)
        .collect();
    let has = |needle: &[u8]| memmem::find(&text, needle).is_some();
    if has(b"<!entity") {
        out.report(
            "pdf.xml_entity",
            Severity::High,
            "XML in the document declares entities",
            offset,
        );
    } else if has(b"<!doctype") {
        out.report(
            "pdf.xml_entity",
            Severity::Medium,
            "XML in the document has a document type declaration",
            offset,
        );
    }
    let includes = has(b"www.w3.org/2001/xinclude") && has(b"include");
    if includes || has(b"<?xml-stylesheet") || has(b"wsdlconnection") {
        out.report(
            "pdf.xml_external_ref",
            Severity::Medium,
            "XML in the document makes its parser fetch another resource",
            offset,
        );
    }
}

/// Whether the program of a Type 4 function is what the format allows: one
/// procedure of numbers, the calculator's operators and nested procedures.
fn is_calculator_program(program: &[u8]) -> bool {
    if program.len() > MAX_FUNCTION {
        return false;
    }
    let mut depth = 0usize;
    let mut pos = 0;
    while let Some(&byte) = program.get(pos) {
        match byte {
            b'%' => pos += memchr::memchr2(b'\n', b'\r', &program[pos..]).unwrap_or(program.len()),
            b'{' => {
                depth += 1;
                pos += 1;
            }
            b'}' => {
                let Some(outer) = depth.checked_sub(1) else {
                    return false;
                };
                depth = outer;
                pos += 1;
            }
            _ if is_whitespace(byte) => pos += 1,
            _ if is_regular(byte) => {
                let length = program[pos..]
                    .iter()
                    .take_while(|&&b| is_regular(b))
                    .count();
                let token = &program[pos..pos + length];
                let number = std::str::from_utf8(token)
                    .ok()
                    .is_some_and(|text| text.parse::<f64>().is_ok());
                if depth == 0 || !(number || CALCULATOR.binary_search(&token).is_ok()) {
                    return false;
                }
                pos += length;
            }
            // Strings, names, arrays and dictionaries have no place in one.
            _ => return false,
        }
    }
    depth == 0
}

/// Where the data of each stream lies, in file order and without overlaps.
/// What stands there is the stream's, whatever it looks like.
fn merged(mut ranges: Vec<Range<usize>>) -> Vec<Range<usize>> {
    ranges.sort_unstable_by_key(|range| range.start);
    let mut merged: Vec<Range<usize>> = Vec::with_capacity(ranges.len());
    for range in ranges {
        match merged.last_mut() {
            Some(last) if range.start <= last.end => last.end = last.end.max(range.end),
            _ => merged.push(range),
        }
    }
    merged
}

fn inside(ranges: &[Range<usize>], position: usize) -> bool {
    let after = ranges.partition_point(|range| range.start <= position);
    after > 0 && position < ranges[after - 1].end
}

/// The parts of the first `end` bytes that are no stream's data, each with
/// where it starts. Most of a large document is stream data, and what is
/// looked for outside it is not looked for inside.
fn gaps<'r>(streams: &'r [Range<usize>], end: usize) -> impl Iterator<Item = Range<usize>> + 'r {
    let starts = std::iter::once(0).chain(streams.iter().map(|stream| stream.end));
    let ends = streams
        .iter()
        .map(|stream| stream.start)
        .chain(std::iter::once(end));
    starts
        .zip(ends)
        .map(move |(start, gap_end)| start..gap_end.min(end))
        .filter(|gap| gap.start < gap.end)
}

/// Where `needle` starts outside every stream, in file order. It may run
/// on into the data of the stream that follows.
fn find_outside<'d>(
    data: &'d [u8],
    streams: &'d [Range<usize>],
    needle: &'d [u8],
) -> impl Iterator<Item = usize> + 'd {
    gaps(streams, data.len()).flat_map(move |gap| {
        let reach = (gap.end + needle.len() - 1).min(data.len());
        memmem::find_iter(&data[gap.start..reach], needle).map(move |at| gap.start + at)
    })
}

/// One object number defined twice with different contents, with no end of
/// file between the two: no update did that. Which of them a reader takes
/// depends on how it reads or repairs the file, so that a scanner and a
/// reader, or two readers, are shown different documents.
fn conflicting_objects(doc: &Document<'_>, streams: &[Range<usize>], out: &mut Output<'_>) {
    let mut order: Vec<&Object> = doc
        .objects
        .iter()
        .filter(|object| !object.packed() && !inside(streams, object.offset))
        .collect();
    order.sort_unstable_by_key(|object| (object.number, object.generation, object.offset));
    let mut ends: Option<Vec<usize>> = None;
    for pair in order.windows(2) {
        let (first, second) = (pair[0], pair[1]);
        if (first.number, first.generation) != (second.number, second.generation) {
            continue;
        }
        let ends = ends.get_or_insert_with(|| find_outside(doc.data, streams, b"%%EOF").collect());
        let next_end = ends.partition_point(|&end| end < first.offset);
        let same_revision = ends.get(next_end).is_none_or(|&end| end > second.offset);
        let differ = || {
            let (one, other) = (doc.value(first), doc.value(second));
            let data = |object: &Object, value: &Value| {
                let dict = value.as_dict()?;
                doc.stream_data(object, dict)
            };
            one != other || data(first, &one) != data(second, &other)
        };
        if same_revision && differ() {
            return out.report(
                "pdf.conflicting_objects",
                Severity::Medium,
                &format!(
                    "object {} is defined twice with different contents; readers disagree \
                     on which one counts",
                    first.number
                ),
                second.offset,
            );
        }
    }
}

/// Signatures of other formats outside every stream, where no part of a PDF
/// has a reason to hold them: the file is laid out to open as something else
/// too.
fn foreign_signatures(data: &[u8], streams: &[Range<usize>], out: &mut Output<'_>) {
    // What follows the last end of file is judged with the end of the file.
    let data = &data[..memmem::rfind(data, b"%%EOF").unwrap_or(data.len())];
    if let Some(at) = find_outside(data, streams, b"PK\x03\x04").next() {
        out.report(
            "pdf.polyglot",
            Severity::High,
            "ZIP file header outside any stream; the file also opens as an archive",
            at,
        );
    }

    let markup = find_outside(data, streams, b"<").find(|&at| {
        let rest = &data[at + 1..];
        [&b"html"[..], b"script", b"svg"].into_iter().any(|tag| {
            rest.len() > tag.len()
                && rest[..tag.len()].eq_ignore_ascii_case(tag)
                && (rest[tag.len()].is_ascii_whitespace() || rest[tag.len()] == b'>')
        })
    });
    if let Some(at) = markup {
        out.report(
            "pdf.polyglot",
            Severity::Medium,
            "HTML or SVG markup outside any stream; the file also opens as a web page",
            at,
        );
    }

    let second_header = find_outside(data, streams, b"%PDF-")
        .filter(|&at| {
            let starts_line = at == 0 || matches!(data[at - 1], b'\n' | b'\r');
            starts_line && data.get(at + 5).is_some_and(u8::is_ascii_digit)
        })
        .nth(1);
    if let Some(at) = second_header {
        out.malformed("more than one PDF header", at);
    }
}

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

    #[test]
    fn addresses() {
        let verdict = |address: &[u8]| dangerous_address(address).map(|(severity, _)| severity);
        for harmless in [
            &b"https://example.com/a?b=c:d"[..],
            b"http://example.com",
            b"mailto:someone@example.com",
            b"page.html#top",
            b"../other.pdf",
            b"tel:+15551234",
            b"",
            b"12:30 is the time",
            // Files on the reader's own machine that do not run.
            b"file:///etc/passwd",
            b"file:///C|/book/index.html",
            b"file://localhost/home/reader/notes.txt",
            b"file://C:/app/scripts/exit.jsp%3fdest=http:/example.com/",
            b"C:\\Users\\reader\\report.docx",
        ] {
            assert_eq!(verdict(harmless), None, "{harmless:?}");
        }
        for script in [
            &b"javascript:alert(1)"[..],
            b"  JavaScript:alert(1)",
            b"java\tscr\nipt:alert(1)",
            b"\xFE\xFF\0j\0a\0v\0a\0s\0c\0r\0i\0p\0t\0:\0x",
            b"data:text/html,<script>",
            b"ms-msdt:/id PCWDiagnostic",
            b"search-ms:query=x",
            b"vbscript:x",
        ] {
            assert_eq!(verdict(script), Some(Severity::High), "{script:?}");
        }
        for local in [
            &b"\\\\host\\share\\file"[..],
            b"smb://host/share",
            b"file://host/share/file.txt",
            b"file:////host/share/file.txt",
            b"FILE:\\\\\\\\host\\share",
            b"C:\\Windows\\system32\\calc.exe",
            b"file:///C:/Windows/System32/calc.exe . ",
            b"file:///C|/tools/run.BAT?x=1",
            b"file:/tmp/x.sh",
        ] {
            assert_eq!(verdict(local), Some(Severity::Medium), "{local:?}");
        }
    }

    #[test]
    fn calculator_programs() {
        assert!(is_calculator_program(b"{ 1 exch sub }"));
        assert!(is_calculator_program(
            b"{ dup 0.5 gt { pop 1 } { 2 mul } ifelse } % tint\n"
        ));
        assert!(CALCULATOR.is_sorted());
        for program in [
            &b"{ (%pipe%id) (r) file }"[..],
            b"{ /x 1 def }",
            b"{ currentfile }",
            b"1 add",
            b"{ 1 add",
            b"{ 1 add } }",
            b"{ [1 2] }",
        ] {
            assert!(!is_calculator_program(program), "{program:?}");
        }
    }

    #[test]
    fn ranges() {
        let ranges = [10..20, 30..40];
        for (position, expected) in [(9, false), (10, true), (19, true), (20, false), (35, true)] {
            assert_eq!(inside(&ranges, position), expected, "{position}");
        }
        assert!(!inside(&[], 5));

        let all: Vec<_> = gaps(&ranges, 50).collect();
        assert_eq!(all, [0..10, 20..30, 40..50]);
        assert_eq!(gaps(&ranges, 35).collect::<Vec<_>>(), [0..10, 20..30]);
        assert_eq!(gaps(&[Range { start: 0, end: 10 }], 10).count(), 0);
        assert_eq!(gaps(&[], 5).next(), Some(0..5));

        // A match may start in a gap and end in a stream, not the reverse.
        let data = b"..abcd....abcd......";
        let found = |streams: &[Range<usize>]| -> Vec<usize> {
            find_outside(data, streams, b"abcd").collect()
        };
        assert_eq!(found(&[]), [2, 10]);
        assert_eq!(found(&[Range { start: 4, end: 12 }]), [2]);
        assert_eq!(found(&[Range { start: 1, end: 9 }]), [10]);
    }
}