From d5f82c2143cbc2f86c4a5e0e7f2ff2e1eda269bd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 17:31:42 -0500 Subject: [PATCH] links: allow only web and mail URLs (#2809) Hyperlinks and buttons accepted any scheme and passed it to the PDF exporter, so an imported file could emit file:, javascript: or UNC links that a recipient's viewer may follow. Add a shared http/https/mailto check at both command entry points and use the same check at export. --- crates/doc/src/lib.rs | 9 ++++++++ crates/engine/src/cmd/buttons.rs | 20 +++++++++++++++- crates/engine/src/cmd/interactive.rs | 16 +++++++++++++ crates/pdf/src/links.rs | 16 +++++++++---- crates/pdf/src/tests.rs | 34 ++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 5 deletions(-) diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index 1efadfe2a..9aec84c1f 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -955,6 +955,15 @@ pub enum HyperlinkDest { Anchor(u64), } +/// Whether a URL may be used as an external link. Only web and mail schemes are allowed: a +/// `file:`, `javascript:` or UNC link can make a recipient's PDF viewer contact an attacker. +pub fn is_safe_link_url(url: &str) -> bool { + let Some((scheme, rest)) = url.split_once(':') else { return false }; + !rest.is_empty() + && !url.chars().any(|c| c.is_control() || c.is_whitespace()) + && matches!(scheme.to_ascii_lowercase().as_str(), "http" | "https" | "mailto") +} + impl HyperlinkDest { /// The name a hyperlink is given when none is supplied. pub fn label(&self) -> String { diff --git a/crates/engine/src/cmd/buttons.rs b/crates/engine/src/cmd/buttons.rs index 5a644503d..9c5174440 100644 --- a/crates/engine/src/cmd/buttons.rs +++ b/crates/engine/src/cmd/buttons.rs @@ -18,7 +18,13 @@ fn parse(p: &Value) -> Result> { "lastPage" => ButtonAction::GoToLastPage, "nextPage" => ButtonAction::GoToNextPage, "previousPage" => ButtonAction::GoToPreviousPage, - "url" => ButtonAction::GoToUrl { url: p.get("url").and_then(Value::as_str).ok_or_else(|| bad("button.set", "`url` required"))?.to_string() }, + "url" => { + let url = p.get("url").and_then(Value::as_str).ok_or_else(|| bad("button.set", "`url` required"))?; + if !designcraft_doc::is_safe_link_url(url) { + return Err(bad("button.set", "`url` must start with http://, https:// or mailto:")); + } + ButtonAction::GoToUrl { url: url.to_string() } + } o => return Err(bad("button.set", format!("unknown action `{o}`"))), })) } @@ -179,4 +185,16 @@ mod tests { assert_eq!(designcraft_render::pdf_page_count(&bytes), Some(3), "the update still reads"); assert!(s.execute("button.list", &json!({})).unwrap().as_array().unwrap().is_empty()); } + + #[test] + fn buttons_refuse_non_web_schemes() { + let mut s = Session::new(); + s.execute("file.new", &json!({"pages": 1})).unwrap(); + let id = s.execute("frame.create", &json!({"rect": [72, 72, 200, 120]})).unwrap()["id"].clone(); + for bad in ["javascript:alert(1)", "file:///etc/passwd", "\\\\attacker\\share"] { + let e = s.execute("button.set", &json!({"ids": [id.clone()], "action": "url", "url": bad})).expect_err("refused").to_string(); + assert!(e.contains("http://"), "{bad}: {e}"); + } + s.execute("button.set", &json!({"ids": [id], "action": "url", "url": "https://example.com"})).unwrap(); + } } diff --git a/crates/engine/src/cmd/interactive.rs b/crates/engine/src/cmd/interactive.rs index 9f340e24d..75347b549 100644 --- a/crates/engine/src/cmd/interactive.rs +++ b/crates/engine/src/cmd/interactive.rs @@ -101,6 +101,9 @@ pub fn specs() -> Vec { /// and kept the old destination (noted in review). fn dest_param(id: &str, d: &designcraft_doc::Document, p: &Value) -> Result> { if let Some(u) = str_param(p, "url") { + if !designcraft_doc::is_safe_link_url(u) { + return Err(bad(id, "`url` must start with http://, https:// or mailto:")); + } return Ok(Some(HyperlinkDest::Url(u.to_string()))); } if let Some(e) = str_param(p, "email") { @@ -195,6 +198,19 @@ mod panel_tests { s.execute("bookmark.rename", &json!({"index": 0, "name": "Back"})).unwrap(); assert_eq!(s.execute("bookmark.list", &json!({})).unwrap()[0]["name"], "Back"); } + + #[test] + fn hyperlinks_refuse_non_web_schemes() { + let mut s = Session::new(); + s.execute("file.new", &json!({"pages": 1})).unwrap(); + let id = s.execute("frame.create", &json!({"rect": [72, 72, 200, 120]})).unwrap()["id"].clone(); + for bad in ["javascript:alert(1)", "file:///etc/passwd", "data:text/html,hi", "\\\\attacker\\share"] { + let e = s.execute("hyperlink.create", &json!({"ids": [id.clone()], "url": bad})).expect_err("refused").to_string(); + assert!(e.contains("http://"), "{bad}: {e}"); + } + s.execute("hyperlink.create", &json!({"ids": [id.clone()], "url": "mailto:info@example.com"})).unwrap(); + s.execute("hyperlink.create", &json!({"ids": [id], "url": "https://example.com"})).unwrap(); + } } #[cfg(test)] diff --git a/crates/pdf/src/links.rs b/crates/pdf/src/links.rs index b03e834ea..0d6c5daa8 100644 --- a/crates/pdf/src/links.rs +++ b/crates/pdf/src/links.rs @@ -15,8 +15,7 @@ use crate::export::Sheet; /// `javascript:`, `data:`, `file:` or custom scheme is left out rather than embedded in the PDF /// (audit: DesignCraft #4). fn allowed_link(url: &str) -> Option<&str> { - let lower = url.to_ascii_lowercase(); - ["http://", "https://", "mailto:", "ftp://"].iter().any(|p| lower.starts_with(p)).then_some(url) + designcraft_doc::is_safe_link_url(url).then_some(url) } /// Index of the output sheet showing absolute page `abs`. @@ -184,10 +183,19 @@ mod tests { #[test] fn only_web_and_mail_schemes_become_link_annotations() { - for ok in ["https://example.com", "HTTP://example.com", "mailto:a@b.example", "ftp://example.com"] { + for ok in ["https://example.com", "HTTP://example.com", "mailto:a@b.example"] { assert_eq!(allowed_link(ok), Some(ok)); } - for bad in ["javascript:alert(1)", "JavaScript:alert(1)", "data:text/html,x", "file:///etc/passwd", "vbscript:x", "example.com", ""] { + for bad in [ + "javascript:alert(1)", + "JavaScript:alert(1)", + "data:text/html,x", + "file:///etc/passwd", + "ftp://example.com", + "vbscript:x", + "example.com", + "", + ] { assert_eq!(allowed_link(bad), None, "{bad}"); } } diff --git a/crates/pdf/src/tests.rs b/crates/pdf/src/tests.rs index 452b5d1b5..a851fcf8d 100644 --- a/crates/pdf/src/tests.rs +++ b/crates/pdf/src/tests.rs @@ -758,6 +758,40 @@ fn small_caps_and_small_cap_figures_extract_as_typed() { assert!(uncompressed(&d, PdfOptions::default()).contains("/ActualText")); } +/// Links and buttons from an imported document still go through the export allowlist. +#[test] +fn link_annotation_export_refuses_non_web_schemes() { + use designcraft_doc::{ButtonAction, Hyperlink, HyperlinkDest, HyperlinkSource, Shape}; + use designcraft_geom::shapes::rectangle; + + let mut d = Document::new(&NewDocument::default()); + let lid = d.default_layer(); + let safe = ItemId(d.alloc()); + let mut safe_item = Item::new(safe, lid, Shape::Rectangle, rectangle(Rect::new(72.0, 72.0, 200.0, 120.0))); + safe_item.button = Some(ButtonAction::GoToUrl { url: "https://example.com/button".into() }); + d.insert_item(SpreadRef::Doc(0), safe_item, None).unwrap(); + let unsafe_id = ItemId(d.alloc()); + let mut unsafe_item = Item::new(unsafe_id, lid, Shape::Rectangle, rectangle(Rect::new(72.0, 200.0, 200.0, 248.0))); + unsafe_item.button = Some(ButtonAction::GoToUrl { url: "file:///etc/passwd".into() }); + d.insert_item(SpreadRef::Doc(0), unsafe_item, None).unwrap(); + d.hyperlinks.push(Hyperlink { + id: 1, + name: "bad".into(), + source: HyperlinkSource::Item { id: safe }, + dest: HyperlinkDest::Url("javascript:alert(1)".into()), + }); + d.hyperlinks.push(Hyperlink { + id: 2, + name: "good".into(), + source: HyperlinkSource::Item { id: safe }, + dest: HyperlinkDest::Url("https://example.com/link".into()), + }); + let s = uncompressed(&d, PdfOptions::default()); + assert!(s.contains("https://example.com/button") && s.contains("https://example.com/link"), "safe links export: {s}"); + assert!(!s.contains("file:///etc/passwd"), "a file URL is not exported: {s}"); + assert!(!s.contains("javascript:"), "a javascript URL is not exported: {s}"); +} + /// A clipping path on a placed image becomes a clip in the page content, inside the frame's. #[test] fn clipping_path_clips_the_image() {