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:
1 parent
8294d5471c
commit
ca31b787b6
13 files changed
+201
-36
No files matched your search
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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,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
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
Reference in new issue
Block a user