vcon-server 0.4.13

Stores vCons encrypted at rest, on local disk or in an S3 bucket (as objects or Parquet files), and returns them to authorized readers.
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
//! The console: a web page served on the TLS listener at `/console/`. It
//! is a client of the HTTP API and nothing more. Its files are built
//! into the binary and need no key: they are the same for every caller,
//! hold no data and change nothing. Every request the page makes is an API
//! request that carries the key the user signed in with.

use axum::Router;
use axum::extract::Request;
use axum::http::header::{CONTENT_SECURITY_POLICY, CONTENT_TYPE, LOCATION};
use axum::http::{HeaderName, HeaderValue, StatusCode};
use axum::middleware::{self, Next};
use axum::response::{IntoResponse, Response};
use axum::routing::{MethodRouter, any, get, post};

/// One console file: its name under `/console/` (empty for the page),
/// its bytes as built into the binary, and its media type.
pub type File = (&'static str, &'static [u8], &'static str);

/// Every file the console is made of. The router serves exactly these, and
/// a test holds this list, the `console` directory and the package's
/// `include` list to one another.
pub const FILES: [File; 6] = [
    (
        "index.html",
        include_bytes!("../console/index.html"),
        "text/html; charset=utf-8",
    ),
    (
        "console.js",
        include_bytes!("../console/console.js"),
        "text/javascript; charset=utf-8",
    ),
    (
        "format.js",
        include_bytes!("../console/format.js"),
        "text/javascript; charset=utf-8",
    ),
    (
        "console.css",
        include_bytes!("../console/console.css"),
        "text/css; charset=utf-8",
    ),
    (
        "player.html",
        include_bytes!("../console/player.html"),
        "text/html; charset=utf-8",
    ),
    (
        "player.js",
        include_bytes!("../console/player.js"),
        "text/javascript; charset=utf-8",
    ),
];

/// The file the bare `/console/` path answers with.
const PAGE: &str = "index.html";

/// The path a console file is served at.
#[must_use]
pub fn path_of(name: &str) -> String {
    if name == PAGE {
        "/console/".to_owned()
    } else {
        format!("/console/{name}")
    }
}

/// The headers on every console answer: no inline code, nothing from
/// another origin, no framing, no sniffing, no referrer, no caching. The
/// player's page alone carries [`PLAYER_POLICY`] in place of the first.
pub const HEADERS: [(&str, &str); 7] = [
    (
        "content-security-policy",
        "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self'; \
         connect-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'; \
         require-trusted-types-for 'script'; trusted-types 'none'; \
         report-uri /console/csp-report",
    ),
    ("x-frame-options", "DENY"),
    ("x-content-type-options", "nosniff"),
    ("referrer-policy", "no-referrer"),
    ("cache-control", "no-store"),
    ("cross-origin-opener-policy", "same-origin"),
    ("cross-origin-resource-policy", "same-origin"),
];

/// The policy of the player's page: the console's, but a recording may
/// play from a blob made in the page (`media-src blob:`) and the page may
/// make no request at all (`connect-src 'none'`). The console hands the
/// player the bytes; the player fetches nothing.
pub const PLAYER_POLICY: &str = "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self'; \
     connect-src 'none'; media-src blob:; base-uri 'none'; form-action 'none'; \
     frame-ancestors 'none'; require-trusted-types-for 'script'; trusted-types 'none'; \
     report-uri /console/csp-report";

/// The path of the player's page, the one console answer with a policy
/// of its own.
pub const PLAYER_PATH: &str = "/console/player.html";

/// The content-security policy of the answer at `path`: the player's for
/// the player's page and the console's for everything else.
#[must_use]
pub fn policy_for(path: &str) -> &'static str {
    if path == PLAYER_PATH {
        PLAYER_POLICY
    } else {
        HEADERS[0].1
    }
}

/// The console's routes, merged into the API's router: its files,
/// `/console` sent on to `/console/`, and 404 for any other path under
/// it, each answer with the headers. A report sent to [`REPORT_PATH`] is
/// answered 204 and dropped; the server's own router counts each one
/// (`api::router`).
pub fn router<S: Clone + Send + Sync + 'static>() -> Router<S> {
    router_with(post(dropped))
}

/// The public router's answer to a report: taken, and dropped.
async fn dropped() -> StatusCode {
    StatusCode::NO_CONTENT
}

/// The path the policies tell a browser to send its reports of a
/// violation to.
pub const REPORT_PATH: &str = "/console/csp-report";

/// [`router`], with `report` serving [`REPORT_PATH`].
pub(crate) fn router_with<S: Clone + Send + Sync + 'static>(report: MethodRouter<S>) -> Router<S> {
    let mut router = Router::new().route(
        "/console",
        get(|| async { (StatusCode::PERMANENT_REDIRECT, [(LOCATION, "/console/")]) }),
    );
    for (name, body, media_type) in FILES {
        router = router.route(&path_of(name), get(move || file(body, media_type)));
    }
    router
        .route(REPORT_PATH, report)
        .route("/console/{*other}", any(|| async { StatusCode::NOT_FOUND }))
        // On the console's own routes only, so the API's answers and its
        // fallback are left as they are.
        .route_layer(middleware::from_fn(with_headers))
}

async fn file(body: &'static [u8], media_type: &'static str) -> Response {
    ([(CONTENT_TYPE, media_type)], body).into_response()
}

/// Puts every header in `HEADERS` on a console answer. The player's
/// page, when it is served, carries its own policy in place of the
/// console's; a refusal at its path is not the page and does not.
async fn with_headers(request: Request, next: Next) -> Response {
    let policy = policy_for(request.uri().path());
    let mut answer = next.run(request).await;
    for (name, value) in HEADERS {
        answer.headers_mut().insert(
            HeaderName::from_static(name),
            HeaderValue::from_static(value),
        );
    }
    if answer.status() == StatusCode::OK {
        answer
            .headers_mut()
            .insert(CONTENT_SECURITY_POLICY, HeaderValue::from_static(policy));
    }
    answer
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    fn source(name: &str) -> String {
        let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("console")
            .join(name);
        std::fs::read_to_string(path).expect("the source file")
    }

    /// Every script of the console, by name.
    fn scripts() -> Vec<(&'static str, String)> {
        super::FILES
            .into_iter()
            .filter(|(name, _, _)| name.ends_with(".js"))
            .map(|(name, _, _)| (name, source(name)))
            .collect()
    }

    /// The directory, the served list and the package's include list name
    /// the same files, so a file added to one and not the others fails
    /// here and not as a 404 in a browser or a broken published crate.
    #[test]
    fn the_directory_the_served_files_and_the_package_agree() {
        let directory = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("console");
        let mut on_disk: Vec<String> = std::fs::read_dir(directory)
            .expect("the console directory")
            .map(|entry| {
                entry
                    .expect("an entry")
                    .file_name()
                    .into_string()
                    .expect("a UTF-8 name")
            })
            .collect();
        on_disk.sort();
        let mut served: Vec<String> = super::FILES
            .into_iter()
            .map(|(name, _, _)| name.to_owned())
            .collect();
        served.sort();
        assert_eq!(served, on_disk, "served files against the directory");
        let manifest =
            std::fs::read_to_string(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml"))
                .expect("the manifest");
        for name in &on_disk {
            assert!(
                manifest.contains(&format!("\"/console/{name}\",")),
                "the package includes {name}"
            );
        }
    }

    /// Each file is served with the media type its extension needs: a
    /// module script under another type is refused by the browser.
    #[test]
    fn each_file_has_the_media_type_of_its_extension() {
        for (name, body, media_type) in super::FILES {
            let expected = match name.rsplit('.').next() {
                Some("html") => "text/html; charset=utf-8",
                Some("js") => "text/javascript; charset=utf-8",
                Some("css") => "text/css; charset=utf-8",
                other => panic!("{name}: no media type for {other:?}"),
            };
            assert_eq!(media_type, expected, "{name}");
            assert!(!body.is_empty(), "{name} is not empty");
            assert!(std::str::from_utf8(body).is_ok(), "{name} is UTF-8");
        }
    }

    #[test]
    fn the_page_is_served_at_the_directory_path_and_the_rest_by_name() {
        assert_eq!(super::path_of("index.html"), "/console/", "index.html");
        assert_eq!(
            super::path_of("console.js"),
            "/console/console.js",
            "console.js"
        );
        assert_eq!(
            super::path_of("console.css"),
            "/console/console.css",
            "console.css"
        );
        assert_eq!(
            super::path_of("format.js"),
            "/console/format.js",
            "format.js"
        );
    }

    /// The headers hold the rules the design sets, so a weaker policy
    /// fails here, not only in a browser: the CSP allows nothing by
    /// default, each source is the console's own origin or none, and no
    /// script may run but the console's file; the page is never framed,
    /// cached, sniffed or sent a referrer.
    #[test]
    fn the_headers_are_the_designed_ones() {
        let headers: std::collections::BTreeMap<&str, &str> = super::HEADERS.into_iter().collect();
        assert_eq!(headers.len(), super::HEADERS.len(), "each header once");
        let csp: std::collections::BTreeMap<&str, Vec<&str>> = headers["content-security-policy"]
            .split(';')
            .map(|d| {
                let mut words = d.split_whitespace();
                (words.next().expect("a directive"), words.collect())
            })
            .collect();
        for (directive, sources) in [
            ("default-src", ["'none'"]),
            ("script-src", ["'self'"]),
            ("frame-ancestors", ["'none'"]),
            ("base-uri", ["'none'"]),
            ("form-action", ["'none'"]),
            ("require-trusted-types-for", ["'script'"]),
            ("trusted-types", ["'none'"]),
        ] {
            assert_eq!(csp.get(directive), Some(&sources.to_vec()), "{directive}");
        }
        // Where violations are reported is a path on the console's own
        // origin; it lets a page load or do nothing.
        let mut csp = csp;
        assert_eq!(
            csp.remove("report-uri"),
            Some(vec!["/console/csp-report"]),
            "report-uri names a path, so the console's own origin"
        );
        for (directive, sources) in &csp {
            assert!(
                sources
                    .iter()
                    .all(|s| ["'none'", "'self'", "'script'"].contains(s)),
                "{directive} allows only the console's own origin: {sources:?}"
            );
        }
        for (name, value) in [
            ("x-frame-options", "DENY"),
            ("x-content-type-options", "nosniff"),
            ("referrer-policy", "no-referrer"),
            ("cache-control", "no-store"),
            ("cross-origin-opener-policy", "same-origin"),
            ("cross-origin-resource-policy", "same-origin"),
        ] {
            assert_eq!(headers.get(name), Some(&value), "{name}");
        }
    }

    /// The page loads its one script and one stylesheet and holds no
    /// inline script, style or event handler, which the CSP would refuse.
    #[test]
    fn the_page_holds_no_inline_code() {
        let page = source("index.html");
        assert!(
            page.contains(r#"<script type="module" src="console.js"></script>"#),
            "the page loads its script"
        );
        assert!(
            page.contains(r#"<link rel="stylesheet" href="console.css">"#),
            "the page loads its styles"
        );
        let lower = page.to_lowercase();
        assert_eq!(lower.matches("<script").count(), 1, "one script element");
        assert!(!lower.contains("<style"), "no style element");
        assert!(!lower.contains(" style="), "no style attribute");
        assert!(!has_event_handler(&page), "no event-handler attribute");
    }

    /// Whether markup holds an attribute such as `onclick=`: whitespace,
    /// `on`, a name of letters, then `=` after any spaces.
    fn has_event_handler(html: &str) -> bool {
        let lower = html.to_lowercase();
        lower
            .match_indices("on")
            .filter(|(at, _)| lower[..*at].ends_with(char::is_whitespace))
            .any(|(at, _)| {
                let rest = &lower[at + 2..];
                let name = rest.chars().take_while(char::is_ascii_lowercase).count();
                name > 0 && rest[name..].trim_start().starts_with('=')
            })
    }

    #[test]
    fn an_event_handler_is_found_wherever_it_is() {
        for found in [
            "<b onclick=\"x\">",
            "<b\nonload = x>",
            "<img src=a ONERROR=x>",
        ] {
            assert!(has_event_handler(found), "{found}");
        }
        for clean in [
            "<b class=on>",
            "<b>on=</b>",
            "<a href=\"x\">done</a>",
            "<b on=x>",
        ] {
            assert!(!has_event_handler(clean), "{clean}");
        }
    }

    /// No script reaches the page through markup, runs code from strings
    /// or keeps anything in browser storage; values are set as text.
    #[test]
    fn no_script_uses_an_html_sink_or_storage() {
        for (name, script) in scripts() {
            for refused in [
                "innerHTML",
                "outerHTML",
                "insertAdjacentHTML",
                "document.write",
                "eval(",
                "Function(",
                "setTimeout(\"",
                "setInterval(\"",
                "localStorage",
                "sessionStorage",
                "indexedDB",
                "document.cookie",
                "caches.",
            ] {
                assert!(!script.contains(refused), "{name} uses {refused}");
            }
        }
        assert!(
            source("console.js").contains("textContent"),
            "values are set as text"
        );
    }

    /// Every request goes through one call of `fetch`, which never lets
    /// the browser keep the answer, send a cookie or follow a redirect.
    #[test]
    fn every_request_goes_through_one_fetch() {
        let calls: usize = scripts()
            .iter()
            .map(|(_, script)| script.matches("fetch(").count())
            .sum();
        assert_eq!(calls, 1, "one fetch call in all the scripts");
        let script = source("console.js");
        for option in [
            r#"cache: "no-store""#,
            r#"credentials: "omit""#,
            r#"redirect: "error""#,
        ] {
            assert!(script.contains(option), "fetch is made with {option}");
        }
    }

    /// A policy as its directives and their sources.
    fn directives(policy: &str) -> std::collections::BTreeMap<&str, Vec<&str>> {
        policy
            .split(';')
            .map(|d| {
                let mut words = d.split_whitespace();
                (words.next().expect("a directive"), words.collect())
            })
            .collect()
    }

    /// The policy every console answer has carried since the console
    /// shipped, written out here so a change to it fails a test.
    const STRICT: &str = "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self'; \
         connect-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'; \
         require-trusted-types-for 'script'; trusted-types 'none'; \
         report-uri /console/csp-report";

    /// The player's page may play a recording the console hands it, from
    /// a blob made in the page, and may make no request of its own. Every
    /// other rule is the console's.
    #[test]
    fn the_player_page_has_the_strict_policy_but_plays_a_blob_and_connects_nowhere() {
        let mut expected = directives(STRICT);
        expected.insert("connect-src", vec!["'none'"]);
        expected.insert("media-src", vec!["blob:"]);
        assert_eq!(
            directives(super::policy_for("/console/player.html")),
            expected,
            "the player's policy"
        );
    }

    /// The console's policy, byte for byte: what a browser is sent.
    #[test]
    fn the_strict_policy_is_the_one_written_out() {
        assert_eq!(
            super::HEADERS[0].0,
            "content-security-policy",
            "the first header"
        );
        assert_eq!(super::HEADERS[0].1, STRICT, "the console's policy");
    }

    /// Both policies tell a browser where to report a violation, with
    /// `report-uri` and the path.
    #[test]
    fn both_policies_report_violations_to_the_report_path() {
        for path in ["/console/", super::PLAYER_PATH] {
            let policy = directives(super::policy_for(path));
            assert_eq!(policy["report-uri"], [super::REPORT_PATH], "{path}");
        }
    }

    /// The reporting directive comes last, so the rules a reader looks
    /// for first are where they were.
    #[test]
    fn the_reporting_directive_ends_both_policies() {
        for path in ["/console/", super::PLAYER_PATH] {
            let policy = super::policy_for(path);
            assert!(
                policy.ends_with("'none'; report-uri /console/csp-report"),
                "{path}: {policy}"
            );
        }
    }

    /// Neither policy has `report-to`, and no answer has the
    /// `Reporting-Endpoints` header it would need. A browser that knows
    /// `report-to` does not act on `report-uri` beside it, and in the
    /// browser tests Chromium, given `report-to`, sent no report to the
    /// test server in 100 seconds, where with `report-uri` alone it sent
    /// one in a second (console-tests/tests/csp-report.spec.js).
    #[test]
    fn no_policy_names_a_reporting_api_endpoint() {
        for path in ["/console/", super::PLAYER_PATH] {
            let policy = directives(super::policy_for(path));
            assert_eq!(policy.get("report-to"), None, "{path}");
        }
        for (name, _) in super::HEADERS {
            assert_ne!(name, "reporting-endpoints", "no endpoint is named");
            assert_ne!(name, "report-to", "nor in the header's older form");
        }
    }

    /// The report path is under the console's, is no file's path, and is
    /// answered with the console's policy, not the player's.
    #[test]
    fn the_report_path_is_the_consoles_and_no_files() {
        assert_eq!(super::REPORT_PATH, "/console/csp-report", "the path");
        for (name, _, _) in super::FILES {
            assert_ne!(super::path_of(name), super::REPORT_PATH, "{name}");
        }
        assert_eq!(super::policy_for(super::REPORT_PATH), STRICT, "its policy");
    }

    /// Reporting adds nothing a page may do: but for the reporting
    /// directive, the policies are the ones the console shipped with.
    #[test]
    fn but_for_reporting_the_policies_are_as_they_were() {
        let before = "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self'; \
             connect-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'; \
             require-trusted-types-for 'script'; trusted-types 'none'";
        let mut expected = directives(before);
        let mut now = directives(super::HEADERS[0].1);
        assert_eq!(
            now.remove("report-uri"),
            Some(vec![super::REPORT_PATH]),
            "report-uri"
        );
        assert_eq!(now, expected, "the console's policy");
        expected.insert("connect-src", vec!["'none'"]);
        expected.insert("media-src", vec!["blob:"]);
        let mut now = directives(super::PLAYER_POLICY);
        assert_eq!(
            now.remove("report-uri"),
            Some(vec![super::REPORT_PATH]),
            "report-uri"
        );
        assert_eq!(now, expected, "the player's policy");
    }

    /// The public router, which has no log to count in, still takes a
    /// report at the path its policies name.
    #[tokio::test]
    async fn the_public_routers_answer_to_a_report_is_204() {
        // Its place in the router, and the headers on it, are the same
        // route's that api::csp_report tests over HTTPS.
        assert_eq!(
            super::dropped().await,
            super::StatusCode::NO_CONTENT,
            "the status"
        );
        let _: super::Router<()> = super::router();
    }

    /// Only the player's page, by its exact path, has the player's policy:
    /// a path that is nearly it, holds it or adds to it has the console's.
    #[test]
    fn a_path_that_is_not_exactly_the_players_has_the_strict_policy() {
        for path in [
            "/console/csp-report",
            "/console/",
            "/console/console.js",
            "/console/player.js",
            "/console/player.html/",
            "/console/player.html/x",
            "/console/PLAYER.HTML",
            "/console/player.htm",
            "/console/x/player.html",
            "/console//player.html",
            "/console/player.html%2f",
            "/player.html",
            "/console/aplayer.html",
            "",
        ] {
            assert_eq!(super::policy_for(path), STRICT, "{path}");
        }
    }

    /// The player's page loads its one script and the console's
    /// stylesheet, holds no inline code, and its audio element has the
    /// browser's own controls and nothing to play until the script is
    /// handed a recording: no source, and it never starts by itself.
    #[test]
    fn the_players_page_holds_no_inline_code_and_nothing_to_play() {
        let page = source("player.html");
        assert!(
            page.contains(r#"<script type="module" src="player.js"></script>"#),
            "the page loads its script"
        );
        assert!(
            page.contains(r#"<link rel="stylesheet" href="console.css">"#),
            "the page loads the console's styles"
        );
        let lower = page.to_lowercase();
        assert_eq!(lower.matches("<script").count(), 1, "one script element");
        assert!(!lower.contains("<style"), "no style element");
        assert!(!lower.contains(" style="), "no style attribute");
        assert!(!has_event_handler(&page), "no event-handler attribute");
        assert_eq!(lower.matches("<audio").count(), 1, "one audio element");
        let audio = &lower[lower.find("<audio").expect("the audio element")..];
        let audio = &audio[..audio.find('>').expect("its end")];
        assert!(audio.contains(" controls"), "the browser's own controls");
        for refused in [" src", " autoplay", " loop", " crossorigin"] {
            assert!(
                !audio.contains(refused),
                "the audio element has no{refused}"
            );
        }
        for refused in [
            "<source", "<track", "<iframe", "<img", "<object", "<embed", "<a ",
        ] {
            assert!(!lower.contains(refused), "the page has no {refused}");
        }
    }

    /// The player is handed its recording: its script makes no request of
    /// any kind, loads no other code, and posts a message to no origin
    /// but its own.
    #[test]
    fn the_players_script_makes_no_request_and_names_its_own_origin() {
        let script = source("player.js");
        for refused in [
            "fetch(",
            "XMLHttpRequest",
            "WebSocket",
            "EventSource",
            "sendBeacon",
            "import(",
            "serviceWorker",
            "Worker(",
            "\"*\"",
            "'*'",
            "http:",
            "https:",
        ] {
            assert!(!script.contains(refused), "player.js uses {refused}");
        }
        assert!(
            script.contains("location.origin"),
            "messages name the page's own origin"
        );
        let imports: Vec<&str> = script
            .lines()
            .filter(|line| line.starts_with("import "))
            .collect();
        assert_eq!(imports.len(), 1, "one import: {imports:?}");
        assert!(
            imports[0].ends_with(r#" from "./format.js";"#),
            "the console's own rules, from its own file: {imports:?}"
        );
    }

    /// The console hands a recording to the player's window alone: no
    /// script posts a message to every origin.
    #[test]
    fn no_script_posts_a_message_to_every_origin() {
        let names: Vec<&str> = scripts().iter().map(|(name, _)| *name).collect();
        assert!(
            names.contains(&"player.js"),
            "the player's script is served"
        );
        for (name, script) in scripts() {
            for refused in ["\"*\"", "'*'"] {
                assert!(!script.contains(refused), "{name} names every origin");
            }
        }
    }
}