//! Keeping remote fetches out of CSS. //! //! A stylesheet can reach the network without a single tag doing so: //! `@import`, `url(…)` in a `background`, a `src` in `@font-face`, the string //! arguments of `image-set(…)`. The `style` *attribute* is only a declaration //! list, but a ``, which no element handler can see, so it is found here in /// the finished document. A `style` with no close tag is left alone rather /// than guessed at. pub fn scrub_style_elements(doc: &str) -> String { let lowered = doc.to_ascii_lowercase(); let mut out = String::with_capacity(doc.len()); let mut at = 0; while let Some(found) = lowered[at..].find("').map(|p| close + p + 1) else { break; }; out.push_str(&doc[at..tag_end]); out.push_str(&scrub_css(&doc[tag_end..close])); out.push_str(&doc[close..close_end]); at = close_end; } out.push_str(&doc[at..]); out } #[cfg(test)] mod tests { use super::*; #[test] fn remote_url_in_a_rule_is_gone_but_the_rule_stays() { let css = "div { background: url(https://t/x.png) no-repeat; color: red }"; let scrubbed = scrub_css(css); assert!(!scrubbed.contains("https://t/x.png"), "{scrubbed}"); assert!(scrubbed.contains("div {"), "{scrubbed}"); assert!(scrubbed.contains("color: red"), "{scrubbed}"); } #[test] fn data_uri_survives() { let css = "div { background: url(data:image/png;base64,UE5HREFUQQ==) }"; assert_eq!(scrub_css(css), css); } #[test] fn quoted_url_is_read_from_inside_the_function() { let scrubbed = scrub_css("a { background: url(\"https://t/x.png\") }"); assert!(!scrubbed.contains("https://t/x.png"), "{scrubbed}"); let scrubbed = scrub_css("a { background: url(\"data:text/css,body{}\") }"); assert!(scrubbed.contains("data:text/css"), "{scrubbed}"); } #[test] fn import_is_dropped_whole_in_any_spelling() { for css in [ "@import url(https://t/a.css); div { color: red }", "@IMPORT \"https://t/b.css\"; div { color: red }", "@import url(\"https://t/c.css\") screen; div { color: red }", ] { let scrubbed = scrub_css(css); assert!( !scrubbed.to_ascii_lowercase().contains("import"), "{scrubbed}" ); assert!(!scrubbed.contains("https://t/"), "{scrubbed}"); assert!(scrubbed.contains("color: red"), "{scrubbed}"); } } #[test] fn escapes_are_not_a_way_around_the_scrub() { let scrubbed = scrub_css("div { background: u\\72l(https://t/x.png) }"); assert!(!scrubbed.contains("https://t/x.png"), "{scrubbed}"); let scrubbed = scrub_css("@\\69 mport \"https://t/a.css\";"); assert!(!scrubbed.contains("https://t/"), "{scrubbed}"); } #[test] fn image_set_strings_are_urls_too() { let scrubbed = scrub_css( "div { background: image-set(\"https://t/a.png\" 1x, \"data:image/png;base64,AA==\" 2x) }", ); assert!(!scrubbed.contains("https://t/a.png"), "{scrubbed}"); assert!(scrubbed.contains("data:image/png"), "{scrubbed}"); } #[test] fn font_face_and_keyframes_are_reached_too() { let scrubbed = scrub_css( "@font-face { font-family: x; src: url(https://t/f.woff2) } @keyframes k { from { background: url(https://t/k.png) } }", ); assert!(!scrubbed.contains("https://t/"), "{scrubbed}"); assert!(scrubbed.contains("@font-face"), "{scrubbed}"); assert!(scrubbed.contains("@keyframes"), "{scrubbed}"); } #[test] fn comments_and_unrelated_functions_are_left_alone() { let css = "/* url(https://t/in-comment.png) */ div { transform: translateX(var(--x)) }"; assert_eq!(scrub_css(css), css); } #[test] fn style_elements_are_scrubbed_in_place() { let doc = "
hi
x"; let scrubbed = scrub_style_elements(doc); assert!(!scrubbed.contains("https://t/x.png"), "{scrubbed}"); assert!(scrubbed.contains("hi
"), "{scrubbed}"); assert!(scrubbed.contains("x"), "{scrubbed}"); assert!(scrubbed.contains("div{background:"), "{scrubbed}"); } #[test] fn a_style_without_a_close_tag_is_not_guessed_at() { let doc = "