From 61d299ff23d939f247d1dcf1b1a11038679a397a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Sat, 14 Nov 2026 00:47:23 +0200 Subject: [PATCH 1/3] fix(ext): quote identifiers in pgsodium create_mask_view Build the generated trigger and function names with %I and pass the trigger body with %L, so table and column names are not spliced into the statement as raw text. Refs: SEC-1049 Co-Authored-By: Claude Fable 5.2 --- .../pgsodium-create-mask-view-quoting.patch | 51 +++++++++++++++++++ nix/ext/pgsodium.nix | 4 ++ nix/tests/expected/pgsodium.out | 33 ++++++++++++ nix/tests/sql/pgsodium.sql | 37 ++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 nix/ext/patches/pgsodium-create-mask-view-quoting.patch diff --git a/nix/ext/patches/pgsodium-create-mask-view-quoting.patch b/nix/ext/patches/pgsodium-create-mask-view-quoting.patch new file mode 100644 index 0000000000..cd111e32ca --- /dev/null +++ b/nix/ext/patches/pgsodium-create-mask-view-quoting.patch @@ -0,0 +1,51 @@ +diff --git a/sql/pgsodium--3.1.6--3.1.7.sql b/sql/pgsodium--3.1.6--3.1.7.sql +index 6dd97c9..7a696a2 100644 +--- a/sql/pgsodium--3.1.6--3.1.7.sql ++++ b/sql/pgsodium--3.1.6--3.1.7.sql +@@ -133,33 +133,29 @@ BEGIN + ELSE + body = format( + $c$ +- DROP FUNCTION IF EXISTS %1$s."%2$s_encrypt_secret_%3$s"() CASCADE; ++ DROP FUNCTION IF EXISTS %1$s.%2$I() CASCADE; + +- CREATE OR REPLACE FUNCTION %1$s."%2$s_encrypt_secret_%3$s"() ++ CREATE OR REPLACE FUNCTION %1$s.%2$I() + RETURNS TRIGGER + LANGUAGE plpgsql +- AS $t$ +- BEGIN +- %4$s; +- RETURN new; +- END; +- $t$; ++ AS %4$L; + +- ALTER FUNCTION %1$s."%2$s_encrypt_secret_%3$s"() OWNER TO %5$s; ++ ALTER FUNCTION %1$s.%2$I() OWNER TO %5$s; + +- DROP TRIGGER IF EXISTS "%2$s_encrypt_secret_trigger_%3$s" ON %6$s; ++ DROP TRIGGER IF EXISTS %3$I ON %6$s; + +- CREATE TRIGGER "%2$s_encrypt_secret_trigger_%3$s" +- BEFORE INSERT OR UPDATE OF "%3$s" ON %6$s ++ CREATE TRIGGER %3$I ++ BEFORE INSERT OR UPDATE OF %7$I ON %6$s + FOR EACH ROW +- EXECUTE FUNCTION %1$s."%2$s_encrypt_secret_%3$s" (); ++ EXECUTE FUNCTION %1$s.%2$I (); + $c$, + rule.relnamespace, +- rule.relname, +- m.attname, +- pgsodium.encrypted_column(relid, m), ++ rule.relname || '_encrypt_secret_' || m.attname, ++ rule.relname || '_encrypt_secret_trigger_' || m.attname, ++ format(E'\nBEGIN\n%s;\nRETURN new;\nEND;\n', pgsodium.encrypted_column(relid, m)), + view_owner, +- source_name ++ source_name, ++ m.attname + ); + if debug THEN + RAISE NOTICE '%', body; diff --git a/nix/ext/pgsodium.nix b/nix/ext/pgsodium.nix index b5a2dcb72c..7f1dd8bc91 100644 --- a/nix/ext/pgsodium.nix +++ b/nix/ext/pgsodium.nix @@ -50,6 +50,10 @@ let inherit hash; }; + patches = lib.optionals (version == latestVersion) [ + ./patches/pgsodium-create-mask-view-quoting.patch + ]; + installPhase = '' runHook preInstall diff --git a/nix/tests/expected/pgsodium.out b/nix/tests/expected/pgsodium.out index 418bf2dbb8..2ff27e8ebf 100644 --- a/nix/tests/expected/pgsodium.out +++ b/nix/tests/expected/pgsodium.out @@ -7,3 +7,36 @@ from valid (1 row) +set client_min_messages = warning; +create table public.pgsodium_mask_quoting ( + secret text, + "quo""te" text, + "dol$t$lar" text +); +security label for pgsodium on column public.pgsodium_mask_quoting.secret + is 'ENCRYPT WITH KEY ID 00000000-0000-0000-0000-000000000000'; +security label for pgsodium on column public.pgsodium_mask_quoting."quo""te" + is 'ENCRYPT WITH KEY ID 00000000-0000-0000-0000-000000000000'; +security label for pgsodium on column public.pgsodium_mask_quoting."dol$t$lar" + is 'ENCRYPT WITH KEY ID 00000000-0000-0000-0000-000000000000'; +select + tgname, + tgfoid::regproc +from + pg_trigger +where + tgrelid = 'public.pgsodium_mask_quoting'::regclass +order by + tgname; + tgname | tgfoid +--------------------------------------------------------+-------------------------------------------------- + pgsodium_mask_quoting_encrypt_secret_trigger_dol$t$lar | "pgsodium_mask_quoting_encrypt_secret_dol$t$lar" + pgsodium_mask_quoting_encrypt_secret_trigger_quo"te | "pgsodium_mask_quoting_encrypt_secret_quo""te" + pgsodium_mask_quoting_encrypt_secret_trigger_secret | pgsodium_mask_quoting_encrypt_secret_secret +(3 rows) + +drop table public.pgsodium_mask_quoting cascade; +drop function public.pgsodium_mask_quoting_encrypt_secret_secret(); +drop function public."pgsodium_mask_quoting_encrypt_secret_quo""te"(); +drop function public."pgsodium_mask_quoting_encrypt_secret_dol$t$lar"(); +reset client_min_messages; diff --git a/nix/tests/sql/pgsodium.sql b/nix/tests/sql/pgsodium.sql index cd3c3821ca..511294a845 100644 --- a/nix/tests/sql/pgsodium.sql +++ b/nix/tests/sql/pgsodium.sql @@ -2,3 +2,40 @@ select status from pgsodium.create_key(); + +set client_min_messages = warning; + +create table public.pgsodium_mask_quoting ( + secret text, + "quo""te" text, + "dol$t$lar" text +); + +security label for pgsodium on column public.pgsodium_mask_quoting.secret + is 'ENCRYPT WITH KEY ID 00000000-0000-0000-0000-000000000000'; + +security label for pgsodium on column public.pgsodium_mask_quoting."quo""te" + is 'ENCRYPT WITH KEY ID 00000000-0000-0000-0000-000000000000'; + +security label for pgsodium on column public.pgsodium_mask_quoting."dol$t$lar" + is 'ENCRYPT WITH KEY ID 00000000-0000-0000-0000-000000000000'; + +select + tgname, + tgfoid::regproc +from + pg_trigger +where + tgrelid = 'public.pgsodium_mask_quoting'::regclass +order by + tgname; + +drop table public.pgsodium_mask_quoting cascade; + +drop function public.pgsodium_mask_quoting_encrypt_secret_secret(); + +drop function public."pgsodium_mask_quoting_encrypt_secret_quo""te"(); + +drop function public."pgsodium_mask_quoting_encrypt_secret_dol$t$lar"(); + +reset client_min_messages; From 91ac34560287bc4c1e98ff6a51a9bd86208dcb88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Sat, 14 Nov 2026 01:54:52 +0200 Subject: [PATCH 2/3] fix(ext): cover pgsodium 3.1.5 and 3.1.6 in create_mask_view patch A restored project can pin pgsodium to an older installed version, so patch the 3.1.4--3.1.5 script as well. Build the trigger body from the upstream template so generated functions stay byte-identical for names that need no quoting. Refs: SEC-1049 Co-Authored-By: Claude Fable 5.2 --- .../pgsodium-create-mask-view-quoting.patch | 83 ++++++++++++++++++- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/nix/ext/patches/pgsodium-create-mask-view-quoting.patch b/nix/ext/patches/pgsodium-create-mask-view-quoting.patch index cd111e32ca..61843809b7 100644 --- a/nix/ext/patches/pgsodium-create-mask-view-quoting.patch +++ b/nix/ext/patches/pgsodium-create-mask-view-quoting.patch @@ -1,8 +1,78 @@ +diff --git a/sql/pgsodium--3.1.4--3.1.5.sql b/sql/pgsodium--3.1.4--3.1.5.sql +index c64d7ae..af43b60 100644 +--- a/sql/pgsodium--3.1.4--3.1.5.sql ++++ b/sql/pgsodium--3.1.4--3.1.5.sql +@@ -63,47 +63,35 @@ BEGIN + ELSE + body = format( + $c$ +- DROP FUNCTION IF EXISTS %s."%s_encrypt_secret_%s"() CASCADE; ++ DROP FUNCTION IF EXISTS %1$s.%2$I() CASCADE; + +- CREATE OR REPLACE FUNCTION %s."%s_encrypt_secret_%s"() ++ CREATE OR REPLACE FUNCTION %1$s.%2$I() + RETURNS TRIGGER + LANGUAGE plpgsql +- AS $t$ +- BEGIN +- %s; +- RETURN new; +- END; +- $t$; ++ AS %4$L; + +- ALTER FUNCTION %s."%s_encrypt_secret_%s"() OWNER TO %s; ++ ALTER FUNCTION %1$s.%2$I() OWNER TO %5$s; + +- DROP TRIGGER IF EXISTS "%s_encrypt_secret_trigger_%s" ON %s; ++ DROP TRIGGER IF EXISTS %3$I ON %6$s; + +- CREATE TRIGGER "%s_encrypt_secret_trigger_%s" +- BEFORE INSERT OR UPDATE OF "%s" ON %s ++ CREATE TRIGGER %3$I ++ BEFORE INSERT OR UPDATE OF %7$I ON %6$s + FOR EACH ROW +- EXECUTE FUNCTION %s."%s_encrypt_secret_%s" (); ++ EXECUTE FUNCTION %1$s.%2$I (); + $c$, + rule.relnamespace, +- rule.relname, +- m.attname, +- rule.relnamespace, +- rule.relname, +- m.attname, +- pgsodium.encrypted_column(relid, m), +- rule.relnamespace, +- rule.relname, +- m.attname, ++ rule.relname || '_encrypt_secret_' || m.attname, ++ rule.relname || '_encrypt_secret_trigger_' || m.attname, ++ format( ++ $t$ ++ BEGIN ++ %s; ++ RETURN new; ++ END; ++ $t$, ++ pgsodium.encrypted_column(relid, m)), + view_owner, +- rule.relname, +- m.attname, +- source_name, +- rule.relname, +- m.attname, +- m.attname, + source_name, +- rule.relnamespace, +- rule.relname, + m.attname + ); + if debug THEN diff --git a/sql/pgsodium--3.1.6--3.1.7.sql b/sql/pgsodium--3.1.6--3.1.7.sql -index 6dd97c9..7a696a2 100644 +index 6dd97c9..64d5c7c 100644 --- a/sql/pgsodium--3.1.6--3.1.7.sql +++ b/sql/pgsodium--3.1.6--3.1.7.sql -@@ -133,33 +133,29 @@ BEGIN +@@ -133,33 +133,36 @@ BEGIN ELSE body = format( $c$ @@ -41,7 +111,14 @@ index 6dd97c9..7a696a2 100644 - pgsodium.encrypted_column(relid, m), + rule.relname || '_encrypt_secret_' || m.attname, + rule.relname || '_encrypt_secret_trigger_' || m.attname, -+ format(E'\nBEGIN\n%s;\nRETURN new;\nEND;\n', pgsodium.encrypted_column(relid, m)), ++ format( ++ $t$ ++ BEGIN ++ %s; ++ RETURN new; ++ END; ++ $t$, ++ pgsodium.encrypted_column(relid, m)), view_owner, - source_name + source_name, From 654f883f96a52ff3873c43612fadffe237923e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Boros?= Date: Mon, 16 Nov 2026 18:55:42 +0200 Subject: [PATCH 3/3] fix(ext): patch only the latest pgsodium create_mask_view script New installs always create pgsodium at the latest version, so the 3.1.6 to 3.1.7 script is the last one to define create_mask_view. Refs: SEC-1049 Co-Authored-By: Claude Fable 5.2 --- .../pgsodium-create-mask-view-quoting.patch | 70 ------------------- 1 file changed, 70 deletions(-) diff --git a/nix/ext/patches/pgsodium-create-mask-view-quoting.patch b/nix/ext/patches/pgsodium-create-mask-view-quoting.patch index 61843809b7..fa8f2e1cb6 100644 --- a/nix/ext/patches/pgsodium-create-mask-view-quoting.patch +++ b/nix/ext/patches/pgsodium-create-mask-view-quoting.patch @@ -1,73 +1,3 @@ -diff --git a/sql/pgsodium--3.1.4--3.1.5.sql b/sql/pgsodium--3.1.4--3.1.5.sql -index c64d7ae..af43b60 100644 ---- a/sql/pgsodium--3.1.4--3.1.5.sql -+++ b/sql/pgsodium--3.1.4--3.1.5.sql -@@ -63,47 +63,35 @@ BEGIN - ELSE - body = format( - $c$ -- DROP FUNCTION IF EXISTS %s."%s_encrypt_secret_%s"() CASCADE; -+ DROP FUNCTION IF EXISTS %1$s.%2$I() CASCADE; - -- CREATE OR REPLACE FUNCTION %s."%s_encrypt_secret_%s"() -+ CREATE OR REPLACE FUNCTION %1$s.%2$I() - RETURNS TRIGGER - LANGUAGE plpgsql -- AS $t$ -- BEGIN -- %s; -- RETURN new; -- END; -- $t$; -+ AS %4$L; - -- ALTER FUNCTION %s."%s_encrypt_secret_%s"() OWNER TO %s; -+ ALTER FUNCTION %1$s.%2$I() OWNER TO %5$s; - -- DROP TRIGGER IF EXISTS "%s_encrypt_secret_trigger_%s" ON %s; -+ DROP TRIGGER IF EXISTS %3$I ON %6$s; - -- CREATE TRIGGER "%s_encrypt_secret_trigger_%s" -- BEFORE INSERT OR UPDATE OF "%s" ON %s -+ CREATE TRIGGER %3$I -+ BEFORE INSERT OR UPDATE OF %7$I ON %6$s - FOR EACH ROW -- EXECUTE FUNCTION %s."%s_encrypt_secret_%s" (); -+ EXECUTE FUNCTION %1$s.%2$I (); - $c$, - rule.relnamespace, -- rule.relname, -- m.attname, -- rule.relnamespace, -- rule.relname, -- m.attname, -- pgsodium.encrypted_column(relid, m), -- rule.relnamespace, -- rule.relname, -- m.attname, -+ rule.relname || '_encrypt_secret_' || m.attname, -+ rule.relname || '_encrypt_secret_trigger_' || m.attname, -+ format( -+ $t$ -+ BEGIN -+ %s; -+ RETURN new; -+ END; -+ $t$, -+ pgsodium.encrypted_column(relid, m)), - view_owner, -- rule.relname, -- m.attname, -- source_name, -- rule.relname, -- m.attname, -- m.attname, - source_name, -- rule.relnamespace, -- rule.relname, - m.attname - ); - if debug THEN diff --git a/sql/pgsodium--3.1.6--3.1.7.sql b/sql/pgsodium--3.1.6--3.1.7.sql index 6dd97c9..64d5c7c 100644 --- a/sql/pgsodium--3.1.6--3.1.7.sql