Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/src/html_inert.rs

22.2 KiB, 1 run

created by r1870400018:75227, 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 stranger's HTML made inert for a sandboxed frame: nothing in it fetches until the reader
2//! asks for pictures.
3//!
4//! Hard-won, 2026-10-26: a `<meta>` Content-Security-Policy is not enough. Firefox's speculative
5//! preload fetches `<img srcset>` and `<picture><source srcset>` before the policy applies, so a
6//! tracking pixel fired on open with pictures off. The markup itself must therefore name nothing
7//! remote, and the policy stays as the second wall.
8//!
9//! The output is re-serialised, not patched: every tag is rebuilt from what was read here, every
10//! attribute value double-quoted and escaped, every text `<` escaped and every comment dropped.
11//! The browser therefore parses exactly the structure read here, and a difference between this
12//! reader and a browser's parser (a quote that runs on, a `<title>` read as text) cannot hide a
13//! tag. A `<style>` body has its `<` and `&` written as CSS escapes, so it reads the same whether
14//! the browser takes it as raw text or, inside `<svg>`, as markup.
15
16/// May the markup reach the network?
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum Remote {
19 Blocked, // pictures off: every remote URL is removed
20 Allowed, // the reader asked for pictures: image URLs stay, for the frame's policy to judge
21}
22
23/// Elements whose tags are dropped and whose content stays: the document's own frame (`html`,
24/// `head`, `body`, whose attributes a browser would merge onto the host body), everything that
25/// loads or embeds a document, plugin or resource, and SVG animation, which can set an `href`.
26const DROPPED_TAGS: [&str; 20] = [
27 "animate", "animatecolor", "animatemotion", "animatetransform", "applet", "base", "bgsound",
28 "body", "embed", "fencedframe", "frame", "frameset", "head", "html", "link", "meta", "object",
29 "param", "portal", "set",
30];
31
32/// Elements dropped with their content, which a browser reads as raw text (or RCDATA) and never
33/// shows: code, a nested document, and the page title.
34const DROPPED_RAW: [&str; 5] = ["iframe", "noembed", "noframes", "script", "title"];
35
36/// Attributes whose value is a URL the browser fetches without being asked. Any other attribute
37/// ending in `href` counts too, except a link's.
38const URL_ATTRS: [&str; 14] = [ // fixed: HTML attribute names.
39 "archive", "background", "classid", "code", "codebase", "data", "dynsrc", "href", "icon",
40 "lowsrc", "manifest", "poster", "profile", "src",
41];
42
43/// Attributes holding a list of URLs, never left with pictures off.
44const URL_LISTS: [&str; 2] = ["imagesrcset", "srcset"]; // fixed: HTML attribute names.
45
46/// The named character references whose expansion holds an ASCII character: the only ones that
47/// could spell `url(` or `cid:` in a value, so the only ones this reader must decode.
48const ASCII_NAMED: [(&str, &str); 49] = [
49 ("AMP", "&"), ("DiacriticalGrave", "`"), ("GT", ">"), ("Hat", "^"), ("LT", "<"),
50 ("NewLine", "\n"), ("QUOT", "\""), ("Tab", "\t"), ("UnderBar", "_"), ("VerticalLine", "|"),
51 ("amp", "&"), ("apos", "'"), ("ast", "*"), ("bne", "=\u{20e5}"), ("bsol", "\\"),
52 ("colon", ":"), ("comma", ","), ("commat", "@"), ("dollar", "$"), ("equals", "="),
53 ("excl", "!"), ("fjlig", "fj"), ("grave", "`"), ("gt", ">"), ("lbrace", "{"),
54 ("lbrack", "["), ("lcub", "{"), ("lowbar", "_"), ("lpar", "("), ("lsqb", "["), ("lt", "<"),
55 ("midast", "*"), ("num", "#"), ("nvgt", ">\u{20d2}"), ("nvlt", "<\u{20d2}"),
56 ("percnt", "%"), ("period", "."), ("plus", "+"), ("quest", "?"), ("quot", "\""),
57 ("rbrace", "}"), ("rbrack", "]"), ("rcub", "}"), ("rpar", ")"), ("rsqb", "]"),
58 ("semi", ";"), ("sol", "/"), ("verbar", "|"), ("vert", "|"),
59];
60
61/// The legacy references a browser decodes without a `;` that expand to ASCII.
62const ASCII_LEGACY: [(&str, &str); 8] = [
63 ("AMP", "&"), ("GT", ">"), ("LT", "<"), ("QUOT", "\""),
64 ("amp", "&"), ("gt", ">"), ("lt", "<"), ("quot", "\""),
65];
66
67/// Windows-1252 for the C1 range of a numeric reference (HTML §13.2.5.80); a code not listed
68/// keeps its value.
69const C1: [(u32, u32); 27] = [
70 (0x80, 0x20ac), (0x82, 0x201a), (0x83, 0x0192), (0x84, 0x201e), (0x85, 0x2026),
71 (0x86, 0x2020), (0x87, 0x2021), (0x88, 0x02c6), (0x89, 0x2030), (0x8a, 0x0160),
72 (0x8b, 0x2039), (0x8c, 0x0152), (0x8e, 0x017d), (0x91, 0x2018), (0x92, 0x2019),
73 (0x93, 0x201c), (0x94, 0x201d), (0x95, 0x2022), (0x96, 0x2013), (0x97, 0x2014),
74 (0x98, 0x02dc), (0x99, 0x2122), (0x9a, 0x0161), (0x9b, 0x203a), (0x9c, 0x0153),
75 (0x9e, 0x017e), (0x9f, 0x0178),
76];
77
78/// The HTML a sandboxed frame is given for a message. With [`Remote::Blocked`] it names no
79/// resource outside itself: `src`, `srcset`, `background`, `poster`, every non-link `href` and
80/// the other fetching attributes are removed unless they hold a `data:` or `cid:` URL, and CSS
81/// `url()`, `image()`, `image-set()` and `src()` (in `style` attributes, `<style>` and SVG
82/// presentation attributes) are renamed so they no longer parse. In either mode `<script>`,
83/// frames, objects, `<link>`, `<meta>` (a refresh), `<base>`, `@import`, `ping`, event handlers
84/// and comments are removed.
85pub fn inert_html(html: &str, remote: Remote) -> String {
86 let s = html;
87 let b = s.as_bytes();
88 let mut out = String::with_capacity(s.len() + s.len() / 8);
89 let mut i = 0;
90 while i < b.len() {
91 let lt = match s[i..].find('<') {
92 Some(k) => i + k,
93 None => {
94 push_text(&mut out, &s[i..]);
95 break;
96 }
97 };
98 push_text(&mut out, &s[i..lt]);
99 i = lt;
100 let rest = &s[i..];
101 if rest.starts_with("<!--") {
102 i = comment_end(s, i + 4);
103 } else if rest.starts_with("<!") || rest.starts_with("<?") {
104 i = bogus_end(s, i + 2);
105 } else if rest.starts_with("</") {
106 match b.get(i + 2) {
107 Some(c) if c.is_ascii_alphabetic() => {
108 let (tag, next) = read_tag(s, i + 2);
109 i = next;
110 if let Some(t) = tag {
111 end_tag(&mut out, &t.name);
112 }
113 }
114 Some(b'>') => i += 3,
115 Some(_) => i = bogus_end(s, i + 2),
116 None => {
117 out.push_str("&lt;/");
118 i += 2;
119 }
120 }
121 } else if b.get(i + 1).map_or(false, |c| c.is_ascii_alphabetic()) {
122 let (tag, next) = read_tag(s, i + 1);
123 i = next;
124 // A tag the input ends inside is dropped, as a browser drops it.
125 if let Some(t) = tag {
126 i = start_tag(&mut out, s, &t, i, remote);
127 }
128 } else {
129 out.push_str("&lt;");
130 i += 1;
131 }
132 }
133 out
134}
135
136struct Tag {
137 name: String,
138 attrs: Vec<(String, String)>, // lower-case name, raw value; the first of a name wins
139 closed: bool, // written `<x/>`
140}
141
142fn is_ws(c: u8) -> bool {
143 matches!(c, b'\t' | b'\n' | b'\x0c' | b'\r' | b' ')
144}
145
146/// After `<!--`: `<!-->` and `<!--->` end at once, else at `-->` or `--!>`, else at the end.
147fn comment_end(s: &str, j: usize) -> usize {
148 let r = &s[j..];
149 if r.starts_with('>') {
150 return j + 1;
151 }
152 if r.starts_with("->") {
153 return j + 2;
154 }
155 let a = r.find("-->").map(|k| j + k + 3);
156 let b = r.find("--!>").map(|k| j + k + 4);
157 match (a, b) {
158 (Some(x), Some(y)) => x.min(y),
159 (Some(x), None) => x,
160 (None, Some(y)) => y,
161 (None, None) => s.len(),
162 }
163}
164
165fn bogus_end(s: &str, j: usize) -> usize {
166 match s[j..].find('>') {
167 Some(k) => j + k + 1,
168 None => s.len(),
169 }
170}
171
172/// A tag from its name's first byte to just past its `>`, read as HTML's tokenizer reads it
173/// (§13.2.5.6 onward); `None` when the input ends inside it.
174fn read_tag(s: &str, j: usize) -> (Option<Tag>, usize) {
175 let b = s.as_bytes();
176 let n = b.len();
177 let mut k = j;
178 while k < n && !is_ws(b[k]) && b[k] != b'/' && b[k] != b'>' {
179 k += 1;
180 }
181 let mut t = Tag { name: s[j..k].to_ascii_lowercase(), attrs: Vec::new(), closed: false };
182 loop {
183 while k < n && is_ws(b[k]) {
184 k += 1;
185 }
186 if k >= n {
187 return (None, n);
188 }
189 match b[k] {
190 b'>' => return (Some(t), k + 1),
191 b'/' => {
192 if b.get(k + 1) == Some(&b'>') {
193 t.closed = true;
194 return (Some(t), k + 2);
195 }
196 k += 1;
197 continue;
198 }
199 _ => {}
200 }
201 // An attribute name; a leading `=` belongs to it.
202 let a0 = k;
203 k += 1;
204 while k < n && !is_ws(b[k]) && b[k] != b'/' && b[k] != b'>' && b[k] != b'=' {
205 k += 1;
206 }
207 let name = s[a0..k].to_ascii_lowercase();
208 while k < n && is_ws(b[k]) {
209 k += 1;
210 }
211 let mut value = String::new();
212 if k < n && b[k] == b'=' {
213 k += 1;
214 while k < n && is_ws(b[k]) {
215 k += 1;
216 }
217 if k >= n {
218 return (None, n);
219 }
220 match b[k] {
221 q @ (b'"' | b'\'') => {
222 let v0 = k + 1;
223 match s[v0..].find(q as char) {
224 Some(e) => {
225 value = s[v0..v0 + e].to_string();
226 k = v0 + e + 1;
227 }
228 None => return (None, n),
229 }
230 }
231 b'>' => {}
232 _ => {
233 let v0 = k;
234 while k < n && !is_ws(b[k]) && b[k] != b'>' {
235 k += 1;
236 }
237 value = s[v0..k].to_string();
238 }
239 }
240 }
241 if !t.attrs.iter().any(|(m, _)| *m == name) {
242 t.attrs.push((name, value));
243 }
244 }
245}
246
247/// Where the raw text of `name` ends: at `</name` followed by a space, `/` or `>`, else at the
248/// end of the input.
249fn raw_end(s: &str, from: usize, name: &str) -> usize {
250 let b = s.as_bytes();
251 let lower = s[from..].to_ascii_lowercase();
252 let pat = format!("</{}", name);
253 let mut at = 0;
254 while let Some(k) = lower[at..].find(&pat) {
255 let e = from + at + k + pat.len();
256 match b.get(e) {
257 None => return from + at + k,
258 Some(c) if is_ws(*c) || *c == b'/' || *c == b'>' => return from + at + k,
259 _ => at += k + pat.len(),
260 }
261 }
262 s.len()
263}
264
265/// Write a start tag and, for the elements a browser reads as raw text, their content; return
266/// where reading resumes.
267fn start_tag(out: &mut String, s: &str, t: &Tag, i: usize, remote: Remote) -> usize {
268 let name = t.name.as_str();
269 if DROPPED_RAW.contains(&name) {
270 return raw_end(s, i, name);
271 }
272 match name {
273 "style" => {
274 let e = raw_end(s, i, name);
275 out.push_str("<style>");
276 push_css(out, &css_inert(&s[i..e], remote));
277 out.push_str("</style>");
278 // The end tag is consumed here, so a missing one is supplied.
279 return if e < s.len() { bogus_end(s, e) } else { e };
280 }
281 "xmp" | "plaintext" => {
282 // Raw text shown as it stands: a `<pre>` of the escaped text.
283 let e = if name == "plaintext" { s.len() } else { raw_end(s, i, name) };
284 out.push_str("<pre>");
285 push_literal(out, &s[i..e]);
286 out.push_str("</pre>");
287 return if e < s.len() { bogus_end(s, e) } else { e };
288 }
289 _ => {}
290 }
291 if DROPPED_TAGS.contains(&name) || !valid_tag(name) {
292 return i;
293 }
294 out.push('<');
295 out.push_str(name);
296 for (n, v) in &t.attrs {
297 if let Some(val) = attr_value(name, n, v, remote) {
298 out.push(' ');
299 out.push_str(n);
300 out.push_str("=\"");
301 out.push_str(&val);
302 out.push('"');
303 }
304 }
305 if t.closed {
306 out.push_str(" /");
307 }
308 out.push('>');
309 if name == "textarea" {
310 // RCDATA: its content is text, whatever it looks like.
311 let e = raw_end(s, i, name);
312 push_text(out, &s[i..e]);
313 return e;
314 }
315 i
316}
317
318fn end_tag(out: &mut String, name: &str) {
319 if DROPPED_TAGS.contains(&name) || DROPPED_RAW.contains(&name) || !valid_tag(name) {
320 return;
321 }
322 out.push_str("</");
323 out.push_str(name);
324 out.push('>');
325}
326
327/// A tag name worth keeping, Outlook's `o:p` and VML's `v:shape` among them.
328fn valid_tag(n: &str) -> bool {
329 let b = n.as_bytes();
330 !b.is_empty() && b[0].is_ascii_lowercase()
331 && b.iter().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || matches!(c, b'-' | b':' | b'_' | b'.'))
332}
333
334fn valid_attr(n: &str) -> bool {
335 let b = n.as_bytes();
336 !b.is_empty() && (b[0].is_ascii_lowercase() || b[0] == b'_' || b[0] == b':')
337 && b.iter().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || matches!(c, b'-' | b':' | b'_' | b'.'))
338}
339
340/// The escaped value an attribute is written with, or `None` when it is dropped.
341fn attr_value(el: &str, n: &str, raw: &str, remote: Remote) -> Option<String> {
342 if !valid_attr(n) || n == "ping" || n.starts_with("on") {
343 return None;
344 }
345 let (dec, esc) = decode_attr(raw);
346 let link = (el == "a" || el == "area") && (n == "href" || n.ends_with(":href"));
347 if link {
348 return Some(esc);
349 }
350 if remote == Remote::Blocked {
351 if URL_LISTS.contains(&n) {
352 return None;
353 }
354 if (URL_ATTRS.contains(&n) || n.ends_with("href")) && !is_local(&dec.to_ascii_lowercase()) {
355 return None;
356 }
357 }
358 let css = css_inert(&dec, remote);
359 if css == dec {
360 Some(esc)
361 } else {
362 let mut o = String::with_capacity(css.len());
363 for c in css.chars() {
364 push_attr_char(&mut o, c);
365 }
366 Some(o)
367 }
368}
369
370/// Is this (lower-case) URL the message's own, a `data:` or `cid:` that reaches no host?
371fn is_local(u: &str) -> bool {
372 let t = u.trim_start_matches(|c: char| c.is_ascii_whitespace() || c == '"' || c == '\'');
373 t.starts_with("data:") || t.starts_with("cid:")
374}
375
376fn push_attr_char(o: &mut String, c: char) {
377 match c {
378 '&' => o.push_str("&amp;"),
379 '"' => o.push_str("&quot;"),
380 '<' => o.push_str("&lt;"),
381 '>' => o.push_str("&gt;"),
382 _ => o.push(c),
383 }
384}
385
386/// An attribute value as a browser decodes it, as far as ASCII goes, and the same value
387/// escaped for a double-quoted attribute. A reference this reader does not know expands, if at
388/// all, to no ASCII character, so it is kept as written for the browser to expand.
389fn decode_attr(raw: &str) -> (String, String) {
390 let mut dec = String::with_capacity(raw.len());
391 let mut esc = String::with_capacity(raw.len() + 8);
392 let b = raw.as_bytes();
393 let mut i = 0;
394 while i < raw.len() {
395 if b[i] != b'&' {
396 let c = raw[i..].chars().next().unwrap_or('\u{fffd}');
397 dec.push(c);
398 push_attr_char(&mut esc, c);
399 i += c.len_utf8();
400 continue;
401 }
402 // A numeric reference, `;` optional.
403 if b.get(i + 1) == Some(&b'#') {
404 let hex = matches!(b.get(i + 2), Some(b'x') | Some(b'X'));
405 let d0 = if hex { i + 3 } else { i + 2 };
406 let mut k = d0;
407 while k < b.len() && (if hex { b[k].is_ascii_hexdigit() } else { b[k].is_ascii_digit() }) {
408 k += 1;
409 }
410 if k > d0 {
411 let code = u32::from_str_radix(&raw[d0..k.min(d0 + 8)], if hex { 16 } else { 10 })
412 .unwrap_or(0x110000);
413 let code = if k - d0 > 8 { 0x110000 } else { code };
414 let code = C1.iter().find(|(f, _)| *f == code).map(|(_, t)| *t).unwrap_or(code);
415 let c = match code {
416 0 => '\u{fffd}',
417 _ => char::from_u32(code).unwrap_or('\u{fffd}'),
418 };
419 dec.push(c);
420 push_attr_char(&mut esc, c);
421 i = if b.get(k) == Some(&b';') { k + 1 } else { k };
422 continue;
423 }
424 }
425 // A named reference.
426 let mut k = i + 1;
427 while k < b.len() && b[k].is_ascii_alphanumeric() {
428 k += 1;
429 }
430 let name = &raw[i + 1..k];
431 if b.get(k) == Some(&b';') {
432 if let Some((_, x)) = ASCII_NAMED.iter().find(|(m, _)| *m == name) {
433 dec.push_str(x);
434 for c in x.chars() {
435 push_attr_char(&mut esc, c);
436 }
437 i = k + 1;
438 continue;
439 }
440 } else if b.get(k) != Some(&b'=') {
441 // In an attribute a legacy name followed by a letter, digit or `=` is not decoded.
442 if let Some((_, x)) = ASCII_LEGACY.iter().find(|(m, _)| *m == name) {
443 dec.push_str(x);
444 for c in x.chars() {
445 push_attr_char(&mut esc, c);
446 }
447 i = k;
448 continue;
449 }
450 }
451 // Not one of ours: kept, `&` and all, for the browser.
452 dec.push('&');
453 esc.push('&');
454 i += 1;
455 }
456 (dec, esc)
457}
458
459/// Text between tags: `<` and `>` escaped, references left for the browser, which expands
460/// them as text and never as markup.
461fn push_text(out: &mut String, s: &str) {
462 for c in s.chars() {
463 match c {
464 '<' => out.push_str("&lt;"),
465 '>' => out.push_str("&gt;"),
466 _ => out.push(c),
467 }
468 }
469}
470
471/// Raw text shown as text: `&` escaped too, since nothing in it was a reference.
472fn push_literal(out: &mut String, s: &str) {
473 for c in s.chars() {
474 match c {
475 '&' => out.push_str("&amp;"),
476 '<' => out.push_str("&lt;"),
477 '>' => out.push_str("&gt;"),
478 _ => out.push(c),
479 }
480 }
481}
482
483/// A stylesheet's text for a `<style>` body: `<` and `&` as CSS escapes, so no end tag and no
484/// reference can appear in it.
485fn push_css(out: &mut String, css: &str) {
486 for c in css.chars() {
487 match c {
488 '<' => out.push_str("\\3c "),
489 '&' => out.push_str("\\26 "),
490 _ => out.push(c),
491 }
492 }
493}
494
495fn is_ident(c: u8) -> bool {
496 c.is_ascii_alphanumeric() || c == b'-' || c == b'_' || c >= 0x80
497}
498
499/// CSS with nothing left that fetches: `@import` always, and with pictures off every `url()`
500/// not naming a `data:` or `cid:` URL, `image()`, `image-set()` and `src()` (any vendor
501/// prefix). Each is renamed with an `x-` the parser does not know, so its declaration or rule
502/// is dropped. Escapes that spell a letter, digit, `-` or `_` are decoded first, so `\75 rl(`
503/// is seen as the `url(` it is.
504pub fn css_inert(css: &str, remote: Remote) -> String {
505 let d = css_unescape(css);
506 let lower = d.to_ascii_lowercase();
507 let b = lower.as_bytes();
508 let mut out = String::with_capacity(d.len() + 16);
509 let mut last = 0;
510 for (k, &c) in b.iter().enumerate() {
511 match c {
512 b'(' => {
513 let mut st = k;
514 while st > 0 && is_ident(b[st - 1]) {
515 st -= 1;
516 }
517 if css_fetches(&lower[st..k], &lower[k + 1..], remote) {
518 out.push_str(&d[last..st]);
519 out.push_str("x-");
520 last = st;
521 }
522 }
523 b'@' if lower[k + 1..].starts_with("import") => {
524 out.push_str(&d[last..k + 1]);
525 out.push_str("x-");
526 last = k + 1;
527 }
528 _ => {}
529 }
530 }
531 out.push_str(&d[last..]);
532 out
533}
534
535/// Does a CSS function of this (lower-case) name, with these arguments, fetch?
536fn css_fetches(name: &str, args: &str, remote: Remote) -> bool {
537 if remote == Remote::Allowed {
538 return false;
539 }
540 // `-webkit-image-set` is `image-set`.
541 let base = match name.strip_prefix('-') {
542 Some(r) => match r.find('-') {
543 Some(k) => &r[k + 1..],
544 None => name,
545 },
546 None => name,
547 };
548 match base {
549 "url" => !is_local(args),
550 "image" | "image-set" | "src" => true,
551 _ => false,
552 }
553}
554
555/// CSS escapes for a letter, digit, `-` or `_` replaced by the character; any other escape kept,
556/// a hexadecimal one rewritten with its terminating space so a decoded character after it
557/// cannot extend it.
558fn css_unescape(s: &str) -> String {
559 let b = s.as_bytes();
560 let mut out = String::with_capacity(s.len());
561 let mut i = 0;
562 while i < s.len() {
563 if b[i] != b'\\' {
564 let c = s[i..].chars().next().unwrap_or('\u{fffd}');
565 out.push(c);
566 i += c.len_utf8();
567 continue;
568 }
569 match b.get(i + 1) {
570 None | Some(b'\n') | Some(b'\r') | Some(b'\x0c') => {
571 out.push('\\');
572 i += 1;
573 }
574 Some(h) if h.is_ascii_hexdigit() => {
575 let mut k = i + 1;
576 while k < b.len() && k < i + 7 && b[k].is_ascii_hexdigit() {
577 k += 1;
578 }
579 let code = u32::from_str_radix(&s[i + 1..k], 16).unwrap_or(0xfffd);
580 let hexs = &s[i + 1..k];
581 // One whitespace ends the escape, `\r\n` counting as one.
582 if s[k..].starts_with("\r\n") {
583 k += 2;
584 } else if k < b.len() && is_ws(b[k]) {
585 k += 1;
586 }
587 match char::from_u32(code) {
588 Some(c) if c.is_ascii_alphanumeric() || c == '-' || c == '_' => out.push(c),
589 _ => {
590 out.push('\\');
591 out.push_str(hexs);
592 out.push(' ');
593 }
594 }
595 i = k;
596 }
597 Some(_) => {
598 let c = s[i + 1..].chars().next().unwrap_or('\u{fffd}');
599 if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
600 out.push(c);
601 } else {
602 out.push('\\');
603 out.push(c);
604 }
605 i += 1 + c.len_utf8();
606 }
607 }
608 }
609 out
610}