diff --git a/crates/core/src/anatomy/svg/clean.rs b/crates/core/src/anatomy/svg/clean.rs
new file mode 100644
index 00000000..7b981e0f
--- /dev/null
+++ b/crates/core/src/anatomy/svg/clean.rs
@@ -0,0 +1,136 @@
+//! Cleaning a drawing before upload (contract §9.2, O19): the page hands the file's bytes to
+//! the core reader before anything is stored, and gets one of three answers.
+//!
+//! - rejected: a hard rejection of the reader (with its reason), a text that is not UTF-8
+//! (`malformed`), or any removal of active or external content (`svg_active_content`, the
+//! names in `active`); no cleaning is offered;
+//! - cleanable: only other removals (editor metadata and namespaces, `data-*`, `"#;
+ let cleaning = clean_svg(styled);
+ assert_eq!(outcome(&cleaning), (Cleanable, None));
+ assert!(cleaning.style_removed);
+ let cleaned = cleaning.cleaned.as_deref().unwrap();
+ assert!(!cleaned.contains(""#[..],
+ br#""#,
+ br#""#,
+ br#""#,
+ ] {
+ assert_eq!(outcome(&clean_svg(hostile)), (Rejected, Some("svg_active_content")));
+ }
+}
+
+#[test]
+fn a_clean_file_is_uploaded_as_it_is_and_shown_as_read() {
+ let horse = std::fs::read(common::repo_path(
+ "supabase/seed/anatomical-sheets/equine/draft/assets/horse.svg",
+ ))
+ .unwrap();
+ let cleaning = clean_svg(&horse);
+ assert_eq!(outcome(&cleaning), (Clean, None));
+ assert_eq!(
+ cleaning.sha256.as_deref(),
+ Some("0ea5202fa3b95bac167b87c12ee969ab0272e085b60c2d34dbca6202c5f08541")
+ );
+ assert_eq!(cleaning.byte_size, Some(132_997));
+ assert!(cleaning.preview.unwrap().starts_with("")),
+ (Rejected, Some("malformed"))
+ );
+ assert_eq!(
+ outcome(&clean_svg(&vec![b' '; 2 * 1024 * 1024 + 1])),
+ (Rejected, Some("too_large"))
+ );
+}
+
+#[test]
+fn the_vector_tool_exports_are_read_or_refused_for_their_raster() {
+ let table = [
+ ("cairo-1.18.4-pycairo-1.27.0-shapes.svg", Cleanable, None),
+ (
+ "cairo-1.18.4-pycairo-1.27.0-raster.svg",
+ Rejected,
+ Some("svg_active_content"),
+ ),
+ ("imagemagick-7.1.2-18-draw.svg", Clean, None),
+ ("imagemagick-7.1.2-18-mvg.svg", Clean, None),
+ ];
+ for (file, expected, reason) in table {
+ assert_eq!(
+ outcome(&clean_svg(&fixture(&format!("exports/{file}")))),
+ (expected, reason),
+ "{file}"
+ );
+ }
+ let raster = clean_svg(&fixture("exports/cairo-1.18.4-pycairo-1.27.0-raster.svg"));
+ assert_eq!(raster.active, ["image"]);
+ // cairo writes its colours as `rgb(20%, 40%, 70%)`: the allowlist takes any function out
+ // of a presentation attribute, so the cleaned drawing keeps its shapes, not its colours.
+ let shapes = clean_svg(&fixture("exports/cairo-1.18.4-pycairo-1.27.0-shapes.svg"));
+ assert_eq!(
+ shapes.removed,
+ [
+ "stop@stop-color",
+ "stop@stop-color",
+ "path@stroke",
+ "path@fill",
+ "g@fill"
+ ]
+ );
+ assert_fixpoint(shapes.cleaned.as_deref().unwrap());
+}
+
+#[test]
+fn the_owner_editor_exports_are_cleaned_or_refused_with_a_reason() {
+ let dir = common::repo_path(&format!("{FIXTURES}/exports/owner"));
+ let files: Vec<_> = std::fs::read_dir(dir)
+ .unwrap()
+ .map(|entry| entry.unwrap().path())
+ .filter(|path| path.extension().is_some_and(|e| e == "svg"))
+ .collect();
+ println!("owner editor exports: {}", files.len());
+ for path in files {
+ let name = path.file_name().unwrap().to_string_lossy().into_owned();
+ let cleaning = clean_svg(&std::fs::read(&path).unwrap());
+ if name.contains("-active-") {
+ assert_eq!(
+ outcome(&cleaning),
+ (Rejected, Some("svg_active_content")),
+ "{name}"
+ );
+ continue;
+ }
+ match cleaning.outcome {
+ Rejected => assert!(cleaning.reason.is_some(), "{name}"),
+ Cleanable => assert_fixpoint(cleaning.cleaned.as_deref().unwrap()),
+ Clean => {}
+ }
+ println!(
+ "{name}: {:?} {:?} {:?}",
+ cleaning.outcome, cleaning.reason, cleaning.removed
+ );
+ }
+}
+
+/// A cleaned text reads back with nothing removed and cleans to itself.
+fn assert_fixpoint(cleaned: &str) {
+ let read = read_svg(cleaned).unwrap_or_else(|e| panic!("{e:?}: {cleaned}"));
+ assert!(read.removed.is_empty(), "{:?}: {cleaned}", read.removed);
+ let again = clean_svg(cleaned.as_bytes());
+ assert_eq!(again.outcome, Clean, "{cleaned}");
+ assert_eq!(again.preview.as_deref(), Some(cleaned));
+}
+
+#[test]
+fn every_cleanable_file_of_the_corpus_reaches_the_fixpoint() {
+ for file in std::fs::read_dir(common::repo_path(&format!("{FIXTURES}/hostile"))).unwrap() {
+ let cleaning = clean_svg(&std::fs::read(file.unwrap().path()).unwrap());
+ if let Some(cleaned) = cleaning.cleaned {
+ assert_fixpoint(&cleaned);
+ }
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Property test: generated drawings mixing what the reader keeps, cleans and refuses.
+
+struct Rng(u64);
+
+impl Rng {
+ fn next(&mut self) -> u64 {
+ self.0 ^= self.0 << 13;
+ self.0 ^= self.0 >> 7;
+ self.0 ^= self.0 << 17;
+ self.0
+ }
+
+ fn pick<'a>(&mut self, items: &[&'a str]) -> &'a str {
+ items[(self.next() % items.len() as u64) as usize]
+ }
+}
+
+const ELEMENTS: [&str; 14] = [
+ "g",
+ "path",
+ "circle",
+ "rect",
+ "defs",
+ "linearGradient",
+ "clipPath",
+ "title",
+ "symbol",
+ "metadata",
+ "sodipodi:namedview",
+ "text",
+ "switch",
+ "use",
+];
+
+const ATTRIBUTES: [&str; 22] = [
+ "id=\"a1\"",
+ "id=\"1bad\"",
+ "id=\"b_2\"",
+ "class=\"st0\"",
+ "style=\"fill:#123456\"",
+ "style=\"fill:rgb(1,2,3)\"",
+ "fill=\"url(#a1)\"",
+ "fill=\"none\"",
+ "d=\"M0 0L1 1\"",
+ "data-name=\"Calque 1\"",
+ "inkscape:label=\"x\"",
+ "xml:space=\"preserve\"",
+ "transform=\"translate(1 2)\"",
+ "stroke-width=\"0.5\"",
+ "opacity=\"0.5\"",
+ "href=\"#b_2\"",
+ "xlink:href=\"#a1\"",
+ "x=\"0px\"",
+ "enable-background=\"new\"",
+ "stdDeviation=\"500\"",
+ "sodipodi:docname=\"a.svg\"",
+ "points=\"0,0 1,1 2,0\"",
+];
+
+/// One element and its subtree; prefixed names only when the root declares them.
+fn element(rng: &mut Rng, depth: usize, out: &mut String, editor: bool) {
+ let name = loop {
+ let name = rng.pick(&ELEMENTS);
+ if editor || !name.contains(':') {
+ break name;
+ }
+ };
+ out.push('<');
+ out.push_str(name);
+ let mut written: Vec<&str> = Vec::new();
+ for _ in 0..rng.next() % 4 {
+ let attribute = rng.pick(&ATTRIBUTES);
+ let attribute_name = attribute.split('=').next().unwrap_or(attribute);
+ let declared =
+ editor || !attribute.starts_with("inkscape:") && !attribute.starts_with("sodipodi:");
+ if declared && !written.contains(&attribute_name) {
+ written.push(attribute_name);
+ out.push(' ');
+ out.push_str(attribute);
+ }
+ }
+ if depth >= 4 || rng.next().is_multiple_of(3) {
+ out.push_str("/>");
+ return;
+ }
+ out.push('>');
+ if name == "title" {
+ out.push_str("Garrot & « encolure »");
+ }
+ for _ in 0..rng.next() % 3 {
+ element(rng, depth + 1, out, editor);
+ }
+ out.push_str("");
+ out.push_str(name);
+ out.push('>');
+}
+
+fn drawing(rng: &mut Rng) -> String {
+ let root = rng.pick(&[
+ "",
+ "",
+ " class=\"page\"",
+ " style=\"width:100%\"",
+ " version=\"1.1\"",
+ ]);
+ let editor = rng.next().is_multiple_of(2);
+ let declarations = if editor {
+ " xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" \
+ xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\""
+ } else {
+ ""
+ };
+ let mut out = format!(
+ "");
+ out
+}
+
+#[test]
+fn generated_drawings_clean_to_a_fixpoint() {
+ let mut rng = Rng(0x5eed_1234_abcd_0001);
+ let (mut clean, mut cleanable, mut rejected) = (0, 0, 0);
+ let mut reasons = std::collections::BTreeMap::new();
+ for _ in 0..3000 {
+ let text = drawing(&mut rng);
+ let cleaning = clean_svg(text.as_bytes());
+ match cleaning.outcome {
+ Clean => clean += 1,
+ Cleanable => {
+ cleanable += 1;
+ assert_fixpoint(cleaning.cleaned.as_deref().unwrap());
+ }
+ Rejected => {
+ rejected += 1;
+ let reason = cleaning.reason.unwrap();
+ assert_eq!(reason, "reference_cycle", "{text}");
+ *reasons.entry(reason).or_insert(0) += 1;
+ }
+ }
+ }
+ println!("generated: {clean} clean, {cleanable} cleanable, {rejected} rejected {reasons:?}");
+ assert!(
+ cleanable > 1000 && clean > 100,
+ "{clean} {cleanable} {rejected}"
+ );
+}