Skip to content

Commit 7f7f5ce

Browse files
committed
Merge branch 'feat/json-tree-derivable-tokens' into test/json-tree-leakage-profile
Carries 8c843ee (the deepest-level container refusal) so #1362 holds #1363's final head before either merges. Claude-Session: https://claude.ai/code/session_01N7bCqa3ZVJMcEE99RzVqEu
2 parents fd22900 + 8c843ee commit 7f7f5ce

4 files changed

Lines changed: 43 additions & 10 deletions

File tree

‎packages/eql/src/v3/json_tree/functions.sql‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,10 @@ $$;
475475
--! fields are computed once per call, so the document is read once, and an
476476
--! entry is copied out of `sv` only when it is returned.
477477
--!
478-
--! **Inlining.** Every chain primitive above is evaluated per probe or per
479-
--! hit, so each carries the `eql-inline-critical` marker that keeps its
480-
--! search_path unpinned: the planner does not inline a function with a
478+
--! **Inlining.** Every chain primitive a probe or a hit evaluates (the
479+
--! hashes, the tags and the token rules; not `json_tree_iv` or
480+
--! `json_tree_sealed_root`, read once per call) carries the
481+
--! `eql-inline-critical` marker that keeps its search_path unpinned: the planner does not inline a function with a
481482
--! pinned search_path, and a probe then costs a full SQL-function call,
482483
--! about ten microseconds, against under one for the hashes it performs.
483484
--! For the same reason a hit's token is derived where it is consumed, in

‎packages/stack-encrypt/src/json/derive.rs‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ const RANK_DOMAIN: &[u8] = b"stack-encrypt/json/rank/v1";
5151
/// The bound is what reading the document back needs, not what the walk
5252
/// could manage. A sealed root's plaintext is the document as JSON, which
5353
/// `serde_json` parses with a nesting limit of 128 containers, so a
54-
/// document whose innermost value sits 128 levels down seals and then never
55-
/// opens. Refusing it here moves that failure from the read, where the
56-
/// value is lost, to the write, where the caller still holds it. The bound
57-
/// is the same under either root option, so the option never changes which
58-
/// documents the index accepts.
54+
/// document whose innermost value sits 128 levels down, or whose innermost
55+
/// container is the 128th (an empty `[]` or `{}` at depth 127), seals and
56+
/// then never opens. Refusing both here moves that failure from the read,
57+
/// where the value is lost, to the write, where the caller still holds it.
58+
/// The bound is the same under either root option, so the option never
59+
/// changes which documents the index accepts.
5960
pub(crate) const MAX_DEPTH: usize = 127;
6061

6162
/// The largest number the index gives an array element's position, a
@@ -602,10 +603,13 @@ impl Walker<'_> {
602603
element: Option<usize>,
603604
depth: usize,
604605
) -> Result<usize, TermError> {
605-
if depth > MAX_DEPTH {
606+
let kind = Kind::of(value);
607+
// A container at MAX_DEPTH, empty or not, would be the 128th nested
608+
// container, which serde_json refuses to read back; only a scalar
609+
// may sit that deep.
610+
if depth > MAX_DEPTH || (depth == MAX_DEPTH && kind.is_container()) {
606611
return Err(TermError::JsonTooDeep);
607612
}
608-
let kind = Kind::of(value);
609613
let s = prf16(
610614
self.prf,
611615
SELECTOR_DOMAIN,

‎packages/stack-encrypt/tests/json_index.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,31 @@ mod given_the_deepest_document {
345345
}
346346
}
347347
}
348+
349+
/// An empty container at the deepest level is itself the 128th nested
350+
/// container: `serde_json` refuses to read the document back, so the
351+
/// index refuses to seal it, under either root option, where a scalar at
352+
/// the same depth seals and opens.
353+
#[tokio::test]
354+
async fn an_empty_container_at_the_deepest_level_is_refused_when_sealed() {
355+
let Scenario { cipher, .. } = scenario().await;
356+
let arrays = (0..127).fold(json!([]), |inner, _| json!([inner]));
357+
let objects = (0..127).fold(json!({}), |inner, _| json!({"k": inner}));
358+
for deepest in [&arrays, &objects] {
359+
assert!(
360+
serde_json::from_str::<Value>(&deepest.to_string()).is_err(),
361+
"128 nested containers: serde_json would not read the sealed root back"
362+
);
363+
for options in [JsonOptions::default(), open_root()] {
364+
let refused = try_seal(&cipher, deepest, &options).await;
365+
assertions::refused_by_term(
366+
&refused,
367+
|e| matches!(e, TermError::JsonTooDeep),
368+
&format!("an empty container 127 levels down under {options:?}"),
369+
);
370+
}
371+
}
372+
}
348373
}
349374

350375
// =============================================================================

‎packages/stack-encrypt/tests/json_properties.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ use stack_kms::FakeDataKeySource;
3333

3434
type Cipher = StackCipher<FakeDataKeySource>;
3535

36+
/// `proptest!` generates synchronous `#[test]` functions and runs each body
37+
/// once per case through a plain closure, so `#[tokio::test]` cannot wrap a
38+
/// property; each test builds one runtime and `block_on`s its cases.
3639
fn runtime() -> tokio::runtime::Runtime {
3740
tokio::runtime::Builder::new_current_thread()
3841
.enable_all()

0 commit comments

Comments
 (0)