Skip to content

Commit 20f099e

Browse files
committed
test(eql,go): pin a document's column and its SQL null through the data plan
Nothing opened a JsonSearch value under another column. The data plan test now seals under users/profile and opens under users/other, which is refused as a context mismatch before any key is retrieved; the Go guest test does the same and gets ErrContextMismatch. A row whose document is a frame null, or absent, under null=sql stores no value and opens as a null, beside a JSON null that round-trips as a document: the doc comment that promised this had no test. Claude-Session: https://claude.ai/code/session_01N7bCqa3ZVJMcEE99RzVqEu
1 parent 05d19b3 commit 20f099e

2 files changed

Lines changed: 121 additions & 2 deletions

File tree

‎languages/golang/encrypt/json_tree_test.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ func TestJSONDocumentsRoundTripThroughTheGuest(t *testing.T) {
8686
}
8787
}
8888

89+
// A document sealed for users/profile does not open as another column's:
90+
// the guest opens it under the field's own column and refuses a stored
91+
// value whose envelope names another.
92+
func TestJSONDocumentsDoNotOpenUnderAnotherColumn(t *testing.T) {
93+
c := deterministicEQLClient(t)
94+
ctx := context.Background()
95+
sealed, err := c.DefaultKeyset().Seal(ctx, jsonPlan(), []record.Source{{"profile": []byte(jsonDocuments[0])}})
96+
if err != nil {
97+
t.Fatal(err)
98+
}
99+
other := jsonPlan()
100+
other.Fields[0].Identity = "other"
101+
if _, err := c.Open(ctx, other, sealed); !errors.Is(err, encrypt.ErrContextMismatch) {
102+
t.Fatalf("a users/profile document opened as users/other: %v", err)
103+
}
104+
}
105+
89106
// Text that is not a document is refused before anything is sealed, with
90107
// the engine's code and without the text.
91108
func TestJSONTextThatIsNotADocumentIsErrEncoding(t *testing.T) {

‎packages/eql/tests/encryption/tests/json_tree_data_plan.rs‎

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,112 @@ async fn the_data_plan_and_the_typed_layout_agree() {
328328
}
329329
}
330330

331+
/// Under `"null": "sql"` a frame null, or a row with no `profile`, stores
332+
/// no value for the field, and opens as a null; a JSON `null` beside them
333+
/// is a document and round-trips as one.
334+
#[tokio::test]
335+
async fn a_sql_null_stores_no_document() {
336+
let (cipher, _) = common::cipher().await;
337+
let keyset = cipher.default_keyset();
338+
let plan = plan();
339+
let rows: Vec<BTreeMap<&str, Option<&str>>> = vec![
340+
BTreeMap::from([("profile", Some(r#"{"name": "Alice"}"#))]),
341+
BTreeMap::from([("profile", None)]),
342+
BTreeMap::new(),
343+
BTreeMap::from([("profile", Some("null"))]),
344+
];
345+
let sealed = record::encrypt_with(&keyset, frame(&rows), &plan, &Eql)
346+
.unwrap()
347+
.await
348+
.unwrap();
349+
let CipherText::Sequence(stored) = &sealed else {
350+
panic!("a batch is a sequence")
351+
};
352+
let stores_profile: Vec<bool> = stored
353+
.iter()
354+
.map(|row| {
355+
let CipherText::Map(fields) = row else {
356+
panic!("a row is a map")
357+
};
358+
fields.iter().any(|(name, _)| name == "profile")
359+
})
360+
.collect();
361+
assert_eq!(stores_profile, [true, false, false, true]);
362+
let opened = record::decrypt_with(Scope::Client(&cipher), sealed, &plan, None, &Eql)
363+
.unwrap()
364+
.await
365+
.unwrap();
366+
let rows: Vec<BTreeMap<String, Option<serde_bytes_like::Bytes>>> =
367+
dynamic::frame::serde::from_slice(opened.risky_ref()).unwrap();
368+
let back: Vec<Option<Value>> = rows
369+
.iter()
370+
.map(|row| {
371+
row["profile"]
372+
.as_ref()
373+
.map(|text| serde_json::from_slice(&text.0).unwrap())
374+
})
375+
.collect();
376+
assert_eq!(
377+
back,
378+
[
379+
Some(json!({"name": "Alice"})),
380+
None,
381+
None,
382+
Some(Value::Null)
383+
]
384+
);
385+
}
386+
387+
/// A document sealed for one column does not open as another's: the data
388+
/// plan opens `JsonSearch` under the field's own column, and a document
389+
/// whose envelope names another is refused before any key is retrieved.
390+
#[tokio::test]
391+
async fn a_document_does_not_open_under_another_column() {
392+
let (cipher, calls) = common::cipher().await;
393+
let keyset = cipher.default_keyset();
394+
let text = documents()[0].to_string();
395+
let sealed = record::encrypt_with(
396+
&keyset,
397+
frame(BTreeMap::from([("profile", text.as_str())])),
398+
&plan(),
399+
&Eql,
400+
)
401+
.unwrap()
402+
.await
403+
.unwrap();
404+
let other = record::plan_with(
405+
&dynamic::frame::serde::to_vec(&json!({
406+
"profile": {
407+
"context": ["users", "other"],
408+
"type": "json",
409+
"null": "sql",
410+
"target": {
411+
"name": "JsonSearch",
412+
"options": {"ordered": ["$.age", "$.visits[*].at"]}
413+
}
414+
}
415+
}))
416+
.unwrap(),
417+
&Eql,
418+
)
419+
.unwrap();
420+
calls.lock().unwrap().retrieve.clear();
421+
let refused = record::decrypt_with(Scope::Client(&cipher), sealed, &other, None, &Eql)
422+
.unwrap()
423+
.await;
424+
assert!(
425+
matches!(refused, Err(stack_encrypt::Error::ContextMismatch { .. })),
426+
"{:?}",
427+
refused.map(drop)
428+
);
429+
assert!(
430+
calls.lock().unwrap().retrieve.is_empty(),
431+
"refused before any key is retrieved"
432+
);
433+
}
434+
331435
/// Text that is not a document is refused when the record is read, before
332436
/// any key is requested, naming the field and what was wrong by its code.
333-
/// A frame null is not a JSON null: under `"null": "sql"` it stores
334-
/// nothing.
335437
#[tokio::test]
336438
async fn text_that_is_not_a_document_is_refused_before_any_key() {
337439
let (cipher, calls) = common::cipher().await;

0 commit comments

Comments
 (0)