authoravatarclover caruso<git@paperclover.net>2026-10-19 17:25:52-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 21:36:57-07:00
log6a33e59faf91f545122198920e2d4379d0054e90
treed7ce3e3af8ff7e38ed1c7e432d11bdba1a57a839
parent2479d36d4fec000481feba1adc0af26656079959
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(canvas): keep stored fonts, links and quotes from breaking out of copied HTML

The HTML Copy writes escaped no single quote, so a stored font name could close the single-quoted style attribute and add its own attributes, handlers among them; a font with a double quote could leave its CSS string and load an outside picture; and every stored link target, javascript: and data: included, became a live link. Quotes are now escaped everywhere text goes, a font name that is not plainly one (letters, digits, spaces, - _ .) is left out of the CSS, and a link leads somewhere only to a web or mail address, the notebook or a relative path, without the white space browsers ignore in a scheme; any other link stays text. - Copying text whose stored fonts or links are malformed or hostile no longer puts script or outside requests into the copied HTML. Assisted-by: claude-opus-5.5

1 files changed, 129 insertions(+), 15 deletions(-)

crates/canvas/src/editor/html.rs+129-15
...@@ -139,9 +139,11 @@ fn runs(text: &Paragraph, out: &mut String) {...@@ -139,9 +139,11 @@ fn runs(text: &Paragraph, out: &mut String) {
139 continue;139 continue;
140 }140 }
141 shown = true;141 shown = true;
142 let link = links.iter().find(|link| link.label.contains(&start));142 let link = (links.iter())
143 if let Some(link) = link {143 .find(|link| link.label.contains(&start))
144 let _ = write!(out, "<a href=\"{}\">", escape(&link.target, &mut false));144 .and_then(|link| linkable(&link.target));
145 if let Some(target) = &link {
146 let _ = write!(out, "<a href=\"{}\">", escape(target, &mut false));
145 }147 }
146 let _ = write!(148 let _ = write!(
147 out,149 out,
...@@ -158,15 +160,51 @@ fn runs(text: &Paragraph, out: &mut String) {...@@ -158,15 +160,51 @@ fn runs(text: &Paragraph, out: &mut String) {
158 }160 }
159}161}
160162
161fn css(format: &Format) -> String {163/// A stored link target as HTML may link it: a web or mail address, a link within the
162 let font = format.font.as_deref().unwrap_or(DEFAULT_FONT);164/// notebook, or a relative path, without the white space and controls browsers ignore in one.
163 let font = if font.contains(' ') {165/// None for anything else, such as a script, which stays text.
164 format!("\"{font}\"")166fn linkable(target: &str) -> Option<String> {
165 } else {167 let target: String = (target.trim_matches(|c: char| c <= ' ').chars())
166 font.to_owned()168 .filter(|c| !matches!(c, '\t' | '\n' | '\r'))
169 .collect();
170 let scheme = (target.split_once(':'))
171 .map(|(scheme, _)| scheme)
172 .filter(|scheme| !scheme.contains(['/', '?', '#']));
173 let allowed = match scheme {
174 Some(scheme) => matches!(
175 scheme.to_ascii_lowercase().as_str(),
176 "http" | "https" | "mailto" | "onenote"
177 ),
178 // A Windows path to a share or a drive names the writer's own files.
179 None => !target.is_empty() && !target.starts_with('\\'),
167 };180 };
168 let mut css = format!(181 allowed.then_some(target)
169 "font-family:{font};font-size:{}pt",182}
183
184/// A font name CSS can hold as it is: letters, digits, spaces and `-`, `_` and `.`; none for
185/// another, which might break out of the style it is put in.
186fn font_name(font: &str) -> Option<&str> {
187 (!font.is_empty()
188 && font
189 .chars()
190 .all(|c| c.is_alphanumeric() || matches!(c, ' ' | '-' | '_' | '.')))
191 .then_some(font)
192}
193
194fn css(format: &Format) -> String {
195 let mut css = String::new();
196 match font_name(format.font.as_deref().unwrap_or(DEFAULT_FONT)) {
197 Some(font) if font.contains(' ') => {
198 let _ = write!(css, "font-family:\"{font}\";");
199 }
200 Some(font) => {
201 let _ = write!(css, "font-family:{font};");
202 }
203 None => {}
204 }
205 let _ = write!(
206 css,
207 "font-size:{}pt",
170 format.font_size.unwrap_or(DEFAULT_FONT_SIZE)208 format.font_size.unwrap_or(DEFAULT_FONT_SIZE)
171 );209 );
172 let set = |value: Option<bool>| value == Some(true);210 let set = |value: Option<bool>| value == Some(true);
...@@ -204,12 +242,12 @@ fn css(format: &Format) -> String {...@@ -204,12 +242,12 @@ fn css(format: &Format) -> String {
204 if let Some(color) = format.highlight.filter(|color| *color != AUTOMATIC) {242 if let Some(color) = format.highlight.filter(|color| *color != AUTOMATIC) {
205 let _ = write!(css, ";background:{}", hex(color));243 let _ = write!(css, ";background:{}", hex(color));
206 }244 }
207 css.replace('\'', "&#39;")245 css
208}246}
209247
210/// Text as HTML keeps it: markup characters escaped, line breaks as `br`, and a space after248/// Text as HTML keeps it, in an element or a quoted attribute: markup characters and quotes
211/// `space`, whether the text follows a space or starts its paragraph, unbreakable so it249/// escaped, line breaks as `br`, and a space after `space`, whether the text follows a space
212/// doesn't collapse.250/// or starts its paragraph, unbreakable so it doesn't collapse.
213fn escape(text: &str, space: &mut bool) -> String {251fn escape(text: &str, space: &mut bool) -> String {
214 let mut escaped = String::with_capacity(text.len());252 let mut escaped = String::with_capacity(text.len());
215 for c in text.chars() {253 for c in text.chars() {
...@@ -218,6 +256,7 @@ fn escape(text: &str, space: &mut bool) -> String {...@@ -218,6 +256,7 @@ fn escape(text: &str, space: &mut bool) -> String {
218 '<' => escaped.push_str("&lt;"),256 '<' => escaped.push_str("&lt;"),
219 '>' => escaped.push_str("&gt;"),257 '>' => escaped.push_str("&gt;"),
220 '"' => escaped.push_str("&quot;"),258 '"' => escaped.push_str("&quot;"),
259 '\'' => escaped.push_str("&#39;"),
221 '\u{b}' | '\n' => escaped.push_str("<br>"),260 '\u{b}' | '\n' => escaped.push_str("<br>"),
222 '\t' => escaped.push_str("&nbsp;&nbsp;&nbsp; "),261 '\t' => escaped.push_str("&nbsp;&nbsp;&nbsp; "),
223 ' ' if *space => escaped.push_str("&nbsp;"),262 ' ' if *space => escaped.push_str("&nbsp;"),
...@@ -1307,6 +1346,81 @@ mod tests {...@@ -1307,6 +1346,81 @@ mod tests {
1307 );1346 );
1308 }1347 }
13091348
1349 /// Stored text, fonts and link targets can't break out of the HTML Copy writes: quotes are
1350 /// escaped, a font name that isn't plainly one is left out, and a link to anything but a
1351 /// web or mail address, the notebook or a relative path stays text.
1352 #[test]
1353 fn hostile_fonts_links_and_quotes_stay_inert() {
1354 let run = |text: &str, format: Format| (text.to_owned(), format);
1355 let link = |target: &str| {
1356 let code = Format {
1357 hyperlink: Some(true),
1358 hidden: Some(true),
1359 ..Format::default()
1360 };
1361 let label = Format {
1362 hyperlink: Some(true),
1363 hyperlink_label: Some(true),
1364 ..Format::default()
1365 };
1366 [run(&field_code(target), code), run("label", label)]
1367 };
1368 let fonts = [
1369 "Evil' tabindex=1 autofocus onfocus='alert(1)//",
1370 "A B\";background-image:url(https://evil.example/px.png);x:\"",
1371 ];
1372 let mut runs: Vec<(String, Format)> = fonts
1373 .iter()
1374 .map(|font| {
1375 let format = Format {
1376 font: Some((*font).to_owned()),
1377 ..Format::default()
1378 };
1379 run("it's <b>", format)
1380 })
1381 .collect();
1382 for target in [
1383 "javascript:alert(document.domain)",
1384 " JaVa\tScRiPt:alert(1)",
1385 "data:text/html,<script>alert(1)</script>",
1386 "vbscript:msgbox",
1387 "\\\\server\\share\\notes.one",
1388 "https://example.invalid/a?b='c'",
1389 "mailto:a@example.invalid",
1390 "onenote:#Page&section-id={00000000-0000-0000-0000-000000000000}&end",
1391 "notes/page.html",
1392 ] {
1393 runs.extend(link(target));
1394 runs.push(run(" ", Format::default()));
1395 }
1396 let paragraph = node(Paragraph::from_runs(runs), Format::default()).unwrap();
1397 let html = Clip::new(vec![paragraph], &BTreeMap::new()).html();
1398 for hostile in [
1399 "onfocus=",
1400 "autofocus",
1401 "url(",
1402 "javascript",
1403 "JaVa",
1404 "data:",
1405 "vbscript",
1406 "server",
1407 "<b>",
1408 "'c'",
1409 ] {
1410 assert!(!html.contains(hostile), "{hostile} in {html}");
1411 }
1412 assert!(html.contains("it&#39;s &lt;b&gt;"), "{html}");
1413 for kept in [
1414 "href=\"https://example.invalid/a?b=&#39;c&#39;\"",
1415 "href=\"mailto:a@example.invalid\"",
1416 "href=\"onenote:#Page",
1417 "href=\"notes/page.html\"",
1418 ] {
1419 assert!(html.contains(kept), "{kept} missing from {html}");
1420 }
1421 assert_eq!(html.matches("<a href").count(), 4, "{html}");
1422 }
1423
1310 /// As OneNote 2010 pasted `<p>before <img> after</p>` (lab, 2026-09-30): the text on1424 /// As OneNote 2010 pasted `<p>before <img> after</p>` (lab, 2026-09-30): the text on
1311 /// either side of the picture, the picture at the size its attributes give; one in a1425 /// either side of the picture, the picture at the size its attributes give; one in a
1312 /// table is left out.1426 /// table is left out.