Skip to content

Commit 76dfac9

Browse files
authored
fix(eql): make the v4 isolation gate and both lints hold with two bundles
verify_no_v3_in_v4.sh piped grep into head under pipefail. When the matches outgrew the pipe buffer, grep died of SIGPIPE, the condition read false, and a wholesale eql_v3 leak passed while a single stray reference failed. It now captures the matches before printing them, and a new self-test (test:v4_isolation, run in CI) feeds it a 20,000-line leak. With v3 and v4 in one database, each lints() reported 3,840 errors about the other version's blocker operators: encrypted_domain_types took every jsonb domain in public, while blockers are recognised only in the lint's own schemas. The public arm is now limited to the lint's own prefix, and the coexistence test asserts both lints are clean with both installed. The coexistence tests also pin OPERATOR(public.=) between versions (ambiguous) and the eq_term text comparison (runs, refuses nothing), and build.sh and mise run clean remove the v4 artefacts too.
1 parent 61dac39 commit 76dfac9

8 files changed

Lines changed: 139 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cipherstash/eql': patch
3+
---
4+
5+
`eql_v3.lints()` now judges only EQL's own `public.eql_v3_*` domains. It used to take every `jsonb` domain in `public`, so a database with another set of encrypted domains installed beside EQL v3 got thousands of false `inlinability_*` errors from the self-check.

‎.github/workflows/test-eql.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,8 @@ jobs:
733733
run: mise run test:installer_complete
734734
- name: Symbol-order cross-check (v3)
735735
run: mise run test:symbol_order_v3
736+
- name: v4 isolation gate and its self-test
737+
run: mise run test:v4_isolation
736738
- name: Build-ordering helper unit tests
737739
run: mise run test:build_ordering_helpers
738740

‎packages/eql/mise.toml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ description = "Clean release"
6666
run = """
6767
rm -f release/cipherstash-encrypt-uninstall.sql
6868
rm -f release/cipherstash-encrypt.sql
69+
rm -f release/cipherstash-encrypt-v4-uninstall.sql
70+
rm -f release/cipherstash-encrypt-v4.sql
6971
"""
7072

7173
[tasks."release:prepare_bindings_assets"]
@@ -247,6 +249,15 @@ bash tasks/test/symbol_order_selftest.sh
247249
bash tasks/test/verify_symbol_order_v3.sh build/render/eql_v3/deps-ordered.txt
248250
"""
249251

252+
[tasks."test:v4_isolation"]
253+
description = "Assert the eql_v4 bundle names nothing from eql_v3, and self-test that check (DB-free)"
254+
depends = ["build"]
255+
dir = "{{config_root}}"
256+
run = """
257+
bash tasks/test/v4_isolation_selftest.sh
258+
bash tasks/test/verify_no_v3_in_v4.sh
259+
"""
260+
250261
[tasks."test:installer_complete"]
251262
description = "Assert the installer contains every ordered src/v3 file's body (DB-free)"
252263
depends = ["build"]

‎packages/eql/src/v3/lint/lints.sql‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ AS $$
8585
-- User-column encrypted domains now live in public so application tables
8686
-- survive EQL uninstall. Keep this separate from owned_schemas(): public is
8787
-- not installer-owned, but its EQL jsonb-backed domains are still the domain
88-
-- types whose blockers/operator surfaces the lint must understand.
88+
-- types whose blockers/operator surfaces the lint must understand. Only this
89+
-- version's public domains: with EQL v3 and v4 installed side by side, the
90+
-- other version's domains and blockers are not this lint's to judge, and its
91+
-- blockers sit outside owned_schemas() where they would read as errors.
8992
encrypted_domain_types AS (
9093
SELECT
9194
dt.oid AS typid
@@ -97,7 +100,7 @@ AS $$
97100
AND bt.typname = 'jsonb'
98101
AND bn.nspname = 'pg_catalog'
99102
AND (
100-
dn.nspname = 'public'
103+
(dn.nspname = 'public' AND dt.typname LIKE '{{prefix}}\_%')
101104
OR dn.nspname = ANY({{prefix}}_internal.owned_schemas())
102105
)
103106
),

‎packages/eql/tasks/build.sh‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ mkdir -p release
4747

4848
rm -f release/cipherstash-encrypt.sql
4949
rm -f release/cipherstash-encrypt-uninstall.sql
50+
rm -f release/cipherstash-encrypt-v4.sql
51+
rm -f release/cipherstash-encrypt-v4-uninstall.sql
5052
rm -f src/deps-ordered-v3.txt src/deps-ordered-v3.txt.tmp
5153
rm -f src/v3/version.sql
5254

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
#MISE description="DB-free self-test for the v4 isolation gate (clean passes, small and wholesale leaks fail)"
3+
set -euo pipefail
4+
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
5+
cd "$REPO_ROOT"
6+
tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
7+
8+
gate() { bash tasks/test/verify_no_v3_in_v4.sh "$@" >/dev/null 2>&1; }
9+
10+
# CLEAN: names eql_v4, checks version 4, names nothing from v3.
11+
printf "CREATE SCHEMA eql_v4;\nCHECK (VALUE->>'v' = '4');\n" > "$tmp/clean.sql"
12+
gate "$tmp/clean.sql" || { echo "FAIL: clean v4 bundle rejected"; exit 1; }
13+
echo "ok: clean v4 bundle accepted"
14+
15+
# ONE stray reference.
16+
printf "CREATE SCHEMA eql_v4;\nSELECT eql_v3.eq_term(x);\n" > "$tmp/one.sql"
17+
if gate "$tmp/one.sql"; then echo "FAIL: one eql_v3 reference accepted"; exit 1; fi
18+
echo "ok: one eql_v3 reference rejected"
19+
20+
# WHOLESALE leak: far more matching lines than a pipe buffer holds. A
21+
# `grep | head` check under pipefail read this as no leak (SIGPIPE).
22+
{
23+
echo "CREATE SCHEMA eql_v4;"
24+
for i in $(seq 1 20000); do echo "SELECT eql_v3_internal.f$i(eql_v4.g());"; done
25+
} > "$tmp/wholesale.sql"
26+
if gate "$tmp/wholesale.sql"; then echo "FAIL: wholesale eql_v3 leak accepted"; exit 1; fi
27+
echo "ok: wholesale eql_v3 leak rejected"
28+
29+
# The version check, wholesale too.
30+
{
31+
echo "CREATE SCHEMA eql_v4;"
32+
for i in $(seq 1 20000); do echo "CHECK (VALUE->>'v' = '3');"; done
33+
} > "$tmp/version.sql"
34+
if gate "$tmp/version.sql"; then echo "FAIL: wholesale version-3 checks accepted"; exit 1; fi
35+
echo "ok: wholesale version-3 checks rejected"
36+
37+
# NON-VACUITY: a file that never names eql_v4 is not a v4 render.
38+
printf "SELECT 1;\n" > "$tmp/none.sql"
39+
if gate "$tmp/none.sql"; then echo "FAIL: non-v4 render accepted"; exit 1; fi
40+
echo "ok: non-v4 render rejected"

‎packages/eql/tasks/test/verify_no_v3_in_v4.sh‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ if [[ $# -eq 0 ]]; then
2323
set -- release/cipherstash-encrypt-v4.sql release/cipherstash-encrypt-v4-uninstall.sql
2424
fi
2525

26+
# Capture matches before printing them. `grep | head -5` under pipefail is
27+
# false whenever grep has more output than the pipe holds: head exits, grep
28+
# dies of SIGPIPE, and a wholesale leak read as no leak at all.
29+
report() {
30+
local f=$1 what=$2 hits=$3
31+
sed -n "1,5s|^|$f:|p" <<<"$hits"
32+
echo "ERROR: $f $what (first 5 shown)" >&2
33+
fail=1
34+
}
35+
2636
fail=0
2737
for f in "$@"; do
2838
if [[ ! -s "$f" ]]; then
@@ -34,15 +44,13 @@ for f in "$@"; do
3444
echo "ERROR: $f never names eql_v4 — not a v4 render" >&2
3545
fail=1
3646
fi
37-
if grep -n 'eql_v3' "$f" | head -5 | sed "s|^|$f:|" | grep .; then
38-
echo "ERROR: $f names eql_v3 (first 5 shown)" >&2
39-
fail=1
47+
if hits=$(grep -n 'eql_v3' "$f"); then
48+
report "$f" "names eql_v3" "$hits"
4049
fi
4150
# The domain CHECKs compare the payload version. Any v3 comparison left in
4251
# the v4 bundle would admit v3 payloads into v4 columns.
43-
if grep -nE "'v'[[:space:]]*=[[:space:]]*'3'" "$f" | head -5 | sed "s|^|$f:|" | grep .; then
44-
echo "ERROR: $f checks for payload version 3 (first 5 shown)" >&2
45-
fail=1
52+
if hits=$(grep -nE "'v'[[:space:]]*=[[:space:]]*'3'" "$f"); then
53+
report "$f" "checks for payload version 3" "$hits"
4654
fi
4755
done
4856

‎packages/eql/tests/sqlx/tests/v3_v4_coexistence_tests.rs‎

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
//! tests pin what the separate names do and do not enforce when both are
66
//! installed:
77
//!
8-
//! - both install without clashing, and each owns its own domains;
8+
//! - both install without clashing, each owns its own domains, and each
9+
//! version's `lints()` stays clean with the other installed;
910
//! - a payload of the wrong version fails the domain CHECK on insert;
1011
//! - the function form across versions (`eql_v3.eq(v3col, v4term)`) is
11-
//! ambiguous, so it errors;
12+
//! ambiguous, so it errors, and so is `OPERATOR(public.=)` between a v3 and
13+
//! a v4 column;
1214
//! - the operator form across versions (`v3col = v4term`) does NOT error.
1315
//! With no operator defined for the exact pair, Postgres treats both domains
1416
//! as `jsonb` and uses the built-in `jsonb = jsonb`, which returns no rows.
1517
//! That is pinned here so it cannot change unnoticed. ADR-0002 defers the
1618
//! cross-version blocker operators that would make it an error; when they
1719
//! land, those assertions flip to expecting one.
20+
//! - `eql_v3.eq_term(v3col) = eql_v4.eq_term(v4col)` does not error either: it
21+
//! compares two text terms.
1822
//!
1923
//! The payloads are real cipherstash-client ciphertexts from the
2024
//! `eql_v3_integer` fixture. A v4 payload is the same row with its envelope
@@ -103,6 +107,22 @@ async fn v3_and_v4_install_side_by_side(pool: PgPool) -> Result<()> {
103107
.fetch_one(&pool)
104108
.await?;
105109
assert_eq!(a, b, "both bundles come from one build");
110+
111+
// Each lint judges only its own version's public domains. Before that was
112+
// scoped, each version reported thousands of errors about the other's
113+
// blocker operators.
114+
for schema in ["eql_v3", "eql_v4"] {
115+
let errors: Vec<String> = sqlx::query_scalar(&format!(
116+
"SELECT category || ': ' || object_name FROM {schema}.lints() \
117+
WHERE severity = 'error' ORDER BY 1 LIMIT 5"
118+
))
119+
.fetch_all(&pool)
120+
.await?;
121+
assert!(
122+
errors.is_empty(),
123+
"{schema}.lints() reports errors with both versions installed: {errors:#?}"
124+
);
125+
}
106126
Ok(())
107127
}
108128

@@ -206,3 +226,41 @@ async fn operator_across_versions_returns_no_rows(pool: PgPool) -> Result<()> {
206226
assert_eq!(joined, 0, "a v3 column joined to a v4 column finds nothing");
207227
Ok(())
208228
}
229+
230+
#[sqlx::test(fixtures(path = "../fixtures", scripts("eql_v3_integer")))]
231+
async fn qualified_operator_across_versions_is_ambiguous(pool: PgPool) -> Result<()> {
232+
install_v4(&pool).await?;
233+
tables_with_one_row(&pool).await?;
234+
235+
let err = sqlx::query("SELECT count(*) FROM t3 JOIN t4 ON t3.c OPERATOR(public.=) t4.c")
236+
.execute(&pool)
237+
.await
238+
.expect_err("OPERATOR(public.=) between a v3 and a v4 column must not resolve");
239+
assert_eq!(
240+
sqlstate(&err).as_deref(),
241+
Some(AMBIGUOUS_FUNCTION),
242+
"t3.c OPERATOR(public.=) t4.c: {err}"
243+
);
244+
Ok(())
245+
}
246+
247+
/// Pins a second silent path: see the module docs. The terms are text, so
248+
/// comparing them across versions is ordinary text equality. The fixture's v4
249+
/// row reuses the v3 row's terms, so here it even matches; with real Stack
250+
/// Encrypt terms it finds nothing. Either way nothing refuses it.
251+
#[sqlx::test(fixtures(path = "../fixtures", scripts("eql_v3_integer")))]
252+
async fn eq_term_across_versions_compares_text(pool: PgPool) -> Result<()> {
253+
install_v4(&pool).await?;
254+
tables_with_one_row(&pool).await?;
255+
256+
let n: i64 = sqlx::query_scalar(
257+
"SELECT count(*) FROM t3 JOIN t4 ON eql_v3.eq_term(t3.c) = eql_v4.eq_term(t4.c)",
258+
)
259+
.fetch_one(&pool)
260+
.await?;
261+
assert_eq!(
262+
n, 1,
263+
"the cross-version term comparison runs as text equality"
264+
);
265+
Ok(())
266+
}

0 commit comments

Comments
 (0)