Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/html_inert.rs

8.1 KiB, 1 run

created by r1870400018:75233, 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//! `html_inert`: a stranger's HTML names nothing remote until the reader asks for pictures.
2
3use oxedyne_fe2o3_text::html_inert::{
4 css_inert,
5 inert_html,
6 Remote,
7};
8
9const OFF: Remote = Remote::Blocked;
10const ON: Remote = Remote::Allowed;
11
12/// Does the inert HTML still name the tracker anywhere a browser could fetch it from? Every
13/// fetching position is removed, or (in CSS) renamed with `x-` so it no longer parses; what a
14/// renamed function or `@x-import` rule holds is cut before looking.
15fn names_host(h: &str) -> bool {
16 let mut s = h.to_ascii_lowercase();
17 for m in ["x-url(", "x--webkit-image-set(", "x-image-set(", "x-image(", "x-src(", "@x-import"] {
18 while let Some(a) = s.find(m) {
19 let stop = if m.starts_with('@') { ';' } else { ')' };
20 let e = s[a..].find(stop).map(|k| a + k + 1).unwrap_or(s.len());
21 s.replace_range(a..e, "");
22 }
23 }
24 s.contains("evil.example")
25}
26
27#[test]
28fn every_fetching_position_is_removed_with_pictures_off() {
29 let cases = [
30 r#"<img src="https://evil.example/p.gif">"#,
31 r#"<img srcset="https://evil.example/p.gif 1x">"#,
32 r#"<picture><source srcset="https://evil.example/p.webp"><img alt=x></picture>"#,
33 r#"<link rel=preload as=image imagesrcset="https://evil.example/a 1x" href="https://evil.example/b">"#,
34 r#"<table background="https://evil.example/bg"><td background=//evil.example/td>"#,
35 r#"<body background="https://evil.example/body">"#,
36 r#"<video poster="https://evil.example/p" src="https://evil.example/v"><track src="https://evil.example/t"></video>"#,
37 r#"<audio src="https://evil.example/a"></audio><input type=image src="https://evil.example/i">"#,
38 r#"<div style="background:url(https://evil.example/u)">"#,
39 r#"<div style='background-image:url("https://evil.example/u")'>"#,
40 r#"<div style="background:image-set('https://evil.example/s' 1x)">"#,
41 r#"<div style="background:-webkit-image-set('https://evil.example/s' 1x)">"#,
42 r#"<div style="list-style:image('https://evil.example/s')">"#,
43 r#"<style>@import 'https://evil.example/i.css'; p{background:url(https://evil.example/c)}</style>"#,
44 r#"<style>@font-face{font-family:x;src:url(https://evil.example/f.woff)}</style>"#,
45 r#"<link rel=stylesheet href="https://evil.example/s.css">"#,
46 r#"<meta http-equiv="refresh" content="0;url=https://evil.example/r">"#,
47 r#"<iframe src="https://evil.example/f"></iframe><object data="https://evil.example/o"></object><embed src="https://evil.example/e">"#,
48 r#"<svg><image href="https://evil.example/s"/><use xlink:href="https://evil.example/u#a"/></svg>"#,
49 r#"<svg><rect fill="url(https://evil.example/g#p)"/></svg>"#,
50 r#"<svg><image><set attributeName="href" to="https://evil.example/s"/></image></svg>"#,
51 r#"<base href="https://evil.example/"><img src="a.gif">"#,
52 r#"<script src="https://evil.example/s.js"></script>"#,
53 r#"<img lowsrc="https://evil.example/l" dynsrc="https://evil.example/d">"#,
54 r#"<a href="https://ok.test/" ping="https://evil.example/ping">link</a>"#,
55 r#"<bgsound src="https://evil.example/b">"#,
56 ];
57 for c in cases {
58 let h = inert_html(c, OFF);
59 assert!(!names_host(&h), "still names the host: {} -> {}", c, h);
60 }
61}
62
63#[test]
64fn encodings_that_hide_a_url_are_seen_through() {
65 let cases = [
66 // Character references in a style attribute, which the browser decodes before CSS.
67 r#"<div style="background:u&#114;l(https://evil.example/a)">"#,
68 r#"<div style="background:url&lpar;https://evil.example/a)">"#,
69 r#"<div style="background:url&#x28;https://evil.example/a)">"#,
70 // CSS escapes.
71 r#"<div style="background:\75 rl(https://evil.example/a)">"#,
72 r#"<div style="background:\000075rl(https://evil.example/a)">"#,
73 r#"<div style="background:u\rl(https://evil.example/a)">"#,
74 r#"<style>p{background:U\52L(https://evil.example/a)}</style>"#,
75 r#"<style>@\69mport "https://evil.example/i.css";</style>"#,
76 // Case and quoting of the tag and attribute.
77 r#"<IMG SRC=https://evil.example/a>"#,
78 r#"<img/src="https://evil.example/a">"#,
79 r#"<img src = 'https://evil.example/a'>"#,
80 ];
81 for c in cases {
82 let h = inert_html(c, OFF);
83 assert!(!names_host(&h), "{} -> {}", c, h);
84 }
85 // `\7\35 ` is U+0007 then `5`, not `\75` (`u`): the kept escape is closed by its space.
86 assert!(css_inert(r"a:\7\35 rl(x)", OFF).starts_with(r"a:\7 5rl(x)"));
87}
88
89#[test]
90fn a_parser_difference_cannot_hide_a_tag() {
91 // A `<title>` and a `<textarea>` are text to a browser: the quote inside does not run on.
92 let h = inert_html(r#"<title><a title='</title><img src=https://evil.example/t>'"#, OFF);
93 assert!(!names_host(&h), "{}", h);
94 let h = inert_html(r#"<textarea><a title='</textarea><img src=https://evil.example/t>'"#, OFF);
95 assert!(!names_host(&h), "{}", h);
96 // Inside `<svg>` a `<style>` is markup, so `<img>` in it breaks out: written as CSS text,
97 // it cannot.
98 let h = inert_html(r#"<svg><style><img src="https://evil.example/s"></style></svg>"#, ON);
99 assert!(h.contains(r"\3c img"), "{}", h);
100 assert!(!h.contains("<img"), "{}", h);
101 // A comment and a bogus comment go, with whatever they hide.
102 let h = inert_html(r#"<!--><img src=https://evil.example/c>--><p>x</p>"#, OFF);
103 assert!(!names_host(&h), "{}", h);
104 // A quote that never closes ends the input inside the tag, which is dropped.
105 assert_eq!(inert_html(r#"<p>a</p><img src="https://evil.example/x"#, OFF), "<p>a</p>");
106}
107
108#[test]
109fn the_message_own_pictures_and_links_stay() {
110 let h = inert_html(r#"<img src="cid:logo@x" alt="Logo"><img src="data:image/png;base64,AAAA">"#, OFF);
111 assert_eq!(h, r#"<img src="cid:logo@x" alt="Logo"><img src="data:image/png;base64,AAAA">"#);
112 let h = inert_html(r#"<td style="background:url('cid:bg')">"#, OFF);
113 assert_eq!(h, r#"<td style="background:url('cid:bg')">"#);
114 let h = inert_html(r#"<a href="https://shop.test/?a=1&amp;b=2">Shop</a>"#, OFF);
115 assert_eq!(h, r#"<a href="https://shop.test/?a=1&amp;b=2">Shop</a>"#);
116 // Text and its references are kept as written; Outlook's `o:p` survives.
117 let h = inert_html("<p class=x>caf&eacute; &copy; 1 &lt; 2<o:p></o:p></p>", OFF);
118 assert_eq!(h, r#"<p class="x">caf&eacute; &copy; 1 &lt; 2<o:p></o:p></p>"#);
119 let h = inert_html("<p style=\"color:red;font:12px Arial\">x</p>", OFF);
120 assert_eq!(h, "<p style=\"color:red;font:12px Arial\">x</p>");
121}
122
123#[test]
124fn with_pictures_allowed_images_load_and_active_content_still_goes() {
125 let h = inert_html(r#"<img src="https://pics.test/a.png" srcset="https://pics.test/b.png 2x">"#, ON);
126 assert_eq!(h, r#"<img src="https://pics.test/a.png" srcset="https://pics.test/b.png 2x">"#);
127 let h = inert_html(r#"<div style="background:url(https://pics.test/bg.png)">"#, ON);
128 assert!(h.contains("url(https://pics.test/bg.png)"), "{}", h);
129 let h = inert_html(concat!(
130 r#"<meta http-equiv=refresh content="0;url=https://evil.example/r">"#,
131 r#"<script>x</script><iframe src="https://evil.example/f"></iframe>"#,
132 r#"<link rel=stylesheet href="https://evil.example/s.css">"#,
133 r#"<style>@import "https://evil.example/i.css";</style>"#,
134 r#"<img src=https://pics.test/a.png onerror="x()">"#,
135 ), ON);
136 assert!(!names_host(&h), "{}", h);
137 assert!(h.contains(r#"<img src="https://pics.test/a.png">"#), "{}", h);
138}
139
140#[test]
141fn css_inert_renames_only_what_fetches() {
142 assert_eq!(css_inert("a{color:rgb(1,2,3);width:calc(1px + 2%)}", OFF), "a{color:rgb(1,2,3);width:calc(1px + 2%)}");
143 assert_eq!(css_inert("a{background:url(x.png)}", OFF), "a{background:x-url(x.png)}");
144 assert_eq!(css_inert("a{background:url( data:image/gif;base64,R0)}", OFF), "a{background:url( data:image/gif;base64,R0)}");
145 assert_eq!(css_inert("a{background:linear-gradient(red,blue)}", OFF), "a{background:linear-gradient(red,blue)}");
146 assert_eq!(css_inert("@import 'x.css';", ON), "@x-import 'x.css';");
147 assert_eq!(css_inert("a{background:url(x.png)}", ON), "a{background:url(x.png)}");
148}