zond-engine 0.15.0

The core networking library powering Zond. Raw packet injection, protocol parsing, service and OS fingerprinting, and asynchronous network scanning.
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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
//
// This file is part of Zond Engine, licensed under the GNU Affero General
// Public License, version 3 or later. See the LICENSE file for details, or
// <https://www.gnu.org/licenses/agpl-3.0.html>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! # A pull parser that declares nothing
//!
//! The XML reader behind every format in this module that has one. It reads a
//! file somebody else wrote, and what makes that defensible is that the dangerous
//! constructs have no representation here rather than care in the parsing.
//!
//! - **`<!ENTITY` is refused anywhere, unconditionally.** No entity is ever
//!   declared, so none can be expanded, so the billion-laughs expansion has
//!   nothing to expand and an external entity has nothing to fetch.
//! - **A `DOCTYPE` is accepted only in its inert form**: a bare name, no
//!   internal subset, no `SYSTEM` or `PUBLIC` identifier. Any of those is
//!   refused.
//! - **Any entity reference other than the five predefined and numeric
//!   character references is refused**, naming it. With no declarations
//!   permitted there is no legitimate way for one to appear.
//! - **Processing instructions are skipped without being interpreted.** This is
//!   not an XSLT engine and never opens what one names.
//! - Nesting depth, element count, name length, attribute length and captured
//!   text length are all bounded, and one element's markup is bounded by
//!   [`ImportLimits::max_line_bytes`](crate::import::ImportLimits::max_line_bytes).
//!
//! Nothing in a document can make this parser open a file, resolve a URL, or
//! allocate without bound. The residue is processing time, which the bounds
//! cover.
//!
//! ## Why a bare DOCTYPE is accepted rather than refused
//!
//! Because refusing it would reject every real nmap file. Nmap writes
//!
//! ```text
//! <?xml version="1.0" encoding="UTF-8"?>
//! <!DOCTYPE nmaprun>
//! <?xml-stylesheet href="file:///usr/share/nmap/nmap.xsl" type="text/xsl"?>
//! ```
//!
//! and a rule that turned all three away would leave this parser unable to read
//! its own subject. `<!DOCTYPE nmaprun>` declares nothing and references nothing.
//! The narrower rule is also the stronger one: a blanket refusal is the kind that
//! acquires an option to disable it the first time somebody needs their file
//! read.
//!
//! ## What a caller reads, and what is skipped unbuffered
//!
//! A caller names the attributes it wants when it builds the parser, and every
//! other value is scanned past without being stored. That lets nmap's very long
//! `args` and `services` attributes through without a limit tuned around them,
//! since only the element's total markup is bounded rather than the value.
//!
//! Text between elements is skipped the same way until a caller asks for it with
//! [`Parser::begin_text`], on entering an element whose content it wants. Text
//! nobody asked for is passed over uninterpreted, so an entity reference standing
//! in it is so many bytes.
//!
//! An attribute value is not treated that way. Every value is scanned as it is
//! passed, so a reference in one is resolved, and an undeclared one refused,
//! whether or not the attribute was wanted: a `banner`
//! attribute no reader here looks at, carrying `&whoami;`, refuses the whole
//! document. That is not the skip-what-nobody-reads rule leaking. An undeclared
//! reference is a well-formedness error anywhere in an XML document, and a parser
//! that accepted one quietly in the half of a tag it had no interest in would be
//! calling a document well-formed on the strength of not having looked.
//!
//! ## Who uses it
//!
//! [`nmap`](super::nmap) reads a document as the targets to scan next.
//! [`report::nmap`](crate::import::report::nmap) reads the same document as the
//! findings of the scan that produced it. One audited parser rather than two, so
//! there is no second one to keep hardened in step.

use std::io::BufRead;

use crate::import::{ImportError, ImportOrigin};

/// How deeply elements may nest.
///
/// Real nmap output reaches five. Sixty-four is unreachable by anything honest and
/// bounds the parser's own bookkeeping, which is all depth costs here, since
/// element content is never accumulated.
pub(crate) const MAX_DEPTH: usize = 64;

/// The longest element or attribute name accepted, in bytes.
pub(crate) const MAX_NAME_BYTES: usize = 64;

/// The longest attribute value kept, in bytes.
///
/// Applies only to the attributes a caller asked for. Every other attribute is
/// scanned past without being stored, which is what lets nmap's very long
/// `args` and `services` attributes through without a limit tuned around them.
pub(crate) const MAX_VALUE_BYTES: usize = 256;

/// The most elements one document may contain.
pub(crate) const MAX_ELEMENTS: u64 = 1 << 24;

/// The longest entity name accepted, in bytes, between the `&` and the `;`.
pub(crate) const MAX_ENTITY_BYTES: usize = 16;

/// The longest run of element text kept, in bytes.
///
/// Applies only to text a caller asked for. The longest thing anything reads
/// this way is a CPE identifier, which runs to a few dozen bytes.
const MAX_TEXT_BYTES: usize = 512;

/// The longest terminator [`Parser::skip_until`] is asked to find: `-->` and
/// `]]>`, with `?>` and `>` shorter.
const MAX_TERMINATOR_BYTES: usize = 3;

// ---------------------------------------------------------------------------
// The parser
// ---------------------------------------------------------------------------

/// What the parser found.
pub(crate) enum Event {
    /// An element opened. `self_closing` means it closed in the same tag.
    Start { self_closing: bool },
    /// An element closed.
    End,
    /// The document ended.
    Eof,
}

/// The element the parser is currently looking at.
///
/// Reused between elements, so a document of any size costs one element's worth
/// of memory. Only the four attributes this module reads are stored; every
/// other value is scanned past without being kept.
#[derive(Debug, Default)]
pub(crate) struct Element {
    pub(crate) name: Vec<u8>,
    values: Vec<(Vec<u8>, Vec<u8>)>,
}

impl Element {
    fn clear(&mut self) {
        self.name.clear();
        self.values.clear();
    }

    /// One attribute's value as text, if the element carried it.
    ///
    /// `None` means the element did not carry the attribute and nothing else. A
    /// value that is not UTF-8 refused the document when it was read, so no
    /// unreadable value ever reaches here.
    ///
    /// A duplicated attribute answers with the first. XML makes a repeated
    /// attribute a fatal error and this parser stores both, so `<address
    /// addr="10.0.0.2" addr="10.0.0.1"/>` reads as `10.0.0.2` rather than
    /// refusing the document. Deliberate, and cheap to change if the argument
    /// moves: first-wins is a rule, it is the same rule every time, and the
    /// values are byte-compared against fixed names rather than merged, so
    /// there is no reading in which two callers of this disagree about what an
    /// element said. What it costs is that a document no conforming parser
    /// accepts is reinterpreted here instead of refused, which is against this
    /// module's usual instinct; what it buys is that the instinct does not turn
    /// into a refusal nobody asked for. Nothing downstream depends on either
    /// answer.
    pub(crate) fn value(&self, name: &[u8]) -> Option<&str> {
        self.values
            .iter()
            .find(|(key, _)| key == name)
            .and_then(|(_, value)| std::str::from_utf8(value).ok())
    }
}

/// The pull parser itself: a reader, the window of it currently being walked,
/// and the counters every one of this module's refusals is measured against.
pub(crate) struct Parser<'a> {
    input: &'a mut dyn BufRead,
    buffer: Vec<u8>,
    position: usize,
    /// The 1-based line the parser is on, counted as bytes go past so an error
    /// can name a place in the file.
    line: u64,
    /// Bytes consumed within the current element's markup.
    element_bytes: usize,
    max_element_bytes: usize,
    elements: u64,
    pub(crate) element: Element,
    /// The format's name in errors, so a refusal names the document a caller
    /// handed over rather than the parser that read it.
    format: &'static str,
    /// The attributes whose values are stored. Everything else is skipped
    /// unbuffered.
    kept: &'static [&'static [u8]],
    /// Kept attributes that are dropped rather than refused when they run past
    /// the bound. See [`with_lossy`](Parser::with_lossy).
    lossy: &'static [&'static [u8]],
    /// The longest kept value, in bytes.
    max_value_bytes: usize,
    /// How deeply elements are currently nested.
    depth: usize,
    /// Whether text between elements is being kept.
    capture: bool,
    /// The text kept since [`begin_text`](Parser::begin_text).
    text: Vec<u8>,
}

/// What to do with one attribute's value.
///
/// Three states rather than two independent flags. A value is only lossy if it is
/// also kept, so one of the four combinations two flags could express does not
/// exist,
/// and two adjacent `bool`s could be swapped at a call site with no diagnostic.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ValuePolicy {
    /// Scanned past and not stored: nothing this parser was asked to keep.
    Skip,

    /// Stored, and a value past the size bound refuses the document.
    Keep,

    /// Stored, and a value past the bound is dropped rather than refused, so
    /// what comes back is the absence of the attribute rather than a prefix of
    /// it. For the attributes where an over-long value is somebody else's
    /// verbosity rather than an attack.
    KeepIfItFits,
}

impl ValuePolicy {
    /// The policy for an attribute this parser was asked to keep, or not, and
    /// to treat leniently, or not. Leniency without keeping is not a state.
    fn of(kept: bool, lossy: bool) -> Self {
        match (kept, lossy) {
            (false, _) => Self::Skip,
            (true, false) => Self::Keep,
            (true, true) => Self::KeepIfItFits,
        }
    }
}

impl<'a> Parser<'a> {
    /// A parser over `input` that keeps the values of `kept` and nothing else.
    pub(crate) fn new(
        input: &'a mut dyn BufRead,
        max_element_bytes: usize,
        format: &'static str,
        kept: &'static [&'static [u8]],
    ) -> Self {
        Self {
            input,
            buffer: Vec::new(),
            position: 0,
            line: 1,
            element_bytes: 0,
            max_element_bytes,
            elements: 0,
            element: Element::default(),
            format,
            kept,
            lossy: &[],
            max_value_bytes: MAX_VALUE_BYTES,
            depth: 0,
            capture: false,
            text: Vec::new(),
        }
    }

    /// Names attributes whose value is dropped, rather than refused, when it
    /// runs past the bound.
    ///
    /// For a value that enriches a record without deciding what it says. Nmap
    /// lists the ports it found uninteresting, and that list is bounded only by
    /// how many ports were scanned, so a sparse sweep of all 65 535 could write
    /// several hundred kilobytes of it. Refusing the file over an attribute
    /// nothing depends on would be the wrong trade, and so would raising every
    /// bound to fit the worst case.
    ///
    /// Dropped whole, never truncated. A prefix of a port list is a claim that
    /// the ports past the cut were not probed, which is a different and
    /// worse answer than not knowing.
    pub(crate) fn with_lossy(mut self, lossy: &'static [&'static [u8]]) -> Self {
        self.lossy = lossy;
        self
    }

    /// Raises the bound on a kept attribute value.
    ///
    /// The default suits a reader whose kept attributes are addresses and port
    /// numbers. One that keeps free text a service reported about itself needs
    /// more room, and says how much rather than inheriting a bound chosen for a
    /// different job.
    pub(crate) fn with_max_value_bytes(mut self, bytes: usize) -> Self {
        self.max_value_bytes = bytes;
        self
    }

    /// Keeps the text that follows, until [`take_text`](Self::take_text).
    ///
    /// Called on entering an element whose content is wanted. Entity references
    /// in kept text are resolved, and an undeclared one is refused exactly as it
    /// is in an attribute value.
    pub(crate) fn begin_text(&mut self) {
        self.text.clear();
        self.capture = true;
    }

    /// The text kept since [`begin_text`](Self::begin_text), and stops keeping.
    ///
    /// Trimmed, since element content is written with the indentation of the
    /// document around it. Text that is not UTF-8 refuses the document rather
    /// than arriving with replacement characters in it: what this carries is a
    /// CPE identifier, and one with a `U+FFFD` in the middle is a corrupted
    /// identifier that reads as a real one.
    pub(crate) fn take_text(&mut self) -> Result<String, ImportError> {
        self.capture = false;
        let text = std::mem::take(&mut self.text);

        let text = std::str::from_utf8(&text).map_err(|_| ImportError::InvalidUtf8 {
            origin: self.origin(),
        })?;

        Ok(text.trim().to_string())
    }

    pub(crate) fn origin(&self) -> ImportOrigin {
        ImportOrigin::line(self.line)
    }

    pub(crate) fn malformed(&self, message: String) -> ImportError {
        ImportError::Malformed {
            format: self.format,
            origin: self.origin(),
            message,
        }
    }

    /// Makes at least one byte available, or reports the end of the input.
    fn fill(&mut self) -> Result<bool, ImportError> {
        if self.position < self.buffer.len() {
            return Ok(true);
        }

        let Self {
            input,
            buffer,
            position,
            ..
        } = self;

        buffer.clear();
        *position = 0;

        let available = input.fill_buf()?;
        if available.is_empty() {
            return Ok(false);
        }
        let taken = available.len();
        buffer.extend_from_slice(available);
        input.consume(taken);

        Ok(true)
    }

    fn peek(&mut self) -> Result<Option<u8>, ImportError> {
        if !self.fill()? {
            return Ok(None);
        }
        Ok(Some(self.buffer[self.position]))
    }

    fn bump(&mut self) -> Result<Option<u8>, ImportError> {
        let Some(byte) = self.peek()? else {
            return Ok(None);
        };
        self.position += 1;
        if byte == b'\n' {
            self.line += 1;
        }
        self.element_bytes += 1;
        if self.element_bytes > self.max_element_bytes {
            return Err(ImportError::LineTooLong {
                origin: self.origin(),
                limit: self.max_element_bytes,
            });
        }
        Ok(Some(byte))
    }

    /// Reads the next element, skipping text, comments and instructions.
    pub(crate) fn next_event(&mut self) -> Result<Event, ImportError> {
        loop {
            // Text between elements is never read, which is what makes an
            // entity reference in content
            // harmless whatever it says.
            loop {
                match self.peek()? {
                    None => return Ok(Event::Eof),
                    Some(b'<') => break,
                    Some(b'&') if self.capture => {
                        self.element_bytes = 0;
                        let resolved = self.entity()?;
                        self.keep_text(&resolved)?;
                    }
                    Some(byte) => {
                        self.element_bytes = 0;
                        self.bump()?;
                        if self.capture {
                            self.keep_text(&[byte])?;
                        }
                    }
                }
            }

            self.element_bytes = 0;
            self.bump()?; // '<'

            match self.peek()? {
                None => return Err(self.malformed("the document ends inside a tag".to_string())),
                Some(b'?') => {
                    self.skip_until(b"?>")?;
                    continue;
                }
                Some(b'!') => {
                    self.declaration()?;
                    continue;
                }
                Some(b'/') => {
                    self.bump()?;
                    self.element.clear();
                    self.read_name()?;
                    self.skip_until(b">")?;
                    self.depth = self.depth.saturating_sub(1);
                    return Ok(Event::End);
                }
                Some(_) => {
                    self.elements += 1;
                    if self.elements > MAX_ELEMENTS {
                        return Err(self.malformed(format!("more than {MAX_ELEMENTS} elements")));
                    }
                    self.element.clear();
                    self.read_name()?;
                    let self_closing = self.read_attributes()?;
                    if !self_closing {
                        self.depth += 1;
                        if self.depth > MAX_DEPTH {
                            return Err(self
                                .malformed(format!("elements nested more than {MAX_DEPTH} deep")));
                        }
                    }
                    return Ok(Event::Start { self_closing });
                }
            }
        }
    }

    /// Handles everything opening `<!`.
    ///
    /// Where the refusals live. A comment declares nothing and is skipped, a
    /// CDATA section is character data and is read as such, a `DOCTYPE` is inert
    /// only in the one form nmap writes, and everything else that can appear here
    /// declares something. Declaring anything is what this parser exists not to
    /// do.
    ///
    /// The comment and the section part company on that distinction. A comment
    /// is not content in any document, dropping it inside captured text is what
    /// XML says to do, where CDATA is exactly content, wearing a syntax that
    /// lets it carry `<` and `&`. See [`capture_until`](Self::capture_until).
    fn declaration(&mut self) -> Result<(), ImportError> {
        self.bump()?; // '!'

        if self.matches(b"--")? {
            return self.skip_until(b"-->");
        }
        if self.matches(b"[CDATA[")? {
            return self.capture_until(b"]]>");
        }
        if self.matches(b"DOCTYPE")? {
            return self.doctype();
        }

        Err(self.malformed(
            "a declaration other than a comment, CDATA or DOCTYPE: this parser \
             accepts no declarations, which is what makes entity expansion \
             impossible rather than merely unimplemented"
                .to_string(),
        ))
    }

    /// Accepts `<!DOCTYPE name>` and nothing else.
    ///
    /// An internal subset is where entity declarations live, and an external
    /// identifier is a document telling the parser to go and fetch something.
    /// Neither is accepted in any form, so neither has to be handled safely.
    ///
    /// The external-identifier arm is a courtesy, not the control. It
    /// matches `SYSTEM` and `PUBLIC` literally and case-sensitively, so a
    /// document spelling either differently, `SY STEM`, or lowercase `system`,
    /// is skipped as inert bytes rather than refused by name. That costs
    /// nothing, because there is nothing here to fetch *with*: this parser
    /// performs no I/O beyond reading the stream it was handed, and parses no
    /// declaration of any kind, so an external identifier that slips past the
    /// match names a document nobody will open. The security property is the
    /// absence of the capability; the message is there to tell an honest author
    /// why their file was turned away. Nothing downstream should treat this
    /// refusal as an XXE control to test against, the control is that the code
    /// to fetch anything does not exist.
    fn doctype(&mut self) -> Result<(), ImportError> {
        loop {
            let Some(byte) = self.bump()? else {
                return Err(self.malformed("the document ends inside a DOCTYPE".to_string()));
            };

            match byte {
                b'>' => return Ok(()),
                b'[' => {
                    return Err(self.malformed(
                        "a DOCTYPE with an internal subset, which is where entity \
                         declarations live. Only a bare '<!DOCTYPE name>' is accepted"
                            .to_string(),
                    ));
                }
                b'S' | b'P' => {
                    // `SYSTEM` and `PUBLIC` both name something to go and fetch.
                    let word = if byte == b'S' {
                        b"YSTEM".as_slice()
                    } else {
                        b"UBLIC".as_slice()
                    };
                    if self.matches(word)? {
                        return Err(self.malformed(
                            "a DOCTYPE naming an external identifier, which asks the \
                             parser to fetch a document. Only a bare \
                             '<!DOCTYPE name>' is accepted"
                                .to_string(),
                        ));
                    }
                }
                _ => {}
            }
        }
    }

    /// Reads an element name into [`Element::name`].
    fn read_name(&mut self) -> Result<(), ImportError> {
        loop {
            match self.peek()? {
                None => return Err(self.malformed("the document ends inside a name".to_string())),
                Some(byte) if is_name_byte(byte) => {
                    self.bump()?;
                    if self.element.name.len() >= MAX_NAME_BYTES {
                        return Err(
                            self.malformed(format!("a name longer than {MAX_NAME_BYTES} bytes"))
                        );
                    }
                    self.element.name.push(byte);
                }
                Some(_) => return Ok(()),
            }
        }
    }

    /// Reads an element's attributes, returning whether the tag closed itself.
    fn read_attributes(&mut self) -> Result<bool, ImportError> {
        loop {
            self.skip_whitespace()?;

            match self.peek()? {
                None => {
                    return Err(self.malformed("the document ends inside a tag".to_string()));
                }
                Some(b'>') => {
                    self.bump()?;
                    return Ok(false);
                }
                Some(b'/') => {
                    self.bump()?;
                    if self.peek()? != Some(b'>') {
                        return Err(self.malformed("'/' not followed by '>'".to_string()));
                    }
                    self.bump()?;
                    return Ok(true);
                }
                Some(_) => self.read_attribute()?,
            }
        }
    }

    /// Reads one `name="value"` pair, keeping the value only if it is wanted.
    fn read_attribute(&mut self) -> Result<(), ImportError> {
        let mut name = Vec::new();
        loop {
            match self.peek()? {
                None => return Err(self.malformed("the document ends inside a tag".to_string())),
                Some(byte) if is_name_byte(byte) => {
                    self.bump()?;
                    if name.len() >= MAX_NAME_BYTES {
                        return Err(self.malformed(format!(
                            "an attribute name longer than {MAX_NAME_BYTES} bytes"
                        )));
                    }
                    name.push(byte);
                }
                Some(_) => break,
            }
        }

        self.skip_whitespace()?;
        if self.peek()? != Some(b'=') {
            return Err(self.malformed(format!(
                "attribute '{}' has no value",
                String::from_utf8_lossy(&name)
            )));
        }
        self.bump()?;
        self.skip_whitespace()?;

        let policy = ValuePolicy::of(
            self.kept.contains(&name.as_slice()),
            self.lossy.contains(&name.as_slice()),
        );

        if let Some(value) = self.read_value(policy)?
            && policy != ValuePolicy::Skip
        {
            // Checked here rather than where the value is read out: `None`
            // there would mean the element did not carry the attribute, and a
            // host whose address is not text would vanish instead of refusing
            // the document. Every other format in this
            // module answers the same bytes with the same error.
            if std::str::from_utf8(&value).is_err() {
                return Err(ImportError::InvalidUtf8 {
                    origin: self.origin(),
                });
            }
            self.element.values.push((name, value));
        }

        Ok(())
    }

    /// Reads a quoted attribute value under `policy`.
    ///
    /// An unwanted value is not accumulated at all, which lets nmap's very long
    /// `args` and `services` attributes through without any limit
    /// tuned around them: only the element's total markup is bounded.
    fn read_value(&mut self, policy: ValuePolicy) -> Result<Option<Vec<u8>>, ImportError> {
        let keep = policy != ValuePolicy::Skip;
        let lossy = policy == ValuePolicy::KeepIfItFits;

        let Some(quote) = self.peek()? else {
            return Err(self.malformed("the document ends inside a tag".to_string()));
        };
        if quote != b'"' && quote != b'\'' {
            return Err(self.malformed("an attribute value that is not quoted".to_string()));
        }
        self.bump()?;

        let mut value = Vec::new();
        // Set once a lossy value has run past the bound. Everything after is
        // scanned past and nothing is kept, so what comes back is the absence of
        // the attribute rather than a prefix of it.
        let mut dropped = false;

        loop {
            let Some(byte) = self.peek()? else {
                return Err(
                    self.malformed("the document ends inside an attribute value".to_string())
                );
            };

            if byte == quote {
                self.bump()?;
                return Ok((!dropped).then_some(value));
            }

            let bytes = if byte == b'&' {
                self.entity()?
            } else {
                self.bump()?;
                vec![byte]
            };

            if keep && !dropped {
                if value.len() + bytes.len() > self.max_value_bytes {
                    if !lossy {
                        return Err(self.malformed(format!(
                            "an attribute value longer than {} bytes",
                            self.max_value_bytes
                        )));
                    }
                    dropped = true;
                    value = Vec::new();
                } else {
                    value.extend_from_slice(&bytes);
                }
            }
        }
    }

    /// Resolves one entity reference, or refuses it.
    ///
    /// The five predefined references and numeric character references are the
    /// whole of what an attribute may contain, since this parser refuses every
    /// declaration and no others can exist. A reference to something undeclared
    /// is the shape an external-entity attack takes, so it
    /// is named in the error rather than skipped.
    fn entity(&mut self) -> Result<Vec<u8>, ImportError> {
        self.bump()?; // '&'

        let mut name = Vec::new();
        loop {
            let Some(byte) = self.peek()? else {
                return Err(self.malformed("the document ends inside an entity".to_string()));
            };
            self.bump()?;
            if byte == b';' {
                break;
            }
            if name.len() >= MAX_ENTITY_BYTES {
                return Err(self.malformed("an entity reference with no ';'".to_string()));
            }
            name.push(byte);
        }

        match name.as_slice() {
            b"lt" => Ok(vec![b'<']),
            b"gt" => Ok(vec![b'>']),
            b"amp" => Ok(vec![b'&']),
            b"quot" => Ok(vec![b'"']),
            b"apos" => Ok(vec![b'\'']),
            reference if reference.first() == Some(&b'#') => self.character_reference(reference),
            other => Err(self.malformed(format!(
                "'&{};' is not a predefined entity, and this parser permits no \
                 declarations, so nothing can have defined it",
                String::from_utf8_lossy(other)
            ))),
        }
    }

    /// Resolves `&#NN;` or `&#xHH;`.
    fn character_reference(&self, reference: &[u8]) -> Result<Vec<u8>, ImportError> {
        let digits = String::from_utf8_lossy(&reference[1..]);
        let code = match digits.strip_prefix(['x', 'X']) {
            Some(hex) => u32::from_str_radix(hex, 16).ok(),
            None => digits.parse::<u32>().ok(),
        };

        let Some(character) = code.and_then(char::from_u32) else {
            return Err(self.malformed(format!("'&{digits};' is not a character")));
        };

        let mut encoded = [0u8; 4];
        Ok(character.encode_utf8(&mut encoded).as_bytes().to_vec())
    }

    /// Consumes `expected` if it is next, and reports whether it was.
    ///
    /// Only ever called where a partial match cannot be the start of anything
    /// else this parser accepts, so a failed match leaves what it read behind
    /// as ordinary content of something already being skipped.
    fn matches(&mut self, expected: &[u8]) -> Result<bool, ImportError> {
        for wanted in expected {
            match self.peek()? {
                Some(byte) if byte == *wanted => {
                    self.bump()?;
                }
                _ => return Ok(false),
            }
        }
        Ok(true)
    }

    fn skip_whitespace(&mut self) -> Result<(), ImportError> {
        while matches!(self.peek()?, Some(b' ' | b'\t' | b'\r' | b'\n')) {
            self.bump()?;
        }
        Ok(())
    }

    /// Skips everything up to and including `terminator`.
    ///
    /// Compares a sliding window of the last few bytes rather than advancing a
    /// match counter. On a mismatch a counter has to decide how much of the
    /// partial match to keep, and the cheap answers are wrong here: `]]]>` ends a
    /// CDATA section whose content is `]`, and dropping back to one matched byte
    /// on the third `]` runs past it to the end of the document. The window
    /// is at most [`MAX_TERMINATOR_BYTES`] wide, so the comparison costs less
    /// than the arithmetic it replaces.
    fn skip_until(&mut self, terminator: &[u8]) -> Result<(), ImportError> {
        self.scan_until(terminator, false)
    }

    /// [`skip_until`](Self::skip_until), keeping what it passes over as element
    /// text when text is being kept.
    ///
    /// For CDATA, which is character data and not a construct. A section inside
    /// an element whose content was asked for used to be skipped like a
    /// comment, so `<cpe><![CDATA[cpe:/a:openbsd:openssh:8.9]]></cpe>` read
    /// back as no CPE at all, a legal document yielding a report with a field
    /// missing, and nothing saying so. Nmap does not write CDATA, so this
    /// changed no document anyone has; a reader that drops legal content
    /// silently is the defect whether or not the writer in front of it happens
    /// to produce it.
    ///
    /// Bounded by [`keep_text`](Self::keep_text) like every other route into the
    /// text buffer, so a section megabytes wide is refused at the same 512 bytes
    /// rather than being the one way past the ceiling.
    fn capture_until(&mut self, terminator: &[u8]) -> Result<(), ImportError> {
        self.scan_until(terminator, self.capture)
    }

    /// The shared loop: read to `terminator`, keeping what precedes it or not.
    ///
    /// A byte is kept as it is *evicted* from the window, which is what keeps the
    /// terminator itself out of the text: when the match lands the window holds
    /// exactly the terminator and everything else has already gone through
    /// [`keep_text`](Self::keep_text).
    fn scan_until(&mut self, terminator: &[u8], keep: bool) -> Result<(), ImportError> {
        let width = terminator.len();
        debug_assert!(
            (1..=MAX_TERMINATOR_BYTES).contains(&width),
            "every terminator this parser skips to fits the window"
        );

        let mut window = [0u8; MAX_TERMINATOR_BYTES];
        let mut filled = 0usize;

        loop {
            let Some(byte) = self.bump()? else {
                return Err(self.malformed(format!(
                    "the document ends before '{}'",
                    String::from_utf8_lossy(terminator)
                )));
            };

            if filled == width {
                let evicted = window[0];
                window.copy_within(1..width, 0);
                window[width - 1] = byte;
                if keep {
                    self.keep_text(&[evicted])?;
                }
            } else {
                window[filled] = byte;
                filled += 1;
            }

            if filled == width && window[..width] == *terminator {
                return Ok(());
            }
        }
    }
}

/// Appends to the kept text, bounded.
impl Parser<'_> {
    fn keep_text(&mut self, bytes: &[u8]) -> Result<(), ImportError> {
        if self.text.len() + bytes.len() > MAX_TEXT_BYTES {
            return Err(self.malformed(format!("element text longer than {MAX_TEXT_BYTES} bytes")));
        }
        self.text.extend_from_slice(bytes);
        Ok(())
    }
}

/// Whether a byte may appear in an element or attribute name.
///
/// Permissive about what a name may contain and strict about what ends one.
/// Everything this parser does with a name is compare it against a fixed list, so
/// a name it does not recognise is skipped whatever it is made
/// of.
fn is_name_byte(byte: u8) -> bool {
    !matches!(
        byte,
        b' ' | b'\t' | b'\r' | b'\n' | b'/' | b'>' | b'=' | b'<'
    )
}

// ╔════════════════════════════════════════════╗
// ║ ████████╗███████╗███████╗████████╗███████╗ ║
// ║ ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝ ║
// ║    ██║   █████╗  ███████╗   ██║   ███████╗ ║
// ║    ██║   ██╔══╝  ╚════██║   ██║   ╚════██║ ║
// ║    ██║   ███████╗███████║   ██║   ███████║ ║
// ║    ╚═╝   ╚══════╝╚══════╝   ╚═╝   ╚══════╝ ║
// ╚════════════════════════════════════════════╝

/// The module doc makes six claims about what this parser refuses and bounds six
/// things it accepts. These hold it to all twelve.
///
/// The refusals are the whole security argument for reading a file somebody else
/// wrote, and an untested refusal is a claim rather than a control.
#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use proptest::prelude::*;

    use super::*;

    const FORMAT: &str = "test";
    const KEPT: &[&[u8]] = &[b"id", b"name"];

    /// Drives a document to completion, returning the first refusal if there is
    /// one.
    fn read(document: &str) -> Result<Vec<Event>, ImportError> {
        let mut input = Cursor::new(document.as_bytes().to_vec());
        let mut parser = Parser::new(&mut input, 64 * 1024, FORMAT, KEPT);
        let mut events = Vec::new();
        loop {
            match parser.next_event()? {
                Event::Eof => return Ok(events),
                event => events.push(event),
            }
        }
    }

    fn refusal(document: &str) -> String {
        match read(document) {
            Ok(_) => panic!("the parser accepted a document it documents as refused"),
            Err(error) => error.to_string(),
        }
    }

    /// The kept attributes of one element, by name, as the parser reads them
    /// back out.
    type Kept = Vec<(&'static str, Option<String>)>;

    /// Reads one document and hands back the first element's kept attributes.
    fn attributes(document: &str) -> Result<Kept, ImportError> {
        let mut input = Cursor::new(document.as_bytes().to_vec());
        let mut parser = Parser::new(&mut input, 64 * 1024, FORMAT, KEPT);

        loop {
            match parser.next_event()? {
                Event::Eof => return Ok(Vec::new()),
                Event::Start { .. } => {
                    return Ok(KEPT
                        .iter()
                        .map(|name| {
                            (
                                std::str::from_utf8(name).expect("the kept names are ASCII"),
                                parser.element.value(name).map(str::to_owned),
                            )
                        })
                        .collect());
                }
                Event::End => {}
            }
        }
    }

    // ─── Skipping to a terminator ────────────────────────────────────────────

    /// `<![CDATA[a]]]>` is a section whose content is `a]`, and the `]]>` that
    /// closes it begins inside the run of brackets. A parser that restarts its
    /// match at the failing byte walks straight past it and reports a document
    /// that ends unexpectedly, which is a legal file refused.
    #[test]
    fn a_terminator_overlapping_its_own_prefix_still_ends_the_section() {
        for document in [
            r#"<a><![CDATA[b]]]></a>"#,
            r#"<a><![CDATA[b]]]]></a>"#,
            r#"<a><!--- a comment ---></a>"#,
            r#"<a><?target ??></a>"#,
        ] {
            let events =
                read(document).unwrap_or_else(|error| panic!("{document} was refused: {error}"));
            assert!(!events.is_empty(), "{document}");
        }
    }

    /// And the ordinary shapes still terminate where they always did.
    #[test]
    fn a_terminator_that_does_not_overlap_still_ends_the_section() {
        for document in [
            r#"<a><![CDATA[b]]></a>"#,
            r#"<a><!-- a comment --></a>"#,
            r#"<a><?target value?></a>"#,
        ] {
            assert!(read(document).is_ok(), "{document}");
        }
    }

    // ─── Bytes that are not text ─────────────────────────────────────────────

    /// A value that is not UTF-8 refuses the document rather than reading as an
    /// attribute the element did not carry.
    ///
    /// The silent form is the dangerous one: `addr` is how a host is named, so a
    /// host whose address held a stray byte would have vanished from the import
    /// with nothing counted, nothing refused and nothing said.
    #[test]
    fn an_attribute_value_that_is_not_text_refuses_the_document() {
        let mut document = b"<a id=\"1".to_vec();
        document.push(0xFF);
        document.extend_from_slice(b"\"/>");

        let mut input = Cursor::new(document);
        let mut parser = Parser::new(&mut input, 64 * 1024, FORMAT, KEPT);

        assert!(
            matches!(parser.next_event(), Err(ImportError::InvalidUtf8 { .. })),
            "a value that is not text has to be refused, not dropped"
        );
    }

    /// An attribute nobody asked for is never stored, so its bytes are never
    /// examined and cannot refuse a document over a value nothing reads.
    #[test]
    fn an_attribute_nobody_kept_is_not_checked_for_text() {
        let mut document = b"<a other=\"".to_vec();
        document.push(0xFF);
        document.extend_from_slice(b"\" id=\"1\"/>");

        let mut input = Cursor::new(document);
        let mut parser = Parser::new(&mut input, 64 * 1024, FORMAT, KEPT);

        assert!(matches!(parser.next_event(), Ok(Event::Start { .. })));
        assert_eq!(parser.element.value(b"id"), Some("1"));
    }

    /// Kept text is held to the same rule, so a CPE identifier never arrives
    /// with a replacement character standing in for a byte.
    #[test]
    fn element_text_that_is_not_text_refuses_the_document() {
        let mut document = b"<a>cpe:/o:x".to_vec();
        document.push(0xFF);
        document.extend_from_slice(b"</a>");

        let mut input = Cursor::new(document);
        let mut parser = Parser::new(&mut input, 64 * 1024, FORMAT, KEPT);

        assert!(matches!(parser.next_event(), Ok(Event::Start { .. })));
        parser.begin_text();
        assert!(matches!(parser.next_event(), Ok(Event::End)));
        assert!(matches!(
            parser.take_text(),
            Err(ImportError::InvalidUtf8 { .. })
        ));
    }

    /// The ordinary case, so the check above is a refusal of bad bytes rather
    /// than of everything.
    #[test]
    fn a_value_and_a_text_run_that_are_text_read_back_whole() {
        let kept = attributes(r#"<a id="7" name="gw" other="ignored"/>"#).expect("reads");
        assert_eq!(
            kept,
            vec![
                ("id", Some("7".to_string())),
                ("name", Some("gw".to_string())),
            ]
        );
    }

    /// CDATA is character data, and a reader that drops it loses a field without
    /// saying so.
    ///
    /// `<cpe><![CDATA[…]]></cpe>` is legal, and used to read back as no CPE at
    /// all: the section was dispatched to `declaration` and skipped like a
    /// comment. An empty CPE qualifies nothing, so the document parsed, the
    /// report came out a field short, and nothing anywhere reported a problem.
    #[test]
    fn cdata_inside_kept_text_is_read_rather_than_skipped() {
        let document = r#"<a><![CDATA[cpe:/a:openbsd:openssh:8.9]]></a>"#;
        let mut input = Cursor::new(document.as_bytes().to_vec());
        let mut parser = Parser::new(&mut input, 64 * 1024, FORMAT, KEPT);

        assert!(matches!(parser.next_event(), Ok(Event::Start { .. })));
        parser.begin_text();
        assert!(matches!(parser.next_event(), Ok(Event::End)));
        assert_eq!(
            parser.take_text().expect("reads"),
            "cpe:/a:openbsd:openssh:8.9"
        );
    }

    /// The two halves either side of a section join up, and the delimiters
    /// themselves stay out of the text.
    ///
    /// The terminator is found through a sliding window, and a byte is kept as
    /// it is evicted from it, so the check that matters is that `]]>` never
    /// lands in the buffer and nothing before it is lost.
    #[test]
    fn text_around_a_cdata_section_joins_up_without_its_delimiters() {
        let document = r#"<a>before <![CDATA[<inside> & ]]]]> after</a>"#;
        let mut input = Cursor::new(document.as_bytes().to_vec());
        let mut parser = Parser::new(&mut input, 64 * 1024, FORMAT, KEPT);

        assert!(matches!(parser.next_event(), Ok(Event::Start { .. })));
        parser.begin_text();
        assert!(matches!(parser.next_event(), Ok(Event::End)));
        assert_eq!(
            parser.take_text().expect("reads"),
            "before <inside> & ]] after"
        );
    }

    /// A comment is not content, so it stays skipped where a section does not.
    /// The two arrive at the same dispatch and part company there.
    #[test]
    fn a_comment_inside_kept_text_is_still_dropped() {
        let document = r#"<a>before <!-- not content --> after</a>"#;
        let mut input = Cursor::new(document.as_bytes().to_vec());
        let mut parser = Parser::new(&mut input, 64 * 1024, FORMAT, KEPT);

        assert!(matches!(parser.next_event(), Ok(Event::Start { .. })));
        parser.begin_text();
        assert!(matches!(parser.next_event(), Ok(Event::End)));
        assert_eq!(parser.take_text().expect("reads"), "before  after");
    }

    /// A section is bounded by the same ceiling as every other route into the
    /// text buffer, rather than being the one way past it.
    #[test]
    fn a_cdata_section_is_held_to_the_text_ceiling() {
        let document = format!("<a><![CDATA[{}]]></a>", "x".repeat(MAX_TEXT_BYTES + 1));
        let mut input = Cursor::new(document.into_bytes());
        let mut parser = Parser::new(&mut input, 1024 * 1024, FORMAT, KEPT);

        assert!(matches!(parser.next_event(), Ok(Event::Start { .. })));
        parser.begin_text();
        assert!(
            parser.next_event().is_err(),
            "a section wider than the ceiling was kept whole"
        );
    }

    // ─── The six refusals ────────────────────────────────────────────────────

    /// "`<!ENTITY` is refused anywhere, unconditionally. No entity is ever
    /// declared, so none can be expanded, so the billion-laughs expansion has
    /// nothing to expand."
    #[test]
    fn an_entity_declaration_is_refused_outright() {
        let message = refusal(r#"<!DOCTYPE lolz [<!ENTITY lol "lol">]><lolz>&lol;</lolz>"#);
        assert!(
            message.contains("internal subset") || message.contains("declaration"),
            "the refusal should name what it refused, said: {message}"
        );
    }

    /// The billion-laughs document itself, in the shape it is usually written.
    #[test]
    fn the_billion_laughs_document_is_refused_before_anything_expands() {
        let message = refusal(
            r#"<?xml version="1.0"?>
<!DOCTYPE lolz [
  <!ENTITY lol "lol">
  <!ENTITY lol2 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
  <!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
]>
<lolz>&lol3;</lolz>"#,
        );
        assert!(!message.is_empty());
    }

    /// "A `DOCTYPE` is accepted only in its inert form: a bare name, no internal
    /// subset, no `SYSTEM` or `PUBLIC` identifier."
    #[test]
    fn a_bare_doctype_is_accepted_because_nmap_writes_one() {
        let events = read(r#"<?xml version="1.0"?><!DOCTYPE nmaprun><nmaprun/>"#)
            .expect("the inert form nmap writes is readable");
        assert!(!events.is_empty());
    }

    #[test]
    fn a_doctype_naming_a_system_identifier_is_refused() {
        let message = refusal(r#"<!DOCTYPE a SYSTEM "file:///etc/passwd"><a/>"#);
        assert!(message.contains("external identifier"), "said: {message}");
    }

    #[test]
    fn a_doctype_naming_a_public_identifier_is_refused() {
        let message =
            refusal(r#"<!DOCTYPE a PUBLIC "-//X//EN" "http://example.invalid/x.dtd"><a/>"#);
        assert!(message.contains("external identifier"), "said: {message}");
    }

    #[test]
    fn a_doctype_with_an_internal_subset_is_refused() {
        let message = refusal(r#"<!DOCTYPE a [<!ELEMENT a EMPTY>]><a/>"#);
        assert!(message.contains("internal subset"), "said: {message}");
    }

    /// The XXE shape: an external entity pointed at a local file.
    #[test]
    fn an_external_entity_reference_has_nothing_to_resolve_against() {
        let message =
            refusal(r#"<!DOCTYPE a [<!ENTITY xxe SYSTEM "file:///etc/passwd">]><a>&xxe;</a>"#);
        assert!(!message.is_empty());
    }

    /// "Any entity reference other than the five predefined and numeric
    /// character references is refused, naming it."
    #[test]
    fn an_undeclared_entity_reference_is_refused_and_named() {
        let message = refusal(r#"<a id="&whoami;"/>"#);
        assert!(
            message.contains("whoami"),
            "the refusal must name the reference, said: {message}"
        );
    }

    #[test]
    fn the_five_predefined_entities_resolve() {
        let mut input = Cursor::new(r#"<a id="&lt;&gt;&amp;&quot;&apos;"/>"#.as_bytes().to_vec());
        let mut parser = Parser::new(&mut input, 64 * 1024, FORMAT, KEPT);
        parser.next_event().expect("the document reads");
        assert_eq!(parser.element.value(b"id"), Some("<>&\"'"));
    }

    #[test]
    fn a_numeric_character_reference_resolves() {
        let mut input = Cursor::new(r#"<a id="&#65;&#x42;"/>"#.as_bytes().to_vec());
        let mut parser = Parser::new(&mut input, 64 * 1024, FORMAT, KEPT);
        parser.next_event().expect("the document reads");
        assert_eq!(parser.element.value(b"id"), Some("AB"));
    }

    /// "Processing instructions are skipped without being interpreted. This is
    /// not an XSLT engine and never opens what one names."
    #[test]
    fn a_processing_instruction_is_skipped_rather_than_read() {
        let events = read(
            r#"<?xml version="1.0"?><?xml-stylesheet href="file:///usr/share/nmap/nmap.xsl" type="text/xsl"?><a/>"#,
        )
        .expect("a stylesheet instruction is skipped, not followed");
        assert!(
            matches!(events.as_slice(), [Event::Start { self_closing: true }]),
            "the two instructions produce no events, leaving only the element"
        );
    }

    // ─── The six bounds ──────────────────────────────────────────────────────

    #[test]
    fn nesting_is_bounded() {
        let deep = |levels: usize| {
            let mut doc = String::new();
            for _ in 0..levels {
                doc.push_str("<a>");
            }
            for _ in 0..levels {
                doc.push_str("</a>");
            }
            doc
        };

        read(&deep(MAX_DEPTH)).expect("nesting at the bound is accepted");
        let message = refusal(&deep(MAX_DEPTH + 2));
        assert!(
            message.contains("nest") || message.contains("deep"),
            "said: {message}"
        );
    }

    #[test]
    fn an_element_name_is_bounded() {
        let long = "n".repeat(MAX_NAME_BYTES + 1);
        let message = refusal(&format!("<{long}/>"));
        assert!(message.contains("name"), "said: {message}");
    }

    #[test]
    fn an_attribute_name_is_bounded() {
        let long = "n".repeat(MAX_NAME_BYTES + 1);
        let message = refusal(&format!(r#"<a {long}="x"/>"#));
        assert!(message.contains("name"), "said: {message}");
    }

    /// A kept attribute's value past the bound is a refusal; the bound itself is
    /// accepted.
    #[test]
    fn an_unwanted_attribute_is_not_accumulated_and_so_is_not_bounded() {
        // The claim `read_value`'s doc makes, and the reason nmap's `args` and
        // `services` attributes need no limit tuned around them: a value this
        // parser was not asked for is scanned past rather than collected, so
        // the size bound never applies to it.
        let enormous = "v".repeat(MAX_VALUE_BYTES * 8);
        let events = read(&format!(r#"<a id="kept" args="{enormous}"/>"#))
            .expect("an unwanted value is never measured against the bound");

        assert!(
            events
                .iter()
                .any(|event| matches!(event, Event::Start { .. })),
            "the element should have been read"
        );

        // And the same value under a name the parser *was* asked for is refused,
        // so the test above is about what is kept and not about the length.
        assert!(!refusal(&format!(r#"<a id="{enormous}"/>"#)).is_empty());
    }

    #[test]
    fn a_kept_attribute_value_is_bounded() {
        let at_bound = "v".repeat(MAX_VALUE_BYTES);
        read(&format!(r#"<a id="{at_bound}"/>"#)).expect("a value at the bound is accepted");

        let past = "v".repeat(MAX_VALUE_BYTES + 1);
        assert!(!refusal(&format!(r#"<a id="{past}"/>"#)).is_empty());
    }

    #[test]
    fn one_elements_markup_is_bounded() {
        let mut input = Cursor::new(format!(r#"<a id="{}"/>"#, "v".repeat(4096)).into_bytes());
        let mut parser = Parser::new(&mut input, 128, FORMAT, KEPT);
        assert!(
            parser.next_event().is_err(),
            "an element past max_element_bytes is refused"
        );
    }

    #[test]
    fn an_entity_reference_that_never_closes_is_bounded() {
        let message = refusal(&format!(
            r#"<a id="&{}"/>"#,
            "x".repeat(MAX_ENTITY_BYTES * 4)
        ));
        assert!(!message.is_empty());
    }

    // ─── Termination ─────────────────────────────────────────────────────────

    #[test]
    fn a_document_that_ends_mid_element_is_an_error_not_a_hang() {
        for truncated in [
            "<a",
            "<a id=",
            r#"<a id=""#,
            "<a><b",
            "<!DOCTYPE",
            "<!--",
            "<?xml",
        ] {
            let _ = read(truncated);
        }
    }

    proptest! {
        /// Nothing a file can contain makes this parser panic or run forever.
        ///
        /// The same campaign `fingerprint` runs over its own parsers, applied to
        /// the one that reads a document somebody else wrote.
        #[test]
        fn the_parser_never_panics_on_arbitrary_input(document in "(?s).{0,2000}") {
            let _ = read(&document);
        }

        /// Arbitrary bytes, not just arbitrary text: a file on disk is not
        /// obliged to be UTF-8.
        #[test]
        fn the_parser_never_panics_on_arbitrary_bytes(bytes in proptest::collection::vec(any::<u8>(), 0..2000)) {
            let mut input = Cursor::new(bytes);
            let mut parser = Parser::new(&mut input, 64 * 1024, FORMAT, KEPT);
            for _ in 0..10_000 {
                match parser.next_event() {
                    Ok(Event::Eof) | Err(_) => break,
                    Ok(_) => {}
                }
            }
        }
    }
}