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.
706 lines
26 KiB
Rust
706 lines
26 KiB
Rust
//! 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<u8> {
|
|
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"),
|
|
),
|
|
// 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),
|
|
("osteo-halo-id.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!(
|
|
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:h=\"http://www.w3.org/1999/xhtml\">{body}</svg>"
|
|
)
|
|
};
|
|
let cases = [
|
|
"<?xml-stylesheet type=\"text/xsl\" href=\"https://attacker.example/x.xsl\"?>".to_owned()
|
|
+ &svg("<path d=\"M0 0h1\"/>"),
|
|
svg("<?xml-stylesheet href=\"x.css\"?><path d=\"M0 0h1\"/>"),
|
|
svg(&format!("<title>{long}<script>alert(1)</script></title>")),
|
|
svg(&format!("<title onload=\"x()\">{long}</title>")),
|
|
svg(&format!(
|
|
"<desc>{long}<foreignObject><iframe src=\"https://e/\"/></foreignObject></desc>"
|
|
)),
|
|
svg("<h:img src=\"https://e/x.png\"/>"),
|
|
svg("<h:meta http-equiv=\"refresh\" content=\"0;url=https://e\"/>"),
|
|
svg("<h:input type=\"image\" src=\"https://e\"/>"),
|
|
svg("<h:source srcset=\"https://e/a.png\"/>"),
|
|
svg("<h:frame src=\"https://e\"/>"),
|
|
svg("<h:form action=\"https://e\"/>"),
|
|
svg("<h:div>x</h:div>"),
|
|
svg("<g><x:thing xmlns:x=\"urn:x\" src=\"https://e\"/></g>"),
|
|
svg("<g xml:base=\"https://e/\"><path d=\"M0 0h1\"/></g>"),
|
|
svg("<path d=\"M0 0h1\" style=\"fill:src('https://e/x')\"/>"),
|
|
svg("<style>p{fill:image('https://e/x.png')}</style>"),
|
|
];
|
|
for case in cases {
|
|
let cleaning = clean_svg(case.as_bytes());
|
|
assert_eq!(
|
|
outcome(&cleaning),
|
|
(Rejected, Some("svg_active_content")),
|
|
"{case}"
|
|
);
|
|
assert!(!cleaning.active.is_empty(), "{case}");
|
|
}
|
|
// A long title alone is still cleaned; editor metadata stays cleanable.
|
|
assert_eq!(
|
|
outcome(&clean_svg(
|
|
svg(&format!("<title>{long}</title>")).as_bytes()
|
|
)),
|
|
(Cleanable, None)
|
|
);
|
|
let inkscape = r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"><sodipodi:namedview id="n"/><path d="M0 0h1"/></svg>"#;
|
|
assert_eq!(outcome(&clean_svg(inkscape.as_bytes())), (Cleanable, None));
|
|
}
|
|
|
|
#[test]
|
|
fn the_verdict_refuses_a_stored_processing_instruction() {
|
|
use faunosteo_core::domain::sheet::verdict::{
|
|
DrawingResult, VerdictBinding, VerdictDrawing, VerdictInput, compute_verdict,
|
|
};
|
|
let bytes = b"<?xml-stylesheet href=\"https://e/x.xsl\"?><svg xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M0 0h1\"/></svg>";
|
|
let asset = "11111111-2222-4333-8444-555555555555";
|
|
let document = serde_json::json!({
|
|
"schema_version": 2, "species_id": "canine", "name": { "fr": "C", "en": "C" },
|
|
"description": {}, "scope_disclaimer": {}, "attribution": { "authors": [], "license": null, "notes": null },
|
|
"vertebral_config": { "regions": [] }, "references": [], "categories": [],
|
|
"views": [{ "id": "main", "kind": "main", "name": { "fr": "V", "en": "V" }, "svg_asset_id": asset, "view_box": null }],
|
|
"annotations": [], "finding_zones": []
|
|
})
|
|
.to_string();
|
|
let sha = sha256_hex(bytes);
|
|
let verdict = compute_verdict(&VerdictInput {
|
|
binding: VerdictBinding {
|
|
sheet_id: "s".into(),
|
|
version_id: "v".into(),
|
|
revision: 1,
|
|
review_id: "r".into(),
|
|
},
|
|
document: &document,
|
|
base_document: None,
|
|
drawings: vec![VerdictDrawing {
|
|
asset_id: asset,
|
|
sha256: &sha,
|
|
byte_size: bytes.len() as u64,
|
|
bytes: Some(bytes),
|
|
}],
|
|
});
|
|
assert_eq!(verdict.drawings[0].result, DrawingResult::SvgRejected);
|
|
assert_eq!(
|
|
verdict.drawings[0].detail.as_deref(),
|
|
Some("processing_instruction")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_plain_style_sheet_is_cleaned_and_named() {
|
|
let styled = br#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 4 4"><defs><style>.st0{fill:#fff}</style></defs><path class="st0" d="M0 0h1"/></svg>"#;
|
|
let cleaning = clean_svg(styled);
|
|
assert_eq!(outcome(&cleaning), (Cleanable, None));
|
|
assert!(cleaning.style_removed);
|
|
let cleaned = cleaning.stored.as_deref().unwrap();
|
|
assert!(!cleaned.contains("<style") && cleaned.contains("class=\"st0\""));
|
|
assert_eq!(
|
|
cleaning.sha256.as_deref(),
|
|
Some(sha256_hex(cleaned.as_bytes()).as_str())
|
|
);
|
|
assert_eq!(cleaning.byte_size, Some(cleaned.len() as u64));
|
|
for hostile in [
|
|
&br#"<svg xmlns="http://www.w3.org/2000/svg"><style>@font-face{src:url(a.woff)}</style></svg>"#[..],
|
|
br#"<svg xmlns="http://www.w3.org/2000/svg"><style>p{fill:red;behavior:url(x)}</style></svg>"#,
|
|
br#"<svg xmlns="http://www.w3.org/2000/svg"><style>p{background:url(javascript:a)}</style></svg>"#,
|
|
br#"<svg xmlns="http://www.w3.org/2000/svg"><style>p{fill:\72 ed}</style></svg>"#,
|
|
] {
|
|
assert_eq!(outcome(&clean_svg(hostile)), (Rejected, Some("svg_active_content")));
|
|
}
|
|
}
|
|
|
|
/// 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_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]]> é</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 < 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"))
|
|
);
|
|
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", Clean, 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"]);
|
|
}
|
|
|
|
/// 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\"")
|
|
);
|
|
}
|
|
|
|
/// 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]
|
|
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 | Clean => assert_fixpoint(cleaning.stored.as_deref().unwrap()),
|
|
}
|
|
println!(
|
|
"{name}: {:?} {:?} {:?}",
|
|
cleaning.outcome, cleaning.reason, cleaning.removed
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 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(stored) = cleaning.stored {
|
|
assert_fixpoint(&stored);
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 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!(
|
|
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"{declarations} \
|
|
viewBox=\"0 0 10 10\"{root}>"
|
|
);
|
|
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
|
|
}
|
|
|
|
#[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.stored.as_deref().unwrap());
|
|
}
|
|
Rejected => {
|
|
rejected += 1;
|
|
let reason = cleaning.reason.unwrap();
|
|
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 && reasons.len() == 2,
|
|
"{clean} {cleanable} {rejected} {reasons:?}"
|
|
);
|
|
}
|