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
"),
'\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