Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/aienos.com/blob/main/crates/site-builder/src/components/csp.rs
# aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`aienos.com / crates / site-builder / src / components / csp.rs`
369 lines ยท 18813 bytes Raw Blame History
1 //! Page-level Content-Security-Policy. The site is served by SiteHub on
2 //! Drake's own hardware; every emitted HTML page carries the policy as the
3 //! first element of its head: a `` governs only what follows it.
4 //!
5 //! Scripts run from first-party files only. The single outside script host is
6 //! no advertising-pixel exception; page requests go
7 //! to the analytics hosts in `img-src` and `connect-src`. `style-src` keeps
8 //! `'unsafe-inline'` because every page carries `style` attributes (the
9 //! shared layout sets them on `#root` and `main`) and hand-written pages
10 //! carry `|").unwrap());
102 /// A `javascript:` URL where a URL starts: after `=` (attributes, and `url=`
103 /// in a refresh) or `(` (CSS `url(`), past optional quotes and the spaces
104 /// and controls a browser trims. Prose such as "needs JavaScript: it" is
105 /// not a URL.
106 static JS_URL: LazyLock = LazyLock::new(|| Regex::new(r#"[=(]["'\x00-\x20]*javascript:"#).unwrap());
107 static PAGE_SCRIPT: LazyLock = LazyLock::new(|| Regex::new(r#"src="(/js/[A-Za-z0-9._-]+\.js)""#).unwrap());
108 static COMPILES_WASM: LazyLock =
109 LazyLock::new(|| Regex::new(r"WebAssembly\.(compile|compileStreaming|instantiate|instantiateStreaming|validate|Module)\b").unwrap());
110
111 /// Lowercased markup with character references decoded (`:`, `&tab;`,
112 /// `&newline;` and numeric ones, with or without `;`) and the tabs and line
113 /// breaks a URL parser drops removed, so `java script:` reads as
114 /// `javascript:`.
115 fn url_text(markup: &str) -> String {
116 let lower = markup.to_ascii_lowercase();
117 let mut out = String::with_capacity(lower.len());
118 let mut rest = lower.as_str();
119 while let Some(amp) = rest.find('&') {
120 out.push_str(&rest[..amp]);
121 rest = &rest[amp..];
122 let (decoded, used) = reference(rest);
123 match decoded {
124 Some(c) => out.push(c),
125 None => out.push('&'),
126 }
127 rest = &rest[used..];
128 }
129 out.push_str(rest);
130 out.retain(|c| !matches!(c, '\t' | '\n' | '\r'));
131 out
132 }
133
134 /// One character reference at the start of `s` (which begins with `&`):
135 /// the character and the bytes consumed, or `None` and 1.
136 fn reference(s: &str) -> (Option, usize) {
137 for (name, c) in [(":", ':'), ("&tab;", '\t'), ("&newline;", '\n'), ("(", '(')] {
138 if s.starts_with(name) {
139 return (Some(c), name.len());
140 }
141 }
142 let Some(num) = s.strip_prefix("") else { return (None, 1) };
143 let (hex, digits) = match num.strip_prefix('x') {
144 Some(h) => (true, h),
145 None => (false, num),
146 };
147 let len = digits.bytes().take_while(|b| if hex { b.is_ascii_hexdigit() } else { b.is_ascii_digit() }).count();
148 if len == 0 {
149 return (None, 1);
150 }
151 let value = u32::from_str_radix(&digits[..len], if hex { 16 } else { 10 }).ok().and_then(char::from_u32);
152 let mut used = 2 + usize::from(hex) + len;
153 if s[used..].starts_with(';') {
154 used += 1;
155 }
156 (Some(value.unwrap_or('\u{fffd}')), used)
157 }
158
159 /// One page: exactly one policy, equal to `policy_for`, ahead of anything it
160 /// governs; no inline script other than JSON-LD data; no inline event
161 /// handler; no `javascript:` URL. The runner loads its script and nothing
162 /// else.
163 pub fn check_page(html: &str) -> Result<(), String> {
164 if html.matches(META_OPEN).count() != 1 {
165 return Err("needs exactly one Content-Security-Policy meta".into());
166 }
167 let start = html.find(META_OPEN).unwrap();
168 let body = &html[start + META_OPEN.len()..];
169 let got = &body[..body.find('"').ok_or("unterminated policy")?];
170 let want = policy_for(html);
171 if got != want {
172 return Err(format!("policy {got:?} differs from {want:?}"));
173 }
174 let lower = html.to_ascii_lowercase();
175 let head = lower.find("()));
184 }
185 if attrs.contains("src=") && !c[2].trim().is_empty() {
186 return Err("a script with src must have an empty body".into());
187 }
188 }
189 if want == RUNNER_POLICY && (lower.matches("x