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:
1 parent
741c25c464
commit
8294d5471c
15 files changed
+448
-72
No files matched your search
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user