Skip to content

Commit dfca909

Browse files
authored
Merge pull request #1547 from cipherstash/claude/project-thread-h06x5y
fix(eql): block a domain compared with another domain's query operand
2 parents 368ac98 + 8c6fc2f commit dfca909

88 files changed

Lines changed: 15249 additions & 35 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@cipherstash/eql": patch
3+
"stash": patch
4+
---
5+
6+
Comparing an encrypted column with a different domain's query operand now raises instead of silently returning no rows. For example `public.eql_v3_text_ord = $1::eql_v3.query_text_eq`, or `eql_v3_text_ord < $1::eql_v3.query_text_ord_ope`, previously fell through to native `jsonb` comparison and answered false for equal values. Within a type family, every pair of a column domain and another domain's query operand now resolves to a blocker for the operators both sides support, naming both operands and telling the caller to use the query domain that matches the column. Use the query domain of the column's own domain (`eql_v3.query_<column domain>`). The install bundle grows by about 1,060 functions and the same number of operators, so a single-transaction uninstall now takes about 8,550 locks (up from about 6,400); raise `max_locks_per_transaction` on busy clusters.

‎packages/eql/crates/eql-codegen/src/context.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ pub enum FnEntry {
229229
function_name: String, // e.g. lt / "->" / "#>"
230230
args: [SqlParam; 2],
231231
returns: String, // boolean / text / jsonb / domain (selection STAYS in Rust)
232+
// `Some("<a> and <b>")` (sql_str-escaped) for a blocker that rejects a
233+
// storage domain paired with ANOTHER domain's query twin: the template
234+
// then emits compact docs (there is one per ordered pair of domains in a
235+
// family) and a message naming both operands. `None` for every other
236+
// blocker.
237+
mismatch: Option<String>,
232238
},
233239
}
234240

@@ -311,6 +317,30 @@ pub fn unsupported_entry(op: &Operator, args: [SqlParam; 2], returns: &str) -> F
311317
function_name: op.function_name.to_string(),
312318
args,
313319
returns: returns.to_string(),
320+
mismatch: None,
321+
}
322+
}
323+
324+
/// Build a blocker for a storage domain paired with another domain's query twin
325+
/// (see [`FnEntry::Unsupported::mismatch`]). Same marker text as every other
326+
/// blocker, so the lints that recognise blockers by message keep seeing it.
327+
pub fn mismatch_blocker_entry(op: &Operator, args: [SqlParam; 2], returns: &str) -> FnEntry {
328+
let operands = sql_str(&format!("{} and {}", args[0].ty, args[1].ty));
329+
match unsupported_entry(op, args, returns) {
330+
FnEntry::Unsupported {
331+
operator_lit,
332+
function_name,
333+
args,
334+
returns,
335+
..
336+
} => FnEntry::Unsupported {
337+
operator_lit,
338+
function_name,
339+
args,
340+
returns,
341+
mismatch: Some(operands),
342+
},
343+
other => other,
314344
}
315345
}
316346

‎packages/eql/crates/eql-codegen/src/generate.rs‎

Lines changed: 151 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
55
use eql_domains::{Domain, DomainFamily, Role, Term};
66

77
use crate::context::{domain_name, is_ord_capable, query_domain_name};
8-
use crate::operator_surface::OPERATORS;
8+
use crate::operator_surface::{Operator, OperatorSignature, TypeSlot, OPERATORS};
99

1010
/// REQUIRE edge for the v3 schema file — pulled in by every generated file.
1111
const V3_SCHEMA: &str = "src/v3/schema.sql";
@@ -248,18 +248,67 @@ fn query_types_path(family_name: &str) -> String {
248248
scalar_path(family_name, &format!("query_{family_name}_types.sql"))
249249
}
250250

251+
/// The family's term-bearing storage domains OTHER than `domain` — the operands
252+
/// that must never be compared with `domain`'s query twin. Storage-only domains
253+
/// (no terms, no operators) are left out: every pair costs two schema objects
254+
/// (see [`cross_pair_operators`]).
255+
fn cross_pair_domains(spec: &DomainFamily, domain: &Domain) -> Vec<&'static Domain> {
256+
spec.domains
257+
.iter()
258+
.filter(|d| d.is_scalar() && d.name != domain.name && !d.terms.is_empty())
259+
.collect()
260+
}
261+
262+
/// The operators (with their `(domain, domain)` signature) blocked for a
263+
/// `(sibling storage domain, this domain's query twin)` pair: those BOTH domains
264+
/// advertise. A query twin of a different domain is a DIFFERENT index family, so
265+
/// there is no sound comparison to offer — the pair raises rather than flatten to
266+
/// native `jsonb <op> jsonb`.
267+
///
268+
/// The intersection, not the union, is deliberate. Each blocked pair costs a
269+
/// function and an operator, and the uninstaller takes a lock per object against
270+
/// a stock budget it already approaches (docs/upgrading/v3.0.md, U-009), so the
271+
/// set is held to the operators a caller can reasonably believe both sides
272+
/// support. The remaining unblocked cases name an operator one side does not
273+
/// support at all, which is a second mistake on top of the wrong query domain.
274+
fn cross_pair_operators(
275+
domain: &Domain,
276+
sibling: &Domain,
277+
) -> Vec<(&'static Operator, OperatorSignature)> {
278+
let ours = Term::operators_for_terms(domain.terms);
279+
let theirs = Term::operators_for_terms(sibling.terms);
280+
OPERATORS
281+
.iter()
282+
.filter(|op| ours.contains(&op.symbol.as_str()) && theirs.contains(&op.symbol.as_str()))
283+
.filter_map(|op| {
284+
op.signatures
285+
.iter()
286+
.find(|s| s.left == TypeSlot::Domain && s.right == TypeSlot::Domain)
287+
.map(|s| (op, *s))
288+
})
289+
.collect()
290+
}
291+
251292
/// Body for a term-bearing domain's query_<name>_functions.sql: the
252293
/// query-operand extractor OVERLOADS (the same extractors, on
253294
/// `eql_v3.query_<name>`) plus the comparison WRAPPERS binding the storage
254295
/// domain to its query twin — for the domain's SUPPORTED operators only, in
255296
/// both directions. Reuses the same `functions.sql` template as the storage
256297
/// surface; a query operand carries the same terms, so each wrapper compares
257298
/// `extractor(a)` to `extractor(b)` with no ciphertext cast.
258-
pub fn render_query_functions_file(family_name: &str, domain: &Domain) -> String {
299+
///
300+
/// `siblings` are the family's OTHER scalar storage domains (see
301+
/// [`cross_pair_domains`]). Each gets exact-signature blockers against this
302+
/// query twin in both directions — see [`cross_pair_operators`].
303+
pub fn render_query_functions_file(
304+
family_name: &str,
305+
domain: &Domain,
306+
siblings: &[&Domain],
307+
) -> String {
259308
use crate::consts::sql_str;
260309
use crate::context::{
261-
domain_name, environment, extractor_entry, query_domain_name, wrapper_entry,
262-
FunctionsContext,
310+
domain_name, environment, extractor_entry, mismatch_blocker_entry, query_domain_name,
311+
wrapper_entry, FunctionsContext, SqlParam,
263312
};
264313
let name = domain.full_name(family_name);
265314
let query_name = domain.query_name(family_name);
@@ -277,14 +326,12 @@ pub fn render_query_functions_file(family_name: &str, domain: &Domain) -> String
277326
// for supported operators only (a query operand is never sent for a blocked
278327
// operator). `is_supported(op) ⟹ term_for_operator is Some`.
279328
//
280-
// Query twins deliberately emit NO blockers (unlike the storage surface). A
281-
// query operand only ever appears as the RHS of `col <op> operand`, and for
282-
// an unsupported `<op>` that predicate resolves against the STORAGE domain's
283-
// `(<storage>, jsonb)` blocker (the query operand degrades to its `jsonb`
284-
// base), which raises — so the realistic path is already protected. The only
285-
// unblocked cases are nonsensical `operand <op> operand` / `operand <op>
286-
// jsonb`, which no caller writes; blocking them would mean emitting the full
287-
// blocker matrix against every query twin for zero real-world coverage.
329+
// Query twins emit no blockers against their OWN storage domain or `jsonb`:
330+
// a query operand only ever appears as the RHS of `col <op> operand`, and
331+
// for an unsupported `<op>` that predicate resolves against the STORAGE
332+
// domain's `(<storage>, jsonb)` blocker, which raises. The pairs that are
333+
// NOT covered that way are a DIFFERENT domain's operand against this twin,
334+
// handled by the cross-pair blockers below.
288335
for op in OPERATORS {
289336
if !supported.contains(&op.symbol.as_str()) {
290337
continue;
@@ -307,6 +354,35 @@ pub fn render_query_functions_file(family_name: &str, domain: &Domain) -> String
307354
));
308355
}
309356

357+
// Cross-pair blockers: a DIFFERENT domain of this family against this query
358+
// twin. Both are domains over `jsonb`, so without an exact signature
359+
// PostgreSQL flattens the pair to native `jsonb <op> jsonb` and answers
360+
// silently (`eql_v3_text_ord = query_text_eq` is false for equal values).
361+
for sibling in siblings {
362+
let sibling_dom = domain_name(&sibling.full_name(family_name));
363+
for (op, sig) in cross_pair_operators(domain, sibling) {
364+
for (left, right) in [
365+
(sibling_dom.clone(), query_dom.clone()),
366+
(query_dom.clone(), sibling_dom.clone()),
367+
] {
368+
entries.push(mismatch_blocker_entry(
369+
op,
370+
[
371+
SqlParam {
372+
name: "a",
373+
ty: left,
374+
},
375+
SqlParam {
376+
name: arg_b_name(op.symbol.as_str()),
377+
ty: right,
378+
},
379+
],
380+
&sig.render(&query_dom).returns,
381+
));
382+
}
383+
}
384+
}
385+
310386
let ctx = FunctionsContext {
311387
requires: vec![
312388
V3_SCHEMA.to_string(),
@@ -330,7 +406,14 @@ pub fn render_query_functions_file(family_name: &str, domain: &Domain) -> String
330406
/// `CREATE OPERATOR` binding `(storage_domain, query_<name>)` for every
331407
/// supported operator, plus its `(query_<name>, storage_domain)` commutator, so
332408
/// `col <op> $1::eql_v3.query_<name>` resolves to the query wrapper.
333-
pub fn render_query_operators_file(family_name: &str, domain: &Domain) -> String {
409+
///
410+
/// `siblings` as for [`render_query_functions_file`]: each binds a blocker for
411+
/// the `(sibling, query)` pair and its commutator.
412+
pub fn render_query_operators_file(
413+
family_name: &str,
414+
domain: &Domain,
415+
siblings: &[&Domain],
416+
) -> String {
334417
use crate::context::{
335418
domain_name, environment, operator_entry, query_domain_name, OperatorsContext,
336419
};
@@ -348,7 +431,13 @@ pub fn render_query_operators_file(family_name: &str, domain: &Domain) -> String
348431
operators.push(operator_entry(op, &storage_dom, &query_dom, true));
349432
operators.push(operator_entry(op, &query_dom, &storage_dom, true));
350433
}
351-
434+
for sibling in siblings {
435+
let sibling_dom = domain_name(&sibling.full_name(family_name));
436+
for (op, _) in cross_pair_operators(domain, sibling) {
437+
operators.push(operator_entry(op, &sibling_dom, &query_dom, false));
438+
operators.push(operator_entry(op, &query_dom, &sibling_dom, false));
439+
}
440+
}
352441
let ctx = OperatorsContext {
353442
requires: vec![
354443
V3_SCHEMA.to_string(),
@@ -947,11 +1036,11 @@ pub fn render_type(spec: &DomainFamily, out_dir: &Path) -> Vec<(PathBuf, String)
9471036
let query_name = d.query_name(family_name);
9481037
rendered.push((
9491038
out_dir.join(format!("{query_name}_functions.sql")),
950-
render_query_functions_file(family_name, d),
1039+
render_query_functions_file(family_name, d, &cross_pair_domains(spec, d)),
9511040
));
9521041
rendered.push((
9531042
out_dir.join(format!("{query_name}_operators.sql")),
954-
render_query_operators_file(family_name, d),
1043+
render_query_operators_file(family_name, d, &cross_pair_domains(spec, d)),
9551044
));
9561045
}
9571046
if let Some(agg) = render_aggregates_file(family_name, d) {
@@ -1127,6 +1216,51 @@ pub fn clean_all(out_root: &Path) -> Result<Vec<PathBuf>, WriteError> {
11271216

11281217
#[cfg(test)]
11291218
mod tests {
1219+
/// A query twin carries blockers for every OTHER term-bearing
1220+
/// domain of its family (both directions, only operators both sides
1221+
/// advertise) and none for the family's own storage domain or storage-only
1222+
/// domains, so a mismatched pair raises instead of flattening to `jsonb`.
1223+
#[test]
1224+
fn query_twin_blocks_other_domains_of_its_family() {
1225+
let text = eql_domains::CATALOG
1226+
.iter()
1227+
.find(|f| f.name == "text")
1228+
.expect("text family");
1229+
let eq = text.domains.iter().find(|d| d.name == "eq").unwrap();
1230+
let ord = text.domains.iter().find(|d| d.name == "ord").unwrap();
1231+
let siblings = cross_pair_domains(text, eq);
1232+
assert!(siblings
1233+
.iter()
1234+
.all(|d| d.name != "eq" && !d.terms.is_empty()));
1235+
assert!(siblings.iter().any(|d| d.name == "ord"));
1236+
1237+
let sql = render_query_functions_file("text", eq, &siblings);
1238+
for (l, r) in [
1239+
("public.eql_v3_text_ord", "eql_v3.query_text_eq"),
1240+
("eql_v3.query_text_eq", "public.eql_v3_text_ord"),
1241+
] {
1242+
assert!(
1243+
sql.contains(&format!("eql_v3_internal.eq(a {l}, b {r})")),
1244+
"missing {l} = {r} blocker"
1245+
);
1246+
}
1247+
// query_text_eq advertises only `=`/`<>`: no ordering blocker.
1248+
assert!(!sql.contains("eql_v3_internal.lt(a public.eql_v3_text_ord"));
1249+
// Never the own twin, never the storage-only base domain.
1250+
assert!(!sql.contains("eql_v3_internal.eq(a public.eql_v3_text_eq"));
1251+
assert!(!sql.contains("eql_v3_internal.eq(a public.eql_v3_text,"));
1252+
1253+
let ops = render_query_operators_file("text", eq, &siblings);
1254+
assert!(ops.contains(
1255+
"FUNCTION = eql_v3_internal.eq,\n LEFTARG = public.eql_v3_text_ord, RIGHTARG = eql_v3.query_text_eq\n"
1256+
));
1257+
1258+
// The ordering twin gets ordering blockers against `search`.
1259+
let ord_sql = render_query_functions_file("text", ord, &cross_pair_domains(text, ord));
1260+
assert!(ord_sql
1261+
.contains("eql_v3_internal.lt(a public.eql_v3_text_search, b eql_v3.query_text_ord)"));
1262+
}
1263+
11301264
use super::*;
11311265
use eql_domains::CATALOG;
11321266

@@ -1220,7 +1354,7 @@ mod tests {
12201354
let s = spec("integer");
12211355
let d = domain(s, "ord_ore");
12221356
let storage = render_functions_file("integer", d);
1223-
let query = render_query_functions_file("integer", d);
1357+
let query = render_query_functions_file("integer", d, &cross_pair_domains(s, d));
12241358
for op in ["=", "<>", "<", "<=", ">", ">="] {
12251359
let body = format!(
12261360
"SELECT eql_v3.ord_term_ore(a) OPERATOR(public.{op}) eql_v3.ord_term_ore(b) $$;"

‎packages/eql/crates/eql-codegen/templates/functions/unsupported.sql.j2‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
{% if e.mismatch -%}
2+
--! @brief Unsupported operator blocker for {{ dom }}: {{ e.mismatch }} (mismatched query domain).
3+
--! @param {{ e.args[0].name }} {{ e.args[0].ty }} left operand
4+
--! @param {{ e.args[1].name }} {{ e.args[1].ty }} right operand
5+
--! @return {{ e.returns }} never returned — the function always raises
6+
{% else -%}
17
--! @brief Unsupported operator blocker for {{ dom }}.
28
--!
39
--! Intercepts an operator that is not supported on {{ dom }} and always raises;
@@ -7,7 +13,12 @@
713
--! @param {{ e.args[0].name }} {{ e.args[0].ty }} left operand of the blocked operator
814
--! @param {{ e.args[1].name }} {{ e.args[1].ty }} right operand of the blocked operator
915
--! @return {{ e.returns }} never returned — the function always raises "operator not supported"
16+
{% endif -%}
1017
CREATE FUNCTION {{ internal_schema }}.{{ e.function_name }}({{ e.args[0].name }} {{ e.args[0].ty }}, {{ e.args[1].name }} {{ e.args[1].ty }})
1118
RETURNS {{ e.returns }} IMMUTABLE PARALLEL SAFE
19+
{% if e.mismatch -%}
20+
AS $$ BEGIN RAISE EXCEPTION 'operator % is not supported for %: use the query domain that matches the column', '{{ e.operator_lit }}', '{{ e.mismatch }}'; END; $$
21+
{% else -%}
1222
AS $$ BEGIN RAISE EXCEPTION 'operator % is not supported for %', '{{ e.operator_lit }}', '{{ domain_lit }}'; END; $$
23+
{% endif -%}
1324
LANGUAGE plpgsql;

0 commit comments

Comments
 (0)