Skip to content

Commit ef1ef1b

Browse files
authored
Merge pull request #1551 from cipherstash/claude/project-thread-rckj8w
fix(eql): schema-qualify operators in comparison wrapper bodies
2 parents 650f0b8 + 63da8b3 commit ef1ef1b

93 files changed

Lines changed: 1617 additions & 1143 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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
'@cipherstash/eql': patch
3+
'stash': patch
4+
---
5+
6+
Fix: the `_ord_ore` comparison functions (`eql_v3.lt`, `lte`, `gt`, `gte`, `eq`,
7+
`neq`), and the operators on `_ord_ore` columns when named with their schema
8+
(`col OPERATOR(public.<) $1`), no longer give wrong answers when `public` is not
9+
on the `search_path`. These functions are kept free of a pinned
10+
`search_path` so PostgreSQL can inline them into index scans, which means they
11+
look up their comparison operator through the caller's `search_path`. The ORE
12+
comparison operators live in `public`, so a caller without it, such as a
13+
`SECURITY DEFINER` function with `SET search_path = ''` (the hardening Supabase
14+
recommends), got PostgreSQL's built-in record comparison instead. That compares
15+
the ciphertext bytes, so equality and ordering were silently wrong rather than
16+
raising an error. Every generated comparison function now names its operator
17+
with its schema (`OPERATOR(public.<)` for ORE, `OPERATOR(pg_catalog.<)` for the
18+
HMAC, bloom filter and OPE terms), so the result no longer depends on the
19+
`search_path`, and the functions still inline. A bare `col < $1` still needs
20+
`public` on the `search_path`, because the operators on encrypted columns are
21+
created in `public`; without it, call the function (`eql_v3.lt(col, $1)`) or
22+
name the operator's schema. Reinstall or upgrade EQL to pick up the fix.
23+
24+
The `stash-postgres` skill now explains how to query encrypted columns from a
25+
function or role whose `search_path` leaves out `public`.

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ pub enum FnEntry {
214214
ctor: String, // e.g. hmac_256 (called as {{ internal_schema }}.{{ ctor }})
215215
},
216216
Wrapper {
217-
op: String, // SQL operator used in the body, e.g. =
217+
op: String, // schema-qualified body operator, e.g. OPERATOR(pg_catalog.=)
218218
function_name: String, // e.g. eq
219219
args: [SqlParam; 2],
220220
call_a: String, // e.g. eql_v3.eq_term(a) (embeds extract_arg cast logic)
@@ -258,17 +258,13 @@ pub fn extractor_entry(term: Term) -> FnEntry {
258258

259259
/// Build an inlinable comparison-wrapper entry for a supported operator.
260260
/// `dom` is the schema-qualified domain name; `op` is the already-resolved
261-
/// operator (the caller iterates `OPERATORS`, so no symbol re-lookup is needed).
262-
pub fn wrapper_entry(
263-
dom: &str,
264-
op: &Operator,
265-
arg_a: &str,
266-
arg_b: &str,
267-
extractor: &str,
268-
) -> FnEntry {
261+
/// operator (the caller iterates `OPERATORS`, so no symbol re-lookup is needed);
262+
/// `term` is the term serving `op`, which supplies both the extractor and the
263+
/// schema the body's operator is named in.
264+
pub fn wrapper_entry(dom: &str, op: &Operator, arg_a: &str, arg_b: &str, term: Term) -> FnEntry {
265+
let extractor = term.extractor();
269266
FnEntry::Wrapper {
270-
// Body operator: `symbol` for all but `@@`, whose body uses bloom `@>`.
271-
op: op.body_operator().to_string(),
267+
op: qualified_body_operator(op, term),
272268
// Supported path → the public wrapper name. Differs from `function_name`
273269
// (the blocker name) only for `@@`, whose wrapper is `eql_v3.matches`.
274270
function_name: op.wrapper_function_name().to_string(),
@@ -288,6 +284,23 @@ pub fn wrapper_entry(
288284
}
289285
}
290286

287+
/// The wrapper body's operator, schema-qualified as `OPERATOR(<schema>.<sym>)`.
288+
///
289+
/// `sym` is `op.body_operator()` (the symbol itself for all but `@@`, whose
290+
/// body uses bloom `@>`); `<schema>` is `term.operator_schema()`. A wrapper is
291+
/// unpinned `LANGUAGE sql` so it can inline, which means its body is parsed
292+
/// under the caller's `search_path` — a bare operator would resolve to whatever
293+
/// that path finds. See [`Term::operator_schema`] for the ORE failure this
294+
/// closes. Qualifying keeps the wrapper inlinable: the inlined
295+
/// expression carries the same operator OID either way.
296+
pub fn qualified_body_operator(op: &Operator, term: Term) -> String {
297+
format!(
298+
"OPERATOR({}.{})",
299+
term.operator_schema(),
300+
op.body_operator()
301+
)
302+
}
303+
291304
/// Build an unsupported-operator entry. Every such entry shares one uniform
292305
/// `RAISE EXCEPTION` body; only signature facts vary. `op` is the
293306
/// already-resolved operator (no symbol re-lookup needed).
@@ -337,7 +350,7 @@ pub fn operator_entry(op: &Operator, leftarg: &str, rightarg: &str, supported: b
337350
};
338351
// A supported operator is backed by a public comparison wrapper (SCHEMA); a
339352
// blocked one by an internal blocker (INTERNAL_SCHEMA). `supported` tracks
340-
// wrapper emission exactly: `is_supported(op) ⟹ extractor_for_operator is
353+
// wrapper emission exactly: `is_supported(op) ⟹ term_for_operator is
341354
// Some`, so `render_functions_file` always emits a wrapper for a supported
342355
// operator and a blocker otherwise.
343356
let function_schema = if supported { SCHEMA } else { INTERNAL_SCHEMA };

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

Lines changed: 116 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ pub fn render_functions_file(family_name: &str, domain: &Domain) -> String {
160160
entries.push(extractor_entry(term));
161161
}
162162
for op in OPERATORS {
163-
let extractor = Term::extractor_for_operator(domain.terms, op.symbol.as_str());
163+
let term = Term::term_for_operator(domain.terms, op.symbol.as_str());
164164
for sig in op.signatures {
165165
let rendered = sig.render(&dom);
166166
// A `blocker_only` overload (the `@@` jsonpath predicate) always falls
167167
// through to the blocker, even when the domain supports the symbol.
168168
if is_supported(op.symbol.as_str()) && !sig.blocker_only {
169-
if let Some(ex) = extractor {
170-
entries.push(wrapper_entry(&dom, op, &rendered.left, &rendered.right, ex));
169+
if let Some(t) = term {
170+
entries.push(wrapper_entry(&dom, op, &rendered.left, &rendered.right, t));
171171
continue;
172172
}
173173
}
@@ -275,7 +275,7 @@ pub fn render_query_functions_file(family_name: &str, domain: &Domain) -> String
275275
}
276276
// Comparison wrappers: (storage, query) and its (query, storage) commutator,
277277
// for supported operators only (a query operand is never sent for a blocked
278-
// operator). `is_supported(op) ⟹ extractor_for_operator is Some`.
278+
// operator). `is_supported(op) ⟹ term_for_operator is Some`.
279279
//
280280
// Query twins deliberately emit NO blockers (unlike the storage surface). A
281281
// query operand only ever appears as the RHS of `col <op> operand`, and for
@@ -289,21 +289,21 @@ pub fn render_query_functions_file(family_name: &str, domain: &Domain) -> String
289289
if !supported.contains(&op.symbol.as_str()) {
290290
continue;
291291
}
292-
let extractor = Term::extractor_for_operator(domain.terms, op.symbol.as_str())
293-
.expect("a supported operator resolves an extractor");
292+
let term = Term::term_for_operator(domain.terms, op.symbol.as_str())
293+
.expect("a supported operator resolves a term");
294294
entries.push(wrapper_entry(
295295
&query_dom,
296296
op,
297297
&storage_dom,
298298
&query_dom,
299-
extractor,
299+
term,
300300
));
301301
entries.push(wrapper_entry(
302302
&query_dom,
303303
op,
304304
&query_dom,
305305
&storage_dom,
306-
extractor,
306+
term,
307307
));
308308
}
309309

@@ -533,22 +533,6 @@ fn json_entry_cross_blocked_operators(spec: &DomainFamily) -> Vec<&'static str>
533533
.collect()
534534
}
535535

536-
/// The extractor this surface uses on BOTH operands: [`JSON_ENTRY_TERM`]'s own
537-
/// (`ord_term` → `ope_cllw`), asked of the `Term` rather than spelled out, so an
538-
/// extractor rename stays a single catalog-side change.
539-
///
540-
/// Used for every operator rather than resolving per-operator through
541-
/// `Term::extractor_for_operator`. That generic rule picks the first capable term
542-
/// in the operand's list, so on a dual-term `[Hm, Ope]` operand (text's `_ord`)
543-
/// it routes `=`/`<>` through `eq_term` — the per-value HMAC a SteVec scalar leaf
544-
/// never carries. That rule is right for a scalar column and wrong for this seam:
545-
/// `json_entry` serves exactly one term, so the extractor is a property of the
546-
/// SEAM, not of the operator. Since [`json_entry_cross_operators`] already limits
547-
/// the emitted set to what this term provides, the pairing is total.
548-
fn json_entry_extractor() -> &'static str {
549-
JSON_ENTRY_TERM.extractor()
550-
}
551-
552536
/// Body for a family's json_entry_<T>_functions.sql: comparison
553537
/// WRAPPERS binding the fixed `public.eql_v3_json_entry` leaf to the family's
554538
/// `_ord` / `_ord_ope` query operands, for every operator json_entry can serve
@@ -576,7 +560,17 @@ pub fn render_json_entry_cross_functions(
576560

577561
let family_name = spec.name;
578562
let json_entry = json_entry_type();
579-
let extractor = json_entry_extractor();
563+
// The seam's own term on BOTH operands, for every operator — not resolved
564+
// per operator through `Term::term_for_operator`. That generic rule picks the
565+
// first capable term in the operand's list, so on a dual-term `[Hm, Ope]`
566+
// operand (text's `_ord`) it would route `=`/`<>` through `eq_term`, the
567+
// per-value HMAC a SteVec scalar leaf never carries. `json_entry` serves
568+
// exactly one term, so the term is a property of the SEAM, not of the
569+
// operator; [`json_entry_cross_operators`] already limits the emitted set to
570+
// what it provides, so the pairing is total. Passing the `Term` (not an
571+
// extractor name) lets `wrapper_entry` ask the catalog for both the
572+
// extractor and the schema of the body's operator.
573+
let term = JSON_ENTRY_TERM;
580574
let supported = json_entry_cross_operators(spec);
581575
let blocked = json_entry_cross_blocked_operators(spec);
582576
let mut requires = vec![
@@ -607,14 +601,14 @@ pub fn render_json_entry_cross_functions(
607601
op,
608602
&json_entry,
609603
&query_dom,
610-
extractor,
604+
term,
611605
));
612606
entries.push(wrapper_entry(
613607
&json_entry,
614608
op,
615609
&query_dom,
616610
&json_entry,
617-
extractor,
611+
term,
618612
));
619613
} else if blocked.contains(&symbol) {
620614
// Both directions, so neither can fall through to `jsonb = jsonb`.
@@ -1152,6 +1146,94 @@ mod tests {
11521146

11531147
use std::fs;
11541148

1149+
/// Every comparison-wrapper body — scalar, query-twin and json_entry
1150+
/// surfaces, every catalog family — names its operator schema-qualified.
1151+
///
1152+
/// Wrappers are unpinned `LANGUAGE sql` (to stay inlinable), so their body
1153+
/// is parsed under the CALLER's `search_path`; a bare operator resolves to
1154+
/// whatever that path finds. For the ORE term, whose operators live in
1155+
/// `public`, a caller without `public` on the path got pg_catalog's RECORD
1156+
/// comparison — byte-wise ciphertext order, no error. A bare
1157+
/// operator token anywhere in a wrapper body fails here.
1158+
#[test]
1159+
fn every_wrapper_body_schema_qualifies_its_operators() {
1160+
let mut bodies = 0;
1161+
for family in CATALOG {
1162+
for (path, sql) in render_type(family, std::path::Path::new("out")) {
1163+
for line in sql
1164+
.lines()
1165+
.filter(|l| l.starts_with("AS $$ SELECT eql_v3."))
1166+
{
1167+
bodies += 1;
1168+
let bare: Vec<&str> = line
1169+
.split_whitespace()
1170+
.filter(|tok| tok.chars().all(|c| "<>=@!~#&|-+*/%^".contains(c)))
1171+
.collect();
1172+
assert!(
1173+
bare.is_empty(),
1174+
"{}: wrapper body uses unqualified operator(s) {bare:?}: {line}",
1175+
path.display()
1176+
);
1177+
// The schema each term's operators actually live in, spelled
1178+
// out here rather than read from `Term::operator_schema`, so a
1179+
// wrong mapping there fails this test for every family. The
1180+
// ORE term compares a composite type whose operators are in
1181+
// `public`; naming pg_catalog there reaches the built-in
1182+
// RECORD comparison, the byte-order bug this guards.
1183+
let want = if line.contains("eql_v3.ord_term_ore(") {
1184+
" OPERATOR(public."
1185+
} else {
1186+
" OPERATOR(pg_catalog."
1187+
};
1188+
assert!(
1189+
line.contains(want),
1190+
"{}: wrapper body does not name its operator as{want}…: {line}",
1191+
path.display()
1192+
);
1193+
let other = if want.contains("public") {
1194+
" OPERATOR(pg_catalog."
1195+
} else {
1196+
" OPERATOR(public."
1197+
};
1198+
assert!(
1199+
!line.contains(other),
1200+
"{}: wrapper body names an operator in the wrong schema: {line}",
1201+
path.display()
1202+
);
1203+
}
1204+
}
1205+
}
1206+
// Wrapper bodies open with an `eql_v3.<extractor>(a)` call (extractor
1207+
// bodies call an `eql_v3_internal` constructor and are skipped). Guards
1208+
// the filter itself: a template change that stopped emitting single-line
1209+
// `AS $$ SELECT eql_v3.` bodies would otherwise pass vacuously.
1210+
assert!(bodies > 500, "only {bodies} wrapper bodies inspected");
1211+
}
1212+
1213+
/// The `_ord_ore` wrappers compare `eql_v3_internal.ore_block_256` values,
1214+
/// a composite type: only `OPERATOR(public.<op>)` reaches the ORE
1215+
/// comparator. Pins the exact body per operator, on both the storage and
1216+
/// query-twin surfaces (the catalog-wide schema check is
1217+
/// `every_wrapper_body_schema_qualifies_its_operators`).
1218+
#[test]
1219+
fn ord_ore_wrappers_name_the_public_ore_operators() {
1220+
let s = spec("integer");
1221+
let d = domain(s, "ord_ore");
1222+
let storage = render_functions_file("integer", d);
1223+
let query = render_query_functions_file("integer", d);
1224+
for op in ["=", "<>", "<", "<=", ">", ">="] {
1225+
let body = format!(
1226+
"SELECT eql_v3.ord_term_ore(a) OPERATOR(public.{op}) eql_v3.ord_term_ore(b) $$;"
1227+
);
1228+
assert!(storage.contains(&body), "storage wrapper for {op}: {body}");
1229+
assert!(query.contains(&body), "query wrapper for {op}: {body}");
1230+
assert!(
1231+
!storage.contains(&format!("eql_v3.ord_term_ore(a) {op} ")),
1232+
"bare {op} on ORE terms"
1233+
);
1234+
}
1235+
}
1236+
11551237
#[test]
11561238
fn arg_b_name_is_selector_only_for_path_operators() {
11571239
assert_eq!(arg_b_name("->"), "selector");
@@ -1548,8 +1630,9 @@ mod tests {
15481630
"CREATE FUNCTION eql_v3.matches(a public.eql_v3_text_match, b public.eql_v3_text_match)"
15491631
));
15501632
assert!(fns.contains(
1551-
"SELECT eql_v3.match_term(a) @> eql_v3.match_term(b) \
1552-
AND (cardinality(eql_v3.match_term(b)) > 0 OR cardinality(eql_v3.match_term(a)) = 0)"
1633+
"SELECT eql_v3.match_term(a) OPERATOR(pg_catalog.@>) eql_v3.match_term(b) \
1634+
AND (pg_catalog.cardinality(eql_v3.match_term(b)) OPERATOR(pg_catalog.>) 0 \
1635+
OR pg_catalog.cardinality(eql_v3.match_term(a)) OPERATOR(pg_catalog.=) 0)"
15531636
));
15541637
// The guarded match wrapper is NOT STRICT: a STRICT SQL function with a
15551638
// non-strict body (the guard's AND/OR) would stop inlining, losing the
@@ -2076,8 +2159,6 @@ mod tests {
20762159
"{excluded} must never reach the extract surface"
20772160
);
20782161
}
2079-
// And the extractor is the term's own, not a spelled-out literal.
2080-
assert_eq!(json_entry_extractor(), Term::Ope.extractor());
20812162
}
20822163

20832164
#[test]
@@ -2193,7 +2274,7 @@ mod tests {
21932274
assert!(sql.contains(
21942275
"CREATE FUNCTION eql_v3.lt(a public.eql_v3_json_entry, b eql_v3.query_integer_ord)"
21952276
));
2196-
assert!(sql.contains("SELECT eql_v3.ord_term(a) < eql_v3.ord_term(b)"));
2277+
assert!(sql.contains("SELECT eql_v3.ord_term(a) OPERATOR(pg_catalog.<) eql_v3.ord_term(b)"));
21972278
assert!(sql.contains(
21982279
"CREATE FUNCTION eql_v3.lt(a public.eql_v3_json_entry, b eql_v3.query_integer_ord_ope)"
21992280
));
@@ -2212,7 +2293,7 @@ mod tests {
22122293
"equality must not be a public wrapper on the extract surface"
22132294
);
22142295
assert!(
2215-
!sql.contains("SELECT eql_v3.ord_term(a) = eql_v3.ord_term(b)"),
2296+
!sql.contains("SELECT eql_v3.ord_term(a) OPERATOR(pg_catalog.=) eql_v3.ord_term(b)"),
22162297
"op-equality is gone — equality is document containment, not an extract op"
22172298
);
22182299
// The dropped `_eq` operand never appears, and no hmac equality route
@@ -2299,7 +2380,7 @@ mod tests {
22992380
assert!(sql.contains(
23002381
"CREATE FUNCTION eql_v3.lt(a public.eql_v3_json_entry, b eql_v3.query_text_ord)"
23012382
));
2302-
assert!(sql.contains("SELECT eql_v3.ord_term(a) < eql_v3.ord_term(b)"));
2383+
assert!(sql.contains("SELECT eql_v3.ord_term(a) OPERATOR(pg_catalog.<) eql_v3.ord_term(b)"));
23032384
assert!(sql.contains(
23042385
"CREATE FUNCTION eql_v3.gte(a public.eql_v3_json_entry, b eql_v3.query_text_ord)"
23052386
));
@@ -2314,7 +2395,7 @@ mod tests {
23142395
);
23152396
}
23162397
assert!(
2317-
!sql.contains("SELECT eql_v3.ord_term(a) = eql_v3.ord_term(b)"),
2398+
!sql.contains("SELECT eql_v3.ord_term(a) OPERATOR(pg_catalog.=) eql_v3.ord_term(b)"),
23182399
"op-equality on text is a FALSE POSITIVE (orderize_string collates)"
23192400
);
23202401

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
--! @return boolean
55
CREATE FUNCTION {{ schema }}.{{ e.function_name }}({{ e.args[0].name }} {{ e.args[0].ty }}, {{ e.args[1].name }} {{ e.args[1].ty }})
66
RETURNS boolean LANGUAGE sql IMMUTABLE{% if not e.empty_bloom_guard %} STRICT{% endif %} PARALLEL SAFE
7-
AS $$ SELECT {{ e.call_a }} {{ e.op }} {{ e.call_b }}{% if e.empty_bloom_guard %} AND (cardinality({{ e.call_b }}) > 0 OR cardinality({{ e.call_a }}) = 0){% endif %} $$;
7+
AS $$ SELECT {{ e.call_a }} {{ e.op }} {{ e.call_b }}{% if e.empty_bloom_guard %} AND (pg_catalog.cardinality({{ e.call_b }}) OPERATOR(pg_catalog.>) 0 OR pg_catalog.cardinality({{ e.call_a }}) OPERATOR(pg_catalog.=) 0){% endif %} $$;

0 commit comments

Comments
 (0)