zond-engine 0.13.0

A lightweight, fast, and highly concurrent networking backend for packet crafting, protocol fingerprinting, and host discovery.
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
// 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 comparison as a page
//!
//! What changed, in one file, opened in a browser and read by a person. The
//! digest a nightly job attaches to an email, where [`json`](super::json) is
//! what its pipeline ingests.
//!
//! Everything [`export::html`](crate::export::html) commits to holds here
//! unchanged, and for the same reasons: the stylesheet is inlined and nothing
//! is fetched from anywhere, there is no JavaScript at all, printing is a
//! first-class output, and every value a scanned network chose goes through the
//! same escaping writer that module defines. They are shared rather than copied,
//! so there is one escaper in this crate and no second one to keep in step.
//!
//! ## What a comparison page leads with
//!
//! Not the hosts. A report's reader is looking for a host; a comparison's reader
//! is asking whether anything happened, and usually wants the answer without
//! scrolling. So the page opens with the counts, and each headline count states
//! how much of it the other scan is known to have looked for — the number the
//! whole comparison is arranged to protect, and the one an email skims past if
//! it is not said out loud.
//!
//! ## Three states, carried by colour
//!
//! A host is one the later scan gained, one it lost, or one both hold. That is
//! the first thing to see and it costs a border rather than a line of prose, so
//! a reader running down the left edge takes in the shape of the night before
//! reading a word of it.
//!
//! ## A change unconfirmed is a change said differently
//!
//! Where the other scan is not known to have covered a host, the card says so.
//! Suppressing those would hide a finding; showing them as though they were
//! findings about the network is what makes a monitoring tool cry wolf. They are
//! shown, and they are labelled.

use std::io::Write;

use crate::diff::{HostDelta, Presence, ScanDiff};
use crate::export::diff::DiffExporter;
use crate::export::diff::schema::{ChangeDto, DiffDto, HostDeltaDto, PortDeltaDto};
use crate::export::html::{Plain, STYLE, TONE_FOUND, TONE_INERT, TONE_NONE, TONE_PARTIAL, Text};
use crate::export::schema::ENGINE_NAME;
use crate::export::{ExportError, ExportOptions};
use crate::scanner::report::ENGINE_VERSION;

/// What the page is called when a caller names nothing.
const DEFAULT_HEADING: &str = "Scan comparison";

/// Writes a comparison as one self-contained HTML page.
///
/// ```no_run
/// use std::fs::File;
/// use zond_engine::diff::ScanDiff;
/// use zond_engine::export::diff::{DiffExporter, HtmlDiffExporter};
///
/// # fn example(diff: &ScanDiff) -> Result<(), Box<dyn std::error::Error>> {
/// let mut file = File::create("changes.html")?;
/// HtmlDiffExporter::default().export(diff, &mut file)?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Default)]
pub struct HtmlDiffExporter {
    options: ExportOptions,
    heading: Option<String>,
}

impl HtmlDiffExporter {
    /// An exporter under `options`.
    pub fn new(options: ExportOptions) -> Self {
        Self {
            options,
            heading: None,
        }
    }

    /// Sets the heading, for a page that is about a named engagement rather
    /// than about a comparison in the abstract.
    pub fn with_heading(mut self, heading: impl Into<String>) -> Self {
        self.heading = Some(heading.into());
        self
    }

    /// The options in force.
    pub fn options(&self) -> &ExportOptions {
        &self.options
    }
}

impl DiffExporter for HtmlDiffExporter {
    fn export(&self, diff: &ScanDiff, out: &mut dyn Write) -> Result<(), ExportError> {
        let document = DiffDto::new(diff, &self.options);
        let heading = self.heading.as_deref().unwrap_or(DEFAULT_HEADING);

        write_head(out, heading)?;
        write_masthead(out, heading, &document)?;
        write_notices(out, diff, &document)?;
        write_tiles(out, &document)?;
        write_hosts(out, diff, &document)?;
        write_colophon(out, &document)?;

        writeln!(out, "</div>\n</body>\n</html>")?;
        Ok(())
    }
}

/// The document, its stylesheet, and the checkbox the theme switch reads.
fn write_head(out: &mut dyn Write, title: &str) -> Result<(), ExportError> {
    writeln!(
        out,
        r#"<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="generator" content="{ENGINE_NAME} {ENGINE_VERSION}">
<meta name="robots" content="noindex, nofollow">
<title>{title}</title>
<style>
{STYLE}</style>
</head>
<body>
<input type="checkbox" id="zond-theme" class="theme-switch" aria-label="Use the other colour scheme">
<div class="sheet">"#,
        title = Plain(title),
    )?;
    Ok(())
}

/// The brand, the heading, and which two scans these are.
fn write_masthead(
    out: &mut dyn Write,
    heading: &str,
    document: &DiffDto<'_>,
) -> Result<(), ExportError> {
    writeln!(
        out,
        r#"<header class="masthead">
<div class="brand">zond<span class="brand-mark">_</span></div>
<div class="masthead-title">
<h1>{heading}</h1>
<p class="subtitle">{before} → {after}</p>
</div>
<label class="theme-label" for="zond-theme" title="Switch between light and dark"><span class="theme-icon"></span>theme</label>
</header>"#,
        heading = Text(heading),
        before = Text(&document.baseline.at),
        after = Text(&document.current.at),
    )?;
    Ok(())
}

/// The things that change how the rest of the page should be read.
///
/// Each is a fact about the two *scans* rather than about the network, and each
/// makes what follows mean something other than it appears to: a comparison
/// against a scan that stated no scope can confirm nothing, and one between two
/// different kinds of scan reports every port only one of them looked at.
fn write_notices(
    out: &mut dyn Write,
    diff: &ScanDiff,
    document: &DiffDto<'_>,
) -> Result<(), ExportError> {
    let mut notices: Vec<(bool, &str, String)> = Vec::new();

    if document.unchanged {
        notices.push((
            false,
            "no change",
            "the two scans describe the same network".into(),
        ));
    }

    for (side, provenance) in [
        ("earlier scan", &document.baseline),
        ("later scan", &document.current),
    ] {
        if !provenance.states_scope {
            notices.push((
                true,
                "unstated scope",
                format!("the {side} does not say what it covered, so nothing it is missing can be confirmed"),
            ));
        }
    }

    let ports = |kinds: &[&'static str]| kinds.contains(&"port_scan");
    match (
        ports(&document.baseline.kinds),
        ports(&document.current.kinds),
    ) {
        (false, true) => notices.push((
            true,
            "different scans",
            "only the later scan looked at ports, so most of what follows is the earlier one not having looked".into(),
        )),
        (true, false) => notices.push((
            true,
            "different scans",
            "only the earlier scan looked at ports, so most of what follows is the later one not having looked".into(),
        )),
        _ => {}
    }

    if diff.baseline().at() > diff.current().at() {
        notices.push((
            true,
            "reversed",
            "the first scan is the later of the two, so this page reads backwards".into(),
        ));
    }

    if notices.is_empty() {
        return Ok(());
    }

    writeln!(out, "<div class=\"notices\">")?;
    for (alert, key, text) in notices {
        let class = if alert {
            "notice notice-alert"
        } else {
            "notice"
        };
        writeln!(
            out,
            "<span class=\"{class}\"><span class=\"notice-key\">{key}</span><span>{text}</span></span>",
            key = Text(key),
            text = Text(&text),
        )?;
    }
    writeln!(out, "</div>")?;
    Ok(())
}

/// The figures somebody reads before they read anything else.
///
/// Each headline count says how much of it the other scan is known to have
/// looked for. A page that printed only the total would be the one place this
/// whole comparison's care about coverage is thrown away.
fn write_tiles(out: &mut dyn Write, document: &DiffDto<'_>) -> Result<(), ExportError> {
    let summary = &document.summary;

    writeln!(out, "<div class=\"tiles\">")?;
    for (count, label) in [
        (&summary.hosts_added, "hosts appeared"),
        (&summary.hosts_removed, "hosts gone"),
        (&summary.ports_opened, "ports opened"),
        (&summary.ports_closed, "ports closed"),
    ] {
        // A count of nothing has nothing to qualify, and "all confirmed" under a
        // zero reads as an answer to a question nobody asked.
        let note = match (count.total, count.total - count.confirmed) {
            (0, _) => "none".to_string(),
            (_, 0) => "all confirmed".to_string(),
            (_, unconfirmed) => format!("{unconfirmed} nobody had looked for"),
        };
        tile(out, count.total, label, &note)?;
    }

    tile(
        out,
        summary.hosts_changed,
        "hosts changed",
        "in both scans, and different",
    )?;
    tile(
        out,
        summary.certificates_rotated + summary.certificates_expiring + summary.certificates_expired,
        "certificates",
        "rotated, expiring or lapsed",
    )?;
    writeln!(out, "</div>")?;
    Ok(())
}

fn tile(out: &mut dyn Write, value: usize, label: &str, note: &str) -> Result<(), ExportError> {
    writeln!(
        out,
        "<div class=\"tile\"><div class=\"tile-value\">{value}</div><div class=\"tile-label\">{label}</div><div class=\"tile-note\">{note}</div></div>",
        label = Text(label),
        note = Text(note),
    )?;
    Ok(())
}

/// One card per host that differs, ascending by address.
fn write_hosts(
    out: &mut dyn Write,
    diff: &ScanDiff,
    document: &DiffDto<'_>,
) -> Result<(), ExportError> {
    if document.hosts.is_empty() {
        writeln!(
            out,
            "<section class=\"section\"><p class=\"empty\">Nothing changed between these two scans.</p></section>"
        )?;
        return Ok(());
    }

    writeln!(
        out,
        "<section class=\"section\">\n<h2 class=\"section-title\">Hosts <span class=\"section-count\">{}</span></h2>",
        document.hosts.len()
    )?;

    for (delta, dto) in diff.hosts().iter().zip(&document.hosts) {
        write_host(out, delta, dto)?;
    }

    writeln!(out, "</section>")?;
    Ok(())
}

/// One host's card: what happened to it, what moved, and which endpoints moved.
fn write_host(
    out: &mut dyn Write,
    delta: &HostDelta,
    dto: &HostDeltaDto<'_>,
) -> Result<(), ExportError> {
    let (border, tone, word) = match delta.presence() {
        Presence::Added { .. } => ("d-added", TONE_FOUND, "appeared"),
        Presence::Removed { .. } => ("d-removed", TONE_NONE, "gone"),
        Presence::Both => ("d-changed", TONE_PARTIAL, "changed"),
    };

    write!(
        out,
        "<article class=\"host {border}\">\n<header class=\"host-head\"><span class=\"host-ip\">{ip}</span>",
        ip = Text(&dto.address),
    )?;

    // From the rendered record rather than the host itself: the masking policy
    // is applied on the way into the document, and reaching past it for a
    // hostname would put an unredacted one on a page meant to carry none.
    let named = dto
        .current
        .as_ref()
        .or(dto.baseline.as_ref())
        .and_then(|host| host.hostname.as_deref());
    if let Some(hostname) = named {
        write!(out, "<span class=\"host-name\">{}</span>", Text(hostname))?;
    }
    write!(out, "<span class=\"tag {tone}\">{word}</span>")?;

    if dto.regrouped {
        write!(
            out,
            "<span class=\"tag {TONE_INERT}\">regrouped {}→{}</span>",
            dto.records.baseline, dto.records.current
        )?;
    }
    writeln!(out, "</header>\n<div class=\"host-body\">")?;

    for change in &dto.changes {
        write_change(out, change)?;
    }
    for port in &dto.ports {
        write_port(out, port)?;
    }

    // Said once, and only where it changes what the card above means.
    if let Some(coverage) = delta.presence().counterpart_coverage()
        && !delta.presence().is_confirmed()
    {
        let other = if delta.presence().is_added() {
            "the earlier scan"
        } else {
            "the later scan"
        };
        writeln!(
            out,
            "<span class=\"unconfirmed\">{other} is not known to have covered this host ({coverage}), so this is a fact about the scans rather than about the network.</span>",
            coverage = Text(coverage_word(coverage)),
        )?;
    }

    writeln!(out, "</div>\n</article>")?;
    Ok(())
}

/// One endpoint's changes, under the endpoint that carries them.
fn write_port(out: &mut dyn Write, port: &PortDeltaDto) -> Result<(), ExportError> {
    let verdict = if port.opened {
        Some(("opened", TONE_FOUND))
    } else if port.closed {
        Some(("closed", TONE_NONE))
    } else {
        None
    };

    write!(
        out,
        "<div class=\"change\"><span class=\"change-kind mono\">{port}/{protocol}</span>",
        port = port.port,
        protocol = Text(port.protocol),
    )?;

    if let Some((word, tone)) = verdict {
        write!(out, "<span class=\"tag {tone}\">{word}</span>")?;
    }
    if !port.confirmed {
        write!(
            out,
            "<span class=\"tag {TONE_INERT}\">not looked for</span>"
        )?;
    }
    writeln!(out, "</div>")?;

    for change in &port.changes {
        write_change(out, change)?;
    }
    Ok(())
}

/// One field that moved, as the pair it is.
fn write_change(out: &mut dyn Write, change: &ChangeDto) -> Result<(), ExportError> {
    write!(
        out,
        "<div class=\"change\"><span class=\"change-kind\">{kind}</span>",
        kind = Text(&change.kind.replace('_', " ")),
    )?;

    match (&change.before, &change.after) {
        (Some(before), Some(after)) => write!(
            out,
            "<span class=\"change-was mono\">{was}</span><span class=\"change-arrow\">→</span><span class=\"mono\">{now}</span>",
            was = Text(before),
            now = Text(after),
        )?,
        (None, Some(after)) => write!(out, "<span class=\"mono\">{}</span>", Text(after))?,
        // A set member that went is said once: the kind above already reads
        // "address lost", and an arrow pointing at "nothing" after it says the
        // same thing a second time.
        (Some(before), None) if change.kind.ends_with("_lost") => {
            write!(
                out,
                "<span class=\"change-was mono\">{}</span>",
                Text(before)
            )?;
        }
        (Some(before), None) => write!(
            out,
            "<span class=\"change-was mono\">{}</span><span class=\"change-arrow\">→</span><span>nothing</span>",
            Text(before)
        )?,
        (None, None) => {}
    }

    writeln!(out, "</div>")?;
    Ok(())
}

/// What a coverage answer is called on a page a person reads.
fn coverage_word(coverage: crate::diff::Coverage) -> &'static str {
    use crate::diff::Coverage;
    match coverage {
        Coverage::Covered => "covered",
        Coverage::Withheld => "a policy forbade it",
        Coverage::OutOfScope => "outside what it walked",
        Coverage::Unstated => "it does not say",
    }
}

/// Which two scans this was, and what wrote the page.
fn write_colophon(out: &mut dyn Write, document: &DiffDto<'_>) -> Result<(), ExportError> {
    writeln!(
        out,
        "<footer class=\"colophon\"><p>Comparing {before} ({before_at}) with {after} ({after_at}). Written by {ENGINE_NAME} {ENGINE_VERSION} at {generated}.</p></footer>",
        before = Text(&document.baseline.engine_version),
        before_at = Text(&document.baseline.at),
        after = Text(&document.current.engine_version),
        after_at = Text(&document.current.at),
        generated = Text(&document.generated_at),
    )?;
    Ok(())
}

// ╔════════════════════════════════════════════╗
// ║ ████████╗███████╗███████╗████████╗███████╗ ║
// ║ ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝ ║
// ║    ██║   █████╗  ███████╗   ██║   ███████╗ ║
// ║    ██║   ██╔══╝  ╚════██║   ██║   ╚════██║ ║
// ║    ██║   ███████╗███████║   ██║   ███████║ ║
// ║    ╚═╝   ╚══════╝╚══════╝   ╚═╝   ╚══════╝ ║
// ╚════════════════════════════════════════════╝
#[cfg(test)]
mod tests {
    use super::*;
    use crate::export::Redaction;
    use crate::export::fixture;
    use crate::scanner::report::ScanReport;

    fn page(diff: &ScanDiff) -> String {
        let mut bytes = Vec::new();
        HtmlDiffExporter::default()
            .export(diff, &mut bytes)
            .expect("the export succeeds");
        String::from_utf8(bytes).expect("a page is text")
    }

    fn compared() -> String {
        let (before, after) = fixture::compared();
        page(&ScanDiff::between(&before, &after))
    }

    #[test]
    fn the_page_is_one_file_that_fetches_nothing() {
        let page = compared();

        assert!(page.starts_with("<!doctype html>"), "{}", &page[..40]);
        assert!(page.trim_end().ends_with("</html>"));
        assert!(page.contains("<style>"), "the stylesheet is inlined");

        for outside in ["http://", "https://", "<script", "<img", "@import", "src="] {
            assert!(
                !page.contains(outside),
                "a comparison page reaches outside itself: {outside}"
            );
        }
    }

    /// The three states are what a reader takes in first, so each has to be on
    /// the page and be told apart.
    #[test]
    fn every_host_carries_what_happened_to_it() {
        let page = compared();

        for state in ["d-added", "d-removed", "d-changed"] {
            assert!(page.contains(state), "no host is {state}: {page}");
        }
        for word in ["appeared", "gone", "changed"] {
            assert!(page.contains(word), "{word} is not said anywhere");
        }
    }

    /// The number the whole comparison is arranged to protect. A page that
    /// printed only the total would throw it away at the last step.
    #[test]
    fn a_headline_count_says_how_much_of_it_nobody_looked_for() {
        let (before, after) = fixture::compared();
        // A sweep that walked no ports: everything the port scan found on them
        // is then something nobody had looked for.
        let unconfirmed = page(&ScanDiff::between(&fixture::report(), &after));

        assert!(
            unconfirmed.contains("nobody had looked for"),
            "the page does not say how much of its counts is unconfirmed: {unconfirmed}"
        );
        // The endpoints nobody had looked for are marked where they are shown.
        assert!(unconfirmed.contains("not looked for"), "{unconfirmed}");

        // And where everything is confirmed it says so rather than staying
        // silent, which would read as the question not having been asked.
        let confirmed = page(&ScanDiff::between(&before, &after));
        assert!(confirmed.contains("all confirmed"), "{confirmed}");

        // And a count of nothing has nothing to qualify.
        let (before, _) = fixture::compared();
        let quiet = page(&ScanDiff::between(&before, &before));
        assert!(quiet.contains("tile-note\">none"), "{quiet}");
        assert!(!quiet.contains("all confirmed"), "{quiet}");
    }

    /// A host on ground the other scan was forbidden is not a host that
    /// appeared, and the card says which of the two it is.
    #[test]
    fn a_host_on_ground_nobody_covered_says_so_on_its_card() {
        use crate::model::host::{Host, HostStatus};
        use std::net::{IpAddr, Ipv4Addr};

        // The report fixture walks 192.168.0.0/25 and is forbidden the rest, so
        // a host in the upper half was ground it was told not to look at.
        let mut withheld = Host::new(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 200)));
        withheld.set_status(HostStatus::Up);

        let later = ScanReport::recorded("test", Vec::new(), vec![withheld]);
        let page = page(&ScanDiff::between(&fixture::report(), &later));

        assert!(
            page.contains("class=\"unconfirmed\""),
            "the card does not say nobody looked: {page}"
        );
        assert!(page.contains("a policy forbade it"), "{page}");
        assert!(
            page.contains("about the scans rather than about the network"),
            "{page}"
        );
    }

    /// A set member that went says so once. The kind already reads "address
    /// lost"; an arrow pointing at "nothing" after it says it twice.
    #[test]
    fn a_set_member_that_went_is_not_also_pointed_at_nothing() {
        let (before, after) = fixture::compared();
        let page = page(&ScanDiff::between(&before, &after));

        for row in page.split("<div class=\"change\">") {
            if row.contains("lost") {
                assert!(!row.contains("nothing"), "a departure is said twice: {row}");
            }
        }
    }

    #[test]
    fn what_moved_is_shown_as_the_pair_it_is() {
        let page = compared();

        assert!(page.contains("change-was"), "no before/after pair: {page}");
        assert!(
            page.contains("service version"),
            "an underscore reached the page"
        );
        assert!(page.contains("8.9p1") && page.contains("9.6p1"), "{page}");
    }

    #[test]
    fn an_unchanged_comparison_still_writes_a_page() {
        let (before, _) = fixture::compared();
        let page = page(&ScanDiff::between(&before, &before));

        assert!(
            page.contains("Nothing changed between these two scans"),
            "{page}"
        );
        assert!(page.contains("no change"), "{page}");
        assert!(page.trim_end().ends_with("</html>"));
    }

    /// A scanned host chooses its own hostname, banner and certificate subject,
    /// and a page is where those are read by a person.
    #[test]
    fn every_value_a_scanned_host_chose_reaches_the_page_escaped() {
        let hostile = fixture::hostile();
        let page = page(&ScanDiff::between(&fixture::report(), &hostile));

        assert!(
            !page.contains(fixture::HOSTILE),
            "an attacker-controlled string reached the page intact"
        );
        assert!(!page.contains("<script>"), "markup survived: {page}");
        assert!(
            !page.contains('\u{202e}'),
            "a right-to-left override survived, which reverses the text after it"
        );
        assert!(
            page.contains("&lt;script&gt;"),
            "and it is on the page, escaped"
        );
    }

    #[test]
    fn redaction_reaches_the_page() {
        let (before, after) = fixture::compared();
        let diff = ScanDiff::between(&before, &after);

        let mut bytes = Vec::new();
        HtmlDiffExporter::new(ExportOptions::new().with_redaction(Redaction::Standard))
            .export(&diff, &mut bytes)
            .expect("the export succeeds");
        let page = String::from_utf8(bytes).expect("text");

        assert!(
            !page.contains("gateway.local"),
            "a hostname survived: {page}"
        );
        assert!(
            !page.contains("2c:cf:67:f2:51:e3"),
            "a MAC survived: {page}"
        );
    }

    #[test]
    fn a_caller_can_name_the_page() {
        let (before, after) = fixture::compared();
        let diff = ScanDiff::between(&before, &after);

        let mut bytes = Vec::new();
        HtmlDiffExporter::default()
            .with_heading("Acme, week 32")
            .export(&diff, &mut bytes)
            .expect("the export succeeds");
        let page = String::from_utf8(bytes).expect("text");

        assert!(page.contains("<title>Acme, week 32</title>"), "{page}");
        assert!(page.contains("<h1>Acme, week 32</h1>"), "{page}");
    }
}