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

+31
View File
@@ -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<dyn Iterator<Item = &Value>> = 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()
+15
View File
@@ -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<Vec<&str>, 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)
}