From 0579ee5e3596fa40c73cc44130b70431b7ee9a70 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Mon, 16 Nov 2026 09:20:24 +1100 Subject: [PATCH 1/6] refactor(stack-encrypt): one definition of the JSON operators' inputs, compiled without the dynamic feature The JSON operators' inputs, their checks and the derivation of their operands lived in the dynamic interface, behind its feature, so a typed query could not share them. They move to json::inputs and json::predicate, which build typed operands (Derived) instead of bound bytes. The dynamic interface reads a binding's frames into the same inputs, in the same order, and writes the typed operand as the bytes it always bound. dynamic::Error and Reason move to a module compiled without the feature, so the core refuses an input with the reason a binding has always seen. JsonElementsQuery and JsonPredicateQuery are the typed forms of the element and predicate operands. No operand byte, refusal or reason changes. Refs #1459 --- .../stack-encrypt/src/dynamic/json_query.rs | 490 ++------------- packages/stack-encrypt/src/dynamic/mod.rs | 586 +---------------- packages/stack-encrypt/src/json/inputs.rs | 595 ++++++++++++++++++ packages/stack-encrypt/src/json/mod.rs | 7 +- packages/stack-encrypt/src/json/path.rs | 2 - .../json_predicate.rs => json/predicate.rs} | 148 +++-- packages/stack-encrypt/src/json/query.rs | 62 ++ packages/stack-encrypt/src/lib.rs | 1 + packages/stack-encrypt/src/refusal.rs | 595 ++++++++++++++++++ 9 files changed, 1393 insertions(+), 1093 deletions(-) create mode 100644 packages/stack-encrypt/src/json/inputs.rs rename packages/stack-encrypt/src/{dynamic/json_predicate.rs => json/predicate.rs} (94%) create mode 100644 packages/stack-encrypt/src/refusal.rs diff --git a/packages/stack-encrypt/src/dynamic/json_query.rs b/packages/stack-encrypt/src/dynamic/json_query.rs index f5b3928cd8..a54ec8e27b 100644 --- a/packages/stack-encrypt/src/dynamic/json_query.rs +++ b/packages/stack-encrypt/src/dynamic/json_query.rs @@ -204,13 +204,13 @@ use zeroize::Zeroizing; use super::query::{Op, Operand}; use super::{Error, Reason, ValueFrame}; -use crate::json::{ - parse_text, wipe_value, JsonLeafQuery, JsonNode, JsonOptions, JsonPath, JsonPlaintext, - JsonQuery, JsonSubtreeKey, +pub(super) use crate::json::inputs::Under; +use crate::json::inputs::{ + elements_base, json_reason, leaf, member_path, node_path, ordered_path, path, scalar, + selector_path, wildcard_bound, Bounds, Compare, Derived, Input, Parsed, Predicate, }; -use crate::sem::TermError; -use crate::target::{AeadContext, CallerContext, DeclaredContext, Index, Json, Owned}; -use crate::{ContextPiece, DecryptFrom, KeysetCipher, Label, NonEmpty, Pending}; +use crate::json::{parse_text, JsonNode, JsonOptions, JsonPath, JsonPlaintext, JsonSubtreeKey}; +use crate::{DecryptFrom, KeysetCipher, Pending}; /// The SQL type a JSON operand binds as. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -310,20 +310,10 @@ impl<'de> Deserialize<'de> for PathText { } } -/// A document parsed from an input, wiped when it drops. -#[derive(PartialEq)] -pub(super) struct Parsed(pub(super) Value); - -impl Drop for Parsed { - fn drop(&mut self) { - wipe_value(&mut self.0); - } -} - impl JsonText { pub(super) fn parse(&self) -> Result { parse_text(&self.0) - .map(Parsed) + .map(Parsed::of) .map_err(|error| Error::Source { field: None, reason: json_reason(error), @@ -331,16 +321,6 @@ impl JsonText { } } -/// The reason a JSON text refusal is reported with. -pub(super) fn json_reason(error: crate::json::JsonTextError) -> Reason { - use crate::json::JsonTextError as E; - match error { - E::Syntax { .. } => Reason::JsonSyntax, - E::DuplicateKey { .. } => Reason::JsonDuplicateKey, - E::IntegerRange => Reason::JsonIntegerRange, - } -} - #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct DocumentInput { @@ -408,47 +388,6 @@ struct MatchInput { wildcard_bound: Option, } -/// How a predicate of `element_match` compares its field with one value. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(super) enum Compare { - Eq, - Lt, - Lte, - Gt, - Gte, -} - -/// A predicate's bounds, as its input gives them. -#[derive(PartialEq)] -pub(super) enum Bounds { - /// One value, compared by the predicate's `op`. - One(Compare, Parsed), - /// `between`: the low bound then the high, both inclusive. - Between(Parsed, Parsed), -} - -/// One predicate of `element_match`, checked: the full path of the -/// element's field, which the field orders, and its bounds. -#[derive(PartialEq)] -pub(super) struct Predicate { - pub(super) path: JsonPath, - pub(super) bounds: Bounds, -} - -/// An operator's input, read from its frame and checked: everything -/// [`derive`] would refuse before it derives anything. `match`'s is its -/// predicate compiled to a tree of the other operators' inputs. -#[derive(PartialEq)] -pub(super) enum Input { - Document(Parsed), - At(JsonPath, Parsed), - Between(JsonPath, Parsed, Parsed), - Path(JsonPath), - Read(JsonPath, u32), - Elements(u32, Vec), - Predicate(Box), -} - fn read_frame Deserialize<'de>>(input: &ValueFrame) -> Result { vitaminc_aead_frame::serde::from_slice(input.risky_ref()).map_err(|_| Error::Source { field: None, @@ -456,59 +395,25 @@ fn read_frame Deserialize<'de>>(input: &ValueFrame) -> Result Result { - let path = - JsonPath::parse(text).map_err(|error| Error::Cipher(TermError::JsonPath(error).into()))?; - if path.segments().len() > crate::json::MAX_DEPTH { - return Err(term(TermError::JsonTooDeep)); - } - Ok(path) -} - -/// A path whose operand is a selector, which reads a position as the -/// wildcard. -fn selector_path(text: &str) -> Result { - let path = path(text)?; - if path.names_a_position() { - return Err(Error::bad_source(Reason::JsonPathPosition)); - } - Ok(path) -} - -/// A path that names one node: no wildcard. -fn node_path(text: &str) -> Result { - let path = path(text)?; - // `is_positional`: every array step names a position, so the path - // has no wildcard and names at most one node. - if path.is_positional() { - Ok(path) - } else { - Err(Error::bad_source(Reason::JsonPathWildcard)) - } -} - -fn term(error: TermError) -> Error { - Error::Cipher(error.into()) -} - -/// Read and check an operator's input against the field's options. +/// Read and check an operator's input against the field's options: the +/// frame read into the input the operator takes ([`Input`]), checked with +/// the rules a field marker's typed JSON queries apply, and in the order a +/// binding has always met them. fn read_input(op: Op, options: &JsonOptions, input: &ValueFrame) -> Result { match op { Op::Contains | Op::ContainedBy => Ok(Input::Document( read_frame::(input)?.json.parse()?, )), Op::EqAt => read_eq_at(input), - Op::LtAt | Op::LteAt | Op::GtAt | Op::GteAt => read_ordering(options, input), + Op::LtAt => read_ordering(options, Compare::Lt, input), + Op::LteAt => read_ordering(options, Compare::Lte, input), + Op::GtAt => read_ordering(options, Compare::Gt, input), + Op::GteAt => read_ordering(options, Compare::Gte, input), Op::BetweenAt => read_between(options, input), - Op::Selector => Ok(Input::Path(selector_path( + Op::Selector => Ok(Input::Selector(selector_path( &read_frame::(input)?.path, )?)), - Op::Children | Op::ChildCount => Ok(Input::Path(node_path( + Op::Children | Op::ChildCount => Ok(Input::Node(node_path( &read_frame::(input)?.path, )?)), Op::Subtree => { @@ -524,15 +429,6 @@ fn read_input(op: Op, options: &JsonOptions, input: &ValueFrame) -> Result) -> Result { - match bound.unwrap_or(JsonSubtreeKey::DEFAULT_BOUND) { - 0 => Err(Error::bad_source(Reason::JsonWildcardBoundZero)), - bound => Ok(bound), - } -} - /// `match`'s input: a jsonpath predicate, its variables as one JSON object /// (the `vars` jsonb `jsonb_path_match` takes, `{}` when absent) and the /// bound its element-scoped leaves are read to. The predicate is compiled @@ -542,29 +438,21 @@ fn read_match(options: &JsonOptions, input: &ValueFrame) -> Result let bound = wildcard_bound(input.wildcard_bound)?; let vars = match &input.vars { Some(vars) => vars.parse()?, - None => Parsed(Value::Object(serde_json::Map::new())), + None => Parsed::of(Value::Object(serde_json::Map::new())), }; if !vars.0.is_object() { return Err(Error::bad_source(Reason::JsonInput)); } let text = std::str::from_utf8(&input.predicate.0) .map_err(|_| Error::bad_source(Reason::JsonInput))?; - Ok(Input::Predicate(Box::new(super::json_predicate::compile( - text, &vars.0, options, bound, - )?))) + Input::predicate(options, text, &vars.0, Some(bound)) } /// `element_match`'s input: an array's elements, by a path with a wildcard /// and no position, and one or more predicates on their fields. fn read_elements(options: &JsonOptions, input: &ValueFrame) -> Result { let elements: ElementsInput = read_frame(input)?; - let base = path(&elements.path)?; - if base.is_positional() { - return Err(Error::bad_source(Reason::JsonPathNoWildcard)); - } - if base.names_a_position() { - return Err(term(TermError::JsonOrderingAtPosition)); - } + let base = elements_base(&elements.path)?; let bound = wildcard_bound(elements.wildcard_bound)?; if elements.predicates.is_empty() { return Err(Error::bad_source(Reason::JsonNoPredicates)); @@ -574,7 +462,7 @@ fn read_elements(options: &JsonOptions, input: &ValueFrame) -> Result, _>>()?; - Ok(Input::Elements(bound, predicates)) + Input::elements(Some(bound), predicates) } /// One predicate of `element_match`: its member's full path below `base`, @@ -607,67 +495,26 @@ fn read_predicate( Ok(Predicate { path, bounds }) } -/// A member's full path: `base` and then the member's steps, read as a -/// path relative to an element (`name`, `address.city`, `["a b"]`). A -/// member names one field of each element, so it has no wildcard of its -/// own, and the full path is an ordering's: no position, and ordered. A -/// member that does not parse is refused at an offset into the member's -/// own text. -fn member_path(options: &JsonOptions, base: &JsonPath, member: &str) -> Result { - let prefix = if member.starts_with('[') { "$" } else { "$." }; - // Sized before it is written, and wiped: the member names keys. - let mut rooted = Zeroizing::new(String::with_capacity(prefix.len() + member.len())); - rooted.push_str(prefix); - rooted.push_str(member); - let relative = JsonPath::parse(&rooted).map_err(|error| { - let error = crate::json::JsonPathError { - at: error.at.saturating_sub(prefix.len()), - ..error - }; - Error::Cipher(TermError::JsonPath(error).into()) - })?; - member_of(options, base, relative) -} - -/// A member's full path, from the steps already parsed: the checks -/// [`member_path`] makes past parsing, which `match` makes of the members -/// of an element-scoped filter. -pub(super) fn member_of( - options: &JsonOptions, - base: &JsonPath, - relative: JsonPath, -) -> Result { - if !relative.is_positional() { - return Err(Error::bad_source(Reason::JsonPathWildcard)); - } - let full = relative - .segments() - .iter() - .fold(base.clone(), |at, segment| at.then(segment.clone())); - if full.segments().len() > crate::json::MAX_DEPTH { - return Err(term(TermError::JsonTooDeep)); - } - ordered_path_of(options, full) -} - /// `eq_at`'s input: a scalar at a path read through a selector. fn read_eq_at(input: &ValueFrame) -> Result { let at: AtInput = read_frame(input)?; let path = selector_path(&at.path)?; let value = at.json.parse()?; - if matches!(value.0, Value::Object(_) | Value::Array(_)) { - return Err(term(TermError::JsonValueNotScalar)); - } - Ok(Input::At(path, value)) + scalar(&value.0)?; + Ok(Input::EqAt(path, value)) } /// An ordering's input: a leaf the index orders at an ordered path. -fn read_ordering(options: &JsonOptions, input: &ValueFrame) -> Result { +fn read_ordering( + options: &JsonOptions, + compare: Compare, + input: &ValueFrame, +) -> Result { let at: AtInput = read_frame(input)?; let path = ordered_path(options, &at.path)?; let value = at.json.parse()?; leaf(&value.0)?; - Ok(Input::At(path, value)) + Ok(Input::Ordering(compare, path, value)) } /// `between_at`'s input: two leaves the index orders at an ordered path. @@ -680,117 +527,23 @@ fn read_between(options: &JsonOptions, input: &ValueFrame) -> Result Result { - ordered_path_of(options, path(text)?) -} - -/// [`ordered_path`] of a path already parsed. -pub(super) fn ordered_path_of(options: &JsonOptions, path: JsonPath) -> Result { - if path.names_a_position() { - Err(term(TermError::JsonOrderingAtPosition)) - } else if options.orders(&path) { - Ok(path) - } else { - Err(term(TermError::JsonPathNotOrdered)) - } -} - -/// A leaf the index orders: a number or a string. -pub(super) fn leaf(value: &Value) -> Result<(), Error> { - match value { - Value::Number(_) | Value::String(_) => Ok(()), - _ => Err(term(TermError::JsonLeafNotOrdered)), - } -} - -/// Check a JSON operator's input without a cipher: what [`derive`] -/// refuses from the input alone (its shape, its paths and values, a path -/// deeper than a document). The limits a subtree read's key hits as it is -/// built, a wildcard expanding too wide or a path too deep for its -/// tokens (`JsonWildcardTooWide`, `JsonTooWide`, `JsonTooDeep`), are -/// [`derive`]'s, after the keyset is resolved. +/// Check a JSON operator's input without a cipher: what [`derive`] refuses +/// from the input alone (its shape, its paths and values, a path deeper +/// than a document). The limits a subtree read's key hits as it is built, +/// a wildcard expanding too wide or a path too deep for its tokens +/// (`JsonWildcardTooWide`, `JsonTooWide`, `JsonTooDeep`), are [`derive`]'s, +/// after the keyset is resolved. pub(super) fn check(op: Op, options: &JsonOptions, input: &ValueFrame) -> Result<(), Error> { read_input(op, options, input).map(drop) } -/// Where a document field's queries and reads run: under the field's -/// label, then the scopes and the bindings of its plan, as the document -/// was sealed. -/// -/// For example, take the field `profile` of the table `users`, in a plan -/// whose caller added the tenant 7 as a scope and the row 9 as a binding. -/// A selector, a read key and every other term derive under -/// `users/profile`, then 7: the query context. An entry that a read -/// returns opens under `users/profile`, then 7, then 9: the stored -/// context. -#[derive(Clone)] -pub(super) struct Under<'p> { - label: &'p Label, - /// The plan's scopes, then its bindings. - parts: DeclaredContext, -} - -impl<'p> Under<'p> { - /// The field with the label `label`, in a plan whose scopes and - /// bindings are `parts`. - pub(super) fn new(label: &'p Label, parts: DeclaredContext) -> Self { - Self { label, parts } - } - - fn column(&self) -> NonEmpty