//! V1-2.2: classifying and cleaning a drawing before upload (contract §9.2). The hostile //! corpus of R3 answers as the table of §9.2 says, file by file; the committed vector-tool //! exports and the product drawing read clean or are refused for their raster; the owner's //! editor exports (`fixtures/svg/exports/owner`) are read when present; and a property test //! over generated drawings proves the fixpoint: a stored text reads back with no removal and //! gives itself again. Every accepted file is uploaded in the stored form (owner decision //! 2026-10-29), never as its original bytes. The strict reader of stored drawings is unchanged. #![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] mod common; use faunosteo_core::anatomy::svg::clean::{CleaningOutcome, SvgCleaning, clean_svg}; use faunosteo_core::anatomy::svg::read_svg; use faunosteo_core::anatomy::svg::serialize::{STORED_PROLOG, serialize, stored_form}; use faunosteo_core::domain::sheet::verdict::sha256_hex; const FIXTURES: &str = "crates/core/tests/fixtures/svg"; fn fixture(path: &str) -> Vec { std::fs::read(common::repo_path(&format!("{FIXTURES}/{path}"))).unwrap() } fn outcome(cleaning: &SvgCleaning) -> (CleaningOutcome, Option<&str>) { (cleaning.outcome, cleaning.reason.as_deref()) } use CleaningOutcome::{Clean, Cleanable, Rejected}; #[test] fn the_hostile_corpus_answers_as_the_contract_table() { let active = Some("svg_active_content"); let table: &[(&str, CleaningOutcome, Option<&str>)] = &[ // Active or external content: refused, never cleaned. ("animation.svg", Rejected, active), ("css.svg", Rejected, active), ("data-and-images.svg", Rejected, active), ("entity-in-attribute.svg", Rejected, active), ("events.svg", Rejected, active), ("external-urls.svg", Rejected, active), ("external-use.svg", Rejected, active), ("foreign.svg", Rejected, active), ("href-unicode-space.svg", Rejected, active), ("script-urls.svg", Rejected, active), ("script.svg", Rejected, active), ("style-element.svg", Rejected, active), ("url-unicode-space.svg", Rejected, active), // A processing instruction survives in the bytes the tree never reads. ("comments-pi-cdata.svg", Rejected, active), // Hard rejections of the reader. ("bom.svg", Rejected, Some("malformed")), ("cdata-desync.svg", Rejected, Some("too_deep")), ( "comment-desync-attributes.svg", Rejected, Some("too_complex"), ), ("comment-desync-depth.svg", Rejected, Some("too_deep")), ("doctype-entities.svg", Rejected, Some("doctype")), ("doctype-in-comment.svg", Rejected, Some("doctype")), ("malformed.svg", Rejected, Some("malformed")), ("not-svg.svg", Rejected, Some("not_svg")), ("prefixed-root.svg", Rejected, Some("not_svg")), ("unbound-prefix.svg", Rejected, Some("malformed")), ("use-mutual-cycle.svg", Rejected, Some("reference_cycle")), ("use-self-cycle.svg", Rejected, Some("reference_cycle")), ("wrong-root-namespace.svg", Rejected, Some("not_svg")), // A reference to an id the drawing does not define (owner decision 2026-10-29). ( "dangling-references.svg", Rejected, Some("dangling_reference"), ), // An id the composition writes (owner brief 2026-10-30). ("osteo-halo-id.svg", Rejected, Some("reserved_id")), // Other removals: cleaning offered. ("id-grammar.svg", Cleanable, None), ("long-title.svg", Cleanable, None), ("namespaces.svg", Cleanable, None), ("refused-subtrees.svg", Cleanable, None), ("root-class-style.svg", Cleanable, None), ("root-presentation.svg", Cleanable, None), ("whitespace-references.svg", Cleanable, None), ("xlink-other-prefix.svg", Cleanable, None), ("xlink-prefix-rebound.svg", Cleanable, None), // Nothing removed (comments, stray text and clamped values are no removal). ("duplicate-ids.svg", Clean, None), ("root-without-namespace.svg", Clean, None), ("std-deviation.svg", Clean, None), ("viewbox-non-finite.svg", Clean, None), ]; let corpus = std::fs::read_dir(common::repo_path(&format!("{FIXTURES}/hostile"))) .unwrap() .count(); assert_eq!(table.len(), corpus, "every hostile file has a line"); for (file, expected, reason) in table { let cleaning = clean_svg(&fixture(&format!("hostile/{file}"))); assert_eq!(outcome(&cleaning), (*expected, *reason), "{file}"); match expected { Rejected => assert!( cleaning.preview.is_none() && cleaning.sha256.is_none(), "{file}" ), Cleanable => assert!( cleaning.active.is_empty() && cleaning.stored.is_some(), "{file}" ), Clean => assert!( cleaning.removed.is_empty() && cleaning.stored.is_some(), "{file}" ), } } } #[test] fn active_names_are_listed_and_ordinary_removals_are_not() { let css = clean_svg(&fixture("hostile/css.svg")); assert_eq!( (css.removed.len(), css.active.len()), (5, 4), "attr() alone would be cleaned; the strict rgb() is kept" ); let events = clean_svg(&fixture("hostile/events.svg")); assert_eq!( events.active, [ "svg@onload", "path@onclick", "path@ONMOUSEOVER", "g@onfocusin" ] ); let namespaces = clean_svg(&fixture("hostile/namespaces.svg")); assert!( namespaces .removed .contains(&"path@inkscape:label".to_owned()) ); let title = clean_svg(&fixture("hostile/long-title.svg")); assert_eq!(title.removed, ["title"]); // The stored-drawing reader still refuses that file whole. let text = String::from_utf8(fixture("hostile/long-title.svg")).unwrap(); assert_eq!(read_svg(&text).unwrap_err().reason(), "too_complex"); } /// The security audit's probes (V1-2.2): content the classification must refuse, never clean. #[test] fn hidden_or_html_content_is_refused_not_cleaned() { let long = "A".repeat(4097); let svg = |body: &str| { format!( "{body}" ) }; let cases = [ "".to_owned() + &svg(""), svg(""), svg(&format!("{long}<script>alert(1)</script>")), svg(&format!("{long}")), svg(&format!( "{long}