Skip to content

Commit 6154cc3

Browse files
author
Meli
committed
fix(db): exclude kind:30179 ciphertext from brownfield FTS
Kind 30179 (NIP-PMA private managed agent) joined AUTHOR_ONLY_KINDS in block#4593, but only fresh installs stopped indexing it: migration 0008's positive allowlist never touches populated databases, and 0014 wrapped the retained brownfield expression for 30350 alone. A relay upgraded in place still tokenizes the NIP-44 ciphertext into events.search_tsv. Add migration 0033 in the 0014 shape: capture the current generated expression and re-add search_tsv wrapped with kind = 30179 -> NULL, so every other kind keeps whatever policy the database already had. Mirror the exclusion in schema/schema.sql. Tests: extend the populated-upgrade migration test with a 30179 row and a pre-0033 checkpoint (fails without the new file); assert 0033's shape in the embedded-migrator test; apply 0033 in the buzz-search FTS setup. Co-authored-by: Meli <5aaa86bce934fc3445fc254aab560a40923f10252f92107e665073dede0e04d3@buzz.block.builderlab.xyz> Signed-off-by: Meli <5aaa86bce934fc3445fc254aab560a40923f10252f92107e665073dede0e04d3@buzz.block.builderlab.xyz>
1 parent f249710 commit 6154cc3

4 files changed

Lines changed: 78 additions & 9 deletions

File tree

‎crates/buzz-db/src/migration.rs‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ mod tests {
645645
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
646646
migrations.sort_by_key(|migration| migration.version);
647647

648-
assert_eq!(migrations.len(), 32);
648+
assert_eq!(migrations.len(), 33);
649649
assert_eq!(migrations[0].version, 1);
650650
assert_eq!(&*migrations[0].description, "initial schema");
651651
assert!(migrations[0]
@@ -844,6 +844,16 @@ mod tests {
844844
assert!(migrations[13].sql.as_str().contains("search_tsv"));
845845
assert!(!migrations[0].sql.as_str().contains("30350"));
846846

847+
// NIP-PMA kind:30179 FTS exclusion (0033): same wrap-the-existing-
848+
// expression shape as 0014 so brownfield databases stop tokenizing
849+
// private managed-agent ciphertext without a heap rewrite.
850+
assert_eq!(migrations[32].version, 33);
851+
assert!(migrations[32].sql.as_str().contains("kind = 30179"));
852+
assert!(migrations[32].sql.as_str().contains("search_tsv"));
853+
assert!(!migrations[0].sql.as_str().contains("30179"));
854+
assert!(include_str!("../../../schema/schema.sql")
855+
.contains("kind IN (1059, 30179, 30300, 30350, 30622, 44100, 44101, 44200)"));
856+
847857
// Public push-gateway authority is intentionally deployment-global and
848858
// durable: immediate revocation and hostile-relay admission cannot be
849859
// honestly provided by a stateless gateway.
@@ -1915,7 +1925,7 @@ mod tests {
19151925

19161926
#[tokio::test]
19171927
#[ignore = "requires Postgres"]
1918-
async fn populated_upgrade_preserves_search_policy_except_for_push_leases() {
1928+
async fn populated_upgrade_preserves_search_policy_except_for_private_kinds() {
19191929
let pool = connect_test_pool().await;
19201930
reset_public_schema(&pool).await;
19211931
MIGRATOR
@@ -1931,7 +1941,7 @@ mod tests {
19311941
.await
19321942
.expect("insert community");
19331943

1934-
for (marker, kind) in [(1_u8, 1_i32), (2_u8, 30_350_i32)] {
1944+
for (marker, kind) in [(1_u8, 1_i32), (2_u8, 30_350_i32), (3_u8, 30_179_i32)] {
19351945
sqlx::query(
19361946
"INSERT INTO events \
19371947
(community_id, id, pubkey, created_at, kind, tags, content, sig, received_at) \
@@ -1958,19 +1968,37 @@ mod tests {
19581968
.fetch_all(&pool)
19591969
.await
19601970
.expect("read pre-push search behavior");
1961-
assert_eq!(before, vec![(1, true), (30_350, true)]);
1971+
assert_eq!(before, vec![(1, true), (30_179, true), (30_350, true)]);
1972+
1973+
// 0014 fixes 30350 only. A brownfield database that stopped here still
1974+
// tokenized kind:30179 ciphertext — the gap 0033 closes.
1975+
MIGRATOR
1976+
.run_to(32, &pool)
1977+
.await
1978+
.expect("apply migrations through 32");
1979+
let pre_0033: Vec<(i32, Option<bool>)> = sqlx::query_as(
1980+
"SELECT kind, search_tsv @@ plainto_tsquery('simple', 'needle') \
1981+
FROM events ORDER BY kind",
1982+
)
1983+
.fetch_all(&pool)
1984+
.await
1985+
.expect("read pre-0033 search behavior");
1986+
assert_eq!(
1987+
pre_0033,
1988+
vec![(1, Some(true)), (30_179, Some(true)), (30_350, None)]
1989+
);
19621990

19631991
run_migrations(&pool)
19641992
.await
1965-
.expect("apply push migrations to populated database");
1993+
.expect("apply remaining migrations to populated database");
19661994
let after: Vec<(i32, Option<bool>)> = sqlx::query_as(
19671995
"SELECT kind, search_tsv @@ plainto_tsquery('simple', 'needle') \
19681996
FROM events ORDER BY kind",
19691997
)
19701998
.fetch_all(&pool)
19711999
.await
1972-
.expect("read post-push search behavior");
1973-
assert_eq!(after, vec![(1, Some(true)), (30_350, None)]);
2000+
.expect("read post-upgrade search behavior");
2001+
assert_eq!(after, vec![(1, Some(true)), (30_179, None), (30_350, None)]);
19742002
}
19752003

19762004
#[tokio::test]

‎crates/buzz-search/tests/fts_integration.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const MIGRATION_0007_SQL: &str = include_str!("../../../migrations/0007_nip_rs_r
2828
const MIGRATION_0008_SQL: &str =
2929
include_str!("../../../migrations/0008_fresh_install_search_allowlist.sql");
3030
const MIGRATION_0014_SQL: &str = include_str!("../../../migrations/0014_push_lease_fts.sql");
31+
const MIGRATION_0033_SQL: &str =
32+
include_str!("../../../migrations/0033_private_managed_agent_fts.sql");
3133

3234
async fn setup() -> (PgPool, String) {
3335
let url = std::env::var("BUZZ_TEST_DATABASE_URL").unwrap_or_else(|_| TEST_DB_URL.to_string());
@@ -81,6 +83,9 @@ async fn setup() -> (PgPool, String) {
8183
pool.execute(MIGRATION_0014_SQL)
8284
.await
8385
.expect("apply 0014 migration");
86+
pool.execute(MIGRATION_0033_SQL)
87+
.await
88+
.expect("apply 0033 migration");
8489
(pool, schema)
8590
}
8691

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- NIP-PMA kind:30179 carries the owner's private managed-agent payload
2+
-- (agent nsec, env vars, prompt) as NIP-44 ciphertext and is author-only.
3+
-- Exclude it from full-text search without changing the search policy of
4+
-- existing installations. Fresh installs are already safe via the positive
5+
-- allowlist from migration 0008; this closes the brownfield gap where a
6+
-- populated database still runs the legacy negative skip-set (0001/0005)
7+
-- and would tokenize the ciphertext into search_tsv.
8+
--
9+
-- Same shape as 0014 (kind:30350): PostgreSQL cannot alter a generated
10+
-- expression in place, so capture the current expression, drop the column,
11+
-- and re-add it wrapped with the new exclusion. Every other kind keeps
12+
-- whatever policy the database had before.
13+
DO $$
14+
DECLARE
15+
existing_expression TEXT;
16+
BEGIN
17+
SELECT pg_get_expr(d.adbin, d.adrelid)
18+
INTO existing_expression
19+
FROM pg_attrdef d
20+
JOIN pg_attribute a
21+
ON a.attrelid = d.adrelid
22+
AND a.attnum = d.adnum
23+
WHERE d.adrelid = 'events'::regclass
24+
AND a.attname = 'search_tsv';
25+
26+
IF existing_expression IS NULL THEN
27+
RAISE EXCEPTION 'events.search_tsv generated expression not found';
28+
END IF;
29+
30+
ALTER TABLE events DROP COLUMN search_tsv;
31+
EXECUTE format(
32+
'ALTER TABLE events ADD COLUMN search_tsv TSVECTOR GENERATED ALWAYS AS (CASE WHEN kind = 30179 THEN NULL::tsvector ELSE (%s) END) STORED',
33+
existing_expression
34+
);
35+
CREATE INDEX idx_events_search_tsv ON events USING GIN (search_tsv);
36+
END $$;

‎schema/schema.sql‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ CREATE TABLE events (
219219
-- Privacy: encrypted/private routing wrappers and p-gated membership notices
220220
-- must never be discoverable through NIP-50 full-text search. NULL tsvector
221221
-- never matches `@@`.
222-
-- Keep in sync with migrations (final state: 0001 + 0005 + 0009).
222+
-- Keep in sync with migrations (final state: 0001 + 0005 + 0014 + 0033).
223223
search_tsv TSVECTOR GENERATED ALWAYS AS (
224-
CASE WHEN kind IN (1059, 30300, 30350, 30622, 44100, 44101, 44200) THEN NULL::tsvector
224+
CASE WHEN kind IN (1059, 30179, 30300, 30350, 30622, 44100, 44101, 44200) THEN NULL::tsvector
225225
ELSE to_tsvector('simple', content)
226226
END
227227
) STORED,

0 commit comments

Comments
 (0)