Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/path_headers.rs

5.0 KiB, 1 run

created by r1870400018:76793, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! A vhost's `path_headers` reach the responses to the paths each entry names and no others,
2//! after `response_headers` and replacing a field of the same name, so a frame's shell can run
3//! under a policy of its own beside a strict page. A path is matched as the static router reads
4//! it, and one ending in `/` covers its directory by whole segments. The key is read from config,
5//! and a path that is not absolute and normal, or a header the vhost could not set, is refused.
6//!
7//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
8//! Anthropic Claude
9
10mod vhost_rig;
11
12use vhost_rig::{
13 Rig,
14 Scratch,
15 Site,
16};
17
18use oxedyne_fe2o3_core::prelude::*;
19use oxedyne_fe2o3_jdat::prelude::*;
20use oxedyne_fe2o3_steel::srv::cfg::{
21 PathHeaders,
22 VhostConfig,
23};
24
25const HOST: &str = "frames.test";
26const PAGE: &str = "script-src 'self'";
27const SHELL: &str = "default-src 'none'; script-src 'unsafe-inline'";
28const DOCS: &str = "noindex";
29
30fn entry(path: &str, headers: &[(&str, &str)]) -> PathHeaders {
31 PathHeaders {
32 path: path.to_string(),
33 headers: headers.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect(),
34 }
35}
36
37#[test]
38fn a_path_gets_its_own_headers_and_no_other_does() -> Outcome<()> {
39 let web = res!(Scratch::new("path_headers"));
40 res!(web.put("index.html", "<p>home</p>"));
41 res!(web.put("frame.html", "<p>shell</p>"));
42 res!(web.put("framex.html", "<p>other</p>"));
43 res!(web.dir("docs"));
44 res!(web.put("docs/a.html", "<p>a</p>"));
45 res!(web.put("docsx.html", "<p>x</p>"));
46 let mut site = Site::new(HOST, &web.0);
47 site.response_headers = vec![(fmt!("content-security-policy"), fmt!("{}", PAGE))];
48 site.path_headers = vec![
49 entry("/frame.html", &[("content-security-policy", SHELL)]),
50 entry("/docs/", &[("x-robots-tag", DOCS)]),
51 ];
52 let rig = res!(Rig::new(vec![site]));
53 let runtime = res!(vhost_rig::runtime());
54 runtime.block_on(async {
55 for (path, csp, robots) in [
56 ("/frame.html", SHELL, None),
57 ("//frame.html", SHELL, None),
58 ("/docs/../frame.html", SHELL, None),
59 ("/framex.html", PAGE, None),
60 ("/", PAGE, None),
61 ("/index.html", PAGE, None),
62 ("/docs/a.html", PAGE, Some(DOCS)),
63 ("/docsx.html", PAGE, None),
64 ("/frame.html/missing", PAGE, None),
65 ] {
66 let r = res!(rig.fetch(HOST, "GET", path, &[], "").await);
67 assert_eq!(r.field("content-security-policy").as_deref(), Some(csp),
68 "GET {}: {:?}", path, r);
69 let n = r.head.lines().skip(1)
70 .filter(|l| l.to_lowercase().starts_with("content-security-policy:")).count();
71 assert_eq!(n, 1, "GET {} doubled the policy: {:?}", path, r);
72 assert_eq!(r.field("x-robots-tag").as_deref(), robots, "GET {}: {:?}", path, r);
73 }
74 Ok(())
75 })
76}
77
78#[test]
79fn a_path_entry_is_matched_as_the_router_reads_it() {
80 let shell = entry("/frame.html", &[]);
81 for (req, want) in [
82 ("/frame.html", true), ("//frame.html", true), ("/./frame.html", true),
83 ("/a/../frame.html", true), ("/frame.htm", false), ("/sub/frame.html", false),
84 ] {
85 assert_eq!(shell.matches(req), want, "{}", req);
86 }
87 let dir = entry("/docs/", &[]);
88 for (req, want) in [
89 ("/docs", true), ("/docs/", true), ("/docs/a/b.html", true), ("/docsx", false),
90 ("/", false),
91 ] {
92 assert_eq!(dir.matches(req), want, "{}", req);
93 }
94 assert!(entry("/", &[]).matches("/anything"));
95}
96
97fn vhost(path_headers: &str) -> Outcome<VhostConfig> {
98 let text = fmt!("{{\"hostnames\": [\"a.test\"], \"path_headers\": {}}}", path_headers);
99 let dat = res!(Dat::decode_string(&text));
100 match dat {
101 Dat::Map(m) => VhostConfig::from_datmap(&m),
102 _ => Err(err!("Not a map."; Invalid, Input)),
103 }
104}
105
106#[test]
107fn the_key_is_read_and_checked() -> Outcome<()> {
108 let v = res!(vhost("[{\"path\": \"/frame.html\", \"headers\": {\"Content-Security-Policy\": \"x\"}}]"));
109 assert_eq!(v.path_headers, vec![entry("/frame.html", &[("content-security-policy", "x")])]);
110 assert!(res!(vhost("[]")).path_headers.is_empty());
111 let none = res!(Dat::decode_string("{\"hostnames\": [\"a.test\"]}"));
112 if let Dat::Map(m) = none {
113 assert!(res!(VhostConfig::from_datmap(&m)).path_headers.is_empty());
114 }
115 for bad in [
116 "{}",
117 "[\"/frame.html\"]",
118 "[{\"headers\": {}}]",
119 "[{\"path\": \"/a\"}]",
120 "[{\"path\": \"frame.html\", \"headers\": {}}]",
121 "[{\"path\": \"//frame.html\", \"headers\": {}}]",
122 "[{\"path\": \"/a/../b\", \"headers\": {}}]",
123 "[{\"path\": \"/a\", \"headers\": {\"set-cookie\": \"s=1\"}}]",
124 "[{\"path\": \"/a\", \"headers\": {\"x-a\": \"one\\r\\nx-b: 2\"}}]",
125 "[{\"path\": \"/a\", \"headers\": [\"x-a\"]}]",
126 ] {
127 assert!(vhost(bad).is_err(), "{} must be refused", bad);
128 }
129 Ok(())
130}