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());