mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-15 23:02:32 +00:00
PDF: allow only web and mail link schemes (#2804)
* PDF: allow only web and mail link schemes Hyperlink and button URLs were written into PDF link annotations whatever their scheme, so a document could carry javascript:, data:, file: or a custom scheme into a reader. Only http, https, mailto and ftp targets are written now; other strings are left out of the PDF. * pdf: move the link-scheme test below the items it tests
This commit is contained in:
1 parent
8b169f8ee7
commit
f5087eae96
1 file changed
+29
-2
+29
-2
@@ -11,6 +11,14 @@ use krilla::outline::{Outline, OutlineNode};
|
||||
|
||||
use crate::export::Sheet;
|
||||
|
||||
/// A URL a PDF link annotation may carry. Only ordinary web and mail schemes are written; a
|
||||
/// `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)
|
||||
}
|
||||
|
||||
/// Index of the output sheet showing absolute page `abs`.
|
||||
pub(crate) fn sheet_of_page(doc: &Document, sheets: &[Sheet], abs: usize) -> Option<usize> {
|
||||
let (si, pi) = doc.page_loc(abs)?;
|
||||
@@ -67,7 +75,10 @@ pub(crate) fn annotations(doc: &Document, cache: &designcraft_compose::Cache, sh
|
||||
);
|
||||
let Some(rect) = rect else { continue };
|
||||
let target = match &h.dest {
|
||||
HyperlinkDest::Url(u) => Target::Action(Action::Link(LinkAction::new(u.clone()))),
|
||||
HyperlinkDest::Url(u) => match allowed_link(u) {
|
||||
Some(u) => Target::Action(Action::Link(LinkAction::new(u.to_string()))),
|
||||
None => continue,
|
||||
},
|
||||
HyperlinkDest::Email(e) => Target::Action(Action::Link(LinkAction::new(format!("mailto:{e}")))),
|
||||
HyperlinkDest::Page(p) => match sheet_of_page(doc, sheets, *p) {
|
||||
Some(i) => Target::Destination(XyzDestination::new(i, krilla::geom::Point::from_xy(0.0, 0.0)).into()),
|
||||
@@ -101,8 +112,9 @@ pub(crate) fn annotations(doc: &Document, cache: &designcraft_compose::Cache, sh
|
||||
let total = doc.page_count();
|
||||
let page = match action {
|
||||
B::GoToUrl { url } => {
|
||||
let Some(url) = allowed_link(url) else { continue };
|
||||
out.push(Annotation::new_link(
|
||||
LinkAnnotation::new(rect, Target::Action(Action::Link(LinkAction::new(url.clone())))),
|
||||
LinkAnnotation::new(rect, Target::Action(Action::Link(LinkAction::new(url.to_string())))),
|
||||
Some(it.name.clone()),
|
||||
));
|
||||
continue;
|
||||
@@ -165,3 +177,18 @@ pub(crate) fn outline(doc: &Document, sheets: &[Sheet]) -> Option<Outline> {
|
||||
}
|
||||
Some(o)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::allowed_link;
|
||||
|
||||
#[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"] {
|
||||
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", ""] {
|
||||
assert_eq!(allowed_link(bad), None, "{bad}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user