From 9f11499f2c6072fc938fb7269527ef66426a45be Mon Sep 17 00:00:00 2001 From: bchanot Date: Fri, 23 Oct 2026 01:08:07 +0200 Subject: [PATCH] fix(anatomy): R5.3 final gates: NUL field segments refused, field path and document depth bounded, --out writes exclusive, .. / missing parent / seed destination refused --- crates/core/src/bin/import_equine.rs | 113 +++++++++++++++-------- crates/core/src/curation/draft/apply.rs | 31 +++++++ crates/core/src/curation/draft/fields.rs | 15 +++ crates/core/tests/curation_draft.rs | 93 +++++++++++++++++++ 4 files changed, 215 insertions(+), 37 deletions(-) diff --git a/crates/core/src/bin/import_equine.rs b/crates/core/src/bin/import_equine.rs index eb8d8e5d..95c4b94a 100644 --- a/crates/core/src/bin/import_equine.rs +++ b/crates/core/src/bin/import_equine.rs @@ -14,7 +14,9 @@ //! `report-compare`, this file is the only one of its tool allowed the file system. #![allow(clippy::disallowed_methods)] -use std::path::{Path, PathBuf}; +use std::fs::OpenOptions; +use std::io::Write; +use std::path::{Component, Path, PathBuf}; use std::{env, fs, process}; use faunosteo_core::curation::draft::{ @@ -179,47 +181,55 @@ fn check(folder: &Folder) -> Result, String> { Ok(problems) } -/// Claims a destination this tool may fill and returns its canonical path: a non-empty path -/// that is absent (created here, so nothing can appear between the check and the writes) or -/// an empty folder that is not a symbolic link. The canonical path is where the files land, -/// symbolic links of the parent folders resolved, and is what the success line prints. +/// Claims a destination this tool may fill and returns its canonical path. The typed path +/// must be non-empty, hold no `..`, have an existing parent folder, and name either nothing +/// (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 { + 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()); } + if typed.components().any(|c| c == Component::ParentDir) { + return Err(refuse("`..` is not accepted")); + } // Rebuilt from its components: a trailing `/` would make the system resolve a final // symbolic link before the check below could see it. let out: PathBuf = typed.components().collect(); - let out = out.as_path(); - match fs::symlink_metadata(out) { - Err(_) => { - if let Some(parent) = out.parent().filter(|p| !p.as_os_str().is_empty()) { - fs::create_dir_all(parent).map_err(|e| format!("{}: {e}", parent.display()))?; - } - fs::create_dir(out).map_err(|e| format!("{}: {e}, nothing written", out.display()))?; - } - Ok(metadata) if metadata.file_type().is_symlink() => { - return Err(format!( - "{}: a symbolic link, nothing written", - out.display() - )); - } - Ok(metadata) if !metadata.is_dir() => { - return Err(format!("{}: not a folder, nothing written", out.display())); + let parent = out.parent().filter(|p| !p.as_os_str().is_empty()); + let parent = match parent { + Some(parent) => { + fs::canonicalize(parent).map_err(|_| refuse("the parent folder does not exist"))? } + None => return Err(refuse("not a destination folder")), + }; + let seed = fs::canonicalize(SEED).map_err(|error| format!("{SEED}: {error}"))?; + if parent.starts_with(&seed) { + return Err(refuse("inside the seed folder")); + } + let name = out + .file_name() + .ok_or_else(|| format!("{}: not a destination folder", typed.display()))?; + let root = parent.join(name); + match fs::symlink_metadata(&root) { + Err(_) => fs::create_dir(&root).map_err(|error| refuse(&error.to_string()))?, + Ok(metadata) if metadata.file_type().is_symlink() => return Err(refuse("a symbolic link")), + Ok(metadata) if !metadata.is_dir() => return Err(refuse("not a folder")), Ok(_) => { let mut entries = - fs::read_dir(out).map_err(|error| format!("{}: {error}", out.display()))?; + fs::read_dir(&root).map_err(|error| format!("{}: {error}", root.display()))?; if entries.next().is_some() { - return Err(format!("{}: not empty, nothing written", out.display())); + return Err(refuse("not empty")); } } } - fs::canonicalize(out).map_err(|error| format!("{}: {error}", out.display())) + Ok(root) } /// Where a relative destination is typed from: `INIT_CWD` when pnpm set it to an absolute -/// path, else the current folder. +/// path, else the current folder (the repository root when the script is run directly: it +/// moves there first). fn base_dir() -> PathBuf { env::var_os("INIT_CWD") .map(PathBuf::from) @@ -228,22 +238,42 @@ fn base_dir() -> PathBuf { .unwrap_or_default() } -/// Writes every file under the claimed destination; returns it and the file count. -fn write(folders: &[Folder], out: &Path) -> Result<(PathBuf, usize), String> { - let root = claim_destination(out)?; +/// 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> { + let mut folder = root.to_path_buf(); + let components: Vec<_> = relative.components().collect(); + 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())), + } + } + let path = folder.join(file); + let mut handle = OpenOptions::new() + .write(true) + .create_new(true) + .open(&path) + .map_err(|error| format!("{}: {error}", path.display()))?; + handle + .write_all(bytes) + .map_err(|error| format!("{}: {error}", path.display())) +} + +/// Writes every file under the claimed destination; returns the file count. +fn write(folders: &[Folder], root: &Path) -> Result { let mut written = 0; for folder in folders { for (name, bytes) in &folder.files { - let path = root.join(folder.name).join(name); - if let Some(parent) = path.parent() { - fs::create_dir_all(parent) - .map_err(|error| format!("{}: {error}", parent.display()))?; - } - fs::write(&path, bytes).map_err(|error| format!("{}: {error}", path.display()))?; + write_exclusive(root, &Path::new(folder.name).join(name), bytes)?; written += 1; } } - Ok((root, written)) + Ok(written) } fn run(args: &[String]) -> Result { @@ -270,7 +300,9 @@ fn run(args: &[String]) -> Result { if dir.is_empty() { return Err("--out needs a destination path".to_owned()); } - let (root, written) = write(&folders, &base_dir().join(dir))?; + let root = claim_destination(&base_dir().join(dir))?; + println!("writing to {}", root.display()); + let written = write(&folders, &root)?; println!("{written} files written to {}", root.display()); Ok(0) } @@ -279,7 +311,14 @@ fn run(args: &[String]) -> Result { } fn main() { - let args: Vec = env::args().skip(1).collect(); + let args: Option> = env::args_os() + .skip(1) + .map(|a| a.into_string().ok()) + .collect(); + let Some(args) = args else { + eprintln!("an argument is not valid UTF-8"); + process::exit(2); + }; match run(&args) { Ok(code) => process::exit(code), Err(message) => { diff --git a/crates/core/src/curation/draft/apply.rs b/crates/core/src/curation/draft/apply.rs index b251c523..27eb537a 100644 --- a/crates/core/src/curation/draft/apply.rs +++ b/crates/core/src/curation/draft/apply.rs @@ -106,6 +106,7 @@ fn apply_change( for (index, op) in change.ops.iter().enumerate() { let mut recorder = Recorder::new(&summary, op, records, &mut next_touch); apply_op(doc, &mut recorder) + .and_then(|()| assert_depth(doc)) .map_err(|error| fail(format!("{} op {index}: {error}", change.id)))?; } if change.level.needs_confirmation() { @@ -115,6 +116,36 @@ fn apply_change( Ok(()) } +/// Nesting a valid sheet document never approaches (the equine document has 10 levels): a +/// resource bound checked after every op, so repeated adds, moves or deep values cannot grow +/// the document until a recursive walk overflows the stack. +pub const MAX_DOCUMENT_DEPTH: usize = 64; + +/// The nesting of `value`, walked with an explicit stack (no recursion). +fn depth_of(value: &Value) -> usize { + let mut deepest = 0; + let mut pending = vec![(value, 1)]; + while let Some((value, depth)) = pending.pop() { + let children: Box> = match value { + Value::Object(map) => Box::new(map.values()), + Value::Array(items) => Box::new(items.iter()), + _ => continue, + }; + deepest = deepest.max(depth); + pending.extend(children.map(|child| (child, depth + 1))); + } + deepest +} + +fn assert_depth(doc: &Value) -> Result<(), PipelineError> { + if depth_of(doc) > MAX_DOCUMENT_DEPTH { + return Err(fail(format!( + "the document nests more than {MAX_DOCUMENT_DEPTH} levels" + ))); + } + Ok(()) +} + fn describe(issues: &[SheetIssue]) -> String { issues .iter() diff --git a/crates/core/src/curation/draft/fields.rs b/crates/core/src/curation/draft/fields.rs index 941622c8..d1eb31b3 100644 --- a/crates/core/src/curation/draft/fields.rs +++ b/crates/core/src/curation/draft/fields.rs @@ -80,11 +80,26 @@ impl FieldKind { } } +/// More segments than any settable path needs (the deepest has two): a resource bound, so a +/// field path cannot nest the document without limit. +pub const MAX_FIELD_SEGMENTS: usize = 8; + +/// The segments of a dot path. A segment starting with NUL is reserved for the applier's +/// touch marks (`records::TOUCH_MARK`): written, it would be stripped with them and the +/// author's value lost, so it is refused (the TypeScript refused such a key at the gate). pub fn split_field(field: &str) -> Result, PipelineError> { let path: Vec<&str> = field.split('.').collect(); if path.iter().any(|key| key.is_empty()) { return Err(fail(format!("malformed field \"{field}\""))); } + if path.len() > MAX_FIELD_SEGMENTS { + return Err(fail(format!( + "field \"{field}\" has more than {MAX_FIELD_SEGMENTS} segments" + ))); + } + if let Some(key) = path.iter().find(|key| key.starts_with('\0')) { + return Err(fail(format!("reserved key {key:?} in field \"{field}\""))); + } Ok(path) } diff --git a/crates/core/tests/curation_draft.rs b/crates/core/tests/curation_draft.rs index 8e599e89..af3f1525 100644 --- a/crates/core/tests/curation_draft.rs +++ b/crates/core/tests/curation_draft.rs @@ -608,3 +608,96 @@ fn the_migration_map_replays_cascade_records_by_their_op_as_the_typescript_does( "should be hip-joint/hip-dysfunctions/motion" ); } + +// --------------------------------------------------------------------------------------------- +// Resource bounds and reserved names reached through a field path (final R5.3 gates). + +#[test] +fn a_reserved_or_unbounded_field_path_is_refused_without_writing() { + for field in [ + "name.\u{0}touched", + "description.\u{0}touched", + "name.\u{0}other", + ] { + let op = json!({ "op": "set", "target": MOBILITY, "field": field, "value": "author text" }); + let error = mini_apply(json!([op])).unwrap_err(); + assert!( + error.contains("CHG-001 op 0: reserved key"), + "{field}: {error}" + ); + } + // 20,000 segments overflowed the stack before the bound (security gate, MEDIUM-1). + let deep = format!("name{}", ".a".repeat(20_000)); + let op = json!({ "op": "set", "target": MOBILITY, "field": deep, "value": "x" }); + let error = mini_apply(json!([op])).unwrap_err(); + assert!(error.contains("more than 8 segments"), "{error}"); +} + +#[test] +fn the_document_cannot_be_nested_past_the_bound_by_repeated_adds() { + let mut ops = Vec::new(); + let mut parent = MOBILITY.to_owned(); + for level in 0..40 { + let id = format!("n{level}"); + ops.push(json!({ "op": "add", "target": parent, "value": { "id": id, "name": { "fr": "a", "en": "a" } } })); + parent = format!("{parent}/{id}"); + } + let error = mini_apply(Value::Array(ops)).unwrap_err(); + assert!(error.contains("nests more than 64 levels"), "{error}"); +} + +fn import_equine(args: &[&str]) -> (i32, String) { + let output = std::process::Command::new(env!("CARGO_BIN_EXE_import-equine")) + .args(args) + .env_remove("INIT_CWD") + .output() + .unwrap(); + let text = String::from_utf8_lossy(&output.stderr).into_owned(); + (output.status.code().unwrap_or(-1), text) +} + +/// Fixed fixtures under the test target's temporary folder: created once, never deleted, and +/// never written by the refusals checked here. +fn out_fixtures() -> std::path::PathBuf { + let base = std::path::Path::new(env!("CARGO_TARGET_TMPDIR")).join("import-equine-out"); + for folder in ["empty", "full"] { + std::fs::create_dir_all(base.join(folder)).unwrap(); + } + std::fs::write(base.join("full/keep.txt"), b"keep").unwrap(); + std::fs::write(base.join("file"), b"file").unwrap(); + let link = base.join("link"); + if std::fs::symlink_metadata(&link).is_err() { + std::os::unix::fs::symlink(base.join("empty"), &link).unwrap(); + } + base +} + +#[test] +fn out_refuses_every_unsafe_destination_and_writes_nothing() { + let base = out_fixtures(); + let at = |rest: &str| format!("{}/{rest}", base.display()); + let seed = std::fs::canonicalize(common::repo_path("supabase/seed/anatomical-sheets/equine")) + .unwrap() + .join("new-folder") + .display() + .to_string(); + let cases = [ + (at("link"), "a symbolic link"), + (at("link/"), "a symbolic link"), + (at("link/."), "a symbolic link"), + (at("file"), "not a folder"), + (at("full"), "not empty"), + (at("empty/../new"), "`..` is not accepted"), + (at("absent/new"), "the parent folder does not exist"), + (seed.clone(), "inside the seed folder"), + ]; + for (path, why) in &cases { + let (code, stderr) = import_equine(&["--out", path]); + assert_eq!(code, 2, "{path}: {stderr}"); + assert!(stderr.contains(why), "{path}: {stderr}"); + } + assert_eq!(std::fs::read_dir(base.join("empty")).unwrap().count(), 0); + assert_eq!(std::fs::read_dir(base.join("full")).unwrap().count(), 1); + assert!(std::fs::symlink_metadata(&seed).is_err()); + assert!(std::fs::symlink_metadata(base.join("absent")).is_err()); +}