Files
faunosteo/crates/core/src/anatomy/svg/mod.rs
T
bchanot 8294d5471c fix(anatomy): V1-2.2 review findings, PIs and HTML refused, bounds
Security audit (PASS, 2 MEDIUM, 2 LOW) and verifier (ECARTS 6) on
741c25c: processing instructions and HTML fetchers refused, active
content under a long title classified, linear review groups, full
fixpoint check, bounded preview drawings, missing cap fixtures and
property checks, record wordings.
2026-10-29 13:38:33 +01:00

50 lines
1.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! SVG text → bounded, sanitised tree → text (plan R3 C2–C4). One reader for every SVG the
//! product draws: the sheet drawings now, the branding and Step 13 uploads later.
pub mod clean;
pub mod number;
pub mod prescan;
pub mod refs;
pub mod sanitize;
pub mod serialize;
pub mod tree;
pub use sanitize::Sanitized;
pub use tree::{Element, Node, SvgRejection};
/// Bounds, parses, sanitises and checks the references of an SVG text.
pub fn read_svg(text: &str) -> Result<Sanitized, SvgRejection> {
let root = tree::parse_svg(text)?;
let sanitized = sanitize::sanitize(root)?;
refs::check_references(&sanitized.root)?;
Ok(sanitized)
}
/// `read_svg` as the cleaning reads a file picked for upload: an oversized `title` or `desc`
/// is a removal, not a refusal (`sanitize::sanitize_for_cleaning`).
pub fn read_svg_for_cleaning(text: &str) -> Result<Sanitized, SvgRejection> {
let root = tree::parse_svg(text)?;
let sanitized = sanitize::sanitize_for_cleaning(root)?;
refs::check_references(&sanitized.root)?;
Ok(sanitized)
}
/// The targets of the processing instructions of a text the reader accepted (the XML
/// declaration is none). The tree drops them, so a drawing "read with no removal" may still
/// carry `<?xml-stylesheet href=…?>` in its bytes: the cleaning and the verdict refuse it.
pub fn processing_instructions(text: &str) -> Vec<String> {
let options = roxmltree::ParsingOptions {
allow_dtd: false,
nodes_limit: 200_000,
..roxmltree::ParsingOptions::default()
};
let Ok(document) = roxmltree::Document::parse_with_options(text, options) else {
return Vec::new();
};
document
.descendants()
.filter_map(|node| node.pi())
.map(|pi| pi.target.to_owned())
.collect()
}