| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
mod css_sanitizer; |
| 26 |
mod html_sanitizer; |
| 27 |
mod url_filter; |
| 28 |
|
| 29 |
pub use css_sanitizer::{sanitize_css, sanitize_item_css}; |
| 30 |
pub use html_sanitizer::sanitize_html; |
| 31 |
pub use url_filter::UrlPolicy; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 37 |
pub enum RejectionKind { |
| 38 |
|
| 39 |
ExternalUrl, |
| 40 |
|
| 41 |
DisallowedScheme, |
| 42 |
|
| 43 |
MalformedUrl, |
| 44 |
|
| 45 |
BlockedAtRule, |
| 46 |
|
| 47 |
BlockedFunction, |
| 48 |
|
| 49 |
HidingProperty, |
| 50 |
|
| 51 |
AnimationBudget, |
| 52 |
|
| 53 |
ComplexityLimit, |
| 54 |
|
| 55 |
MalformedCss, |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 60 |
pub struct Rejection { |
| 61 |
pub kind: RejectionKind, |
| 62 |
|
| 63 |
pub location: String, |
| 64 |
|
| 65 |
pub original_value: String, |
| 66 |
|
| 67 |
pub reason: String, |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
pub(crate) const MAX_RULES: usize = 5000; |
| 73 |
|
| 74 |
pub(crate) const MAX_SELECTORS: usize = 10000; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
pub fn sanitize_page( |
| 83 |
html: &str, |
| 84 |
css: &str, |
| 85 |
owner_scope: &str, |
| 86 |
policy: &UrlPolicy, |
| 87 |
) -> (String, String, Vec<Rejection>) { |
| 88 |
let (clean_html, mut rejections) = sanitize_html(html, policy); |
| 89 |
let (clean_css, css_rejections) = sanitize_css(css, owner_scope, policy); |
| 90 |
rejections.extend(css_rejections); |
| 91 |
(clean_html, clean_css, rejections) |
| 92 |
} |
| 93 |
|
| 94 |
#[cfg(test)] |
| 95 |
mod tests { |
| 96 |
use super::*; |
| 97 |
|
| 98 |
fn policy() -> UrlPolicy { |
| 99 |
UrlPolicy::new( |
| 100 |
"https://u.makenot.work/alice/proj", |
| 101 |
["makenot.work".to_string(), "u.makenot.work".to_string(), "cdn.makenot.work".to_string()], |
| 102 |
) |
| 103 |
.unwrap() |
| 104 |
} |
| 105 |
|
| 106 |
#[test] |
| 107 |
fn page_sanitizes_both_and_collects_rejections() { |
| 108 |
let (html, css, rej) = sanitize_page( |
| 109 |
"<p>hi</p><script>evil()</script><img src=\"https://evil.com/x\">", |
| 110 |
"body { color: red } .x { background: url(https://evil.com/y) }", |
| 111 |
"11111111-1111-1111-1111-111111111111", |
| 112 |
&policy(), |
| 113 |
); |
| 114 |
assert!(html.contains("hi")); |
| 115 |
assert!(!html.contains("evil")); |
| 116 |
|
| 117 |
assert!(css.contains(".user-canvas#uc-11111111-1111-1111-1111-111111111111")); |
| 118 |
|
| 119 |
assert!(!css.contains("evil.com")); |
| 120 |
|
| 121 |
assert!(rej.len() >= 2, "expected rejections, got {rej:?}"); |
| 122 |
} |
| 123 |
} |
| 124 |
|