feat(anatomy): V1-2.2 classify and clean drawings before upload
The sanitiser walk names removals that carried active or external content; clean_svg answers clean, cleanable or rejected, the cleaned text reading back with no removal. Committed cairo and ImageMagick exports; the owner's editor exports go under exports/owner.
This commit is contained in:
1 parent
64a43840ce
commit
cf37aa265a
11 files changed
+840
-5
No files matched your search
@@ -0,0 +1,423 @@
|
||||
//! 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 cleaned text reads back with no removal and
|
||||
//! cleans to itself. 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::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),
|
||||
// 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")),
|
||||
// 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).
|
||||
("comments-pi-cdata.svg", Clean, None),
|
||||
("dangling-references.svg", Clean, None),
|
||||
("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.cleaned.is_some(),
|
||||
"{file}"
|
||||
),
|
||||
Clean => assert!(
|
||||
cleaning.removed.is_empty() && cleaning.cleaned.is_none(),
|
||||
"{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()),
|
||||
(6, 4),
|
||||
"rgb() and attr() alone would be cleaned"
|
||||
);
|
||||
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");
|
||||
}
|
||||
|
||||
#[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.cleaned.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")));
|
||||
}
|
||||
}
|
||||
|
||||
#[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("<svg"));
|
||||
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", 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!(
|
||||
"<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);
|
||||
}
|
||||
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.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}"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user