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.
This commit is contained in:
bchanot committed 2026-10-29 13:58:32 +01:00
1 parent 8294d5471c
commit ca31b787b6
13 files changed
+201 -36

No files matched your search

+3 -2
View File
@@ -32,14 +32,15 @@ pub fn read_svg_for_cleaning(text: &str) -> Result<Sanitized, SvgRejection> {
/// 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.
/// Fail-closed: a text this pass cannot parse answers one `unreadable` target.
pub fn processing_instructions(text: &str) -> Vec<String> {
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()
+8 -2
View File
@@ -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()
}
+1 -1
View File
@@ -136,7 +136,7 @@ impl From<PrescanError> 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 `<svg>` in the
/// SVG namespace (or none), written unprefixed.
+40 -6
View File
@@ -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<String, (Option<String>, Option<ViewBox>)> {
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<String>> = 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<String>>,
) -> Option<String> {
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,
+25 -2
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::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
+7 -7
View File
@@ -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()
}
+23
View File
@@ -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()
);
}
+45
View File
@@ -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#"<svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0h{i}"/></svg>"#)
.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#"<?xml-stylesheet href="https://e/x.xsl"?><svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0h1"/></svg>"#.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")
);
}
+3
View File
@@ -161,6 +161,9 @@ fn hidden_or_html_content_is_refused_not_cleaned() {
svg("<h:form action=\"https://e\"/>"),
svg("<h:div>x</h:div>"),
svg("<g><x:thing xmlns:x=\"urn:x\" src=\"https://e\"/></g>"),
svg("<g xml:base=\"https://e/\"><path d=\"M0 0h1\"/></g>"),
svg("<path d=\"M0 0h1\" style=\"fill:src('https://e/x')\"/>"),
svg("<style>p{fill:image('https://e/x.png')}</style>"),
];
for case in cases {
let cleaning = clean_svg(case.as_bytes());
+11 -8
View File
@@ -72,8 +72,8 @@ impl SheetHandle {
#[wasm_bindgen]
pub struct PreviewBuilder {
drawings: Vec<PreviewDrawing>,
/// 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<PreviewSheet, JsError> {
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 })
+29 -4
View File
@@ -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 `<style>` cleaned and
@@ -3954,6 +3958,13 @@ commit): ECARTS(6), all closed in the same step (the long-title MEDIUM, the prop
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).
Second round (fresh, on 8294d54): verifier ECARTS(3) (A6 waits on the owner's exports; A12 outside its permitted
commands, covered by `check:all`; the app test stub, now recorded in §12.2) and security PASS with findings fixed in
the same step: one indexed walk per view chain in `sheet.edit` (2 000 chained views were cubic, 5 s per edit; now a
test bound of 1 s debug), a set for the referenced drawings, a replaced asset releases its parse, `xml:base` and the
CSS loaders `src(`, `image(`, `cross-fade(` refused as active, the PI scan fail-closed (`?unreadable`), a drawing
with a PI shown `rejected` by the preview as the verdict refuses it.
### 12.6 Residual risks and points for V1-2.3
- 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
@@ -3967,3 +3978,17 @@ in quadratic time, now one indexed pass; the runtime fixpoint check now compares
(`complete_sheet_verification`) must accept a failed verdict without them, or the round would wait for its attempt limit.
- `PreviewSheet` handles are not yet under `CoreRuntime`'s poison and recovery (§8.3): the first page that holds one adds
it.
- Dangling `#id` references are clean and pass the verdict: once a view is inlined into the page, such an id resolves
against the page, so a detail drawing with 256 `<use href="#big"/>` can instantiate the main drawing's group
(security audit, MEDIUM, probe on the shipped module) or show page icons. §9.7's « ids scoped per view » becomes
necessary before V1-2.6 draws two uploaded drawings in one page; refusing references to ids a drawing does not
define is the alternative. Owner decision.
- A clean file is uploaded as its original bytes (contract §9.2 « used as is »): an HTML-only comment form
(`<!-->…-->`) or an XML declaration naming another encoding stays in the stored object, inert for every current
reader (the bucket serves `image/svg+xml`, the app parses core serialisations). Uploading the core's serialisation
in the clean case too would remove it; that changes §9.2 and is the owner's call.
- `validate_sheet_document`'s `view.parent.cycle` rule (R2 code) walks each view's parent chain: quadratic on a long
chain (8 000 chained views, 2.4 s per `preview.state` in the module; the server cap of 2 MiB allows that order).
Left as it was in this step; memoising the chain is the fix, with the conformance fixtures as its oracle.
- The glue copies a drawing's bytes into linear memory before the 2 MiB check (transient, bounded by the bucket).
+5 -3
View File
@@ -1205,8 +1205,8 @@ supplied; they are not invented.
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
(`duplicate_key`, `unknown_key`, `invalid_target`, `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).
@@ -1423,7 +1423,9 @@ A drawing never blocks authoring: only the round waits for its verdict.
given are reported in the state, never a load failure; `caps` and
`drawing_missing` refuse a published or deprecated version only (owner
decision 2026-10-29). A hash mismatch and an unreadable document refuse every
mode.
mode, and so do the handle's own resource bounds (V1-2.2): a drawing over
2 MiB (`caps: drawing_bytes`) and more than 16 drawings held
(`caps: drawings`).
- `LoadedSheet` (the report editor's sheet) keeps its mandatory migration map;
`archive::SheetStatus` is not extended; a deprecated version previews like a
published one with a « retirée » mark. The readers of user data
+1 -1
View File
@@ -34,7 +34,7 @@ export interface GlueModule {
/** A stored version being gathered (crates/wasm `PreviewBuilder`). */
export interface GluePreviewBuilder {
/** `false` past 16 drawings or a drawing over 2 MiB; the build then throws `caps: drawings`. */
/** `false` past 16 drawings or a drawing over 2 MiB; the build then throws `caps: drawing_bytes` or `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`). */