fix(anatomy): R5.3 non-object children refused at add, reads confined to the seed, array length in the shape check, only run-created folders, seed guard tested in place

This commit is contained in:
bchanot committed 2026-10-23 10:11:51 +02:00
1 parent 42bbc48fcc
commit 86273b6206
6 files changed
+149 -21

No files matched your search

+52 -16
View File
@@ -61,17 +61,25 @@ struct Folder {
/// 1.1 MB): a resource cap on what the tool reads.
const MAX_FILE_BYTES: u64 = 16 * 1024 * 1024;
/// A regular file of the seed (never a symbolic link, a device or a pipe), read under the cap.
/// A regular file of the seed folder, read under the cap: its canonical path (every symbolic
/// link resolved, folders included) must lie in the canonical seed folder, so a link never
/// leads a read outside it; devices and pipes are refused. The repository is a single user's
/// working copy: a swap between the check and the read is not defended (RUST.md §11.13).
fn read(path: &Path) -> Result<Vec<u8>, String> {
let refuse = |why: &str| format!("{}: {why}", path.display());
let metadata = fs::symlink_metadata(path).map_err(|error| refuse(&error.to_string()))?;
let seed = fs::canonicalize(SEED).map_err(|error| format!("{SEED}: {error}"))?;
let canonical = fs::canonicalize(path).map_err(|error| refuse(&error.to_string()))?;
if !inside(&canonical, &seed) {
return Err(refuse("leads outside the seed folder"));
}
let metadata = fs::metadata(&canonical).map_err(|error| refuse(&error.to_string()))?;
if !metadata.file_type().is_file() {
return Err(refuse("not a regular file"));
}
if metadata.len() > MAX_FILE_BYTES {
return Err(refuse(&format!("larger than {MAX_FILE_BYTES} bytes")));
}
fs::read(path).map_err(|error| refuse(&error.to_string()))
fs::read(&canonical).map_err(|error| refuse(&error.to_string()))
}
/// `readFile(path, 'utf8')`: invalid UTF-8 becomes U+FFFD, as the TypeScript read the
@@ -201,7 +209,7 @@ fn check(folder: &Folder) -> Result<Vec<String>, String> {
/// (the folder is created here, its parent's symbolic links resolved) or an empty folder that
/// is not a symbolic link. A destination inside the seed folder is refused: the tool never
/// writes into the official seed. The writes that follow are exclusive (`write_exclusive`).
fn claim_destination(typed: &Path) -> Result<PathBuf, String> {
fn claim_destination(typed: &Path, seed: &Path) -> Result<PathBuf, String> {
let refuse = |why: &str| format!("{}: {why}, nothing written", typed.display());
if typed.as_os_str().is_empty() {
return Err("--out needs a destination path".to_owned());
@@ -219,8 +227,7 @@ fn claim_destination(typed: &Path) -> Result<PathBuf, String> {
}
None => return Err(refuse("not a destination folder")),
};
let seed = fs::canonicalize(SEED).map_err(|error| format!("{SEED}: {error}"))?;
if inside(&parent, &seed) {
if inside(&parent, seed) {
return Err(refuse("inside the seed folder"));
}
let name = out
@@ -258,10 +265,15 @@ fn base_dir() -> PathBuf {
.unwrap_or_default()
}
/// Creates `path` and the folders leading to it under `root`, each one new: an entry that
/// appeared since the claim, file, folder or symbolic link, is refused, never followed or
/// truncated (`create_dir`, `create_new`).
fn write_exclusive(root: &Path, relative: &Path, bytes: &[u8]) -> Result<(), String> {
/// Creates `relative` and the folders leading to it under `root`: every folder is one this run
/// created (`created`), every file new; an entry that appeared since the claim, file, folder
/// or symbolic link, is refused, never followed or truncated (`create_dir`, `create_new`).
fn write_exclusive(
root: &Path,
relative: &Path,
bytes: &[u8],
created: &mut Vec<PathBuf>,
) -> Result<(), String> {
// The claimed folder is checked again: a swap for a symbolic link since the claim is refused.
if !fs::symlink_metadata(root).is_ok_and(|m| m.file_type().is_dir()) {
return Err(format!("{}: no longer the claimed folder", root.display()));
@@ -277,11 +289,11 @@ fn write_exclusive(root: &Path, relative: &Path, bytes: &[u8]) -> Result<(), Str
let (file, folders) = components.split_last().ok_or("an empty file name")?;
for component in folders {
folder.push(component);
match fs::create_dir(&folder) {
Ok(()) => {}
Err(_) if fs::symlink_metadata(&folder).is_ok_and(|m| m.is_dir()) => {}
Err(error) => return Err(format!("{}: {error}", folder.display())),
if created.contains(&folder) {
continue;
}
fs::create_dir(&folder).map_err(|error| format!("{}: {error}", folder.display()))?;
created.push(folder.clone());
}
let path = folder.join(file);
let mut handle = OpenOptions::new()
@@ -297,9 +309,11 @@ fn write_exclusive(root: &Path, relative: &Path, bytes: &[u8]) -> Result<(), Str
/// Writes every file under the claimed destination; returns the file count.
fn write(folders: &[Folder], root: &Path) -> Result<usize, String> {
let mut written = 0;
let mut created = Vec::new();
for folder in folders {
for (name, bytes) in &folder.files {
write_exclusive(root, &Path::new(folder.name).join(name), bytes)?;
let relative = Path::new(folder.name).join(name);
write_exclusive(root, &relative, bytes, &mut created)?;
written += 1;
}
}
@@ -330,7 +344,8 @@ fn run(args: &[String]) -> Result<i32, String> {
if dir.is_empty() {
return Err("--out needs a destination path".to_owned());
}
let root = claim_destination(&base_dir().join(dir))?;
let seed = fs::canonicalize(SEED).map_err(|error| format!("{SEED}: {error}"))?;
let root = claim_destination(&base_dir().join(dir), &seed)?;
println!("writing to {}", root.display());
let written = write(&folders, &root)?;
println!("{written} files written to {}", root.display());
@@ -362,6 +377,27 @@ fn main() {
mod tests {
use super::*;
/// Fixed fixtures under the system's temporary folder: created once, never deleted, never
/// written by the refusals below.
fn fixture_seed() -> PathBuf {
let base = env::temp_dir().join("faunosteo-import-equine-claim");
fs::create_dir_all(base.join("seed/draft")).unwrap_or_else(|e| panic!("{e}"));
fs::canonicalize(base.join("seed")).unwrap_or_else(|e| panic!("{e}"))
}
#[test]
fn the_claim_refuses_a_destination_inside_the_seed_and_creates_nothing() {
let seed = fixture_seed();
for typed in [seed.join("new"), seed.join("draft/new")] {
let refused = claim_destination(&typed, &seed).err().unwrap_or_default();
assert!(refused.contains("inside the seed folder"), "{refused}");
assert!(fs::symlink_metadata(&typed).is_err());
}
let parent = seed.parent().map(Path::to_path_buf).unwrap_or_default();
let refused = claim_destination(&parent, &seed).err().unwrap_or_default();
assert!(refused.contains("not empty"), "{refused}");
}
#[test]
fn a_folder_under_the_seed_or_the_seed_itself_is_inside_it() {
let seed = Path::new("/repo/supabase/seed/anatomical-sheets/equine");
+26
View File
@@ -238,6 +238,31 @@ fn add_destination(doc: &mut Value, op: &AddOp) -> Result<Destination, PipelineE
})
}
/// Every entry of a `children` or `categories` list of an added value must be an object. The
/// TypeScript met a null or a primitive there inconsistently (a TypeError when it walked it,
/// silently skipped when a rename walked past it, refused by the gate when it stayed); the
/// port refuses it at the add (RUST.md §11.12). Walked with an explicit stack.
fn assert_object_children(value: &Value) -> Result<(), PipelineError> {
let mut pending = vec![value];
while let Some(item) = pending.pop() {
let Some(map) = item.as_object() else {
continue;
};
for key in ["children", "categories"] {
let Some(list) = map.get(key).and_then(Value::as_array) else {
continue;
};
if list.iter().any(|entry| !entry.is_object()) {
return Err(fail(format!(
"the added item holds a \"{key}\" entry that is not an object"
)));
}
pending.extend(list);
}
}
Ok(())
}
fn apply_add(
doc: &mut Value,
recorder: &mut Recorder<'_>,
@@ -246,6 +271,7 @@ fn apply_add(
let Some(value) = op.value.as_object() else {
return Err(fail("add needs an object value"));
};
assert_object_children(&op.value)?;
let dest = add_destination(doc, op)?;
let Some(id) = value.get(dest.key).and_then(Value::as_str) else {
return Err(fail(format!(
+6 -1
View File
@@ -44,7 +44,12 @@ fn first_mismatch(written: &Value, typed: &Value, path: &str) -> Option<String>
None => Some(format!("unexpected key at {here}")),
}
}),
(Value::Array(w), Value::Array(t)) if w.len() == t.len() => w
(Value::Array(w), Value::Array(t)) if w.len() != t.len() => Some(format!(
"expected {} items at {path}, received {}",
t.len(),
w.len()
)),
(Value::Array(w), Value::Array(t)) => w
.iter()
.zip(t)
.enumerate()
+19 -3
View File
@@ -309,7 +309,10 @@ 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; 1] = ["declared-set-without-value-drops-key"];
const DECLARED_REFUSALS: [&str; 2] = [
"declared-set-without-value-drops-key",
"declared-children-string-renamed-then-removed",
];
/// Cases the TypeScript refused and the port accepts, declared in RUST.md §11.12 (checked by
/// `an_inherited_member_name_is_a_plain_key_and_bypasses_nothing`).
@@ -331,6 +334,14 @@ fn has_set_without_value(changes_file: &Value) -> bool {
/// Declared family: an inherited `Object.prototype` member read at the end of a field path
/// made `structuredClone` throw at the op; the port writes a plain key the shape gate refuses.
fn assert_same_refusal(name: &str, typescript: &str, rust: &str) {
// Declared family: a non-object child of an added item made the TypeScript throw a
// TypeError where it walked it; the port refuses it at the add.
if typescript.contains("Cannot read properties of")
|| typescript.contains("Cannot create property")
{
assert!(rust.contains("that is not an object"), "{name}: {rust}");
return;
}
if typescript.ends_with("could not be cloned.") {
assert!(
rust.starts_with("curated document is not shape-valid"),
@@ -374,7 +385,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 has_set_without_value(&case["changes_file"]) {
if name == "declared-children-string-renamed-then-removed" {
assert!(
error.to_string().contains("that is not an object"),
"{name}"
);
} else if has_set_without_value(&case["changes_file"]) {
// The declared family: refused at parse, whatever the TypeScript made of it.
assert!(
error.to_string().contains("missing field `value`"),
@@ -422,7 +438,7 @@ fn the_synthetic_cases_match_the_typescript_or_are_refused_alike() {
"{name} seed"
);
}
assert_eq!((compared, refused), (11, 62));
assert_eq!((compared, refused), (11, 65));
let reference_cases = frozen["reference_cases"].as_array().unwrap();
assert_eq!(reference_cases.len(), 7);
for case in reference_cases {
@@ -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
@@ -491,6 +491,20 @@ const ACCEPTED: Case[] = [
{ op: 'rename', target: 'reference:atlas', id: 'atlas-2' },
]),
},
{
// The TypeScript accepted it (the rename's citation walk skipped the string, the remove
// took it away); the port refuses a non-object child at the add (declared).
name: 'declared-children-string-renamed-then-removed',
changes_file: one([
{
op: 'add',
target: MOBILITY,
value: { id: 'n1', name: { fr: 'a', en: 'b' }, children: ['s'] },
},
{ op: 'rename', target: 'reference:atlas', id: 'atlas2' },
{ op: 'remove', target: `${MOBILITY}/n1` },
]),
},
{
name: 'two-changes',
changes_file: {
@@ -523,6 +537,37 @@ const ACCEPTED: Case[] = [
]
const REFUSED: Case[] = [
// A null or a primitive among the children of an added item (final R5.3 gates): the
// TypeScript threw where it walked it; the port refuses it at the add.
{
name: 'children-null-then-removed',
changes_file: one([
{
op: 'add',
target: MOBILITY,
value: { id: 'n1', name: { fr: 'a', en: 'b' }, children: [null] },
},
{ op: 'remove', target: `${MOBILITY}/n1` },
]),
},
{
name: 'children-string-marked-then-removed',
changes_file: {
changes: [
change(
[
{
op: 'add',
target: MOBILITY,
value: { id: 'n1', name: { fr: 'a', en: 'b' }, children: ['s'] },
},
],
{ level: 'B' },
),
change([{ op: 'remove', target: `${MOBILITY}/n1` }], { id: 'CHG-002' }),
],
},
},
// serde's derives read a struct from an array and a unit variant from a one-key map; Zod
// refused both (final R5.3 gates, 2026-10-23).
{