fix(anatomy): R5.3 __proto__ field segments refused, seed identity guard over the whole seed tree, hashed id comparisons
This commit is contained in:
1 parent
3ece1a64b1
commit
f245b3ec25
7 files changed
+86
-33
No files matched your search
@@ -260,15 +260,35 @@ fn inside(folder: &Path, seed: &Path) -> bool {
|
||||
folder.starts_with(seed)
|
||||
}
|
||||
|
||||
/// Whether the seed folder is `folder` or one of its ancestors, by file identity (device and
|
||||
/// inode): a second path to the seed (a bind mount, a share mounted twice) is caught too.
|
||||
/// The file identities (device, inode) of the seed folder and of every folder under it, links
|
||||
/// never followed; `None` when the walk fails (the caller then refuses).
|
||||
fn seed_identities(seed: &Path) -> Option<Vec<(u64, u64)>> {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let mut identities = Vec::new();
|
||||
let mut pending = vec![seed.to_path_buf()];
|
||||
while let Some(folder) = pending.pop() {
|
||||
let metadata = fs::symlink_metadata(&folder).ok()?;
|
||||
identities.push((metadata.dev(), metadata.ino()));
|
||||
for entry in fs::read_dir(&folder).ok()? {
|
||||
let entry = entry.ok()?;
|
||||
if entry.file_type().ok()?.is_dir() {
|
||||
pending.push(entry.path());
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(identities)
|
||||
}
|
||||
|
||||
/// Whether `folder` or one of its ancestors is the seed folder or a folder under it, by file
|
||||
/// identity: a second path to any of them (a bind mount, a share mounted twice on the same
|
||||
/// device) is caught too. A walk that fails counts as inside.
|
||||
fn inside_by_identity(folder: &Path, seed: &Path) -> bool {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let Ok(seed) = fs::metadata(seed) else {
|
||||
let Some(identities) = seed_identities(seed) else {
|
||||
return true;
|
||||
};
|
||||
folder.ancestors().any(|ancestor| {
|
||||
fs::metadata(ancestor).is_ok_and(|m| m.dev() == seed.dev() && m.ino() == seed.ino())
|
||||
fs::metadata(ancestor).is_ok_and(|m| identities.contains(&(m.dev(), m.ino())))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -101,6 +101,13 @@ pub fn split_field(field: &str) -> Result<Vec<&str>, PipelineError> {
|
||||
if let Some(key) = path.iter().find(|key| key.starts_with('\0')) {
|
||||
return Err(fail(format!("reserved key {key:?} in field \"{field}\"")));
|
||||
}
|
||||
// A JavaScript assignment through `__proto__` never wrote a key: it set a prototype,
|
||||
// polluted `Object.prototype` or did nothing. No faithful reading exists, so it is refused.
|
||||
if path.contains(&"__proto__") {
|
||||
return Err(fail(format!(
|
||||
"reserved key \"__proto__\" in field \"{field}\""
|
||||
)));
|
||||
}
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
//! its target, edits the working document in place and records what it changed; anything it
|
||||
//! cannot do exactly as written refuses (the applier adds the change id and op index).
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use super::fields::{FieldKind, is_settable, read_field, write_field};
|
||||
@@ -353,7 +355,7 @@ fn apply_move(
|
||||
|
||||
/// Every cited id must be declared in the document references.
|
||||
pub fn assert_declared(doc: &Value, ids: &[String]) -> Result<(), PipelineError> {
|
||||
let declared: Vec<&str> = list_at(doc, &top_collection_path(TargetKind::Reference))?
|
||||
let declared: HashSet<&str> = list_at(doc, &top_collection_path(TargetKind::Reference))?
|
||||
.iter()
|
||||
.filter_map(|reference| reference.get("id").and_then(Value::as_str))
|
||||
.collect();
|
||||
@@ -373,13 +375,8 @@ pub fn assert_declared(doc: &Value, ids: &[String]) -> Result<(), PipelineError>
|
||||
|
||||
/// `[...new Set(ids)]`: first occurrences, in order.
|
||||
fn distinct(ids: &[String]) -> Vec<&String> {
|
||||
let mut out: Vec<&String> = Vec::new();
|
||||
for id in ids {
|
||||
if !out.contains(&id) {
|
||||
out.push(id);
|
||||
}
|
||||
}
|
||||
out
|
||||
let mut seen: HashSet<&str> = HashSet::new();
|
||||
ids.iter().filter(|id| seen.insert(id.as_str())).collect()
|
||||
}
|
||||
|
||||
/// Appends citations in order, never twice on the same item.
|
||||
|
||||
@@ -367,16 +367,8 @@ fn non_negative_int<'de, D: serde::Deserializer<'de>>(d: D) -> Result<u64, D::Er
|
||||
}
|
||||
|
||||
fn duplicates<'a>(ids: impl Iterator<Item = &'a str>) -> Vec<&'a str> {
|
||||
let mut seen: Vec<&str> = Vec::new();
|
||||
let mut repeated = Vec::new();
|
||||
for id in ids {
|
||||
if seen.contains(&id) {
|
||||
repeated.push(id);
|
||||
} else {
|
||||
seen.push(id);
|
||||
}
|
||||
}
|
||||
repeated
|
||||
let mut seen: std::collections::HashSet<&str> = std::collections::HashSet::new();
|
||||
ids.filter(|id| !seen.insert(id)).collect()
|
||||
}
|
||||
|
||||
fn refuse(file: &str, error: impl std::fmt::Display) -> PipelineError {
|
||||
|
||||
@@ -309,7 +309,8 @@ fn synthetic_seed(document_json: &str) -> String {
|
||||
}
|
||||
|
||||
/// Cases the TypeScript accepted and the port refuses, declared in RUST.md §11.12.
|
||||
const DECLARED_REFUSALS: [&str; 3] = [
|
||||
const DECLARED_REFUSALS: [&str; 4] = [
|
||||
"declared-proto-segment-overwritten",
|
||||
"declared-set-without-value-drops-key",
|
||||
"declared-children-string-renamed-then-removed",
|
||||
"declared-children-object-then-added-to",
|
||||
@@ -386,7 +387,12 @@ fn the_synthetic_cases_match_the_typescript_or_are_refused_alike() {
|
||||
let error = outcome
|
||||
.err()
|
||||
.unwrap_or_else(|| panic!("{name}: the TypeScript refused, the port accepted"));
|
||||
if name == "declared-children-object-then-added-to" {
|
||||
if name == "declared-proto-segment-overwritten" {
|
||||
assert!(
|
||||
error.to_string().contains("reserved key \"__proto__\""),
|
||||
"{name}"
|
||||
);
|
||||
} else if name == "declared-children-object-then-added-to" {
|
||||
assert!(error.to_string().contains("that is not a list"), "{name}");
|
||||
} else if name == "declared-children-string-renamed-then-removed" {
|
||||
assert!(
|
||||
@@ -441,7 +447,7 @@ fn the_synthetic_cases_match_the_typescript_or_are_refused_alike() {
|
||||
"{name} seed"
|
||||
);
|
||||
}
|
||||
assert_eq!((compared, refused), (14, 66));
|
||||
assert_eq!((compared, refused), (14, 67));
|
||||
let reference_cases = frozen["reference_cases"].as_array().unwrap();
|
||||
assert_eq!(reference_cases.len(), 7);
|
||||
for case in reference_cases {
|
||||
@@ -517,17 +523,12 @@ fn mobility(document: &Value) -> &Value {
|
||||
/// strict, so the key cannot reach the document. The only accepted form is a key a later op
|
||||
/// replaces: the document is then the one the later op alone gives; one record traces the
|
||||
/// overwritten write. The TypeScript refused that form (structuredClone of the function read).
|
||||
/// `__proto__` is refused at the op (`a_proto_segment_is_refused_at_the_op`).
|
||||
#[test]
|
||||
fn an_inherited_member_name_is_a_plain_key_and_bypasses_nothing() {
|
||||
let renamed = json!({ "fr": "Mobilité", "en": "Mobility" });
|
||||
let set_name = json!({ "op": "set", "target": MOBILITY, "field": "name", "value": renamed });
|
||||
for member in [
|
||||
"constructor",
|
||||
"toString",
|
||||
"__proto__",
|
||||
"hasOwnProperty",
|
||||
"valueOf",
|
||||
] {
|
||||
for member in ["constructor", "toString", "hasOwnProperty", "valueOf"] {
|
||||
let field = format!("name.{member}");
|
||||
let write = json!({ "op": "set", "target": MOBILITY, "field": field, "value": "x" });
|
||||
let kept = mini_apply(json!([write])).unwrap_err();
|
||||
@@ -753,3 +754,25 @@ fn the_document_cannot_hold_more_values_than_the_bound() {
|
||||
let error = mini_apply(json!([op])).unwrap_err();
|
||||
assert!(error.contains("more than 200000 values"), "{error}");
|
||||
}
|
||||
|
||||
/// A `__proto__` segment had no faithful reading in the TypeScript (a prototype set, a
|
||||
/// polluted `Object.prototype`, or nothing written): the port refuses it at the op.
|
||||
#[test]
|
||||
fn a_proto_segment_is_refused_at_the_op() {
|
||||
let renamed = json!({ "fr": "a", "en": "b" });
|
||||
let set_name = json!({ "op": "set", "target": MOBILITY, "field": "name", "value": renamed });
|
||||
for field in [
|
||||
"name.__proto__",
|
||||
"name.__proto__.x",
|
||||
"description.__proto__.fr",
|
||||
] {
|
||||
let write = json!({ "op": "set", "target": MOBILITY, "field": field, "value": "x" });
|
||||
for ops in [json!([write]), json!([write, set_name])] {
|
||||
let error = mini_apply(ops).unwrap_err();
|
||||
assert!(
|
||||
error.contains("CHG-001 op 0: reserved key \"__proto__\""),
|
||||
"{field}: {error}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
{"baseline":{"schema_version":2,"species_id":"equine","name":{"fr":"Mini","en":"Mini"},"description":{},"scope_disclaimer":{},"attribution":{"authors":[],"license":null,"notes":null},"vertebral_config":{"regions":[{"code":"C","name":{"fr":"Cervicales","en":"Cervical"},"count":7},{"code":"T","name":{"fr":"Thoraciques","en":"Thoracic"},"count":18}]},"references":[],"categories":[{"id":"joints","name":{"fr":"Articulations","en":"Joints"},"color":null},{"id":"bones","name":{"fr":"Os","en":"Bones"},"color":null}],"views":[{"id":"main","kind":"main","name":{"fr":"Vue","en":"View"},"svg_asset_id":"3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8","view_box":null},{"id":"head","kind":"detail","parent_view_id":"main","name":{"fr":"Tête","en":"Head"},"svg_asset_id":null,"view_box":{"min_x":0,"min_y":0,"width":10,"height":10}}],"annotations":[{"id":"hip","kind":"point","view_id":"main","category_id":"joints","laterality":"bilateral","geometry":{"type":"circle","cx":1,"cy":2,"r":3},"name":{"fr":"Hanche","en":"Hip"},"finding_zone_id":"hip","vertebral_region_code":null},{"id":"neck","kind":"zone","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":{"fr":"Cou","en":"Neck"},"finding_zone_id":"neck","vertebral_region_code":"C"},{"id":"head-trigger","kind":"detail_trigger","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":null,"opens_view_id":"head"}],"finding_zones":[{"id":"hip","name":{"fr":"Hanche","en":"Hip"},"categories":[{"id":"hip-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"mobility","name":{"fr":"mobility fr","en":"mobility en"},"children":[{"id":"flexion","name":{"fr":"flexion fr","en":"flexion en"}},{"id":"extension","name":{"fr":"extension fr","en":"extension en"}}]},{"id":"pain","name":{"fr":"pain fr","en":"pain en"}}]},{"id":"anamnesis","kind":"anamnesis","name":{"fr":"Anamnèse","en":"Anamnesis"},"children":[{"id":"trauma","name":{"fr":"trauma fr","en":"trauma en"}}]}]},{"id":"neck","name":{"fr":"Cou","en":"Neck"},"categories":[{"id":"neck-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"stiffness","name":{"fr":"stiffness fr","en":"stiffness en"}},{"id":"vet","name":{"fr":"vet fr","en":"vet en"},"requires_vet_referral":true}]}]}]},"references_file":{"references":[{"id":"atlas","type":"book","citation":"Atlas of anatomy","url":null,"doi":null},{"id":"paper","type":"journal","citation":"A paper","url":null,"doi":"10.1/x"}]},"cases":[{"name":"set-fields","changes_file":{"changes":[{"id":"CHG-001","level":"A","kind":"technical","title":{"fr":"Essai","en":"Test"},"rationale":"Essai","reference_ids":[],"ops":[{"op":"set","target":"region:C","field":"count_range","value":{"min":6,"max":8}},{"op":"set","target":"document","field":"name.en","value":"Mini sheet"},{"op":"set","target":"document","field":"scope_disclaimer.fr","value":"Périmètre"},{"op":"set","target":"annotation:hip","field":"geometry","value":{"type":"circle","cx":1.5,"cy":2,"r":0.25}},{"op":"set","target":"annotation:neck","field":"color","value":"#aabbcc"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"description.en","value":"Flexion described"},{"op":"set","target":"finding:hip/anamnesis","field":"name.fr","value":"Anamnèse complète"},{"op":"set","target":"zone:neck","field":"name","value":{"fr":"Encolure","en":"Neck"}},{"op":"set","target":"view:head","field":"name.en","value":"Skull"},{"op":"set","target":"category:bones","field":"color","value":"#112233"},{"op":"set","target":"reference:paper","field":"doi","value":"10.1/y"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"review_status","value":"reviewed"}]}]},"ok":true,"document":"{\n \"schema_version\": 2,\n \"species_id\": \"equine\",\n \"name\": {\n \"fr\": \"Mini\",\n \"en\": \"Mini sheet\"\n },\n \"description\": {},\n \"scope_disclaimer\": {\n \"fr\": \"Périmètre\"\n },\n \"attribution\": {\n \"authors\": [],\n \"license\": null,\n \"notes\": null\n },\n \"vertebral_config\": {\n \"regions\": [\n {\n \"code\": \"C\",\n \"name\": {\n \"fr\": \"Cervicales\",\n \"en\": \"Cervical\"\n },\n \"count\": 7,\n \"count_range\": {\n \"min\": 6,\n \"max\": 8\n }\n },\n {\n \"code\": \"T\",\n \"name\": {\n \"fr\": \"Thoraciques\",\n \"en\": \"Thoracic\"\n },\n \"count\": 18\n }\n ]\n },\n \"references\": [\n {\n \"id\": \"atlas\",\n \"type\": \"book\",\n \"citation\": \"Atlas of anatomy\",\n \"url\": null,\n \"doi\": null\n },\n {\n \"id\": \"paper\",\n \"type\": \"journal\",\n \"citation\": \"A paper\",\n \"url\": null,\n \"doi\": \"10.1/y\"\n }\n ],\n \"categories\": [\n {\n \"id\": \"joints\",\n \"name\": {\n Line truncated
|
||||
{"baseline":{"schema_version":2,"species_id":"equine","name":{"fr":"Mini","en":"Mini"},"description":{},"scope_disclaimer":{},"attribution":{"authors":[],"license":null,"notes":null},"vertebral_config":{"regions":[{"code":"C","name":{"fr":"Cervicales","en":"Cervical"},"count":7},{"code":"T","name":{"fr":"Thoraciques","en":"Thoracic"},"count":18}]},"references":[],"categories":[{"id":"joints","name":{"fr":"Articulations","en":"Joints"},"color":null},{"id":"bones","name":{"fr":"Os","en":"Bones"},"color":null}],"views":[{"id":"main","kind":"main","name":{"fr":"Vue","en":"View"},"svg_asset_id":"3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8","view_box":null},{"id":"head","kind":"detail","parent_view_id":"main","name":{"fr":"Tête","en":"Head"},"svg_asset_id":null,"view_box":{"min_x":0,"min_y":0,"width":10,"height":10}}],"annotations":[{"id":"hip","kind":"point","view_id":"main","category_id":"joints","laterality":"bilateral","geometry":{"type":"circle","cx":1,"cy":2,"r":3},"name":{"fr":"Hanche","en":"Hip"},"finding_zone_id":"hip","vertebral_region_code":null},{"id":"neck","kind":"zone","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":{"fr":"Cou","en":"Neck"},"finding_zone_id":"neck","vertebral_region_code":"C"},{"id":"head-trigger","kind":"detail_trigger","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":null,"opens_view_id":"head"}],"finding_zones":[{"id":"hip","name":{"fr":"Hanche","en":"Hip"},"categories":[{"id":"hip-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"mobility","name":{"fr":"mobility fr","en":"mobility en"},"children":[{"id":"flexion","name":{"fr":"flexion fr","en":"flexion en"}},{"id":"extension","name":{"fr":"extension fr","en":"extension en"}}]},{"id":"pain","name":{"fr":"pain fr","en":"pain en"}}]},{"id":"anamnesis","kind":"anamnesis","name":{"fr":"Anamnèse","en":"Anamnesis"},"children":[{"id":"trauma","name":{"fr":"trauma fr","en":"trauma en"}}]}]},{"id":"neck","name":{"fr":"Cou","en":"Neck"},"categories":[{"id":"neck-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"stiffness","name":{"fr":"stiffness fr","en":"stiffness en"}},{"id":"vet","name":{"fr":"vet fr","en":"vet en"},"requires_vet_referral":true}]}]}]},"references_file":{"references":[{"id":"atlas","type":"book","citation":"Atlas of anatomy","url":null,"doi":null},{"id":"paper","type":"journal","citation":"A paper","url":null,"doi":"10.1/x"}]},"cases":[{"name":"set-fields","changes_file":{"changes":[{"id":"CHG-001","level":"A","kind":"technical","title":{"fr":"Essai","en":"Test"},"rationale":"Essai","reference_ids":[],"ops":[{"op":"set","target":"region:C","field":"count_range","value":{"min":6,"max":8}},{"op":"set","target":"document","field":"name.en","value":"Mini sheet"},{"op":"set","target":"document","field":"scope_disclaimer.fr","value":"Périmètre"},{"op":"set","target":"annotation:hip","field":"geometry","value":{"type":"circle","cx":1.5,"cy":2,"r":0.25}},{"op":"set","target":"annotation:neck","field":"color","value":"#aabbcc"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"description.en","value":"Flexion described"},{"op":"set","target":"finding:hip/anamnesis","field":"name.fr","value":"Anamnèse complète"},{"op":"set","target":"zone:neck","field":"name","value":{"fr":"Encolure","en":"Neck"}},{"op":"set","target":"view:head","field":"name.en","value":"Skull"},{"op":"set","target":"category:bones","field":"color","value":"#112233"},{"op":"set","target":"reference:paper","field":"doi","value":"10.1/y"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"review_status","value":"reviewed"}]}]},"ok":true,"document":"{\n \"schema_version\": 2,\n \"species_id\": \"equine\",\n \"name\": {\n \"fr\": \"Mini\",\n \"en\": \"Mini sheet\"\n },\n \"description\": {},\n \"scope_disclaimer\": {\n \"fr\": \"Périmètre\"\n },\n \"attribution\": {\n \"authors\": [],\n \"license\": null,\n \"notes\": null\n },\n \"vertebral_config\": {\n \"regions\": [\n {\n \"code\": \"C\",\n \"name\": {\n \"fr\": \"Cervicales\",\n \"en\": \"Cervical\"\n },\n \"count\": 7,\n \"count_range\": {\n \"min\": 6,\n \"max\": 8\n }\n },\n {\n \"code\": \"T\",\n \"name\": {\n \"fr\": \"Thoraciques\",\n \"en\": \"Thoracic\"\n },\n \"count\": 18\n }\n ]\n },\n \"references\": [\n {\n \"id\": \"atlas\",\n \"type\": \"book\",\n \"citation\": \"Atlas of anatomy\",\n \"url\": null,\n \"doi\": null\n },\n {\n \"id\": \"paper\",\n \"type\": \"journal\",\n \"citation\": \"A paper\",\n \"url\": null,\n \"doi\": \"10.1/y\"\n }\n ],\n \"categories\": [\n {\n \"id\": \"joints\",\n \"name\": {\n Line truncated
|
||||
@@ -564,6 +564,20 @@ const ACCEPTED: Case[] = [
|
||||
{ op: 'add', target: `${MOBILITY}/n1`, value: leaf('child') },
|
||||
]),
|
||||
},
|
||||
{
|
||||
// The TypeScript's assignment through `__proto__` ignored the string (no pollution: safe
|
||||
// in this process); the port refuses the segment at the op (declared).
|
||||
name: 'declared-proto-segment-overwritten',
|
||||
changes_file: one([
|
||||
{ op: 'set', target: MOBILITY, field: 'name.__proto__', value: 'x' },
|
||||
{
|
||||
op: 'set',
|
||||
target: MOBILITY,
|
||||
field: 'name',
|
||||
value: { fr: 'a', en: 'b' },
|
||||
},
|
||||
]),
|
||||
},
|
||||
{
|
||||
name: 'two-changes',
|
||||
changes_file: {
|
||||
|
||||
Reference in new issue
Block a user