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,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-*`, `<style>`,
|
||||
//! `class` or `style` on the root, ids outside the grammar, unsupported elements, long
|
||||
//! titles or descriptions): the cleaned text is the sanitised tree serialised, and it is
|
||||
//! that text, read back with no removal, that is hashed and uploaded;
|
||||
//! - clean: no removal; the original bytes are uploaded as they are.
|
||||
//!
|
||||
//! In every case but a rejection, `preview` is the drawing as the core reads it: the page
|
||||
//! shows that, never the original file. Stored drawings are never repaired afterwards
|
||||
//! (LRN-055): the verdict reads each one again and refuses any removal.
|
||||
|
||||
use serde::Serialize;
|
||||
#[cfg(feature = "ts")]
|
||||
use ts_rs::TS;
|
||||
|
||||
use super::prescan::SVG_CAPS;
|
||||
use super::serialize::serialize;
|
||||
use super::{read_svg, read_svg_for_cleaning};
|
||||
use crate::domain::sheet::verdict::sha256_hex;
|
||||
|
||||
/// The reason of a refusal for active or external content.
|
||||
pub const ACTIVE_CONTENT: &str = "svg_active_content";
|
||||
/// A preview larger than the canvas accepts (`ops::MAX_COMPOSED_BYTES`).
|
||||
const MAX_PREVIEW_BYTES: usize = 4 * 1024 * 1024;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
||||
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum CleaningOutcome {
|
||||
Clean,
|
||||
Cleanable,
|
||||
Rejected,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
|
||||
pub struct SvgCleaning {
|
||||
pub outcome: CleaningOutcome,
|
||||
/// Rejected: the reader's reason, `malformed`, `too_large` or `svg_active_content`.
|
||||
pub reason: Option<String>,
|
||||
/// Every removal, `tag` or `tag@attribute`, in document order.
|
||||
pub removed: Vec<String>,
|
||||
/// The removals that carried active or external content.
|
||||
pub active: Vec<String>,
|
||||
/// A `<style>` element was removed: the classes it styled render unstyled.
|
||||
pub style_removed: bool,
|
||||
/// The drawing as the core reads it (sanitised, serialised).
|
||||
pub preview: Option<String>,
|
||||
/// Cleanable only: the text to upload.
|
||||
pub cleaned: Option<String>,
|
||||
/// SHA-256 and size of the bytes to upload: the original (clean) or the cleaned text.
|
||||
pub sha256: Option<String>,
|
||||
#[cfg_attr(feature = "ts", ts(type = "number | null"))]
|
||||
pub byte_size: Option<u64>,
|
||||
}
|
||||
|
||||
fn rejected(reason: &str, removed: Vec<String>, active: Vec<String>) -> SvgCleaning {
|
||||
SvgCleaning {
|
||||
outcome: CleaningOutcome::Rejected,
|
||||
reason: Some(reason.to_owned()),
|
||||
style_removed: removed.iter().any(|name| name == "style"),
|
||||
removed,
|
||||
active,
|
||||
preview: None,
|
||||
cleaned: None,
|
||||
sha256: None,
|
||||
byte_size: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The answer for the bytes of one file.
|
||||
pub fn clean_svg(bytes: &[u8]) -> SvgCleaning {
|
||||
if bytes.len() > SVG_CAPS.max_bytes {
|
||||
return rejected("too_large", Vec::new(), Vec::new());
|
||||
}
|
||||
let Ok(text) = std::str::from_utf8(bytes) else {
|
||||
return rejected("malformed", Vec::new(), Vec::new());
|
||||
};
|
||||
let read = match read_svg_for_cleaning(text) {
|
||||
Ok(read) => read,
|
||||
Err(rejection) => return rejected(rejection.reason(), Vec::new(), Vec::new()),
|
||||
};
|
||||
if !read.active.is_empty() {
|
||||
return rejected(ACTIVE_CONTENT, read.removed, read.active);
|
||||
}
|
||||
let preview = serialize(&read.root);
|
||||
if preview.len() > MAX_PREVIEW_BYTES {
|
||||
return rejected("too_large", read.removed, read.active);
|
||||
}
|
||||
if read.removed.is_empty() {
|
||||
return accepted(CleaningOutcome::Clean, bytes, preview, Vec::new());
|
||||
}
|
||||
if preview.len() > SVG_CAPS.max_bytes {
|
||||
return rejected("too_large", read.removed, read.active);
|
||||
}
|
||||
if !reads_back_clean(&preview) {
|
||||
return rejected("cleaning_unstable", read.removed, read.active);
|
||||
}
|
||||
accepted(
|
||||
CleaningOutcome::Cleanable,
|
||||
preview.as_bytes(),
|
||||
preview.clone(),
|
||||
read.removed,
|
||||
)
|
||||
}
|
||||
|
||||
fn accepted(
|
||||
outcome: CleaningOutcome,
|
||||
upload: &[u8],
|
||||
preview: String,
|
||||
removed: Vec<String>,
|
||||
) -> SvgCleaning {
|
||||
let cleaned = (outcome == CleaningOutcome::Cleanable).then(|| preview.clone());
|
||||
SvgCleaning {
|
||||
outcome,
|
||||
reason: None,
|
||||
style_removed: removed.iter().any(|name| name == "style"),
|
||||
removed,
|
||||
active: Vec::new(),
|
||||
preview: Some(preview),
|
||||
cleaned,
|
||||
sha256: Some(sha256_hex(upload)),
|
||||
byte_size: Some(upload.len() as u64),
|
||||
}
|
||||
}
|
||||
|
||||
/// The cleaned text reads back with nothing to remove (the fixpoint the property test of
|
||||
/// `tests/svg_clean.rs` proves; checked here too, so no upload rests on it alone).
|
||||
fn reads_back_clean(text: &str) -> bool {
|
||||
read_svg(text).is_ok_and(|read| read.removed.is_empty())
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
//! SVG text → bounded, sanitised tree → text (plan R3 C2–C4). One reader for every SVG the
|
||||
//! product draws: the sheet drawings now, the branding and Step 13 uploads later.
|
||||
|
||||
pub mod clean;
|
||||
pub mod number;
|
||||
pub mod prescan;
|
||||
pub mod refs;
|
||||
@@ -18,3 +19,12 @@ pub fn read_svg(text: &str) -> Result<Sanitized, SvgRejection> {
|
||||
refs::check_references(&sanitized.root)?;
|
||||
Ok(sanitized)
|
||||
}
|
||||
|
||||
/// `read_svg` as the cleaning reads a file picked for upload: an oversized `title` or `desc`
|
||||
/// is a removal, not a refusal (`sanitize::sanitize_for_cleaning`).
|
||||
pub fn read_svg_for_cleaning(text: &str) -> Result<Sanitized, SvgRejection> {
|
||||
let root = tree::parse_svg(text)?;
|
||||
let sanitized = sanitize::sanitize_for_cleaning(root)?;
|
||||
refs::check_references(&sanitized.root)?;
|
||||
Ok(sanitized)
|
||||
}
|
||||
@@ -4,6 +4,11 @@
|
||||
//! no `class`/`style` on the root (inline SVG lives under the page's CSS), `id` values in the
|
||||
//! reference grammar, no text outside `title`/`desc`. A refused element goes with its
|
||||
//! subtree, a refused attribute alone; both are named in `removed` (names, never values).
|
||||
//! The removals that carried active or external content (script, embedded documents and
|
||||
//! images, animation, links, event handlers, a reference outside the document, `@import`,
|
||||
//! `javascript:` and the other forbidden style tokens, a backslash escape) are also named in
|
||||
//! `active` (contract §9.2, V1-2.2): a file with any is refused, never cleaned. The walk and
|
||||
//! what it keeps are unchanged by that classification.
|
||||
|
||||
use super::number::{js_number_of, js_split_list, js_to_string};
|
||||
use super::tree::{Element, Node, SvgRejection, XLINK_NS};
|
||||
@@ -162,41 +167,91 @@ const STYLE_FORBIDDEN: [&str; 5] = [
|
||||
"behavior:",
|
||||
];
|
||||
|
||||
/// Elements whose removal means the file carried active or embedded content, by local name
|
||||
/// (lowercase, compared without case: an unknown spelling is refused rather than cleaned).
|
||||
const ACTIVE_ELEMENTS: [&str; 19] = [
|
||||
"script",
|
||||
"foreignobject",
|
||||
"image",
|
||||
"feimage",
|
||||
"a",
|
||||
"animate",
|
||||
"animatemotion",
|
||||
"animatetransform",
|
||||
"animatecolor",
|
||||
"set",
|
||||
"discard",
|
||||
"mpath",
|
||||
"iframe",
|
||||
"embed",
|
||||
"object",
|
||||
"video",
|
||||
"audio",
|
||||
"canvas",
|
||||
"handler",
|
||||
];
|
||||
|
||||
/// A sanitised tree and what was taken out of it.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Sanitized {
|
||||
pub root: Element,
|
||||
/// `tag` and `tag@attribute` in document order (empty on the product's SVGs).
|
||||
pub removed: Vec<String>,
|
||||
/// The entries of `removed` that carried active or external content, same order.
|
||||
pub active: Vec<String>,
|
||||
}
|
||||
|
||||
/// Sanitises a parsed root (the caller parsed it with `tree::parse_svg`).
|
||||
pub fn sanitize(mut root: Element) -> Result<Sanitized, SvgRejection> {
|
||||
pub fn sanitize(root: Element) -> Result<Sanitized, SvgRejection> {
|
||||
sanitize_with(root, false)
|
||||
}
|
||||
|
||||
/// The cleaning's reading (contract §9.2): the same walk, except that a `title` or `desc`
|
||||
/// over the text cap is removed and named instead of refusing the file. Stored drawings are
|
||||
/// always read by `sanitize`, which refuses it.
|
||||
pub fn sanitize_for_cleaning(root: Element) -> Result<Sanitized, SvgRejection> {
|
||||
sanitize_with(root, true)
|
||||
}
|
||||
|
||||
fn sanitize_with(mut root: Element, drop_long_text: bool) -> Result<Sanitized, SvgRejection> {
|
||||
let mut walk = Walk {
|
||||
removed: Vec::new(),
|
||||
active: Vec::new(),
|
||||
uses: 0,
|
||||
drop_long_text,
|
||||
};
|
||||
// The inline root lives under the page: no page classes or styles, nothing that paints
|
||||
// outside its box (security audit R3, LOW).
|
||||
for name in ["class", "style", "transform", "filter", "mask", "clip-path"] {
|
||||
if root.attr(name).is_some() {
|
||||
if let Some(value) = root.attr(name) {
|
||||
let active = attribute_is_active(name, value);
|
||||
root.remove_attr(name);
|
||||
walk.removed.push(format!("svg@{name}"));
|
||||
walk.note(format!("svg@{name}"), active);
|
||||
}
|
||||
}
|
||||
walk.visit(&mut root)?;
|
||||
Ok(Sanitized {
|
||||
root,
|
||||
removed: walk.removed,
|
||||
active: walk.active,
|
||||
})
|
||||
}
|
||||
|
||||
struct Walk {
|
||||
removed: Vec<String>,
|
||||
active: Vec<String>,
|
||||
uses: usize,
|
||||
drop_long_text: bool,
|
||||
}
|
||||
|
||||
impl Walk {
|
||||
fn note(&mut self, name: String, active: bool) {
|
||||
if active {
|
||||
self.active.push(name.clone());
|
||||
}
|
||||
self.removed.push(name);
|
||||
}
|
||||
|
||||
fn visit(&mut self, element: &mut Element) -> Result<(), SvgRejection> {
|
||||
if element.qname == "use" {
|
||||
self.uses += 1;
|
||||
@@ -209,11 +264,17 @@ impl Walk {
|
||||
let children = std::mem::take(&mut element.children);
|
||||
for child in children {
|
||||
match child {
|
||||
Node::Element(child) if self.drop_long_text && has_long_text(&child) => {
|
||||
self.note(child.qname, false);
|
||||
}
|
||||
Node::Element(mut child) if element_allowed(&child) => {
|
||||
self.visit(&mut child)?;
|
||||
element.children.push(Node::Element(child));
|
||||
}
|
||||
Node::Element(child) => self.removed.push(child.qname),
|
||||
Node::Element(child) => {
|
||||
let active = subtree_is_active(&child);
|
||||
self.note(child.qname, active);
|
||||
}
|
||||
Node::Text(text) if is_xml_space(&text) => element.children.push(Node::Text(text)),
|
||||
Node::Text(text) if keeps_text => {
|
||||
if text.len() > MAX_TEXT_BYTES {
|
||||
@@ -234,7 +295,8 @@ impl Walk {
|
||||
&& namespace_fits(&a.qname, a.namespace.as_deref())
|
||||
&& value_acceptable(&a.qname, &a.value);
|
||||
if !keep {
|
||||
self.removed.push(format!("{tag}@{}", a.qname));
|
||||
let active = attribute_is_active(&a.qname, &a.value);
|
||||
self.note(format!("{tag}@{}", a.qname), active);
|
||||
}
|
||||
keep
|
||||
});
|
||||
@@ -242,6 +304,57 @@ impl Walk {
|
||||
}
|
||||
}
|
||||
|
||||
/// A `title` or `desc` whose text is over the cap the walk refuses.
|
||||
fn has_long_text(element: &Element) -> bool {
|
||||
matches!(element.qname.as_str(), "title" | "desc")
|
||||
&& element
|
||||
.children
|
||||
.iter()
|
||||
.any(|c| matches!(c, Node::Text(t) if !is_xml_space(t) && t.len() > MAX_TEXT_BYTES))
|
||||
}
|
||||
|
||||
/// A removed subtree carried active content: an active element anywhere in it, an active
|
||||
/// attribute, or a style sheet with an active value.
|
||||
fn subtree_is_active(element: &Element) -> bool {
|
||||
let name = element.local_name().to_ascii_lowercase();
|
||||
if ACTIVE_ELEMENTS.contains(&name.as_str()) {
|
||||
return true;
|
||||
}
|
||||
if element
|
||||
.attrs
|
||||
.iter()
|
||||
.any(|a| attribute_is_active(&a.qname, &a.value))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
element.children.iter().any(|child| match child {
|
||||
Node::Element(child) => subtree_is_active(child),
|
||||
Node::Text(text) => name == "style" && value_is_active(text),
|
||||
})
|
||||
}
|
||||
|
||||
/// An event handler, a link outside the document, or an active value.
|
||||
fn attribute_is_active(qname: &str, value: &str) -> bool {
|
||||
let local = qname
|
||||
.rsplit(':')
|
||||
.next()
|
||||
.unwrap_or(qname)
|
||||
.to_ascii_lowercase();
|
||||
if local.starts_with("on") {
|
||||
return true;
|
||||
}
|
||||
if local == "href" && !is_local_reference(local_reference_text(value)) {
|
||||
return true;
|
||||
}
|
||||
value_is_active(value)
|
||||
}
|
||||
|
||||
/// A reference outside the document, a forbidden style token, or a backslash (a CSS escape
|
||||
/// can spell any of them).
|
||||
fn value_is_active(value: &str) -> bool {
|
||||
value.contains('\\') || !urls_are_local(value) || style_forbidden(value)
|
||||
}
|
||||
|
||||
fn element_allowed(element: &Element) -> bool {
|
||||
ALLOWED_ELEMENTS.contains(&element.qname.as_str()) && element.is_svg_namespace()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Vector-tool exports (V1-2.2, contract §9.2)
|
||||
|
||||
Real files written by drawing tools, read by `crates/core/tests/svg_clean.rs` through the
|
||||
core's cleaning (`anatomy/svg/clean.rs`). Each file name starts with the tool and its version.
|
||||
None is edited by hand.
|
||||
|
||||
| File | Tool | How | Expected |
|
||||
|---|---|---|---|
|
||||
| `cairo-1.18.4-pycairo-1.27.0-shapes.svg` | cairo 1.18.4 through pycairo 1.27.0 | `generate.sh`: curves, gradient, dash, clip, alpha, text as glyph paths | cleanable: its colours are written `rgb(20%, 40%, 70%)`, a function the allowlist takes out of presentation attributes (`stop@stop-color`, `path@stroke`, `path@fill`, `g@fill`), so the cleaned drawing keeps its shapes and loses its colours |
|
||||
| `cairo-1.18.4-pycairo-1.27.0-raster.svg` | same | `generate.sh`: an 8 × 8 image surface painted | refused, `svg_active_content` (an embedded raster `<image>`) |
|
||||
| `imagemagick-7.1.2-18-draw.svg` | ImageMagick 7.1.2-18 Q16 | `generate.sh`: `-draw` circle, rectangle, polyline | read with no removal |
|
||||
| `imagemagick-7.1.2-18-mvg.svg` | same | `generate.sh`: an MVG file (its style settings land as bare text between elements, which the reader drops) | read with no removal |
|
||||
|
||||
`bash generate.sh` rewrites them from the tools installed here (no network).
|
||||
|
||||
## Editor exports, supplied by the owner (`owner/`)
|
||||
|
||||
The editors an administrator actually uses (Inkscape, Illustrator, Figma, Affinity) are not
|
||||
installed on the development machine; the owner exports a small drawing from each and puts
|
||||
the files in `owner/`, named `<tool>-<version>-<what>.svg` (for example
|
||||
`inkscape-1.4.2-plain-svg.svg`, `illustrator-29.3-export-as.svg`). The suite reads every file
|
||||
there: each must be refused (with a closed reason) or cleaned into a drawing that reads back
|
||||
with no removal, and a file whose name contains `-active-` must be refused for active content.
|
||||
Until files arrive, that test runs on an empty folder and the report of V1-2.2 says so.
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="150" viewBox="0 0 200 150">
|
||||
<defs>
|
||||
<image id="source-15" x="0" y="0" width="8" height="8" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAABmJLR0QA/wD/AP+gvaeTAAAAFElEQVQImWM8IynJgA0wYRUdtBIAzC8BDibnFZsAAAAASUVORK5CYII="/>
|
||||
</defs>
|
||||
<use xlink:href="#source-15" transform="matrix(1, 0, 0, 1, 10, 10)"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 483 B |
+41
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="150" viewBox="0 0 200 150">
|
||||
<defs>
|
||||
<g>
|
||||
<g id="glyph-0-0">
|
||||
<path d="M 7.140625 -1.25 L 7.140625 -3.59375 L 5.203125 -3.59375 L 5.203125 -4.578125 L 8.3125 -4.578125 L 8.3125 -0.8125 C 7.851562 -0.488281 7.347656 -0.242188 6.796875 -0.078125 C 6.253906 0.0859375 5.671875 0.171875 5.046875 0.171875 C 3.671875 0.171875 2.597656 -0.226562 1.828125 -1.03125 C 1.054688 -1.832031 0.671875 -2.941406 0.671875 -4.359375 C 0.671875 -5.796875 1.054688 -6.910156 1.828125 -7.703125 C 2.597656 -8.503906 3.671875 -8.90625 5.046875 -8.90625 C 5.609375 -8.90625 6.144531 -8.832031 6.65625 -8.6875 C 7.175781 -8.550781 7.65625 -8.347656 8.09375 -8.078125 L 8.09375 -6.8125 C 7.65625 -7.1875 7.1875 -7.46875 6.6875 -7.65625 C 6.195312 -7.84375 5.679688 -7.9375 5.140625 -7.9375 C 4.066406 -7.9375 3.257812 -7.632812 2.71875 -7.03125 C 2.1875 -6.4375 1.921875 -5.546875 1.921875 -4.359375 C 1.921875 -3.179688 2.1875 -2.289062 2.71875 -1.6875 C 3.257812 -1.09375 4.066406 -0.796875 5.140625 -0.796875 C 5.554688 -0.796875 5.925781 -0.832031 6.25 -0.90625 C 6.582031 -0.976562 6.878906 -1.09375 7.140625 -1.25 Z M 7.140625 -1.25 "/>
|
||||
</g>
|
||||
<g id="glyph-0-1">
|
||||
<path d="M 4.109375 -3.296875 C 3.242188 -3.296875 2.640625 -3.195312 2.296875 -3 C 1.960938 -2.800781 1.796875 -2.460938 1.796875 -1.984375 C 1.796875 -1.597656 1.921875 -1.289062 2.171875 -1.0625 C 2.429688 -0.84375 2.773438 -0.734375 3.203125 -0.734375 C 3.804688 -0.734375 4.285156 -0.941406 4.640625 -1.359375 C 5.003906 -1.785156 5.1875 -2.351562 5.1875 -3.0625 L 5.1875 -3.296875 Z M 6.265625 -3.75 L 6.265625 0 L 5.1875 0 L 5.1875 -1 C 4.9375 -0.601562 4.628906 -0.304688 4.265625 -0.109375 C 3.898438 0.078125 3.453125 0.171875 2.921875 0.171875 C 2.242188 0.171875 1.707031 -0.015625 1.3125 -0.390625 C 0.914062 -0.773438 0.71875 -1.28125 0.71875 -1.90625 C 0.71875 -2.644531 0.960938 -3.203125 1.453125 -3.578125 C 1.953125 -3.953125 2.691406 -4.140625 3.671875 -4.140625 L 5.1875 -4.140625 L 5.1875 -4.25 C 5.1875 -4.75 5.019531 -5.132812 4.6875 -5.40625 C 4.363281 -5.675781 3.910156 -5.8125 3.328125 -5.8125 C 2.953125 -5.8125 2.582031 -5.765625 2.21875 -5.671875 C 1.863281 -5.578125 1.523438 -5.441406 1.203125 -5.265625 L 1.203125 -6.265625 C 1.597656 -6.421875 1.976562 -6.535156 2.34375 -6.609375 C 2.71875 -6.679688 3.082031 -6.71875 3.4375 -6.71875 C 4.382812 -6.71875 5.09375 -6.472656 5.5625 -5.984375 C 6.03125 -5.492188 6.265625 -4.75 6.265625 -3.75 Z M 6.265625 -3.75 "/>
|
||||
</g>
|
||||
<g id="glyph-0-2">
|
||||
<path d="M 4.9375 -5.5625 C 4.8125 -5.625 4.675781 -5.671875 4.53125 -5.703125 C 4.394531 -5.742188 4.238281 -5.765625 4.0625 -5.765625 C 3.457031 -5.765625 2.988281 -5.566406 2.65625 -5.171875 C 2.332031 -4.773438 2.171875 -4.203125 2.171875 -3.453125 L 2.171875 0 L 1.09375 0 L 1.09375 -6.5625 L 2.171875 -6.5625 L 2.171875 -5.546875 C 2.398438 -5.941406 2.695312 -6.234375 3.0625 -6.421875 C 3.425781 -6.617188 3.867188 -6.71875 4.390625 -6.71875 C 4.460938 -6.71875 4.539062 -6.710938 4.625 -6.703125 C 4.71875 -6.691406 4.816406 -6.675781 4.921875 -6.65625 Z M 4.9375 -5.5625 "/>
|
||||
</g>
|
||||
<g id="glyph-0-3">
|
||||
<path d="M 3.671875 -5.8125 C 3.097656 -5.8125 2.640625 -5.582031 2.296875 -5.125 C 1.960938 -4.675781 1.796875 -4.0625 1.796875 -3.28125 C 1.796875 -2.488281 1.960938 -1.867188 2.296875 -1.421875 C 2.628906 -0.972656 3.085938 -0.75 3.671875 -0.75 C 4.242188 -0.75 4.695312 -0.972656 5.03125 -1.421875 C 5.375 -1.878906 5.546875 -2.5 5.546875 -3.28125 C 5.546875 -4.050781 5.375 -4.664062 5.03125 -5.125 C 4.695312 -5.582031 4.242188 -5.8125 3.671875 -5.8125 Z M 3.671875 -6.71875 C 4.609375 -6.71875 5.34375 -6.410156 5.875 -5.796875 C 6.414062 -5.191406 6.6875 -4.351562 6.6875 -3.28125 C 6.6875 -2.207031 6.414062 -1.363281 5.875 -0.75 C 5.34375 -0.132812 4.609375 0.171875 3.671875 0.171875 C 2.734375 0.171875 1.992188 -0.132812 1.453125 -0.75 C 0.921875 -1.363281 0.65625 -2.207031 0.65625 -3.28125 C 0.65625 -4.351562 0.921875 -5.191406 1.453125 -5.796875 C 1.992188 -6.410156 2.734375 -6.71875 3.671875 -6.71875 Z M 3.671875 -6.71875 "/>
|
||||
</g>
|
||||
<g id="glyph-0-4">
|
||||
<path d="M 2.203125 -8.421875 L 2.203125 -6.5625 L 4.421875 -6.5625 L 4.421875 -5.71875 L 2.203125 -5.71875 L 2.203125 -2.15625 C 2.203125 -1.625 2.273438 -1.28125 2.421875 -1.125 C 2.566406 -0.976562 2.863281 -0.90625 3.3125 -0.90625 L 4.421875 -0.90625 L 4.421875 0 L 3.3125 0 C 2.476562 0 1.898438 -0.15625 1.578125 -0.46875 C 1.265625 -0.78125 1.109375 -1.34375 1.109375 -2.15625 L 1.109375 -5.71875 L 0.328125 -5.71875 L 0.328125 -6.5625 L 1.109375 -6.5625 L 1.109375 -8.421875 Z M 2.203125 -8.421875 "/>
|
||||
</g>
|
||||
</g>
|
||||
<linearGradient id="linear-pattern-0" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="200" y2="0">
|
||||
<stop offset="0" stop-color="rgb(60%, 30%, 20%)" stop-opacity="1"/>
|
||||
<stop offset="1" stop-color="rgb(90%, 80%, 60%)" stop-opacity="0.7"/>
|
||||
</linearGradient>
|
||||
<clipPath id="clip-0">
|
||||
<path clip-rule="nonzero" d="M 65 60 L 135 60 L 135 100 L 65 100 Z M 65 60 "/>
|
||||
</clipPath>
|
||||
</defs>
|
||||
<path fill-rule="nonzero" fill="url(#linear-pattern-0)" stroke-width="2" stroke-linecap="butt" stroke-linejoin="miter" stroke="rgb(20%, 20%, 20%)" stroke-opacity="1" stroke-dasharray="6 3" stroke-miterlimit="10" d="M 20 120 C 40 20 160 20 180 120 Z M 20 120 "/>
|
||||
<g clip-path="url(#clip-0)">
|
||||
<path fill-rule="nonzero" fill="rgb(20%, 40%, 70%)" fill-opacity="0.5" d="M 135 80 C 135 89.28125 131.3125 98.183594 124.75 104.75 C 118.183594 111.3125 109.28125 115 100 115 C 90.71875 115 81.816406 111.3125 75.25 104.75 C 68.6875 98.183594 65 89.28125 65 80 C 65 70.71875 68.6875 61.816406 75.25 55.25 C 81.816406 48.6875 90.71875 45 100 45 C 109.28125 45 118.183594 48.6875 124.75 55.25 C 131.3125 61.816406 135 70.71875 135 80 "/>
|
||||
</g>
|
||||
<g fill="rgb(0%, 0%, 0%)" fill-opacity="1">
|
||||
<use xlink:href="#glyph-0-0" x="70" y="140"/>
|
||||
<use xlink:href="#glyph-0-1" x="79.298828" y="140"/>
|
||||
<use xlink:href="#glyph-0-2" x="86.652344" y="140"/>
|
||||
<use xlink:href="#glyph-0-2" x="91.585938" y="140"/>
|
||||
<use xlink:href="#glyph-0-3" x="96.519531" y="140"/>
|
||||
<use xlink:href="#glyph-0-4" x="103.861328" y="140"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 6.0 KiB |
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env bash
|
||||
# Regenerates the vector-tool exports of `tests/svg_clean.rs` (V1-2.2, contract §9.2) with
|
||||
# the tools installed on the development machine; each file name carries the tool and its
|
||||
# version. Run from this folder: bash generate.sh. The editors' own exports (Inkscape,
|
||||
# Illustrator, Figma, Affinity) come from the owner, under owner/ (see README.md).
|
||||
set -euo pipefail
|
||||
|
||||
cairo_version=$(python3 -I -c 'import cairo; print(cairo.cairo_version_string())')
|
||||
pycairo_version=$(python3 -I -c 'import cairo; print(cairo.version)')
|
||||
prefix="cairo-${cairo_version}-pycairo-${pycairo_version}"
|
||||
|
||||
python3 -I - "$prefix" <<'EOF'
|
||||
import sys
|
||||
import cairo
|
||||
|
||||
prefix = sys.argv[1]
|
||||
|
||||
def shapes(context):
|
||||
gradient = cairo.LinearGradient(0, 0, 200, 0)
|
||||
gradient.add_color_stop_rgba(0, 0.6, 0.3, 0.2, 1)
|
||||
gradient.add_color_stop_rgba(1, 0.9, 0.8, 0.6, 0.7)
|
||||
context.set_source(gradient)
|
||||
context.move_to(20, 120)
|
||||
context.curve_to(40, 20, 160, 20, 180, 120)
|
||||
context.close_path()
|
||||
context.fill_preserve()
|
||||
context.set_source_rgb(0.2, 0.2, 0.2)
|
||||
context.set_line_width(2)
|
||||
context.set_dash([6, 3])
|
||||
context.stroke()
|
||||
context.set_dash([])
|
||||
context.save()
|
||||
context.rectangle(60, 60, 80, 40)
|
||||
context.clip()
|
||||
context.set_source_rgba(0.2, 0.4, 0.7, 0.5)
|
||||
context.arc(100, 80, 35, 0, 6.2832)
|
||||
context.fill()
|
||||
context.restore()
|
||||
context.set_source_rgb(0, 0, 0)
|
||||
context.select_font_face('DejaVu Sans')
|
||||
context.set_font_size(12)
|
||||
context.move_to(70, 140)
|
||||
context.show_text('Garrot')
|
||||
|
||||
surface = cairo.SVGSurface(f'{prefix}-shapes.svg', 200, 150)
|
||||
shapes(cairo.Context(surface))
|
||||
surface.finish()
|
||||
|
||||
surface = cairo.SVGSurface(f'{prefix}-raster.svg', 200, 150)
|
||||
context = cairo.Context(surface)
|
||||
pixels = cairo.ImageSurface(cairo.FORMAT_RGB24, 8, 8)
|
||||
inner = cairo.Context(pixels)
|
||||
inner.set_source_rgb(0.8, 0.1, 0.1)
|
||||
inner.paint()
|
||||
context.set_source_surface(pixels, 10, 10)
|
||||
context.paint()
|
||||
surface.finish()
|
||||
EOF
|
||||
|
||||
magick_version=$(magick -version | sed -n 's/^Version: ImageMagick \([^ ]*\).*/\1/p')
|
||||
magick -size 200x150 xc:white -fill '#336699' -stroke '#222222' -strokewidth 2 \
|
||||
-draw 'circle 100,75 100,30 rectangle 10,10 60,40 polyline 10,140 60,100 110,140' \
|
||||
"imagemagick-${magick_version}-draw.svg"
|
||||
# The MVG path writes its style settings as bare text between the elements.
|
||||
printf 'viewbox 0 0 200 150\nfill "#336699"\ncircle 100,75 100,30\nrectangle 10,10 60,40\nstroke "#222222"\nfill none\npolyline 10,140 60,100 110,140\n' >drawing.mvg
|
||||
magick mvg:drawing.mvg "imagemagick-${magick_version}-mvg.svg"
|
||||
rm -f drawing.mvg
|
||||
ls -1 ./*.svg
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<svg width="200" height="150" xmlns="http://www.w3.org/2000/svg">
|
||||
<circle cx="100" cy="75" r="45"/>
|
||||
<rect x="10" y="10" width="50" height="30"/>
|
||||
<polyline points="10,140 60,100 110,140 "/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 257 B |
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<svg width="200" height="150" xmlns="http://www.w3.org/2000/svg">
|
||||
fill:#336699; <circle cx="100" cy="75" r="45"/>
|
||||
<rect x="10" y="10" width="50" height="30"/>
|
||||
stroke:#222222;fill:none; <polyline points="10,140 60,100 110,140 "/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 295 B |
@@ -0,0 +1 @@
|
||||
Owner-supplied editor exports land here (see ../README.md).
|
||||
@@ -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