fix(anatomy): R5.3 authored values read as JSON.parse does (key order, big integers), non-list children refused, document value bound, --check and seed guard by identity
This commit is contained in:
1 parent
6c0fe88725
commit
07d63076d3
10 files changed
+222
-23
No files matched your search
@@ -181,6 +181,12 @@ fn listed(dir: &Path, prefix: &str, out: &mut Vec<String>) -> Result<(), String>
|
||||
/// draft are reported, not counted.
|
||||
fn check(folder: &Folder) -> Result<Vec<String>, String> {
|
||||
let target = Path::new(SEED).join(folder.name);
|
||||
// The generated folder itself must lie in the seed: a link there is never listed through.
|
||||
let seed = fs::canonicalize(SEED).map_err(|error| format!("{SEED}: {error}"))?;
|
||||
let canonical = fs::canonicalize(&target).map_err(|e| format!("{}: {e}", target.display()))?;
|
||||
if canonical != seed.join(folder.name) {
|
||||
return Ok(vec![format!("{}: not a folder of the seed", folder.name)]);
|
||||
}
|
||||
let mut problems = Vec::new();
|
||||
for (name, bytes) in &folder.files {
|
||||
match read(&target.join(name)) {
|
||||
@@ -227,7 +233,7 @@ fn claim_destination(typed: &Path, seed: &Path) -> Result<PathBuf, String> {
|
||||
}
|
||||
None => return Err(refuse("not a destination folder")),
|
||||
};
|
||||
if inside(&parent, seed) {
|
||||
if inside(&parent, seed) || inside_by_identity(&parent, seed) {
|
||||
return Err(refuse("inside the seed folder"));
|
||||
}
|
||||
let name = out
|
||||
@@ -254,6 +260,18 @@ 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.
|
||||
fn inside_by_identity(folder: &Path, seed: &Path) -> bool {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let Ok(seed) = fs::metadata(seed) else {
|
||||
return true;
|
||||
};
|
||||
folder.ancestors().any(|ancestor| {
|
||||
fs::metadata(ancestor).is_ok_and(|m| m.dev() == seed.dev() && m.ino() == seed.ino())
|
||||
})
|
||||
}
|
||||
|
||||
/// Where a relative destination is typed from: `INIT_CWD` when pnpm set it to an absolute
|
||||
/// path, else the current folder (the repository root when the script is run directly: it
|
||||
/// moves there first).
|
||||
@@ -377,10 +395,11 @@ fn main() {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Fixed fixtures under the system's temporary folder: created once, never deleted, never
|
||||
/// written by the refusals below.
|
||||
/// Fixed fixtures under the workspace's target folder (private to the checkout): created
|
||||
/// once, never deleted, never written by the refusals below.
|
||||
fn fixture_seed() -> PathBuf {
|
||||
let base = env::temp_dir().join("faunosteo-import-equine-claim");
|
||||
let base = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../../target/tmp"))
|
||||
.join("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}"))
|
||||
}
|
||||
|
||||
@@ -122,11 +122,21 @@ fn apply_change(
|
||||
/// 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;
|
||||
/// Values a valid sheet document never approaches (the equine document holds 6,374): a
|
||||
/// resource bound checked with the depth, so the document, and the work done after each op,
|
||||
/// cannot grow without limit.
|
||||
pub const MAX_DOCUMENT_VALUES: usize = 200_000;
|
||||
|
||||
/// The nesting and the number of values of `value`, walked with an explicit stack (no
|
||||
/// recursion); the walk stops counting past the value bound.
|
||||
fn measure(value: &Value) -> (usize, usize) {
|
||||
let (mut deepest, mut count) = (0, 0);
|
||||
let mut pending = vec![(value, 1)];
|
||||
while let Some((value, depth)) = pending.pop() {
|
||||
count += 1;
|
||||
if count > MAX_DOCUMENT_VALUES {
|
||||
break;
|
||||
}
|
||||
let children: Box<dyn Iterator<Item = &Value>> = match value {
|
||||
Value::Object(map) => Box::new(map.values()),
|
||||
Value::Array(items) => Box::new(items.iter()),
|
||||
@@ -135,11 +145,17 @@ fn depth_of(value: &Value) -> usize {
|
||||
deepest = deepest.max(depth);
|
||||
pending.extend(children.map(|child| (child, depth + 1)));
|
||||
}
|
||||
deepest
|
||||
(deepest, count)
|
||||
}
|
||||
|
||||
fn assert_depth(doc: &Value) -> Result<(), PipelineError> {
|
||||
if depth_of(doc) > MAX_DOCUMENT_DEPTH {
|
||||
let (depth, count) = measure(doc);
|
||||
if count > MAX_DOCUMENT_VALUES {
|
||||
return Err(fail(format!(
|
||||
"the document holds more than {MAX_DOCUMENT_VALUES} values"
|
||||
)));
|
||||
}
|
||||
if depth > MAX_DOCUMENT_DEPTH {
|
||||
return Err(fail(format!(
|
||||
"the document nests more than {MAX_DOCUMENT_DEPTH} levels"
|
||||
)));
|
||||
|
||||
@@ -6,6 +6,7 @@ use serde_json::{Map, Value};
|
||||
|
||||
use super::targets::{Item, ParsedTarget, TargetKind, fail};
|
||||
use crate::curation::PipelineError;
|
||||
use crate::curation::text::js_insert;
|
||||
|
||||
/// Item kind of the settable table; a finding is a category or a node.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -129,13 +130,13 @@ pub fn write_field(item: &mut Item, field: &str, value: &Value) -> Result<(), Pi
|
||||
let mut holder = item;
|
||||
for key in holders {
|
||||
if !holder.get(*key).is_some_and(Value::is_object) {
|
||||
holder.insert((*key).to_owned(), Value::Object(Map::new()));
|
||||
js_insert(holder, key, Value::Object(Map::new()));
|
||||
}
|
||||
holder = holder
|
||||
.get_mut(*key)
|
||||
.and_then(Value::as_object_mut)
|
||||
.ok_or_else(|| fail("field holder vanished"))?;
|
||||
}
|
||||
holder.insert((*last).to_owned(), value.clone());
|
||||
js_insert(holder, last, value.clone());
|
||||
Ok(())
|
||||
}
|
||||
@@ -238,7 +238,9 @@ 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
|
||||
/// A `children` or `categories` of an added value must be a list (a later add would otherwise
|
||||
/// replace the author's value by `[]`, as the TypeScript's `childrenOf` did), and each entry 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.
|
||||
@@ -249,8 +251,14 @@ fn assert_object_children(value: &Value) -> Result<(), PipelineError> {
|
||||
continue;
|
||||
};
|
||||
for key in ["children", "categories"] {
|
||||
let Some(list) = map.get(key).and_then(Value::as_array) else {
|
||||
continue;
|
||||
let list = match map.get(key) {
|
||||
None => continue,
|
||||
Some(Value::Array(list)) => list,
|
||||
Some(_) => {
|
||||
return Err(fail(format!(
|
||||
"the added item holds a \"{key}\" that is not a list"
|
||||
)));
|
||||
}
|
||||
};
|
||||
if list.iter().any(|entry| !entry.is_object()) {
|
||||
return Err(fail(format!(
|
||||
|
||||
@@ -14,6 +14,7 @@ use serde_json::{Map, Value};
|
||||
|
||||
use super::shape::assert_same_shape;
|
||||
use crate::curation::PipelineError;
|
||||
use crate::curation::text::js_normalize;
|
||||
use crate::domain::js;
|
||||
use crate::domain::sheet::Reference;
|
||||
|
||||
@@ -383,9 +384,12 @@ fn refuse(file: &str, error: impl std::fmt::Display) -> PipelineError {
|
||||
}
|
||||
|
||||
/// `JSON.parse`: a repeated key keeps its first position and its last value (the parsed
|
||||
/// value is read into the typed shape afterwards, so duplicates never reach serde's derive).
|
||||
/// value is read into the typed shape afterwards, so duplicates never reach serde's derive),
|
||||
/// array index keys come first, integers beyond 2^53 are rounded (`js_normalize`).
|
||||
fn json_parse(file: &str, text: &str) -> Result<Value, PipelineError> {
|
||||
js::parse_json(text).map_err(|error| refuse(file, error))
|
||||
let mut value = js::parse_json(text).map_err(|error| refuse(file, error))?;
|
||||
js_normalize(&mut value);
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
/// A key starting with NUL is reserved for the applier's transient touch marks
|
||||
|
||||
@@ -240,11 +240,17 @@ fn with(path: &[Step], step: Step) -> Path {
|
||||
}
|
||||
|
||||
/// `childrenOf`: children of a finding item; a lean leaf gets an empty list on demand (the key
|
||||
/// is appended to the item, as the TypeScript did).
|
||||
/// is appended to the item, as the TypeScript did); a present value that is not a list is
|
||||
/// refused, never replaced.
|
||||
pub fn children_path(doc: &mut Value, item: &[Step]) -> Result<Path, PipelineError> {
|
||||
let holder = item_at_mut(doc, item)?;
|
||||
if !holder.get("children").is_some_and(Value::is_array) {
|
||||
holder.insert("children".into(), Value::Array(Vec::new()));
|
||||
match holder.get("children") {
|
||||
None => {
|
||||
holder.insert("children".into(), Value::Array(Vec::new()));
|
||||
}
|
||||
Some(Value::Array(_)) => {}
|
||||
// Never replaced: the author's value would be lost (refused at the add already).
|
||||
Some(_) => return Err(fail("\"children\" is not a list")),
|
||||
}
|
||||
Ok(with(item, Step::Key("children")))
|
||||
}
|
||||
|
||||
@@ -295,6 +295,50 @@ pub fn js_object<I: IntoIterator<Item = (String, Value)>>(entries: I) -> Map<Str
|
||||
sorted
|
||||
}
|
||||
|
||||
/// `object[key] = value` on an object JavaScript holds: an existing key keeps its place, a new
|
||||
/// array index key goes among the leading index keys in ascending order, any other key last.
|
||||
pub fn js_insert(map: &mut Map<String, Value>, key: &str, value: Value) {
|
||||
if map.contains_key(key) {
|
||||
map.insert(key.to_owned(), value);
|
||||
return;
|
||||
}
|
||||
let Some(index) = array_index(key) else {
|
||||
map.insert(key.to_owned(), value);
|
||||
return;
|
||||
};
|
||||
let position = map
|
||||
.keys()
|
||||
.take_while(|k| array_index(k).is_some_and(|other| other < index))
|
||||
.count();
|
||||
map.shift_insert(position, key.to_owned(), value);
|
||||
}
|
||||
|
||||
/// What `JSON.parse` makes of a parsed value: every object in `Object.keys` order (array index
|
||||
/// keys first), every integer beyond 2^53 rounded to the nearest double, as a JavaScript number
|
||||
/// holds it. Recursion bounded by the parser's depth limit (128).
|
||||
pub fn js_normalize(value: &mut Value) {
|
||||
match value {
|
||||
Value::Object(map) => {
|
||||
let ordered = js_object(std::mem::take(map));
|
||||
*map = ordered;
|
||||
map.values_mut().for_each(js_normalize);
|
||||
}
|
||||
Value::Array(items) => items.iter_mut().for_each(js_normalize),
|
||||
Value::Number(number) if !number.is_f64() => {
|
||||
let exact = number
|
||||
.as_i64()
|
||||
.map(i128::from)
|
||||
.or_else(|| number.as_u64().map(i128::from));
|
||||
if exact.is_some_and(|n| n.unsigned_abs() > 1u128 << 53)
|
||||
&& let Some(rounded) = number.as_f64().and_then(serde_json::Number::from_f64)
|
||||
{
|
||||
*number = rounded;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// `Math.round`: the nearest integer, a half rounding toward +∞; −0 for −0 and for values in
|
||||
/// [−0.5, 0).
|
||||
pub fn js_round(value: f64) -> f64 {
|
||||
|
||||
Reference in new issue
Block a user