fix(anatomy): R5.3 final gates: NUL field segments refused, field path and document depth bounded, --out writes exclusive, .. / missing parent / seed destination refused

This commit is contained in:
bchanot committed 2026-10-23 01:08:07 +02:00
1 parent 865490512e
commit 9f11499f2c
4 files changed
+215 -37

No files matched your search

+93
View File
@@ -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());
}