fix(anatomy): V1-2.2 review findings, PIs and HTML refused, bounds

Security audit (PASS, 2 MEDIUM, 2 LOW) and verifier (ECARTS 6) on
741c25c: processing instructions and HTML fetchers refused, active
content under a long title classified, linear review groups, full
fixpoint check, bounded preview drawings, missing cap fixtures and
property checks, record wordings.
This commit is contained in:
bchanot committed 2026-10-29 13:38:33 +01:00
1 parent 741c25c464
commit 8294d5471c
15 files changed
+448 -72

No files matched your search

+2 -1
View File
@@ -27,7 +27,8 @@ paths: ["crates/core/src/domain/sheet/**", "docs/development/ANATOMICAL_SHEET_MO
- Omitted `review_status` means `inherited` (`#[serde(default)]`): any check on statuses treats an absent value as
`inherited`.
- Since V1-2.2 the core also holds the review model (`review.rs`: groups, digests, `coverable`, change list), the server
verdict (`verdict.rs`, the shape the M5 SQL reads; never exported to the browser), the document measure (`size.rs`:
verdict (`verdict.rs`, the shape the M5 SQL reads; the function is never exported to the browser, only its TypeScript type for the
review screen), the document measure (`size.rs`:
PostgreSQL's `jsonb` text, never `JSON.stringify`), `PreviewSheet` and the editor's commands (`edit.rs`, the §6.6
marking rules). A change to the SVG reader, the rules, the caps, the fingerprint or the digests is a core change
(contract §6.4: the release withdraws open rounds). RUST.md §12.
+15 -7
View File
@@ -2,8 +2,9 @@
//! 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;
//! (`malformed`), or any removal of active or external content, or a processing
//! instruction (`svg_active_content`, the names in `active`, a PI as `?<target>`); 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
@@ -20,7 +21,7 @@ use ts_rs::TS;
use super::prescan::SVG_CAPS;
use super::serialize::serialize;
use super::{read_svg, read_svg_for_cleaning};
use super::{processing_instructions, read_svg, read_svg_for_cleaning};
use crate::domain::sheet::verdict::sha256_hex;
/// The reason of a refusal for active or external content.
@@ -85,8 +86,14 @@ pub fn clean_svg(bytes: &[u8]) -> SvgCleaning {
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 mut active = read.active.clone();
active.extend(
processing_instructions(text)
.into_iter()
.map(|t| format!("?{t}")),
);
if !active.is_empty() {
return rejected(ACTIVE_CONTENT, read.removed, active);
}
let preview = serialize(&read.root);
if preview.len() > MAX_PREVIEW_BYTES {
@@ -129,8 +136,9 @@ fn accepted(
}
}
/// The cleaned text reads back with nothing to remove (the fixpoint the property test of
/// The cleaned text reads back with nothing to remove and serialises to itself, so what the
/// admin confirms is what every reader draws (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())
read_svg(text).is_ok_and(|read| read.removed.is_empty() && serialize(&read.root) == text)
}
+19
View File
@@ -28,3 +28,22 @@ pub fn read_svg_for_cleaning(text: &str) -> Result<Sanitized, SvgRejection> {
refs::check_references(&sanitized.root)?;
Ok(sanitized)
}
/// The targets of the processing instructions of a text the reader accepted (the XML
/// declaration is none). The tree drops them, so a drawing "read with no removal" may still
/// carry `<?xml-stylesheet href=…?>` in its bytes: the cleaning and the verdict refuse it.
pub fn processing_instructions(text: &str) -> Vec<String> {
let options = roxmltree::ParsingOptions {
allow_dtd: false,
nodes_limit: 200_000,
..roxmltree::ParsingOptions::default()
};
let Ok(document) = roxmltree::Document::parse_with_options(text, options) else {
return Vec::new();
};
document
.descendants()
.filter_map(|node| node.pi())
.map(|pi| pi.target.to_owned())
.collect()
}
+39 -4
View File
@@ -169,7 +169,7 @@ const STYLE_FORBIDDEN: [&str; 5] = [
/// 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] = [
const ACTIVE_ELEMENTS: [&str; 33] = [
"script",
"foreignobject",
"image",
@@ -189,6 +189,20 @@ const ACTIVE_ELEMENTS: [&str; 19] = [
"audio",
"canvas",
"handler",
"img",
"input",
"source",
"frame",
"frameset",
"form",
"meta",
"link",
"base",
"picture",
"track",
"applet",
"param",
"portal",
];
/// A sanitised tree and what was taken out of it.
@@ -265,7 +279,8 @@ impl Walk {
for child in children {
match child {
Node::Element(child) if self.drop_long_text && has_long_text(&child) => {
self.note(child.qname, false);
let active = subtree_is_active(&child);
self.note(child.qname, active);
}
Node::Element(mut child) if element_allowed(&child) => {
self.visit(&mut child)?;
@@ -304,6 +319,26 @@ impl Walk {
}
}
/// HTML attributes that load or send something: an element carrying one was written to fetch.
const FETCHING_ATTRIBUTES: [&str; 13] = [
"src",
"srcset",
"action",
"formaction",
"data",
"poster",
"background",
"lowsrc",
"dynsrc",
"http-equiv",
"codebase",
"ping",
"manifest",
];
/// Elements of the XHTML namespace are HTML inside the drawing: active whatever their name.
const XHTML_NS: &str = "http://www.w3.org/1999/xhtml";
/// 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")
@@ -317,7 +352,7 @@ fn has_long_text(element: &Element) -> bool {
/// 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()) {
if ACTIVE_ELEMENTS.contains(&name.as_str()) || element.namespace.as_deref() == Some(XHTML_NS) {
return true;
}
if element
@@ -340,7 +375,7 @@ fn attribute_is_active(qname: &str, value: &str) -> bool {
.next()
.unwrap_or(qname)
.to_ascii_lowercase();
if local.starts_with("on") {
if local.starts_with("on") || FETCHING_ATTRIBUTES.contains(&local.as_str()) {
return true;
}
if local == "href" && !is_local_reference(local_reference_text(value)) {
+11
View File
@@ -25,6 +25,7 @@ use super::report::{SheetReport, referenced_asset_ids, sheet_report};
use super::review::DigestError;
use super::schema::{AnatomicalSheetDocument, LocalizedText};
use super::verdict::{read_document, sha256_hex};
use crate::domain::archive::SnapshotCaps;
use crate::domain::js;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -107,6 +108,10 @@ impl PreviewFailure {
}
}
/// Drawings a preview holds at most: the four a version may reference, and room for the
/// uploads of one editing session (each one at most 2 MiB, refused above).
pub const MAX_HELD_DRAWINGS: usize = 16;
struct Asset {
sha256: String,
bytes: u64,
@@ -168,6 +173,12 @@ impl PreviewSheet {
/// Hashes the bytes against the row, reads a new sha256 once, keeps the asset.
fn take_drawing(&mut self, drawing: PreviewDrawing) -> Result<(), PreviewFailure> {
if drawing.bytes.len() > SnapshotCaps::ASSET_BYTES {
return Err(PreviewFailure::Caps(vec![CapName::DrawingBytes]));
}
if self.assets.len() >= MAX_HELD_DRAWINGS && !self.assets.contains_key(&drawing.asset_id) {
return Err(PreviewFailure::Caps(vec![CapName::Drawings]));
}
if !js::is_sha256_hex(&drawing.sha256) || sha256_hex(&drawing.bytes) != drawing.sha256 {
return Err(PreviewFailure::DrawingHashMismatch(drawing.asset_id));
}
+52 -35
View File
@@ -25,7 +25,7 @@ use sha2::{Digest, Sha256};
use ts_rs::TS;
use super::fingerprint::canonical_json;
use super::keys::{ElementKey, GENERAL_GROUP, Marked, marked_elements, view_group, zone_group};
use super::keys::{ElementKey, GENERAL_GROUP, marked_elements, view_group, zone_group};
use super::schema::{AnatomicalSheetDocument, FindingNode, FindingZone, ReviewStatus};
/// Elements per review marking.
@@ -98,30 +98,61 @@ pub fn review_groups(
base: Option<&AnatomicalSheetDocument>,
) -> Result<Vec<ReviewGroup>, DigestError> {
let value = serde_json::to_value(document).map_err(|_| DigestError)?;
let marked = marked_elements(document);
let mut groups = vec![group(GENERAL_GROUP, general_content(&value), &marked)?];
let statuses = statuses_by_group(document);
let views = by_field(&value, "views", "id");
let drawn = by_field(&value, "annotations", "view_id");
let zones = by_field(&value, "finding_zones", "id");
let mut groups = vec![group(GENERAL_GROUP, general_content(&value), &statuses)?];
for id in distinct(document.views.iter().map(|v| v.id())) {
let content = view_content(&value, id);
groups.push(group(&view_group(id), content, &marked)?);
let content = json!({ "views": members(&views, id), "annotations": members(&drawn, id) });
groups.push(group(&view_group(id), content, &statuses)?);
}
for id in distinct(document.finding_zones.iter().map(|z| z.id.as_str())) {
let content = members_with_id(&value, "finding_zones", id);
groups.push(group(
&zone_group(id),
json!({ "finding_zones": content }),
&marked,
)?);
let content = json!({ "finding_zones": members(&zones, id) });
groups.push(group(&zone_group(id), content, &statuses)?);
}
if let Some(base) = base {
for key in group_keys(base) {
if !groups.iter().any(|g| g.key == key) {
groups.push(removal_group(key)?);
}
let present: HashSet<String> = groups.iter().map(|g| g.key.clone()).collect();
for key in group_keys(base)
.into_iter()
.filter(|k| !present.contains(k))
{
groups.push(removal_group(key)?);
}
}
Ok(groups)
}
/// The markings of each group, counted in one pass.
fn statuses_by_group(document: &AnatomicalSheetDocument) -> HashMap<String, StatusCounts> {
let mut out: HashMap<String, StatusCounts> = HashMap::new();
for element in marked_elements(document) {
out.entry(element.group).or_default().add(element.status);
}
out
}
/// The items of a collection grouped by the string value of `field`, document order kept.
fn by_field<'a>(
document: &'a Value,
collection: &str,
field: &str,
) -> HashMap<&'a str, Vec<&'a Value>> {
let mut out: HashMap<&str, Vec<&Value>> = HashMap::new();
for item in array(document, collection) {
if let Some(key) = item.get(field).and_then(Value::as_str) {
out.entry(key).or_default().push(item);
}
}
out
}
fn members(index: &HashMap<&str, Vec<&Value>>, id: &str) -> Vec<Value> {
index.get(id).map_or_else(Vec::new, |items| {
items.iter().map(|v| (*v).clone()).collect()
})
}
/// The group keys of a document, `general` first (no removal groups).
pub fn group_keys(document: &AnatomicalSheetDocument) -> Vec<String> {
let views = distinct(document.views.iter().map(|v| v.id())).map(view_group);
@@ -137,11 +168,12 @@ fn distinct<'a>(ids: impl Iterator<Item = &'a str>) -> impl Iterator<Item = &'a
ids.filter(move |id| seen.insert(*id))
}
fn group(key: &str, content: Value, marked: &[Marked]) -> Result<ReviewGroup, DigestError> {
let mut statuses = StatusCounts::default();
for element in marked.iter().filter(|m| m.group == key) {
statuses.add(element.status);
}
fn group(
key: &str,
content: Value,
by_group: &HashMap<String, StatusCounts>,
) -> Result<ReviewGroup, DigestError> {
let statuses = by_group.get(key).copied().unwrap_or_default();
Ok(ReviewGroup {
key: key.to_owned(),
digest: digest(key, content)?,
@@ -183,21 +215,6 @@ fn general_content(document: &Value) -> Value {
Value::Object(map)
}
fn view_content(document: &Value, view_id: &str) -> Value {
let annotations: Vec<Value> = array(document, "annotations")
.filter(|a| a.get("view_id").and_then(Value::as_str) == Some(view_id))
.cloned()
.collect();
json!({ "views": members_with_id(document, "views", view_id), "annotations": annotations })
}
fn members_with_id(document: &Value, collection: &str, id: &str) -> Vec<Value> {
array(document, collection)
.filter(|item| item.get("id").and_then(Value::as_str) == Some(id))
.cloned()
.collect()
}
fn array<'a>(document: &'a Value, collection: &str) -> impl Iterator<Item = &'a Value> {
document
.get(collection)
+6 -2
View File
@@ -7,7 +7,7 @@
//!
//! Field names are the ones the SQL reads (`groups[{key, digest, coverable}]`,
//! `drawings[{asset_id, sha256}]`, `fingerprint`: `20261028110000_sheet_review_workflow.sql`).
//! The submission rules are the first five lines of §6.7; the marking, disclaimer, coverage
//! The submission rules are the first six lines of §6.7; the marking, disclaimer, coverage
//! and separation rules belong to approval and publication, in SQL. A strict-parse refusal is
//! an integrity fault of the stored text (§6.5, owner decision 2026-10-29).
@@ -25,7 +25,7 @@ use super::review::{Completeness, ElementChange, ReviewGroup, change_list};
use super::schema::AnatomicalSheetDocument;
use super::size::document_text_bytes;
use super::validate::{SheetIssue, SheetIssueSeverity};
use crate::anatomy::svg::read_svg;
use crate::anatomy::svg::{processing_instructions, read_svg};
use crate::domain::js::parse_json;
/// The format of the verdict object, bumped with any change of its meaning.
@@ -336,6 +336,10 @@ fn check_bytes(row: &VerdictDrawing<'_>) -> (DrawingResult, Option<String>) {
Ok(read) if !read.removed.is_empty() => {
(DrawingResult::SvgRemoval, Some(read.removed.join(", ")))
}
Ok(_) if !processing_instructions(text).is_empty() => (
DrawingResult::SvgRejected,
Some("processing_instruction".to_owned()),
),
Ok(_) => (DrawingResult::Ok, None),
}
}
+57 -2
View File
@@ -590,7 +590,42 @@ fn random_command(doc: &AnatomicalSheetDocument, rng: &mut Rng, counter: &mut us
.iter()
.filter(|k| matches!(k, ElementKey::Finding { path, .. } if path.len() < 8))
.collect();
match rng.below(9) {
match rng.below(12) {
9 => {
let view = rng.pick(&doc.views).cloned();
match view {
Some(view) => {
let mut value = serde_json::to_value(&view).unwrap();
if value["view_box"].is_object() {
value["view_box"]["width"] = json!(1 + rng.below(5));
} else {
value["svg_asset_id"] =
json!(format!("11111111-2222-4333-8444-{:012}", rng.below(3)));
}
json!({ "op": "update", "key": { "kind": "view", "id": view.id() }, "element": { "kind": "view", "value": value } })
}
None => json!({ "op": "remove", "key": { "kind": "sheet" } }),
}
}
10 => match rng.below(3) {
0 => {
json!({ "op": "insert", "element": { "kind": "reference", "value": { "id": id, "type": "web", "citation": id } } })
}
1 => {
json!({ "op": "remove", "key": { "kind": "reference", "id": rng.pick(&doc.references).map(|r| r.id.clone()).unwrap_or_default() } })
}
_ => {
json!({ "op": "move", "key": { "kind": "zone", "id": rng.pick(&doc.finding_zones).map(|z| z.id.clone()).unwrap_or_default() }, "position": rng.below(3) })
}
},
11 => {
let zone = rng
.pick(&doc.finding_zones)
.map(|z| z.id.clone())
.unwrap_or_default();
json!({ "op": "update", "key": { "kind": "zone", "id": zone },
"element": { "kind": "zone", "value": { "id": zone, "name": name("Zone renommée"), "categories": [] } } })
}
0 => {
json!({ "op": "insert", "position": rng.below(2), "element": { "kind": "annotation", "value": {
"id": id, "kind": "point", "view_id": view, "category_id": "c", "laterality": "left",
@@ -738,7 +773,7 @@ fn check_command(
"{raw}"
);
}
"update" => {
"update" if before.contains_key(&key()) => {
let key = key();
let expected = if changes == Some(&json!(true)) {
ReviewStatus::ToConfirm
@@ -775,8 +810,28 @@ fn check_command(
"{raw}"
);
}
"mark_reviewed" => {
let ElementKey::Zone { id } = key() else {
unreachable!()
};
for (k, status) in after {
let inside = matches!(k, ElementKey::Finding { zone_id, .. } if *zone_id == id);
let expected = if inside {
ReviewStatus::Reviewed
} else {
before[k]
};
assert_eq!(*status, expected, "{raw}");
}
}
_ => {}
}
if op != "remove" {
assert!(
before.keys().all(|k| after.contains_key(k)),
"keys kept: {raw}"
);
}
}
fn is_below(key: &ElementKey, ancestor: &ElementKey) -> bool {
+43
View File
@@ -348,3 +348,46 @@ fn no_report_operation_runs_on_a_preview() {
let state = canvas(call_with_preview(&preview, "preview.state", "{}"));
assert_eq!(state["value"]["status"], json!("published"));
}
#[test]
fn a_preview_holds_a_bounded_number_of_bounded_drawings() {
use faunosteo_core::domain::sheet::preview::MAX_HELD_DRAWINGS;
let huge = vec![b' '; 2 * 1024 * 1024 + 1];
let sha = faunosteo_core::domain::sheet::verdict::sha256_hex(&huge);
let oversized = PreviewDrawing {
asset_id: "a".into(),
sha256: sha,
bytes: huge,
};
let failure = load_err(
input(PreviewStatus::Draft, document_text(), None),
vec![oversized],
);
assert_eq!(
(failure.kind(), failure.detail()),
("caps", "drawing_bytes".to_owned())
);
let mut draft = PreviewSheet::load(
input(PreviewStatus::Draft, document_text(), None),
vec![horse_drawing()],
)
.unwrap();
for i in 1..MAX_HELD_DRAWINGS {
let added = PreviewDrawing {
asset_id: format!("extra-{i}"),
..horse_drawing()
};
draft.add_drawing(added).unwrap();
}
let one_more = PreviewDrawing {
asset_id: "one-more".into(),
..horse_drawing()
};
assert_eq!(
draft.add_drawing(one_more),
Err(PreviewFailure::Caps(vec![
faunosteo_core::domain::sheet::caps::CapName::Drawings
]))
);
assert_eq!(draft.drawing_reads(), 1, "the same bytes are read once");
}
+43 -1
View File
@@ -110,7 +110,7 @@ fn a_clean_sheet_passes_bound_to_its_round() {
let keys: Vec<&str> = verdict.groups.iter().map(|g| g.key.as_str()).collect();
assert_eq!(keys, ["general", "view:main", "zone:z1"]);
assert!(
verdict.groups.iter().skip(1).all(|g| !g.coverable),
verdict.groups[1..].iter().all(|g| !g.coverable),
"to_confirm left"
);
assert!(verdict.changes.is_none());
@@ -540,3 +540,45 @@ fn a_document_at_every_cap_is_verified_in_time() {
"the core's share of the 120 s budget"
);
}
#[test]
fn every_cap_holds_at_cap_minus_one_and_cap_and_fails_one_over() {
use faunosteo_core::domain::sheet::AnatomicalSheetDocument;
use faunosteo_core::domain::sheet::caps::caps_report;
const MIB: u64 = 1024 * 1024;
let typed =
|doc: &Value| -> AnatomicalSheetDocument { serde_json::from_value(doc.clone()).unwrap() };
let names = |doc: &AnatomicalSheetDocument, sizes: &[u64]| -> Vec<&'static str> {
caps_report(doc, 0, sizes)
.exceeded
.iter()
.map(|c| c.as_str())
.collect()
};
let base = typed(&document());
for (sizes, expected) in [
(vec![1; 3], vec![]),
(vec![1; 4], vec![]),
(vec![1; 5], vec!["drawings"]),
(vec![2 * MIB - 1], vec![]),
(vec![2 * MIB], vec![]),
(vec![2 * MIB + 1], vec!["drawing_bytes"]),
(vec![2 * MIB, 2 * MIB, 2 * MIB - 1], vec![]),
(vec![2 * MIB, 2 * MIB, 2 * MIB], vec![]),
(
vec![2 * MIB, 2 * MIB, 2 * MIB, 1],
vec!["drawings_total_bytes"],
),
] {
assert_eq!(names(&base, &sizes), expected, "{sizes:?}");
}
for (count, expected) in [(63, vec![]), (64, vec![]), (65, vec!["views"])] {
let mut doc = document();
let extra = (1..count).map(|i| {
json!({ "id": format!("d{i}"), "kind": "detail", "name": name("D"), "svg_asset_id": null,
"view_box": { "min_x": 0, "min_y": 0, "width": 1, "height": 1 }, "parent_view_id": "main" })
});
doc["views"].as_array_mut().unwrap().extend(extra);
assert_eq!(names(&typed(&doc), &[1]), expected, "{count} views");
}
}
+88 -1
View File
@@ -42,6 +42,8 @@ fn the_hostile_corpus_answers_as_the_contract_table() {
("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")),
@@ -71,7 +73,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).
("comments-pi-cdata.svg", Clean, None),
("dangling-references.svg", Clean, None),
("duplicate-ids.svg", Clean, None),
("osteo-halo-id.svg", Clean, None),
@@ -134,6 +135,92 @@ fn active_names_are_listed_and_ordinary_removals_are_not() {
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>"),
];
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>"#;
+17 -2
View File
@@ -3,8 +3,9 @@
//! and `packages/core-wasm/pkg-proofs` (with the trap exports, tests only); loaded lazily by
//! the application behind the sheet and import paths (`packages/core-wasm/src/runtime.ts`).
use faunosteo_core::domain::archive::SnapshotCaps;
use faunosteo_core::domain::sheet::LoadedSheet;
use faunosteo_core::domain::sheet::preview::PreviewDrawing;
use faunosteo_core::domain::sheet::preview::{MAX_HELD_DRAWINGS, PreviewDrawing};
use faunosteo_core::pdf::read::MAX_PDF_BYTES;
use wasm_bindgen::prelude::{JsError, wasm_bindgen};
@@ -71,6 +72,8 @@ impl SheetHandle {
#[wasm_bindgen]
pub struct PreviewBuilder {
drawings: Vec<PreviewDrawing>,
/// A drawing was refused: the build answers `caps`.
refused: bool,
}
#[wasm_bindgen]
@@ -79,21 +82,33 @@ impl PreviewBuilder {
pub fn new() -> PreviewBuilder {
PreviewBuilder {
drawings: Vec::new(),
refused: false,
}
}
pub fn add_drawing(&mut self, asset_id: &str, sha256: &str, bytes: &[u8]) {
/// `false` (nothing kept) past the preview's drawing count or a drawing over 2 MiB; the
/// build then refuses with `caps` as the core does.
pub fn add_drawing(&mut self, asset_id: &str, sha256: &str, bytes: &[u8]) -> bool {
let full = self.drawings.len() >= MAX_HELD_DRAWINGS;
if full || bytes.len() > SnapshotCaps::ASSET_BYTES {
self.refused = true;
return false;
}
self.drawings.push(PreviewDrawing {
asset_id: asset_id.to_owned(),
sha256: sha256.to_owned(),
bytes: bytes.to_vec(),
});
true
}
/// Consumes the builder; throws an `Error` worded `<kind>: <detail>` (`invalid_document`,
/// `drawing_missing`, `drawing_hash_mismatch`, `fingerprint_mismatch`, `caps`, or
/// `invalid_shape` / `invalid_json` for the input).
pub fn build(self, input: &str) -> Result<PreviewSheet, JsError> {
if self.refused {
return Err(JsError::new("caps: drawings"));
}
faunosteo_core::facade::load_preview(input, self.drawings)
.map(|sheet| PreviewSheet { sheet })
.map_err(|failure| JsError::new(&format!("{}: {}", failure.kind, failure.detail)))
+35 -16
View File
@@ -3874,8 +3874,9 @@ Free (`call`): `sheet.edit` `{document, command}` → `{document}`, failures `un
budget); `sheet.dependents` `{document, key}` → `{dependents: [{element, relation}]}`. Wasm classes: `PreviewBuilder`
(`add_drawing(assetId, sha256, bytes)`, `build(PreviewInput JSON)` throwing `<kind>: <detail>`: `invalid_document`,
`drawing_missing`, `drawing_hash_mismatch`, `fingerprint_mismatch`, `caps`), `PreviewSheet` (`call`: `preview.state`,
`view.canvas`; `replace_document`, `add_drawing`: `{"ok":true}` or `invalid_document`, `drawing_hash_mismatch`,
`read_only`); the function `clean_svg(bytes)` (`SvgCleaning`). `view.canvas` is one composition for `LoadedSheet` and
`view.canvas`; `replace_document`, `add_drawing`: `{"ok":true,"value":null}` or `invalid_document`,
`drawing_hash_mismatch`, `caps`, `read_only`); a preview holds at most 16 drawings of at most 2 MiB each
(`MAX_HELD_DRAWINGS`; the builder's `add_drawing` answers `false` past them and its build throws `caps: drawings`); the function `clean_svg(bytes)` (`SvgCleaning`). `view.canvas` is one composition for `LoadedSheet` and
`PreviewSheet` (`ops::canvas_of`). The runtime (`CoreRuntime`) does not manage preview handles yet: their poison and
recovery come with their first page (V1-2.5). ts-rs: 31 new type files, `types/index.ts` 126 entries.
@@ -3883,12 +3884,19 @@ recovery come with their first page (V1-2.5). ts-rs: 31 new type files, `types/i
1. The sanitiser keeps, removes and refuses exactly as before (R3 oracles O4/O5 green); its walk also lists, in
`active`, each removal that carried a script, an embedded document or image (`image`, `feImage`, `foreignObject`,
`iframe`, `object`, `embed`, media), animation, a link (`a`), an event handler (`on*`), a reference outside the
document (`href`, `url(…)`), `@import`, `javascript:`, `expression(`, `image-set(`, `behavior:`, or a backslash. A
`<style>` element is active when its text holds such a value, cleanable otherwise.
`iframe`, `object`, `embed`, media, and the HTML fetchers `img`, `input`, `source`, `frame`, `form`, `meta`, `link`,
`base`…), any element of the XHTML namespace, animation, a link (`a`), an event handler (`on*`), a fetching attribute
(`src`, `srcset`, `action`, `data`, `poster`, `http-equiv`…), a reference outside the document (`href`, `url(…)`),
`@import`, `javascript:`, `expression(`, `image-set(`, `behavior:`, or a backslash. A `<style>` element is active
when its text holds such a value, cleanable otherwise. A processing instruction other than the XML declaration
(`<?xml-stylesheet …?>`), which the tree never reads but the stored bytes would keep, refuses the file in the
cleaning (`svg_active_content`, named `?<target>`) and in the verdict (`svg_rejected`, `processing_instruction`);
the hostile file `comments-pi-cdata.svg` moved from clean to refused.
2. The cleaning reads a picked file with one difference from the stored-drawing reader: an oversized `title` or `desc`
(over 4 KiB) is a removal (the cleaned text drops it) where the stored reader refuses the file (`too_complex`).
3. `clean_svg` answers `cleaning_unstable` if a cleaned text did not read back with no removal; the property test proves
(over 4 KiB) is a removal (the cleaned text drops it) where the stored reader refuses the file (`too_complex`); its
subtree is still classified, so active content inside it refuses the file.
3. `clean_svg` answers `cleaning_unstable` if a cleaned text did not read back with no removal and serialise to
itself (what the admin confirms is what every reader draws); the property test proves
it never happens on the corpus and 3 000 generated drawings, and the check stays so no upload rests on the test.
4. The document measure is exact for texts written as JavaScript writes numbers (every core output); a page that sends
another spelling (`1.50`, `1E2`) is stored as written and may measure a few bytes differently.
@@ -3905,21 +3913,26 @@ recovery come with their first page (V1-2.5). ts-rs: 31 new type files, `types/i
and local to their group, an omitted marking digesting as `inherited`, coverage, a derived version deleting a zone
with its removal group and change list, removal digests bound to their key, an inherited element alone keeping its
group uncoverable, added / changed / status-changed / removed-plus-added, a reorder changing the digest only.
- `sheet_verdict` (13): a clean sheet and the equine draft pass (fingerprint `12d78872…`, 30 groups, 184 105 bytes); the
- `sheet_verdict` (14): a clean sheet and the equine draft pass (fingerprint `12d78872…`, 30 groups, 184 105 bytes); the
shape the SQL reads; each rule and drawing fault alone; caps at cap − 1 / cap / cap + 1 (annotations, labels in
astral characters, depth, nodes, drawings, document bytes); code order; determinism; base and removal groups; a
astral characters, depth, nodes, document bytes; views, drawings, drawing bytes and total bytes through
`caps_report`); code order; determinism; base and removal groups; a
document at every cap (64 views, 1 024 annotations, 8 192 nodes, depth 8) verified.
- `sheet_preview` (8): the equine draft previews with the bundle's index tables and canvases and its fingerprint; hash
- `sheet_preview` (9): the equine draft previews with the bundle's index tables and canvases and its fingerprint; hash
mismatch refused in every mode; fingerprint, missing drawing and caps refused for published and deprecated, reported
for drafts; an invalid draft draws with its issues; five replacements, one drawing read; read-only published; no
report operation on a preview.
report operation on a preview; at most 16 drawings of at most 2 MiB.
- `sheet_edit` (11): each marking rule of §6.6, the refusals, `dependents`, the facade, and 400 random commands on the
small sheet and on the equine draft (strict re-read, keys kept unless removed, nothing turned `inherited`).
- `svg_clean` (8): the 42 hostile files, each on the §9.2 line it belongs to; active names; a plain `<style>` cleaned and
small sheet and on the equine draft, view crops and drawings, references, zones included (strict re-read, every
key kept by every command but `remove`, which takes only the key and its descendants, the marking rule of each
command, `mark_reviewed` on its zone only, nothing turned `inherited`).
- `svg_clean` (10): the 42 hostile files, each on the §9.2 line it belongs to; the security audit's 13 probes
(stylesheet PIs, active content under a long title, HTML fetchers, XHTML elements) refused; a stored PI refused by the
verdict; active names; a plain `<style>` cleaned and
named, four hostile ones refused; the product drawing clean with its sha256; the four committed exports; the owner's
folder (empty so far); the fixpoint on the corpus and on 3 000 generated drawings (123 clean, 2 697 cleanable, 180
reference cycles refused).
- Mutation run (10 mutants, each against its suite): caps off by one, digest without its key, no active class, hash
- Mutation run (10 mutants, each against its suite; `.claude/audits/2026-10-29-v1-2-2-mutations.py`): caps off by one, digest without its key, no active class, hash
unchecked, an update keeping `reviewed`, fingerprint unchecked, `inherited` counted as read, redrawn views ignored,
numeric scale ignored, removal groups never made: 10 killed (the `inherited` one survived the first run; the test
that kills it was added).
@@ -3931,13 +3944,19 @@ recovery come with their first page (V1-2.5). ts-rs: 31 new type files, `types/i
| Measure | Value |
|---|---|
| shipped module | 1 771 003 B raw, 582 722 B gzip -9 (script), 592 136 B by Node's gzip in `size.test.ts`; budget 600 000 (D-R13); 531 715 B before V1-2.2 |
| shipped module | 1 777 104 B raw, 584 686 B gzip -9 (script), 593 582 B by Node's gzip in `size.test.ts`; budget 600 000 (D-R13); 531 715 B before V1-2.2 (RUST.md §11.22) |
| verdict at every cap, native | 107 ms release, 493 ms debug (document 1 165 850 bytes) |
| `clean_svg`, shipped module | 45 hostile inputs and floods, slowest 0.4 ms |
Security audit (fresh, on 741c25c): PASS, two MEDIUM and two LOW fixed in the same step. Fresh verifier (same
commit): ECARTS(6), all closed in the same step (the long-title MEDIUM, the property test's insert and
`mark_reviewed` checks, the cap − 1 fixtures of views, drawings and bytes, a floor false positive, record wordings) (processing instructions
kept in uploaded bytes; active content classed cleanable under a long title or as HTML fetchers; review groups built
in quadratic time, now one indexed pass; the runtime fixpoint check now compares the serialisation too).
### 12.6 Residual risks and points for V1-2.3
- The module's margin under its budget is about 8 KB by the test's measure: the V1-2.5 and V1-2.6 pages must not grow the
- The module's margin under its budget is about 6 KB by the test's measure: the V1-2.5 and V1-2.6 pages must not grow the
core's browser surface without measuring.
- cairo writes colours as `rgb(…)`: the allowlist removes any function from presentation attributes, so its cleaned
exports lose their colours (named, visible in the preview). Allowing `rgb()` / `rgba()` with numeric arguments would be a
@@ -1201,6 +1201,15 @@ supplied; they are not invented.
referential gaps are left to the validation panel. A `dependents(key)`
query feeds the delete confirmations. TypeScript holds no schema of its own
and no Zod copy; it reads the ts-rs types.
- As built in V1-2.2 (implementation reading, for the owner's confirmation): the
wire ops are `insert`, `update`, `remove`, `move` (the reorder), `set_status`
and `mark_reviewed`; besides what breaks strict parsing they refuse what would
leave an element unaddressable or outside the editor's offer
(`duplicate_key`, `unknown_key`, `id_changed`, `invalid_position`, `too_deep`
beyond 8 finding levels); removing a view or a category leaves the
annotations that name it in place, dangling for the validation panel, which
`dependents` lists before the confirmation (§6.6 « with descendants » read
as the finding tree's children).
- Ids are generated by the page (`<prefix>-<10 lowercase base32
characters>`, alphabet `a-z2-7`, inside the stable-id rule), passed to the
core, never derived from labels, never editable, never reused.
@@ -1305,6 +1314,16 @@ The page reads the file with the core SVG reader before anything is stored.
| Any other removal (editor metadata and namespaces, `data-*` attributes, `<style>` elements, `class` or `style` on the root, ids outside the grammar, unsupported elements such as `symbol`, oversized `title` or `desc`) | cleaning offered: « Nettoyer et utiliser cette version », after the composed preview of the cleaned drawing and one confirmation; a removed `<style>` element is named, since the `class` attributes it styled then render unstyled |
| No removal | used as is |
- Stricter than the table, as built in V1-2.2 (for the owner's confirmation):
an `<a>` link is refused even when it points inside the document, and so is
a value holding a backslash (a CSS escape can spell `url(` or `javascript:`),
`expression(`, `image-set(` or `behavior:`, an HTML element (XHTML namespace,
or a fetcher such as `img`, `meta`, `form`) and an HTML fetching attribute
(`src`, `srcset`, `action`, `http-equiv`…).
- A processing instruction other than the XML declaration (`<?xml-stylesheet …?>`)
is refused as active content: the reader's tree never sees it, but the stored
bytes would keep it; the verdict refuses a stored drawing that carries one
(V1-2.2, security audit).
- An oversized `title` or `desc` is a removal for the cleaning only: the reader
of stored drawings refuses such a file whole (`too_complex`), so it reaches
Storage only cleaned (V1-2.2).
+2 -1
View File
@@ -34,7 +34,8 @@ export interface GlueModule {
/** A stored version being gathered (crates/wasm `PreviewBuilder`). */
export interface GluePreviewBuilder {
add_drawing(assetId: string, sha256: string, bytes: Uint8Array): void
/** `false` past 16 drawings or a drawing over 2 MiB; the build then throws `caps: drawings`. */
add_drawing(assetId: string, sha256: string, bytes: Uint8Array): boolean
/** Consumes the builder; throws `<kind>: <detail>` (`invalid_document`, `drawing_missing`,
* `drawing_hash_mismatch`, `fingerprint_mismatch`, `caps`, `invalid_shape`, `invalid_json`). */
build(inputJson: string): GluePreviewSheet