| ... | ... | @@ -139,9 +139,11 @@ fn runs(text: &Paragraph, out: &mut String) { |
| 139 | 139 | continue; |
| 140 | 140 | } |
| 141 | 141 | shown = true; |
| 142 | | let link = links.iter().find(|link| link.label.contains(&start)); |
| 143 | | if let Some(link) = link { |
| 144 | | let _ = write!(out, "<a href=\"{}\">", escape(&link.target, &mut false)); |
| 142 | let link = (links.iter()) |
| 143 | .find(|link| link.label.contains(&start)) |
| 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 | 148 | let _ = write!( |
| 147 | 149 | out, |
| ... | ... | @@ -158,15 +160,51 @@ fn runs(text: &Paragraph, out: &mut String) { |
| 158 | 160 | } |
| 159 | 161 | } |
| 160 | 162 | |
| 161 | | fn css(format: &Format) -> String { |
| 162 | | let font = format.font.as_deref().unwrap_or(DEFAULT_FONT); |
| 163 | | let font = if font.contains(' ') { |
| 164 | | format!("\"{font}\"") |
| 165 | | } else { |
| 166 | | font.to_owned() |
| 163 | /// A stored link target as HTML may link it: a web or mail address, a link within the |
| 164 | /// notebook, or a relative path, without the white space and controls browsers ignore in one. |
| 165 | /// None for anything else, such as a script, which stays text. |
| 166 | fn linkable(target: &str) -> Option<String> { |
| 167 | let target: String = (target.trim_matches(|c: char| c <= ' ').chars()) |
| 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!( |
| 169 | | "font-family:{font};font-size:{}pt", |
| 181 | allowed.then_some(target) |
| 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. |
| 186 | fn 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 | |
| 194 | fn 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 | 208 | format.font_size.unwrap_or(DEFAULT_FONT_SIZE) |
| 171 | 209 | ); |
| 172 | 210 | let set = |value: Option<bool>| value == Some(true); |
| ... | ... | @@ -204,12 +242,12 @@ fn css(format: &Format) -> String { |
| 204 | 242 | if let Some(color) = format.highlight.filter(|color| *color != AUTOMATIC) { |
| 205 | 243 | let _ = write!(css, ";background:{}", hex(color)); |
| 206 | 244 | } |
| 207 | | css.replace('\'', "&#39;") |
| 245 | css |
| 208 | 246 | } |
| 209 | 247 | |
| 210 | | /// Text as HTML keeps it: markup characters escaped, line breaks as `br`, and a space after |
| 211 | | /// `space`, whether the text follows a space or starts its paragraph, unbreakable so it |
| 212 | | /// doesn't collapse. |
| 248 | /// Text as HTML keeps it, in an element or a quoted attribute: markup characters and quotes |
| 249 | /// escaped, line breaks as `br`, and a space after `space`, whether the text follows a space |
| 250 | /// or starts its paragraph, unbreakable so it doesn't collapse. |
| 213 | 251 | fn escape(text: &str, space: &mut bool) -> String { |
| 214 | 252 | let mut escaped = String::with_capacity(text.len()); |
| 215 | 253 | for c in text.chars() { |
| ... | ... | @@ -218,6 +256,7 @@ fn escape(text: &str, space: &mut bool) -> String { |
| 218 | 256 | '<' => escaped.push_str("&lt;"), |
| 219 | 257 | '>' => escaped.push_str("&gt;"), |
| 220 | 258 | '"' => escaped.push_str("&quot;"), |
| 259 | '\'' => escaped.push_str("&#39;"), |
| 221 | 260 | '\u{b}' | '\n' => escaped.push_str("<br>"), |
| 222 | 261 | '\t' => escaped.push_str("&nbsp;&nbsp;&nbsp; "), |
| 223 | 262 | ' ' if *space => escaped.push_str("&nbsp;"), |
| ... | ... | @@ -1307,6 +1346,81 @@ mod tests { |
| 1307 | 1346 | ); |
| 1308 | 1347 | } |
| 1309 | 1348 | |
| 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 | 1424 | /// As OneNote 2010 pasted `<p>before <img> after</p>` (lab, 2026-09-30): the text on |
| 1311 | 1425 | /// either side of the picture, the picture at the size its attributes give; one in a |
| 1312 | 1426 | /// table is left out. |