From ca31b787b61432499c3b1df33bf64ac87ff55db4 Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 29 Oct 2026 13:58:32 +0100 Subject: [PATCH] fix(domain): V1-2.2 second review round, linear walks, bounds, CSS loaders Verifier ECARTS(3) and security PASS on 8294d54: one indexed walk per view chain in sheet.edit, a set for referenced drawings, released preview parses, xml:base and CSS loaders refused, fail-closed PI scan, PI drawings rejected by the preview, the builder naming its cap. --- crates/core/src/anatomy/svg/mod.rs | 5 ++- crates/core/src/anatomy/svg/sanitize.rs | 10 ++++- crates/core/src/anatomy/svg/tree.rs | 2 +- crates/core/src/domain/sheet/edit.rs | 46 +++++++++++++++++++--- crates/core/src/domain/sheet/preview.rs | 27 ++++++++++++- crates/core/src/domain/sheet/report.rs | 14 +++---- crates/core/tests/sheet_edit.rs | 23 +++++++++++ crates/core/tests/sheet_preview.rs | 45 +++++++++++++++++++++ crates/core/tests/svg_clean.rs | 3 ++ crates/wasm/src/lib.rs | 19 +++++---- docs/development/RUST.md | 33 ++++++++++++++-- docs/development/V1-2_ANATOMICAL_SHEETS.md | 8 ++-- packages/core-wasm/src/runtime.ts | 2 +- 13 files changed, 201 insertions(+), 36 deletions(-) diff --git a/crates/core/src/anatomy/svg/mod.rs b/crates/core/src/anatomy/svg/mod.rs index 77efced2..150d8def 100644 --- a/crates/core/src/anatomy/svg/mod.rs +++ b/crates/core/src/anatomy/svg/mod.rs @@ -32,14 +32,15 @@ pub fn read_svg_for_cleaning(text: &str) -> Result { /// 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 `` in its bytes: the cleaning and the verdict refuse it. +/// Fail-closed: a text this pass cannot parse answers one `unreadable` target. pub fn processing_instructions(text: &str) -> Vec { let options = roxmltree::ParsingOptions { allow_dtd: false, - nodes_limit: 200_000, + nodes_limit: tree::NODES_LIMIT, ..roxmltree::ParsingOptions::default() }; let Ok(document) = roxmltree::Document::parse_with_options(text, options) else { - return Vec::new(); + return vec!["unreadable".to_owned()]; }; document .descendants() diff --git a/crates/core/src/anatomy/svg/sanitize.rs b/crates/core/src/anatomy/svg/sanitize.rs index b61a7a07..f12283a9 100644 --- a/crates/core/src/anatomy/svg/sanitize.rs +++ b/crates/core/src/anatomy/svg/sanitize.rs @@ -375,7 +375,8 @@ fn attribute_is_active(qname: &str, value: &str) -> bool { .next() .unwrap_or(qname) .to_ascii_lowercase(); - if local.starts_with("on") || FETCHING_ATTRIBUTES.contains(&local.as_str()) { + let fetching = FETCHING_ATTRIBUTES.contains(&local.as_str()) || qname == "xml:base"; + if local.starts_with("on") || fetching { return true; } if local == "href" && !is_local_reference(local_reference_text(value)) { @@ -387,9 +388,14 @@ fn attribute_is_active(qname: &str, value: &str) -> bool { /// 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) + let lower = value.to_ascii_lowercase(); + let loads = CSS_LOADERS.iter().any(|loader| lower.contains(loader)); + value.contains('\\') || !urls_are_local(value) || style_forbidden(value) || loads } +/// CSS functions that fetch a resource besides `url(`. +const CSS_LOADERS: [&str; 3] = ["src(", "image(", "cross-fade("]; + fn element_allowed(element: &Element) -> bool { ALLOWED_ELEMENTS.contains(&element.qname.as_str()) && element.is_svg_namespace() } diff --git a/crates/core/src/anatomy/svg/tree.rs b/crates/core/src/anatomy/svg/tree.rs index 4827db52..319b0ebb 100644 --- a/crates/core/src/anatomy/svg/tree.rs +++ b/crates/core/src/anatomy/svg/tree.rs @@ -136,7 +136,7 @@ impl From for SvgRejection { } /// Never reached once the prescan passed (its caps are lower); a backstop on parser memory. -const NODES_LIMIT: u32 = 200_000; +pub const NODES_LIMIT: u32 = 200_000; /// Bounds, parses and reads an SVG text into the core's tree; the root must be `` in the /// SVG namespace (or none), written unprefixed. diff --git a/crates/core/src/domain/sheet/edit.rs b/crates/core/src/domain/sheet/edit.rs index c1b329b9..f20edafc 100644 --- a/crates/core/src/domain/sheet/edit.rs +++ b/crates/core/src/domain/sheet/edit.rs @@ -34,7 +34,6 @@ use super::schema::{ FindingNode, FindingZone, LocalizedOptionalText, LocalizedText, Reference, ReviewStatus, SheetView, VertebralRegion, ViewBox, }; -use crate::anatomy::compose::asset_id_of_view; use crate::domain::archive::SnapshotCaps; /// The sheet's own texts, set as one element. @@ -782,16 +781,51 @@ fn children_mut<'a>( fn view_drawings( doc: &AnatomicalSheetDocument, ) -> HashMap, Option)> { - let mut out = HashMap::new(); + let mut first: HashMap<&str, &SheetView> = HashMap::new(); for view in &doc.views { - out.entry(view.id().to_owned()).or_insert_with(|| { - let asset = asset_id_of_view(doc, view.id()).map(str::to_owned); - (asset, view.view_box().cloned()) - }); + first.entry(view.id()).or_insert(view); + } + let mut assets: HashMap<&str, Option> = HashMap::new(); + let mut out = HashMap::new(); + for (&id, view) in &first { + let asset = drawn_asset(id, &first, &mut assets); + out.insert(id.to_owned(), (asset, view.view_box().cloned())); } out } +/// The drawing a view shows: its own, else the first up its parent chain (as +/// `asset_id_of_view`), each chain walked once thanks to `known`; a cycle draws nothing. +fn drawn_asset<'a>( + id: &'a str, + views: &HashMap<&'a str, &'a SheetView>, + known: &mut HashMap<&'a str, Option>, +) -> Option { + let mut chain: Vec<&str> = Vec::new(); + let mut current = Some(id); + let found = loop { + let Some(here) = current else { break None }; + if let Some(done) = known.get(here) { + break done.clone(); + } + if chain.contains(&here) { + break None; + } + chain.push(here); + let Some(view) = views.get(here) else { + break None; + }; + if let Some(asset) = view.svg_asset_id() { + break Some(asset.to_owned()); + } + current = view.parent_view_id(); + }; + for link in chain { + known.insert(link, found.clone()); + } + found +} + /// Annotations of a view whose drawing or crop changed turn `to_confirm` (§6.6). fn reconfirm_redrawn_views( doc: &mut AnatomicalSheetDocument, diff --git a/crates/core/src/domain/sheet/preview.rs b/crates/core/src/domain/sheet/preview.rs index c58cfa55..e69be276 100644 --- a/crates/core/src/domain/sheet/preview.rs +++ b/crates/core/src/domain/sheet/preview.rs @@ -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::anatomy::svg::processing_instructions; use crate::domain::archive::SnapshotCaps; use crate::domain::js; @@ -112,6 +113,14 @@ impl PreviewFailure { /// uploads of one editing session (each one at most 2 MiB, refused above). pub const MAX_HELD_DRAWINGS: usize = 16; +/// A drawing as the verdict would read it: a processing instruction refuses it here too. +fn preview_drawing(text: &str) -> Drawing { + if !processing_instructions(text).is_empty() { + return Err("processing_instruction".to_owned()); + } + read_drawing(text) +} + struct Asset { sha256: String, bytes: u64, @@ -185,17 +194,26 @@ impl PreviewSheet { if !self.drawings.contains_key(&drawing.sha256) { self.reads += 1; let read = std::str::from_utf8(&drawing.bytes) - .map_or_else(|_| Err("malformed".to_owned()), read_drawing); + .map_or_else(|_| Err("malformed".to_owned()), preview_drawing); self.drawings.insert(drawing.sha256.clone(), read); } let asset = Asset { sha256: drawing.sha256, bytes: drawing.bytes.len() as u64, }; - self.assets.insert(drawing.asset_id, asset); + if let Some(replaced) = self.assets.insert(drawing.asset_id, asset) { + self.release(&replaced.sha256); + } Ok(()) } + /// Drops a parse no asset holds any more, so re-adding an asset never accumulates them. + fn release(&mut self, sha256: &str) { + if !self.assets.values().any(|a| a.sha256 == sha256) { + self.drawings.remove(sha256); + } + } + /// A draft's document after an edit; the drawings are kept as read. On a refusal the /// previous document stays. pub fn replace_document(&mut self, text: &str) -> Result<(), PreviewFailure> { @@ -228,6 +246,11 @@ impl PreviewSheet { self.drawings.get(&asset.sha256) } + /// Parsed drawings held now (one per sha256 still referenced by an asset). + pub fn held_parses(&self) -> usize { + self.drawings.len() + } + /// How many times a drawing was read (each sha256 once). pub fn drawing_reads(&self) -> usize { self.reads diff --git a/crates/core/src/domain/sheet/report.rs b/crates/core/src/domain/sheet/report.rs index 9ac1c26b..03546c7b 100644 --- a/crates/core/src/domain/sheet/report.rs +++ b/crates/core/src/domain/sheet/report.rs @@ -71,11 +71,11 @@ fn geometry_of(annotation: &Annotation) -> Option<&super::schema::Geometry> { /// The distinct drawings the views reference, in view order. pub fn referenced_asset_ids(document: &AnatomicalSheetDocument) -> Vec<&str> { - let mut ids: Vec<&str> = Vec::new(); - for id in document.views.iter().filter_map(|v| v.svg_asset_id()) { - if !ids.contains(&id) { - ids.push(id); - } - } - ids + let mut seen = std::collections::HashSet::new(); + document + .views + .iter() + .filter_map(|v| v.svg_asset_id()) + .filter(|id| seen.insert(*id)) + .collect() } diff --git a/crates/core/tests/sheet_edit.rs b/crates/core/tests/sheet_edit.rs index b1ffcc36..6b45aabb 100644 --- a/crates/core/tests/sheet_edit.rs +++ b/crates/core/tests/sheet_edit.rs @@ -850,3 +850,26 @@ fn is_below(key: &ElementKey, ancestor: &ElementKey) -> bool { _ => false, } } + +#[test] +fn an_edit_on_a_long_chain_of_views_stays_linear() { + let mut doc = serde_json::to_value(small()).unwrap(); + let views = doc["views"].as_array_mut().unwrap(); + for i in 0..2000 { + let parent = if i == 0 { + "main".to_owned() + } else { + format!("c{}", i - 1) + }; + views.push(json!({ "id": format!("c{i}"), "kind": "detail", "name": name("C"), "svg_asset_id": null, + "view_box": { "min_x": 0, "min_y": 0, "width": 1, "height": 1 }, "parent_view_id": parent })); + } + let doc: AnatomicalSheetDocument = serde_json::from_value(doc).unwrap(); + let started = std::time::Instant::now(); + edit(&doc, json!({ "op": "remove", "key": annotation("a1") })); + assert!( + started.elapsed().as_millis() < 1000, + "{:?}", + started.elapsed() + ); +} diff --git a/crates/core/tests/sheet_preview.rs b/crates/core/tests/sheet_preview.rs index 6559ae72..71779bb6 100644 --- a/crates/core/tests/sheet_preview.rs +++ b/crates/core/tests/sheet_preview.rs @@ -391,3 +391,48 @@ fn a_preview_holds_a_bounded_number_of_bounded_drawings() { ); assert_eq!(draft.drawing_reads(), 1, "the same bytes are read once"); } + +#[test] +fn a_replaced_asset_releases_its_parse_and_a_pi_drawing_shows_rejected() { + let mut draft = PreviewSheet::load( + input(PreviewStatus::Draft, document_text(), None), + vec![horse_drawing()], + ) + .unwrap(); + for i in 0..40 { + let svg = format!(r#""#) + .into_bytes(); + let sha = faunosteo_core::domain::sheet::verdict::sha256_hex(&svg); + draft + .add_drawing(PreviewDrawing { + asset_id: "same-asset".into(), + sha256: sha, + bytes: svg, + }) + .unwrap(); + } + assert_eq!( + draft.held_parses(), + 2, + "the horse and the last bytes of the re-added asset" + ); + let pi = br#""#.to_vec(); + let sha = faunosteo_core::domain::sheet::verdict::sha256_hex(&pi); + let mut doc = parse_json(&document_text()).unwrap(); + doc["views"][0]["svg_asset_id"] = json!("33333333-0000-4000-8000-000000000001"); + let preview = PreviewSheet::load( + input(PreviewStatus::Draft, doc.to_string(), None), + vec![PreviewDrawing { + asset_id: "33333333-0000-4000-8000-000000000001".into(), + sha256: sha, + bytes: pi, + }], + ) + .unwrap(); + let state = state(&preview); + assert_eq!(state.drawings[0].state, PreviewDrawingStatus::Rejected); + assert_eq!( + state.drawings[0].detail.as_deref(), + Some("processing_instruction") + ); +} diff --git a/crates/core/tests/svg_clean.rs b/crates/core/tests/svg_clean.rs index 7c71a438..e8fe5c83 100644 --- a/crates/core/tests/svg_clean.rs +++ b/crates/core/tests/svg_clean.rs @@ -161,6 +161,9 @@ fn hidden_or_html_content_is_refused_not_cleaned() { svg(""), svg("x"), svg(""), + svg(""), + svg(""), + svg(""), ]; for case in cases { let cleaning = clean_svg(case.as_bytes()); diff --git a/crates/wasm/src/lib.rs b/crates/wasm/src/lib.rs index 7fe1f45a..83d0ec5e 100644 --- a/crates/wasm/src/lib.rs +++ b/crates/wasm/src/lib.rs @@ -72,8 +72,8 @@ impl SheetHandle { #[wasm_bindgen] pub struct PreviewBuilder { drawings: Vec, - /// A drawing was refused: the build answers `caps`. - refused: bool, + /// The cap a refused drawing passed: the build answers `caps` with it, as the core does. + refused: Option<&'static str>, } #[wasm_bindgen] @@ -82,16 +82,19 @@ impl PreviewBuilder { pub fn new() -> PreviewBuilder { PreviewBuilder { drawings: Vec::new(), - refused: false, + refused: None, } } /// `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; + if bytes.len() > SnapshotCaps::ASSET_BYTES { + self.refused = Some("drawing_bytes"); + return false; + } + if self.drawings.len() >= MAX_HELD_DRAWINGS { + self.refused = Some("drawings"); return false; } self.drawings.push(PreviewDrawing { @@ -106,8 +109,8 @@ impl PreviewBuilder { /// `drawing_missing`, `drawing_hash_mismatch`, `fingerprint_mismatch`, `caps`, or /// `invalid_shape` / `invalid_json` for the input). pub fn build(self, input: &str) -> Result { - if self.refused { - return Err(JsError::new("caps: drawings")); + if let Some(cap) = self.refused { + return Err(JsError::new(&format!("caps: {cap}"))); } faunosteo_core::facade::load_preview(input, self.drawings) .map(|sheet| PreviewSheet { sheet }) diff --git a/docs/development/RUST.md b/docs/development/RUST.md index 8a0f2949..87c4b17b 100644 --- a/docs/development/RUST.md +++ b/docs/development/RUST.md @@ -3878,7 +3878,9 @@ budget); `sheet.dependents` `{document, key}` → `{dependents: [{element, relat `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. +recovery come with their first page (V1-2.5). ts-rs: 31 new type files, `types/index.ts` 126 entries. The app's +trapping test stub (`apps/app/tests/helpers/core.ts`) lists the two new glue members, as it lists every member of +`GlueModule`. ### 12.3 Behaviour (declared) @@ -3923,9 +3925,11 @@ recovery come with their first page (V1-2.5). ts-rs: 31 new type files, `types/i for drafts; an invalid draft draws with its issues; five replacements, one drawing read; read-only published; no 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, 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`). + small sheet and on the equine draft, view crops and drawings, references, zones included (strict re-read; + on the marked elements, annotations, categories and nodes: every key kept by every command but `remove`, which + takes only its key and descendants, the rule of insert, update, move, set_status and `mark_reviewed`; nothing + turned `inherited`). The keys of views, references, regions and annotation categories and the redraw rule are held + by the case tests, not by the property test. - `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 `