fix(anatomy): R5.3 __proto__ field segments refused, seed identity guard over the whole seed tree, hashed id comparisons

This commit is contained in:
bchanot committed 2026-10-23 10:51:39 +02:00
1 parent 3ece1a64b1
commit f245b3ec25
7 files changed
+86 -33

No files matched your search

+24 -4
View File
@@ -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())))
})
}
+7
View File
@@ -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)
}
+5 -8
View File
@@ -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.
+2 -10
View File
@@ -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 {