diff --git a/crates/canvas/src/editor/html.rs b/crates/canvas/src/editor/html.rs index b6a1894633668e0de5682b8af751914acfcb576c..54319e0569fd1f969a62defe9041cabda60e334e 100644 --- a/crates/canvas/src/editor/html.rs +++ b/crates/canvas/src/editor/html.rs @@ -139,9 +139,11 @@ fn runs(text: &Paragraph, out: &mut String) { continue; } shown = true; - let link = links.iter().find(|link| link.label.contains(&start)); - if let Some(link) = link { - let _ = write!(out, "", escape(&link.target, &mut false)); + let link = (links.iter()) + .find(|link| link.label.contains(&start)) + .and_then(|link| linkable(&link.target)); + if let Some(target) = &link { + let _ = write!(out, "", escape(target, &mut false)); } let _ = write!( out, @@ -158,15 +160,51 @@ fn runs(text: &Paragraph, out: &mut String) { } } +/// A stored link target as HTML may link it: a web or mail address, a link within the +/// notebook, or a relative path, without the white space and controls browsers ignore in one. +/// None for anything else, such as a script, which stays text. +fn linkable(target: &str) -> Option { + let target: String = (target.trim_matches(|c: char| c <= ' ').chars()) + .filter(|c| !matches!(c, '\t' | '\n' | '\r')) + .collect(); + let scheme = (target.split_once(':')) + .map(|(scheme, _)| scheme) + .filter(|scheme| !scheme.contains(['/', '?', '#'])); + let allowed = match scheme { + Some(scheme) => matches!( + scheme.to_ascii_lowercase().as_str(), + "http" | "https" | "mailto" | "onenote" + ), + // A Windows path to a share or a drive names the writer's own files. + None => !target.is_empty() && !target.starts_with('\\'), + }; + allowed.then_some(target) +} + +/// A font name CSS can hold as it is: letters, digits, spaces and `-`, `_` and `.`; none for +/// another, which might break out of the style it is put in. +fn font_name(font: &str) -> Option<&str> { + (!font.is_empty() + && font + .chars() + .all(|c| c.is_alphanumeric() || matches!(c, ' ' | '-' | '_' | '.'))) + .then_some(font) +} + fn css(format: &Format) -> String { - let font = format.font.as_deref().unwrap_or(DEFAULT_FONT); - let font = if font.contains(' ') { - format!("\"{font}\"") - } else { - font.to_owned() - }; - let mut css = format!( - "font-family:{font};font-size:{}pt", + let mut css = String::new(); + match font_name(format.font.as_deref().unwrap_or(DEFAULT_FONT)) { + Some(font) if font.contains(' ') => { + let _ = write!(css, "font-family:\"{font}\";"); + } + Some(font) => { + let _ = write!(css, "font-family:{font};"); + } + None => {} + } + let _ = write!( + css, + "font-size:{}pt", format.font_size.unwrap_or(DEFAULT_FONT_SIZE) ); let set = |value: Option| value == Some(true); @@ -204,12 +242,12 @@ fn css(format: &Format) -> String { if let Some(color) = format.highlight.filter(|color| *color != AUTOMATIC) { let _ = write!(css, ";background:{}", hex(color)); } - css.replace('\'', "'") + css } -/// Text as HTML keeps it: markup characters escaped, line breaks as `br`, and a space after -/// `space`, whether the text follows a space or starts its paragraph, unbreakable so it -/// doesn't collapse. +/// Text as HTML keeps it, in an element or a quoted attribute: markup characters and quotes +/// escaped, line breaks as `br`, and a space after `space`, whether the text follows a space +/// or starts its paragraph, unbreakable so it doesn't collapse. fn escape(text: &str, space: &mut bool) -> String { let mut escaped = String::with_capacity(text.len()); for c in text.chars() { @@ -218,6 +256,7 @@ fn escape(text: &str, space: &mut bool) -> String { '<' => escaped.push_str("<"), '>' => escaped.push_str(">"), '"' => escaped.push_str("""), + '\'' => escaped.push_str("'"), '\u{b}' | '\n' => escaped.push_str("
"), '\t' => escaped.push_str("    "), ' ' if *space => escaped.push_str(" "), @@ -1307,6 +1346,81 @@ mod tests { ); } + /// Stored text, fonts and link targets can't break out of the HTML Copy writes: quotes are + /// escaped, a font name that isn't plainly one is left out, and a link to anything but a + /// web or mail address, the notebook or a relative path stays text. + #[test] + fn hostile_fonts_links_and_quotes_stay_inert() { + let run = |text: &str, format: Format| (text.to_owned(), format); + let link = |target: &str| { + let code = Format { + hyperlink: Some(true), + hidden: Some(true), + ..Format::default() + }; + let label = Format { + hyperlink: Some(true), + hyperlink_label: Some(true), + ..Format::default() + }; + [run(&field_code(target), code), run("label", label)] + }; + let fonts = [ + "Evil' tabindex=1 autofocus onfocus='alert(1)//", + "A B\";background-image:url(https://evil.example/px.png);x:\"", + ]; + let mut runs: Vec<(String, Format)> = fonts + .iter() + .map(|font| { + let format = Format { + font: Some((*font).to_owned()), + ..Format::default() + }; + run("it's ", format) + }) + .collect(); + for target in [ + "javascript:alert(document.domain)", + " JaVa\tScRiPt:alert(1)", + "data:text/html,", + "vbscript:msgbox", + "\\\\server\\share\\notes.one", + "https://example.invalid/a?b='c'", + "mailto:a@example.invalid", + "onenote:#Page§ion-id={00000000-0000-0000-0000-000000000000}&end", + "notes/page.html", + ] { + runs.extend(link(target)); + runs.push(run(" ", Format::default())); + } + let paragraph = node(Paragraph::from_runs(runs), Format::default()).unwrap(); + let html = Clip::new(vec![paragraph], &BTreeMap::new()).html(); + for hostile in [ + "onfocus=", + "autofocus", + "url(", + "javascript", + "JaVa", + "data:", + "vbscript", + "server", + "", + "'c'", + ] { + assert!(!html.contains(hostile), "{hostile} in {html}"); + } + assert!(html.contains("it's <b>"), "{html}"); + for kept in [ + "href=\"https://example.invalid/a?b='c'\"", + "href=\"mailto:a@example.invalid\"", + "href=\"onenote:#Page", + "href=\"notes/page.html\"", + ] { + assert!(html.contains(kept), "{kept} missing from {html}"); + } + assert_eq!(html.matches("
before after

` (lab, 2026-09-30): the text on /// either side of the picture, the picture at the size its attributes give; one in a /// table is left out.