Skip to content

Commit 2a2884a

Browse files
committed
test(outbox): reproduce recursive ADMIN SET escape
1 parent e9c3bfd commit 2a2884a

1 file changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
image="pg-llm-batch-postgres:ci"
5+
component_image="pg-llm-batch:ci"
6+
container="pg-llm-batch-outbox-recursive-admin-${GITHUB_RUN_ID:-local}-$$"
7+
8+
cleanup() {
9+
docker rm --force "${container}" >/dev/null 2>&1 || true
10+
}
11+
trap cleanup EXIT
12+
13+
docker run --detach \
14+
--name "${container}" \
15+
--env POSTGRES_HOST_AUTH_METHOD=trust \
16+
"${image}" >/dev/null
17+
18+
ready=0
19+
for _ in $(seq 1 60); do
20+
if docker exec "${container}" pg_isready -h 127.0.0.1 -U postgres -d postgres \
21+
>/dev/null 2>&1 && \
22+
docker exec "${container}" psql -h 127.0.0.1 -U postgres -d postgres -Atqc \
23+
"SELECT (to_regclass('public.llm_context_lifecycle_outbox') IS NOT NULL)::int" \
24+
2>/dev/null | grep -qx '1'; then
25+
ready=1
26+
break
27+
fi
28+
sleep 1
29+
done
30+
if [[ "${ready}" != "1" ]]; then
31+
docker logs "${container}" >&2 || true
32+
echo "fresh PostgreSQL image did not finish lifecycle-outbox initialization" >&2
33+
exit 1
34+
fi
35+
36+
docker exec -i "${container}" psql -U postgres -d postgres -v ON_ERROR_STOP=1 <<'SQL'
37+
INSERT INTO public.llm_context_lifecycle_outbox (
38+
tenant_scope,
39+
evidence_id,
40+
event_type,
41+
tenant_scope_sha256,
42+
subject_ref_sha256,
43+
authority_ref_sha256,
44+
origin_ref_sha256,
45+
truth_status,
46+
valid_time,
47+
system_time,
48+
provenance_ref_sha256,
49+
evidence_ref_sha256
50+
) VALUES (
51+
'tenant-a',
52+
'recursive-admin-authority-probe',
53+
'batch.lifecycle.observed',
54+
repeat('a', 64),
55+
repeat('b', 64),
56+
repeat('c', 64),
57+
repeat('d', 64),
58+
'observed',
59+
'1970-01-01T00:00:00Z',
60+
'1970-01-01T00:00:00Z',
61+
repeat('e', 64),
62+
repeat('f', 64)
63+
);
64+
65+
CREATE ROLE cwl_llm_batch_outbox_recursive_leaf NOLOGIN
66+
NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS;
67+
CREATE ROLE cwl_llm_batch_outbox_recursive_inner NOLOGIN
68+
NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS;
69+
CREATE ROLE cwl_llm_batch_outbox_recursive_outer NOLOGIN
70+
NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS;
71+
CREATE ROLE cwl_llm_batch_outbox_recursive_login LOGIN
72+
NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS;
73+
74+
GRANT USAGE ON SCHEMA public
75+
TO cwl_llm_batch_outbox_recursive_leaf,
76+
cwl_llm_batch_outbox_recursive_inner,
77+
cwl_llm_batch_outbox_recursive_outer,
78+
cwl_llm_batch_outbox_recursive_login;
79+
GRANT SELECT, INSERT ON public.llm_context_lifecycle_outbox
80+
TO cwl_llm_batch_outbox_recursive_login;
81+
GRANT TRUNCATE ON public.llm_context_lifecycle_outbox
82+
TO cwl_llm_batch_outbox_recursive_leaf;
83+
GRANT cwl_llm_batch_outbox_recursive_leaf
84+
TO cwl_llm_batch_outbox_recursive_inner
85+
WITH INHERIT FALSE, SET TRUE;
86+
GRANT cwl_llm_batch_outbox_recursive_inner
87+
TO cwl_llm_batch_outbox_recursive_outer
88+
WITH ADMIN TRUE, INHERIT FALSE, SET FALSE;
89+
GRANT cwl_llm_batch_outbox_recursive_outer
90+
TO cwl_llm_batch_outbox_recursive_login
91+
WITH INHERIT FALSE, SET TRUE;
92+
SQL
93+
94+
initial_inner_set="$(
95+
docker exec "${container}" psql -U postgres -d postgres -Atqc \
96+
"SELECT pg_catalog.pg_has_role('cwl_llm_batch_outbox_recursive_login', 'cwl_llm_batch_outbox_recursive_inner', 'SET')"
97+
)"
98+
if [[ "${initial_inner_set}" != "f" ]]; then
99+
echo "recursive ADMIN specimen unexpectedly began with inner SET authority" >&2
100+
exit 1
101+
fi
102+
103+
admission="$(
104+
docker run --rm -i --network "container:${container}" "${component_image}" python - <<'PY'
105+
from pg_llm_batch.context_lifecycle_outbox import PostgresContextLifecycleOutboxStore
106+
from pg_llm_batch.exceptions import ConfigError
107+
108+
store = PostgresContextLifecycleOutboxStore(
109+
"postgresql://cwl_llm_batch_outbox_recursive_login@127.0.0.1/postgres",
110+
tenant_scope="tenant-a",
111+
tenant_scope_sha256="a" * 64,
112+
)
113+
try:
114+
store.load("recursive-admin-authority-probe")
115+
except ConfigError as exc:
116+
assert "separated forced RLS authority" in str(exc)
117+
print("REJECTED")
118+
else:
119+
print("ADMITTED")
120+
PY
121+
)"
122+
123+
docker exec -i "${container}" psql -h 127.0.0.1 \
124+
-U cwl_llm_batch_outbox_recursive_login -d postgres -v ON_ERROR_STOP=1 <<'SQL'
125+
SET ROLE cwl_llm_batch_outbox_recursive_outer;
126+
GRANT cwl_llm_batch_outbox_recursive_inner
127+
TO cwl_llm_batch_outbox_recursive_login
128+
WITH INHERIT FALSE, SET TRUE;
129+
RESET ROLE;
130+
SET ROLE cwl_llm_batch_outbox_recursive_inner;
131+
SET ROLE cwl_llm_batch_outbox_recursive_leaf;
132+
TRUNCATE TABLE public.llm_context_lifecycle_outbox;
133+
SQL
134+
135+
remaining="$(
136+
docker exec "${container}" psql -U postgres -d postgres -Atqc \
137+
"SELECT pg_catalog.count(*) FROM public.llm_context_lifecycle_outbox"
138+
)"
139+
if [[ "${remaining}" != "0" ]]; then
140+
echo "recursive ADMIN specimen did not materialize SET-reachable TRUNCATE authority" >&2
141+
exit 1
142+
fi
143+
144+
if [[ "${admission}" != "REJECTED" ]]; then
145+
echo "runtime admitted selectable-role ADMIN path to destructive outbox authority" >&2
146+
exit 1
147+
fi

0 commit comments

Comments
 (0)