fix(anatomy): V1-2.2 owner arbitrations on SVG references and bytes

Refuse local references to ids a drawing does not define, scope canvas
ids per view, upload every drawing in one stored form checked by the
verdict and the preview, keep strict numeric rgb() colours, and commit
real Inkscape 1.4.3 exports with their generator.
This commit is contained in:
bchanot committed 2026-10-29 16:24:07 +01:00
1 parent 17d919971e
commit e823655f09
31 files changed
+1362 -110

No files matched your search

+230 -38
View File
@@ -2,14 +2,16 @@
//! 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 cleaned text reads back with no removal and
//! cleans to itself. The strict reader of stored drawings is unchanged.
//! 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";
@@ -62,6 +64,12 @@ fn the_hostile_corpus_answers_as_the_contract_table() {
("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"),
),
// Other removals: cleaning offered.
("id-grammar.svg", Cleanable, None),
("long-title.svg", Cleanable, None),
@@ -73,7 +81,6 @@ fn the_hostile_corpus_answers_as_the_contract_table() {
("xlink-other-prefix.svg", Cleanable, None),
("xlink-prefix-rebound.svg", Cleanable, None),
// Nothing removed (comments, stray text and clamped values are no removal).
("dangling-references.svg", Clean, None),
("duplicate-ids.svg", Clean, None),
("osteo-halo-id.svg", Clean, None),
("root-without-namespace.svg", Clean, None),
@@ -93,11 +100,11 @@ fn the_hostile_corpus_answers_as_the_contract_table() {
"{file}"
),
Cleanable => assert!(
cleaning.active.is_empty() && cleaning.cleaned.is_some(),
cleaning.active.is_empty() && cleaning.stored.is_some(),
"{file}"
),
Clean => assert!(
cleaning.removed.is_empty() && cleaning.cleaned.is_none(),
cleaning.removed.is_empty() && cleaning.stored.is_some(),
"{file}"
),
}
@@ -109,8 +116,8 @@ 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()),
(6, 4),
"rgb() and attr() alone would be cleaned"
(5, 4),
"attr() alone would be cleaned; the strict rgb() is kept"
);
let events = clean_svg(&fixture("hostile/events.svg"));
assert_eq!(
@@ -230,7 +237,7 @@ fn a_plain_style_sheet_is_cleaned_and_named() {
let cleaning = clean_svg(styled);
assert_eq!(outcome(&cleaning), (Cleanable, None));
assert!(cleaning.style_removed);
let cleaned = cleaning.cleaned.as_deref().unwrap();
let cleaned = cleaning.stored.as_deref().unwrap();
assert!(!cleaned.contains("<style") && cleaned.contains("class=\"st0\""));
assert_eq!(
cleaning.sha256.as_deref(),
@@ -247,20 +254,56 @@ fn a_plain_style_sheet_is_cleaned_and_named() {
}
}
/// Owner decision 2026-10-29: the bytes uploaded are the stored form, never the original. The
/// seed's equine drawing already is in that form, so its sha256 does not move; another clean
/// file loses its declaration, comments, CDATA, entity spellings and quoting, nothing else.
#[test]
fn a_clean_file_is_uploaded_as_it_is_and_shown_as_read() {
fn a_clean_file_is_uploaded_in_its_stored_form_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.stored.as_deref().map(str::as_bytes),
Some(&horse[..])
);
assert_eq!(
cleaning.sha256.as_deref(),
Some("0ea5202fa3b95bac167b87c12ee969ab0272e085b60c2d34dbca6202c5f08541")
);
assert_eq!(cleaning.byte_size, Some(132_997));
assert!(cleaning.preview.unwrap().starts_with("<svg"));
let loose = "<?xml version='1.0' encoding='ISO-8859-1' standalone='no'?>\n<!-- editor -->\
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 4 4'><!-->--><title><![CDATA[a < b]]> &#233;</title>\
<path d='M0 0h1' fill='#ABC'/></svg>";
let cleaning = clean_svg(loose.as_bytes());
assert_eq!(outcome(&cleaning), (Clean, None));
let stored = cleaning.stored.as_deref().unwrap();
assert_eq!(
stored,
format!(
"{STORED_PROLOG}<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 4 4\"><title>a &lt; b é</title>\
<path d=\"M0 0h1\" fill=\"#ABC\"/></svg>"
)
);
assert_eq!(
cleaning.sha256.as_deref(),
Some(sha256_hex(stored.as_bytes()).as_str())
);
assert_eq!(cleaning.byte_size, Some(stored.len() as u64));
assert_eq!(
cleaning.preview.as_deref(),
stored.strip_prefix(STORED_PROLOG)
);
assert_fixpoint(stored);
assert_eq!(
serialize(&read_svg(stored).unwrap().root),
cleaning.preview.unwrap()
);
assert_eq!(
outcome(&clean_svg(b"\xff\xfe<svg/>")),
(Rejected, Some("malformed"))
@@ -274,7 +317,7 @@ fn a_clean_file_is_uploaded_as_it_is_and_shown_as_read() {
#[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-shapes.svg", Clean, None),
(
"cairo-1.18.4-pycairo-1.27.0-raster.svg",
Rejected,
@@ -292,20 +335,161 @@ fn the_vector_tool_exports_are_read_or_refused_for_their_raster() {
}
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"
]
}
/// Real Inkscape 1.4.3 exports (owner go 2026-10-29; `exports/generate-inkscape.sh`): its
/// editor metadata and namespaces and the `text` it keeps as text are removed and named, so
/// each is cleanable; shapes, gradient, clip path, internal references and colours stay; the
/// stored text is a fixpoint.
#[test]
fn the_inkscape_exports_are_cleaned_with_their_shapes() {
let cases: [(&str, &[&str]); 3] = [
(
"inkscape-1.4.3-inkscape-svg.svg",
&[
"svg@xmlns:inkscape",
"svg@xmlns:sodipodi",
"svg@xmlns:svg",
"svg@sodipodi:docname",
"sodipodi:namedview",
"text",
],
),
("inkscape-1.4.3-plain-svg.svg", &["svg@xmlns:svg", "text"]),
(
"inkscape-1.4.3-paths.svg",
&["svg@xmlns:svg", "path@aria-label"],
),
];
for (file, removed) in cases {
let cleaning = clean_svg(&fixture(&format!("exports/{file}")));
assert_eq!(outcome(&cleaning), (Cleanable, None), "{file}");
assert_eq!(cleaning.removed, removed, "{file}");
assert!(
cleaning.active.is_empty() && !cleaning.style_removed,
"{file}"
);
let stored = cleaning.stored.as_deref().unwrap();
for kept in [
"<linearGradient",
"<clipPath",
"clip-path=\"url(#frame)\"",
"fill=\"url(#coat)\"",
"stroke-dasharray=\"6 3\"",
] {
assert!(stored.contains(kept), "{file}: {kept}");
}
assert_fixpoint(stored);
}
let plain = clean_svg(&fixture("exports/inkscape-1.4.3-plain-svg.svg"));
assert!(plain.stored.unwrap().contains("fill=\"rgb(51, 102, 179)\""));
let paths = clean_svg(&fixture("exports/inkscape-1.4.3-paths.svg"));
assert!(
paths
.stored
.unwrap()
.contains("style=\"fill:#3366b3;fill-opacity:0.5\"")
);
assert_fixpoint(shapes.cleaned.as_deref().unwrap());
}
/// Owner decision 2026-10-29: cairo writes its colours `rgb(20%, 40%, 70%)`; the strict
/// numeric `rgb()` is kept, so its export reads with no removal, colours, shapes and internal
/// references included, and its text is stable.
#[test]
fn the_cairo_export_keeps_its_colours_shapes_and_references() {
let bytes = fixture("exports/cairo-1.18.4-pycairo-1.27.0-shapes.svg");
let shapes = clean_svg(&bytes);
assert!(shapes.removed.is_empty(), "{:?}", shapes.removed);
let preview = shapes.preview.as_deref().unwrap();
for colour in [
"stop-color=\"rgb(60%, 30%, 20%)\"",
"stop-color=\"rgb(90%, 80%, 60%)\"",
"stroke=\"rgb(20%, 20%, 20%)\"",
"fill=\"rgb(20%, 40%, 70%)\"",
"fill=\"rgb(0%, 0%, 0%)\"",
] {
assert!(preview.contains(colour), "{colour}");
}
let original = std::str::from_utf8(&bytes).unwrap();
let count = |text: &str, tag: &str| text.matches(tag).count();
for tag in ["<path", "<use", "<g", "<clipPath", "<linearGradient"] {
assert_eq!(count(preview, tag), count(original, tag), "{tag}");
}
for reference in [
"clip-path=\"url(#clip-0)\"",
"fill=\"url(#linear-pattern-0)\"",
"xlink:href=\"#glyph-0-0\"",
] {
assert!(preview.contains(reference), "{reference}");
}
assert!(read_svg(preview).is_ok_and(|read| read.removed.is_empty()));
assert_eq!(
clean_svg(preview.as_bytes()).preview.as_deref(),
Some(preview)
);
}
/// The colour exception opens nothing else: a reference outside the document, an active CSS
/// token or a script next to a colour still refuses the file; any other function, or a colour
/// outside the strict form, is still removed (and cleaned, never kept).
#[test]
fn colours_open_no_way_for_active_or_unknown_functions() {
let svg = |body: &str| {
format!("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 10 10\">{body}</svg>")
};
for (body, active) in [
(
"<path d=\"M0 0\" fill=\"rgb(1,2,3) url(http://e/x)\"/>",
"path@fill",
),
(
"<path d=\"M0 0\" style=\"fill:rgb(1,2,3);background:url(x)\"/>",
"path@style",
),
(
"<path d=\"M0 0\" style=\"fill:rgb(1,2,expression(alert(1)))\"/>",
"path@style",
),
(
"<path d=\"M0 0\" fill=\"rgb(1,2,3)\" onclick=\"x()\"/>",
"path@onclick",
),
(
"<path d=\"M0 0\" style=\"fill:rgb(1,2,3);@import 'x'\"/>",
"path@style",
),
("<path d=\"M0 0\" fill=\"\\72 gb(1,2,3)\"/>", "path@fill"),
(
"<path d=\"M0 0\" fill=\"rgb(1,2,3)\"/><script>alert(1)</script>",
"script",
),
] {
let cleaning = clean_svg(svg(body).as_bytes());
assert_eq!(
outcome(&cleaning),
(Rejected, Some("svg_active_content")),
"{body}"
);
assert_eq!(cleaning.active, [active], "{body}");
}
for (body, removed) in [
("<path d=\"M0 0\" fill=\"rgb(1,2,var(--x))\"/>", "path@fill"),
("<path d=\"M0 0\" fill=\"RGB(1,2,3)\"/>", "path@fill"),
("<path d=\"M0 0\" stroke=\"rgb(300,0,0)\"/>", "path@stroke"),
(
"<path d=\"M0 0\" style=\"fill:hsl(1,2%,3%)\"/>",
"path@style",
),
("<stop stop-color=\"rgba(1,2,3)\"/>", "stop@stop-color"),
] {
let cleaning = clean_svg(svg(body).as_bytes());
assert_eq!(outcome(&cleaning), (Cleanable, None), "{body}");
assert_eq!(cleaning.removed, [removed], "{body}");
assert_fixpoint(cleaning.stored.as_deref().unwrap());
}
let kept =
svg("<path d=\"M0 0\" fill=\"rgba(10%, 20%, 30%, 50%)\" style=\"stroke:rgb(1, 2, 3)\"/>");
assert_eq!(outcome(&clean_svg(kept.as_bytes())), (Clean, None));
}
#[test]
@@ -330,8 +514,7 @@ fn the_owner_editor_exports_are_cleaned_or_refused_with_a_reason() {
}
match cleaning.outcome {
Rejected => assert!(cleaning.reason.is_some(), "{name}"),
Cleanable => assert_fixpoint(cleaning.cleaned.as_deref().unwrap()),
Clean => {}
Cleanable | Clean => assert_fixpoint(cleaning.stored.as_deref().unwrap()),
}
println!(
"{name}: {:?} {:?} {:?}",
@@ -340,21 +523,22 @@ fn the_owner_editor_exports_are_cleaned_or_refused_with_a_reason() {
}
}
/// 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));
/// A stored text reads back with nothing removed, is clean and is its own stored form.
fn assert_fixpoint(stored: &str) {
let read = read_svg(stored).unwrap_or_else(|e| panic!("{e:?}: {stored}"));
assert!(read.removed.is_empty(), "{:?}: {stored}", read.removed);
let again = clean_svg(stored.as_bytes());
assert_eq!(again.outcome, Clean, "{stored}");
assert_eq!(again.stored.as_deref(), Some(stored));
assert_eq!(stored_form(&read.root), stored);
}
#[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);
if let Some(stored) = cleaning.stored {
assert_fixpoint(&stored);
}
}
}
@@ -479,6 +663,11 @@ fn drawing(rng: &mut Rng) -> String {
for _ in 0..1 + rng.next() % 4 {
element(rng, 1, &mut out, editor);
}
// Most drawings define the ids the references name, after the generated ones (which stay
// first, so their cycles still count); the others may dangle.
if !rng.next().is_multiple_of(4) {
out.push_str("<defs><linearGradient id=\"a1\"/><path id=\"b_2\" d=\"M0 0\"/></defs>");
}
out.push_str("</svg>");
out
}
@@ -495,19 +684,22 @@ fn generated_drawings_clean_to_a_fixpoint() {
Clean => clean += 1,
Cleanable => {
cleanable += 1;
assert_fixpoint(cleaning.cleaned.as_deref().unwrap());
assert_fixpoint(cleaning.stored.as_deref().unwrap());
}
Rejected => {
rejected += 1;
let reason = cleaning.reason.unwrap();
assert_eq!(reason, "reference_cycle", "{text}");
assert!(
matches!(reason.as_str(), "reference_cycle" | "dangling_reference"),
"{reason}: {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}"
cleanable > 1000 && clean > 100 && reasons.len() == 2,
"{clean} {cleanable} {rejected} {reasons:?}"
);
}