@nodii/dsar 0.15.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bind-pattern-a.d.ts +18 -9
- package/dist/bind-pattern-a.d.ts.map +1 -1
- package/dist/bind-pattern-a.js +12 -5
- package/dist/bind-pattern-a.js.map +1 -1
- package/dist/canonical.d.ts +27 -4
- package/dist/canonical.d.ts.map +1 -1
- package/dist/canonical.js +68 -53
- package/dist/canonical.js.map +1 -1
- package/dist/hold-failure-mode.d.ts +35 -0
- package/dist/hold-failure-mode.d.ts.map +1 -0
- package/dist/hold-failure-mode.js +78 -0
- package/dist/hold-failure-mode.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -1
- package/dist/index.js.map +1 -1
- package/dist/migrations/index.d.ts +62 -2
- package/dist/migrations/index.d.ts.map +1 -1
- package/dist/migrations/index.js +342 -4
- package/dist/migrations/index.js.map +1 -1
- package/dist/orchestrator.d.ts +13 -5
- package/dist/orchestrator.d.ts.map +1 -1
- package/dist/orchestrator.js +10 -3
- package/dist/orchestrator.js.map +1 -1
- package/package.json +4 -4
|
@@ -8,7 +8,7 @@ type SqlClient = {
|
|
|
8
8
|
* ENABLE ROW LEVEL SECURITY
|
|
9
9
|
* + nodii_owner FOR ALL bypass policy (BYPASSRLS role)
|
|
10
10
|
* + authenticated per-verb policies gated on
|
|
11
|
-
* `auth.
|
|
11
|
+
* `auth.session_tenant_ids()` (the session tenant INTERSECT membership)
|
|
12
12
|
* + nodii_services per-verb policies gated on the `app.tenant_id` GUC
|
|
13
13
|
* (`auth.get_tenant_id()`)
|
|
14
14
|
*
|
|
@@ -44,7 +44,7 @@ export declare function a1RlsDDL(table: string): string;
|
|
|
44
44
|
*
|
|
45
45
|
* **Guarded on `tenant_id` being `uuid`:** the per-verb `authenticated` +
|
|
46
46
|
* `nodii_services` policies, whose predicates compare `tenant_id` to a `uuid`
|
|
47
|
-
* (`auth.get_tenant_id()` / `auth.
|
|
47
|
+
* (`auth.get_tenant_id()` / `auth.session_tenant_ids()`). `lifecycle_requests` +
|
|
48
48
|
* `data_residency_holds` are converted to `tenant_id UUID` earlier in this
|
|
49
49
|
* migration; `platform_lifecycle_requests` may still be mid-rewrite TEXT on a
|
|
50
50
|
* deployment whose orchestrator (tenant-service) has not yet run its b1461a50
|
|
@@ -60,6 +60,66 @@ export declare function a1RlsDDL(table: string): string;
|
|
|
60
60
|
*/
|
|
61
61
|
export declare function a1RlsMigrationBlock(table: string): string;
|
|
62
62
|
export declare function getDsarMigrationSQL(): string;
|
|
63
|
+
/**
|
|
64
|
+
* THE DELIVERY UNIT — byte-equal inlined copy of `SESSION_FENCE_BOOTSTRAP_SQL`
|
|
65
|
+
* from `@nodii/db-rls`: the `auth.session_tenant_ids()` resolver that the
|
|
66
|
+
* policies below reference, plus the `auth.assert_tenant_fence()` gate.
|
|
67
|
+
*
|
|
68
|
+
* WHY THIS PACKAGE INSTALLS IT RATHER THAN DEMANDING SOMEBODY ELSE DID.
|
|
69
|
+
*
|
|
70
|
+
* The previous revision only CHECKED (`SESSION_RESOLVER_PRECONDITION_SQL`) and
|
|
71
|
+
* refused to migrate when the resolver was absent. That was correct as far as
|
|
72
|
+
* it went — it turned a silent, destructive half-migration into a clean refusal
|
|
73
|
+
* — but it left the service unable to boot, with no path to fix itself.
|
|
74
|
+
*
|
|
75
|
+
* There is no such path for most of the fleet. Measured, not assumed: of the
|
|
76
|
+
* ten services that call neither `applyAuthSchemaMigrations` nor
|
|
77
|
+
* `authSchemaHelpers`, SIX (billing, kyc, notification, task-tracking, tenant,
|
|
78
|
+
* geo) have NO database step in `deploy-dev.yml` at all — their `db:setup`
|
|
79
|
+
* runs only in `ci.yml`, against a throwaway Postgres. And a fleet-wide grep
|
|
80
|
+
* for `session_tenant_ids` across every service's vendored infra SQL returns
|
|
81
|
+
* ZERO files. So a refusal here is a service that cannot start and cannot be
|
|
82
|
+
* repaired by any pipeline that exists.
|
|
83
|
+
*
|
|
84
|
+
* Meanwhile THIS function already runs at boot in billing (`src/lib/bootstrap
|
|
85
|
+
* .ts`) and task-tracking (`src/bootstrap.ts`), against the real database, with
|
|
86
|
+
* owner privileges, immediately before the policies that need the resolver.
|
|
87
|
+
* That makes it the only reliable delivery channel these services have — so it
|
|
88
|
+
* delivers, rather than complaining that nobody else did.
|
|
89
|
+
*
|
|
90
|
+
* INLINED as a string constant, deliberately, so the published package carries
|
|
91
|
+
* NO dependency on `@nodii/db-rls` — the same posture as every other DDL
|
|
92
|
+
* constant in this module. The copy is asserted byte-equal to the db-rls
|
|
93
|
+
* original by `tests/fence-bootstrap-parity.test.ts`; it is not kept in step by
|
|
94
|
+
* anyone remembering to.
|
|
95
|
+
*
|
|
96
|
+
* SELF-CLASSIFYING. It installs the STRICT body where a real membership helper
|
|
97
|
+
* exists and the DEGRADED (fence-only, D415) body where `auth.user_tenant_ids`
|
|
98
|
+
* is absent or still the bootstrap stub. That distinction is derived from the
|
|
99
|
+
* catalog on every run, never from a per-service list — a list of exactly this
|
|
100
|
+
* kind was already wrong by six services on its first pass. It can upgrade
|
|
101
|
+
* degraded to strict once a mirror lands; it can never downgrade strict to
|
|
102
|
+
* degraded.
|
|
103
|
+
*/
|
|
104
|
+
export declare const SESSION_FENCE_BOOTSTRAP_SQL = "CREATE SCHEMA IF NOT EXISTS auth;\nDO $fence_install$\nDECLARE\n v_src text;\n v_is_stub boolean;\nBEGIN\n SELECT p.prosrc INTO v_src\n FROM pg_proc p\n JOIN pg_namespace n ON n.oid = p.pronamespace\n WHERE n.nspname = 'auth' AND p.proname = 'user_tenant_ids'\n LIMIT 1;\n\n -- Structural, not an exact-string compare: the bootstrap stub is\n -- `SELECT NULL::uuid WHERE false` \u2014 an unsatisfiable WHERE and no FROM. A\n -- reformatted or re-indented copy of the same dead body still classifies as a\n -- stub. Mirrors isStubMembershipHelper() in tenant-fence.ts exactly; the two\n -- are asserted equivalent in the test-suite so a fix to one cannot leave the\n -- other behind.\n v_is_stub := v_src IS NULL\n OR (v_src ~* 'where\\s+false' AND v_src !~* 'from\\s+\\w');\n\n IF v_is_stub THEN\n EXECUTE $fence_degraded$CREATE OR REPLACE FUNCTION auth.session_tenant_ids() RETURNS SETOF uuid\nLANGUAGE plpgsql STABLE SECURITY DEFINER\nSET search_path = pg_catalog, public, pg_temp\nAS $session$\nDECLARE\n v_tenant uuid;\nBEGIN\n -- DB_RLS_DEGRADED_NO_MEMBERSHIP_MIRROR: no membership mirror in this service,\n -- so the session tenant is accepted WITHOUT proving the principal holds it.\n -- Replace with the strict resolver as soon as a mirror exists.\n IF nullif(current_setting('app.user_id', true), '') IS NULL THEN\n RAISE EXCEPTION 'db_rls_session_user_missing: app.user_id is not bound on this connection'\n USING ERRCODE = '42501',\n HINT = 'Open the session with withUserContext({ userId, tenantId }, fn) from @nodii/db-rls.';\n END IF;\n v_tenant := nullif(current_setting('app.tenant_id', true), '')::uuid;\n IF v_tenant IS NULL THEN\n RAISE EXCEPTION 'db_rls_session_tenant_missing: app.tenant_id is not bound on this connection'\n USING ERRCODE = '42501',\n HINT = 'Every authenticated-pool session must be EXPLICITLY tenant-scoped. Open it with withUserContext({ userId, tenantId }, fn) from @nodii/db-rls.';\n END IF;\n RETURN QUERY SELECT v_tenant;\nEND\n$session$;$fence_degraded$;\n ELSE\n EXECUTE $fence_strict$CREATE OR REPLACE FUNCTION auth.session_tenant_ids() RETURNS SETOF uuid\nLANGUAGE plpgsql STABLE SECURITY DEFINER\nSET search_path = pg_catalog, public, pg_temp\nAS $session$\nDECLARE\n v_user uuid;\n v_tenant uuid;\nBEGIN\n v_user := nullif(current_setting('app.user_id', true), '')::uuid;\n v_tenant := nullif(current_setting('app.tenant_id', true), '')::uuid;\n IF v_user IS NULL THEN\n RAISE EXCEPTION 'db_rls_session_user_missing: app.user_id is not bound on this connection'\n USING ERRCODE = '42501',\n HINT = 'The authenticated pool is the user-session pool; a session with no principal cannot be membership-checked. Open it with withUserContext({ userId, tenantId }, fn) from @nodii/db-rls.';\n END IF;\n IF v_tenant IS NULL THEN\n RAISE EXCEPTION 'db_rls_session_tenant_missing: app.tenant_id is not bound on this connection'\n USING ERRCODE = '42501',\n HINT = 'Every authenticated-pool session must be EXPLICITLY tenant-scoped. Open it with withUserContext({ userId, tenantId }, fn) from @nodii/db-rls.';\n END IF;\n RETURN QUERY SELECT v_tenant WHERE v_tenant IN (SELECT auth.user_tenant_ids(v_user));\nEND\n$session$;$fence_strict$;\n END IF;\nEND\n$fence_install$;\nCREATE SCHEMA IF NOT EXISTS auth;\nCREATE OR REPLACE FUNCTION auth._fence_reason(p_expr text) RETURNS text\nLANGUAGE sql IMMUTABLE\nSET search_path = pg_catalog, public, pg_temp\nAS $reason$\n SELECT CASE\n WHEN p_expr ~* '(auth\\s*\\.\\s*)?user_tenant_ids\\s*\\('\n THEN 'consults auth.user_tenant_ids directly instead of auth.session_tenant_ids() \u2014 resolves across EVERY tenant the principal belongs to'\n WHEN p_expr IS NULL OR btrim(p_expr) = '' OR p_expr ~* '^\\(?\\s*true\\s*\\)?$'\n THEN 'is permissive with no tenant reference at all \u2014 every tenant, every row'\n ELSE 'does not resolve through auth.session_tenant_ids() \u2014 it may be fenced some other way, but nothing here proves it, and an unrecognized shape is not a safe shape'\n END\n$reason$;\nCREATE OR REPLACE FUNCTION auth.assert_tenant_fence(\n p_schemas text[] DEFAULT ARRAY['public'],\n p_exempt_policies text[] DEFAULT ARRAY[]::text[],\n p_exempt_tables text[] DEFAULT ARRAY[]::text[],\n p_tenant_columns text[] DEFAULT ARRAY['tenant_id','org_id','organization_id'],\n p_allow_degraded boolean DEFAULT true\n) RETURNS void\nLANGUAGE plpgsql STABLE SECURITY DEFINER\nSET search_path = pg_catalog, public, pg_temp\nAS $fence_check$\nDECLARE\n v_offenders text[] := ARRAY[]::text[];\n v_row record;\n v_resolver record;\n v_membership record;\n v_degraded boolean := false;\nBEGIN\n -- ---------------------------------------------------------------------\n -- 1. The resolver itself. If it is missing, every policy referencing it is\n -- already broken, and nothing below can be interpreted.\n -- ---------------------------------------------------------------------\n SELECT p.prosrc, p.prosecdef INTO v_resolver\n FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace\n WHERE n.nspname = 'auth' AND p.proname = 'session_tenant_ids' LIMIT 1;\n\n IF v_resolver IS NULL THEN\n RAISE EXCEPTION 'tenant_fence: auth.session_tenant_ids() is not installed'\n USING ERRCODE = 'P0001',\n HINT = 'Apply INSTALL_SESSION_RESOLVER_SQL (applyAuthSchemaMigrations ships it, and @nodii/saga + @nodii/dsar + @nodii/audit-chain install it before their policies).';\n END IF;\n\n v_degraded := v_resolver.prosrc LIKE '%DB_RLS_DEGRADED_NO_MEMBERSHIP_MIRROR%';\n\n IF v_resolver.prosrc !~* 'RAISE\\s+EXCEPTION' THEN\n v_offenders := v_offenders || 'auth.session_tenant_ids() [session_tenant_ids] does not RAISE on an unbound session \u2014 it was replaced with a NULL-returning getter, which restores silent failure';\n END IF;\n\n IF NOT v_resolver.prosecdef THEN\n v_offenders := v_offenders || 'auth.session_tenant_ids() [session_tenant_ids] is not SECURITY DEFINER \u2014 it cannot read the membership mirror (permission denied), or will recurse into the mirror''s own policy (54001)';\n END IF;\n\n IF v_degraded AND NOT p_allow_degraded THEN\n v_offenders := v_offenders || 'auth.session_tenant_ids() [session_tenant_ids] is the DEGRADED fence-only resolver \u2014 the session tenant is trusted WITHOUT proving membership';\n END IF;\n\n -- The stub check applies ONLY to the strict resolver. Under the degraded body\n -- auth.user_tenant_ids is never consulted, so a stub there is the expected,\n -- D415-sanctioned terminal state \u2014 not a defect. Flagging it would make every\n -- legitimately-degraded service permanently red, which is how a gate gets\n -- switched off.\n IF NOT v_degraded THEN\n SELECT p.prosrc INTO v_membership\n FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace\n WHERE n.nspname = 'auth' AND p.proname = 'user_tenant_ids' LIMIT 1;\n IF v_membership IS NOT NULL\n AND v_membership.prosrc ~* 'where\\s+false'\n AND v_membership.prosrc !~* 'from\\s+\\w' THEN\n v_offenders := v_offenders || 'auth.user_tenant_ids(uuid) [user_tenant_ids] is still the bootstrap stub returning the empty set \u2014 the strict resolver intersects to nothing, so the authenticated pool is silently deny-all';\n END IF;\n END IF;\n\n -- ---------------------------------------------------------------------\n -- 2. The positive rule over every authenticated-applicable policy.\n -- ---------------------------------------------------------------------\n FOR v_row IN\n SELECT n.nspname AS schemaname,\n c.relname AS tablename,\n pol.polname AS policyname,\n CASE pol.polcmd WHEN 'r' THEN 'SELECT' WHEN 'a' THEN 'INSERT'\n WHEN 'w' THEN 'UPDATE' WHEN 'd' THEN 'DELETE'\n ELSE 'ALL' END AS cmd,\n pg_get_expr(pol.polqual, pol.polrelid) AS qual,\n pg_get_expr(pol.polwithcheck, pol.polrelid) AS with_check\n FROM pg_policy pol\n JOIN pg_class c ON c.oid = pol.polrelid\n JOIN pg_namespace n ON n.oid = c.relnamespace\n WHERE n.nspname = ANY(p_schemas)\n -- RESTRICTIVE policies are ANDed; they can only narrow access, so they\n -- cannot be the source of a cross-tenant grant.\n AND pol.polpermissive\n AND (\n EXISTS (SELECT 1 FROM unnest(pol.polroles) r WHERE r = 0)\n OR (\n EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'authenticated')\n AND EXISTS (\n SELECT 1 FROM unnest(pol.polroles) r\n WHERE pg_has_role('authenticated', r, 'USAGE')\n )\n )\n )\n AND NOT (pol.polname = ANY(p_exempt_policies))\n AND NOT ((c.relname || '.' || pol.polname) = ANY(p_exempt_policies))\n AND NOT (c.relname = ANY(p_exempt_tables))\n AND NOT ((n.nspname || '.' || c.relname) = ANY(p_exempt_tables))\n ORDER BY c.relname, pol.polname\n LOOP\n -- A policy with NEITHER clause is permissive-everything.\n IF v_row.qual IS NULL AND v_row.with_check IS NULL THEN\n v_offenders := v_offenders || format(\n '%s.%s [%s %s] is permissive with no tenant reference at all \u2014 every tenant, every row',\n v_row.schemaname, v_row.tablename, v_row.policyname, v_row.cmd);\n CONTINUE;\n END IF;\n\n -- EVERY clause present must resolve. A fenced read with an unfenced\n -- WITH CHECK is a cross-tenant WRITE, which is what the refund incident\n -- actually exercised.\n IF v_row.qual IS NOT NULL\n AND v_row.qual !~* '(auth\\s*\\.\\s*)?session_tenant_ids\\s*\\(' THEN\n v_offenders := v_offenders || format('%s.%s [%s %s USING] %s :: %s',\n v_row.schemaname, v_row.tablename, v_row.policyname, v_row.cmd,\n auth._fence_reason(v_row.qual), v_row.qual);\n END IF;\n\n IF v_row.with_check IS NOT NULL\n AND v_row.with_check !~* '(auth\\s*\\.\\s*)?session_tenant_ids\\s*\\(' THEN\n v_offenders := v_offenders || format('%s.%s [%s %s WITH CHECK] %s :: %s',\n v_row.schemaname, v_row.tablename, v_row.policyname, v_row.cmd,\n auth._fence_reason(v_row.with_check), v_row.with_check);\n END IF;\n END LOOP;\n\n -- ---------------------------------------------------------------------\n -- 3. Table coverage. THE HOLE A POLICY-LEVEL CHECK CANNOT SEE: a table with\n -- a tenant column and RLS switched off has no rows in pg_policy to\n -- inspect, and under the canonical blanket grants it is readable AND\n -- writable across every tenant \u2014 strictly more open than the union\n -- predicate this release removes.\n --\n -- Discovered FROM THE CATALOG, never from a caller-supplied list. The\n -- author who forgets ENABLE ROW LEVEL SECURITY is the same author who\n -- forgets the list entry, in the same migration.\n -- ---------------------------------------------------------------------\n FOR v_row IN\n SELECT n.nspname AS schemaname, c.relname AS tablename,\n c.relkind::text AS relkind, c.relrowsecurity,\n COALESCE((SELECT true FROM unnest(COALESCE(c.reloptions,'{}')) o\n WHERE lower(replace(o,' ','')) = 'security_invoker=true'), false) AS security_invoker\n FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace\n WHERE c.relkind = ANY('{r,p,v,m,f}')\n AND n.nspname = ANY(p_schemas)\n AND NOT (c.relname = ANY(p_exempt_tables))\n AND NOT ((n.nspname || '.' || c.relname) = ANY(p_exempt_tables))\n AND EXISTS (SELECT 1 FROM pg_attribute a\n WHERE a.attrelid = c.oid AND a.attnum > 0\n AND NOT a.attisdropped AND a.attname = ANY(p_tenant_columns))\n ORDER BY n.nspname, c.relname\n LOOP\n IF v_row.relkind IN ('v','m','f') THEN\n -- A view with security_invoker=true executes as the CALLER, so the\n -- underlying table's RLS applies and the view is genuinely safe.\n IF v_row.relkind = 'v' AND v_row.security_invoker THEN CONTINUE; END IF;\n v_offenders := v_offenders || format(\n '%s.%s exposes a tenant column but cannot enforce RLS (relkind=%s) \u2014 a materialized/foreign relation has no row policies at all, and a view without security_invoker=true runs as its OWNER and bypasses the underlying table''s policies',\n v_row.schemaname, v_row.tablename, v_row.relkind);\n ELSIF NOT v_row.relrowsecurity THEN\n v_offenders := v_offenders || format(\n '%s.%s has a tenant column but ROW LEVEL SECURITY is NOT ENABLED \u2014 with the canonical blanket grants this table is readable and writable across EVERY tenant, and no policy is ever consulted',\n v_row.schemaname, v_row.tablename);\n END IF;\n END LOOP;\n\n IF array_length(v_offenders, 1) > 0 THEN\n RAISE EXCEPTION 'tenant_fence: % authenticated-pool policy/helper defect(s). A user session must reach exactly ONE tenant \u2014 the one the edge resolved from the request \u2014 and nothing else, ever.%',\n array_length(v_offenders, 1),\n E'\\n - ' || array_to_string(v_offenders, E'\\n - ')\n USING ERRCODE = 'P0001',\n HINT = 'Regenerate policies with @nodii/db-rls withRls() / generateRlsPolicy(), which emit \"<col> IN (SELECT auth.session_tenant_ids())\". Nodii-staff cross-tenant access is a different path entirely: the platform_ops pool + withPlatformOpsContext.';\n END IF;\nEND\n$fence_check$;\nGRANT EXECUTE ON FUNCTION auth._fence_reason(text) TO PUBLIC;\n";
|
|
105
|
+
/**
|
|
106
|
+
* PRECONDITION. Fails the migration BEFORE it drops anything if the resolver
|
|
107
|
+
* `auth.session_tenant_ids()` is absent.
|
|
108
|
+
*
|
|
109
|
+
* This library only REFERENCES the resolver; `@nodii/db-rls` installs it, and
|
|
110
|
+
* there is deliberately no package dependency between them (the published
|
|
111
|
+
* artifact carries no db-rls dep). So nothing in the module graph can enforce
|
|
112
|
+
* "upgrade db-rls first" — which makes this the enforcement point.
|
|
113
|
+
*
|
|
114
|
+
* Without it the failure is silent and destructive. The apply loop runs
|
|
115
|
+
* statements INDIVIDUALLY, not in one transaction, so on a database missing the
|
|
116
|
+
* resolver the DROP POLICY statements succeed and the CREATE POLICY statements
|
|
117
|
+
* fail with 42883 — leaving the tables RLS-enabled, `authenticated` holding its
|
|
118
|
+
* blanket CRUD grant, and ZERO policies. Deny-all, mid-migration, with nothing
|
|
119
|
+
* in the error to say the policies were dropped. Running this first turns that
|
|
120
|
+
* into a clean refusal with the old policies still in place.
|
|
121
|
+
*/
|
|
122
|
+
export declare const SESSION_RESOLVER_PRECONDITION_SQL = "DO $precondition$\nBEGIN\n IF to_regprocedure('auth.session_tenant_ids()') IS NULL THEN\n RAISE EXCEPTION 'db_rls_resolver_missing: auth.session_tenant_ids() is not installed, so the RLS policies in this migration cannot be created'\n USING ERRCODE = '42883',\n HINT = 'Upgrade @nodii/db-rls and apply its auth schema (applyAuthSchemaMigrations, or vendor 0004_session_tenant_ids.sql) BEFORE upgrading this package. Postgres validates the function reference at CREATE POLICY time.';\n END IF;\nEND\n$precondition$;";
|
|
63
123
|
/**
|
|
64
124
|
* Apply the DSAR migrations idempotently. Safe to call at every service boot.
|
|
65
125
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":"AAQA,KAAK,SAAS,GAAG;IAEf,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;CACzD,CAAC;AAwMF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":"AAQA,KAAK,SAAS,GAAG;IAEf,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;CACzD,CAAC;AAwMF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAsC9C;AAaD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAoCzD;AAmDD,wBAAgB,mBAAmB,IAAI,MAAM,CAE5C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,eAAO,MAAM,2BAA2B,kzaAkQvC,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,iCAAiC,4hBAQ9B,CAAC;AAEjB;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,GAAG,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAkBvE;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CA4BrD"}
|
package/dist/migrations/index.js
CHANGED
|
@@ -207,7 +207,7 @@ ${a1RlsMigrationBlock("data_residency_holds")}
|
|
|
207
207
|
* ENABLE ROW LEVEL SECURITY
|
|
208
208
|
* + nodii_owner FOR ALL bypass policy (BYPASSRLS role)
|
|
209
209
|
* + authenticated per-verb policies gated on
|
|
210
|
-
* `auth.
|
|
210
|
+
* `auth.session_tenant_ids()` (the session tenant INTERSECT membership)
|
|
211
211
|
* + nodii_services per-verb policies gated on the `app.tenant_id` GUC
|
|
212
212
|
* (`auth.get_tenant_id()`)
|
|
213
213
|
*
|
|
@@ -246,8 +246,8 @@ export function a1RlsDDL(table) {
|
|
|
246
246
|
};
|
|
247
247
|
// 1. Owner: FOR ALL bypass.
|
|
248
248
|
emit(`${table}_owner_policy`, "FOR ALL TO nodii_owner USING (true) WITH CHECK (true)");
|
|
249
|
-
// 2. authenticated — gated on auth.
|
|
250
|
-
const authCond = "tenant_id IN (SELECT auth.
|
|
249
|
+
// 2. authenticated — gated on auth.session_tenant_ids().
|
|
250
|
+
const authCond = "tenant_id IN (SELECT auth.session_tenant_ids())";
|
|
251
251
|
for (const verb of A1_RLS_VERBS) {
|
|
252
252
|
emit(`${table}_auth_${verb.toLowerCase()}_policy`, verbClause(verb, "authenticated", authCond));
|
|
253
253
|
}
|
|
@@ -282,7 +282,7 @@ function verbClause(verb, role, cond) {
|
|
|
282
282
|
*
|
|
283
283
|
* **Guarded on `tenant_id` being `uuid`:** the per-verb `authenticated` +
|
|
284
284
|
* `nodii_services` policies, whose predicates compare `tenant_id` to a `uuid`
|
|
285
|
-
* (`auth.get_tenant_id()` / `auth.
|
|
285
|
+
* (`auth.get_tenant_id()` / `auth.session_tenant_ids()`). `lifecycle_requests` +
|
|
286
286
|
* `data_residency_holds` are converted to `tenant_id UUID` earlier in this
|
|
287
287
|
* migration; `platform_lifecycle_requests` may still be mid-rewrite TEXT on a
|
|
288
288
|
* deployment whose orchestrator (tenant-service) has not yet run its b1461a50
|
|
@@ -381,6 +381,332 @@ $do$;`;
|
|
|
381
381
|
export function getDsarMigrationSQL() {
|
|
382
382
|
return DSAR_MIGRATION_SQL;
|
|
383
383
|
}
|
|
384
|
+
/**
|
|
385
|
+
* THE DELIVERY UNIT — byte-equal inlined copy of `SESSION_FENCE_BOOTSTRAP_SQL`
|
|
386
|
+
* from `@nodii/db-rls`: the `auth.session_tenant_ids()` resolver that the
|
|
387
|
+
* policies below reference, plus the `auth.assert_tenant_fence()` gate.
|
|
388
|
+
*
|
|
389
|
+
* WHY THIS PACKAGE INSTALLS IT RATHER THAN DEMANDING SOMEBODY ELSE DID.
|
|
390
|
+
*
|
|
391
|
+
* The previous revision only CHECKED (`SESSION_RESOLVER_PRECONDITION_SQL`) and
|
|
392
|
+
* refused to migrate when the resolver was absent. That was correct as far as
|
|
393
|
+
* it went — it turned a silent, destructive half-migration into a clean refusal
|
|
394
|
+
* — but it left the service unable to boot, with no path to fix itself.
|
|
395
|
+
*
|
|
396
|
+
* There is no such path for most of the fleet. Measured, not assumed: of the
|
|
397
|
+
* ten services that call neither `applyAuthSchemaMigrations` nor
|
|
398
|
+
* `authSchemaHelpers`, SIX (billing, kyc, notification, task-tracking, tenant,
|
|
399
|
+
* geo) have NO database step in `deploy-dev.yml` at all — their `db:setup`
|
|
400
|
+
* runs only in `ci.yml`, against a throwaway Postgres. And a fleet-wide grep
|
|
401
|
+
* for `session_tenant_ids` across every service's vendored infra SQL returns
|
|
402
|
+
* ZERO files. So a refusal here is a service that cannot start and cannot be
|
|
403
|
+
* repaired by any pipeline that exists.
|
|
404
|
+
*
|
|
405
|
+
* Meanwhile THIS function already runs at boot in billing (`src/lib/bootstrap
|
|
406
|
+
* .ts`) and task-tracking (`src/bootstrap.ts`), against the real database, with
|
|
407
|
+
* owner privileges, immediately before the policies that need the resolver.
|
|
408
|
+
* That makes it the only reliable delivery channel these services have — so it
|
|
409
|
+
* delivers, rather than complaining that nobody else did.
|
|
410
|
+
*
|
|
411
|
+
* INLINED as a string constant, deliberately, so the published package carries
|
|
412
|
+
* NO dependency on `@nodii/db-rls` — the same posture as every other DDL
|
|
413
|
+
* constant in this module. The copy is asserted byte-equal to the db-rls
|
|
414
|
+
* original by `tests/fence-bootstrap-parity.test.ts`; it is not kept in step by
|
|
415
|
+
* anyone remembering to.
|
|
416
|
+
*
|
|
417
|
+
* SELF-CLASSIFYING. It installs the STRICT body where a real membership helper
|
|
418
|
+
* exists and the DEGRADED (fence-only, D415) body where `auth.user_tenant_ids`
|
|
419
|
+
* is absent or still the bootstrap stub. That distinction is derived from the
|
|
420
|
+
* catalog on every run, never from a per-service list — a list of exactly this
|
|
421
|
+
* kind was already wrong by six services on its first pass. It can upgrade
|
|
422
|
+
* degraded to strict once a mirror lands; it can never downgrade strict to
|
|
423
|
+
* degraded.
|
|
424
|
+
*/
|
|
425
|
+
export const SESSION_FENCE_BOOTSTRAP_SQL = `CREATE SCHEMA IF NOT EXISTS auth;
|
|
426
|
+
DO $fence_install$
|
|
427
|
+
DECLARE
|
|
428
|
+
v_src text;
|
|
429
|
+
v_is_stub boolean;
|
|
430
|
+
BEGIN
|
|
431
|
+
SELECT p.prosrc INTO v_src
|
|
432
|
+
FROM pg_proc p
|
|
433
|
+
JOIN pg_namespace n ON n.oid = p.pronamespace
|
|
434
|
+
WHERE n.nspname = 'auth' AND p.proname = 'user_tenant_ids'
|
|
435
|
+
LIMIT 1;
|
|
436
|
+
|
|
437
|
+
-- Structural, not an exact-string compare: the bootstrap stub is
|
|
438
|
+
-- \`SELECT NULL::uuid WHERE false\` — an unsatisfiable WHERE and no FROM. A
|
|
439
|
+
-- reformatted or re-indented copy of the same dead body still classifies as a
|
|
440
|
+
-- stub. Mirrors isStubMembershipHelper() in tenant-fence.ts exactly; the two
|
|
441
|
+
-- are asserted equivalent in the test-suite so a fix to one cannot leave the
|
|
442
|
+
-- other behind.
|
|
443
|
+
v_is_stub := v_src IS NULL
|
|
444
|
+
OR (v_src ~* 'where\\s+false' AND v_src !~* 'from\\s+\\w');
|
|
445
|
+
|
|
446
|
+
IF v_is_stub THEN
|
|
447
|
+
EXECUTE $fence_degraded$CREATE OR REPLACE FUNCTION auth.session_tenant_ids() RETURNS SETOF uuid
|
|
448
|
+
LANGUAGE plpgsql STABLE SECURITY DEFINER
|
|
449
|
+
SET search_path = pg_catalog, public, pg_temp
|
|
450
|
+
AS $session$
|
|
451
|
+
DECLARE
|
|
452
|
+
v_tenant uuid;
|
|
453
|
+
BEGIN
|
|
454
|
+
-- DB_RLS_DEGRADED_NO_MEMBERSHIP_MIRROR: no membership mirror in this service,
|
|
455
|
+
-- so the session tenant is accepted WITHOUT proving the principal holds it.
|
|
456
|
+
-- Replace with the strict resolver as soon as a mirror exists.
|
|
457
|
+
IF nullif(current_setting('app.user_id', true), '') IS NULL THEN
|
|
458
|
+
RAISE EXCEPTION 'db_rls_session_user_missing: app.user_id is not bound on this connection'
|
|
459
|
+
USING ERRCODE = '42501',
|
|
460
|
+
HINT = 'Open the session with withUserContext({ userId, tenantId }, fn) from @nodii/db-rls.';
|
|
461
|
+
END IF;
|
|
462
|
+
v_tenant := nullif(current_setting('app.tenant_id', true), '')::uuid;
|
|
463
|
+
IF v_tenant IS NULL THEN
|
|
464
|
+
RAISE EXCEPTION 'db_rls_session_tenant_missing: app.tenant_id is not bound on this connection'
|
|
465
|
+
USING ERRCODE = '42501',
|
|
466
|
+
HINT = 'Every authenticated-pool session must be EXPLICITLY tenant-scoped. Open it with withUserContext({ userId, tenantId }, fn) from @nodii/db-rls.';
|
|
467
|
+
END IF;
|
|
468
|
+
RETURN QUERY SELECT v_tenant;
|
|
469
|
+
END
|
|
470
|
+
$session$;$fence_degraded$;
|
|
471
|
+
ELSE
|
|
472
|
+
EXECUTE $fence_strict$CREATE OR REPLACE FUNCTION auth.session_tenant_ids() RETURNS SETOF uuid
|
|
473
|
+
LANGUAGE plpgsql STABLE SECURITY DEFINER
|
|
474
|
+
SET search_path = pg_catalog, public, pg_temp
|
|
475
|
+
AS $session$
|
|
476
|
+
DECLARE
|
|
477
|
+
v_user uuid;
|
|
478
|
+
v_tenant uuid;
|
|
479
|
+
BEGIN
|
|
480
|
+
v_user := nullif(current_setting('app.user_id', true), '')::uuid;
|
|
481
|
+
v_tenant := nullif(current_setting('app.tenant_id', true), '')::uuid;
|
|
482
|
+
IF v_user IS NULL THEN
|
|
483
|
+
RAISE EXCEPTION 'db_rls_session_user_missing: app.user_id is not bound on this connection'
|
|
484
|
+
USING ERRCODE = '42501',
|
|
485
|
+
HINT = 'The authenticated pool is the user-session pool; a session with no principal cannot be membership-checked. Open it with withUserContext({ userId, tenantId }, fn) from @nodii/db-rls.';
|
|
486
|
+
END IF;
|
|
487
|
+
IF v_tenant IS NULL THEN
|
|
488
|
+
RAISE EXCEPTION 'db_rls_session_tenant_missing: app.tenant_id is not bound on this connection'
|
|
489
|
+
USING ERRCODE = '42501',
|
|
490
|
+
HINT = 'Every authenticated-pool session must be EXPLICITLY tenant-scoped. Open it with withUserContext({ userId, tenantId }, fn) from @nodii/db-rls.';
|
|
491
|
+
END IF;
|
|
492
|
+
RETURN QUERY SELECT v_tenant WHERE v_tenant IN (SELECT auth.user_tenant_ids(v_user));
|
|
493
|
+
END
|
|
494
|
+
$session$;$fence_strict$;
|
|
495
|
+
END IF;
|
|
496
|
+
END
|
|
497
|
+
$fence_install$;
|
|
498
|
+
CREATE SCHEMA IF NOT EXISTS auth;
|
|
499
|
+
CREATE OR REPLACE FUNCTION auth._fence_reason(p_expr text) RETURNS text
|
|
500
|
+
LANGUAGE sql IMMUTABLE
|
|
501
|
+
SET search_path = pg_catalog, public, pg_temp
|
|
502
|
+
AS $reason$
|
|
503
|
+
SELECT CASE
|
|
504
|
+
WHEN p_expr ~* '(auth\\s*\\.\\s*)?user_tenant_ids\\s*\\('
|
|
505
|
+
THEN 'consults auth.user_tenant_ids directly instead of auth.session_tenant_ids() — resolves across EVERY tenant the principal belongs to'
|
|
506
|
+
WHEN p_expr IS NULL OR btrim(p_expr) = '' OR p_expr ~* '^\\(?\\s*true\\s*\\)?$'
|
|
507
|
+
THEN 'is permissive with no tenant reference at all — every tenant, every row'
|
|
508
|
+
ELSE 'does not resolve through auth.session_tenant_ids() — it may be fenced some other way, but nothing here proves it, and an unrecognized shape is not a safe shape'
|
|
509
|
+
END
|
|
510
|
+
$reason$;
|
|
511
|
+
CREATE OR REPLACE FUNCTION auth.assert_tenant_fence(
|
|
512
|
+
p_schemas text[] DEFAULT ARRAY['public'],
|
|
513
|
+
p_exempt_policies text[] DEFAULT ARRAY[]::text[],
|
|
514
|
+
p_exempt_tables text[] DEFAULT ARRAY[]::text[],
|
|
515
|
+
p_tenant_columns text[] DEFAULT ARRAY['tenant_id','org_id','organization_id'],
|
|
516
|
+
p_allow_degraded boolean DEFAULT true
|
|
517
|
+
) RETURNS void
|
|
518
|
+
LANGUAGE plpgsql STABLE SECURITY DEFINER
|
|
519
|
+
SET search_path = pg_catalog, public, pg_temp
|
|
520
|
+
AS $fence_check$
|
|
521
|
+
DECLARE
|
|
522
|
+
v_offenders text[] := ARRAY[]::text[];
|
|
523
|
+
v_row record;
|
|
524
|
+
v_resolver record;
|
|
525
|
+
v_membership record;
|
|
526
|
+
v_degraded boolean := false;
|
|
527
|
+
BEGIN
|
|
528
|
+
-- ---------------------------------------------------------------------
|
|
529
|
+
-- 1. The resolver itself. If it is missing, every policy referencing it is
|
|
530
|
+
-- already broken, and nothing below can be interpreted.
|
|
531
|
+
-- ---------------------------------------------------------------------
|
|
532
|
+
SELECT p.prosrc, p.prosecdef INTO v_resolver
|
|
533
|
+
FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
|
|
534
|
+
WHERE n.nspname = 'auth' AND p.proname = 'session_tenant_ids' LIMIT 1;
|
|
535
|
+
|
|
536
|
+
IF v_resolver IS NULL THEN
|
|
537
|
+
RAISE EXCEPTION 'tenant_fence: auth.session_tenant_ids() is not installed'
|
|
538
|
+
USING ERRCODE = 'P0001',
|
|
539
|
+
HINT = 'Apply INSTALL_SESSION_RESOLVER_SQL (applyAuthSchemaMigrations ships it, and @nodii/saga + @nodii/dsar + @nodii/audit-chain install it before their policies).';
|
|
540
|
+
END IF;
|
|
541
|
+
|
|
542
|
+
v_degraded := v_resolver.prosrc LIKE '%DB_RLS_DEGRADED_NO_MEMBERSHIP_MIRROR%';
|
|
543
|
+
|
|
544
|
+
IF v_resolver.prosrc !~* 'RAISE\\s+EXCEPTION' THEN
|
|
545
|
+
v_offenders := v_offenders || 'auth.session_tenant_ids() [session_tenant_ids] does not RAISE on an unbound session — it was replaced with a NULL-returning getter, which restores silent failure';
|
|
546
|
+
END IF;
|
|
547
|
+
|
|
548
|
+
IF NOT v_resolver.prosecdef THEN
|
|
549
|
+
v_offenders := v_offenders || 'auth.session_tenant_ids() [session_tenant_ids] is not SECURITY DEFINER — it cannot read the membership mirror (permission denied), or will recurse into the mirror''s own policy (54001)';
|
|
550
|
+
END IF;
|
|
551
|
+
|
|
552
|
+
IF v_degraded AND NOT p_allow_degraded THEN
|
|
553
|
+
v_offenders := v_offenders || 'auth.session_tenant_ids() [session_tenant_ids] is the DEGRADED fence-only resolver — the session tenant is trusted WITHOUT proving membership';
|
|
554
|
+
END IF;
|
|
555
|
+
|
|
556
|
+
-- The stub check applies ONLY to the strict resolver. Under the degraded body
|
|
557
|
+
-- auth.user_tenant_ids is never consulted, so a stub there is the expected,
|
|
558
|
+
-- D415-sanctioned terminal state — not a defect. Flagging it would make every
|
|
559
|
+
-- legitimately-degraded service permanently red, which is how a gate gets
|
|
560
|
+
-- switched off.
|
|
561
|
+
IF NOT v_degraded THEN
|
|
562
|
+
SELECT p.prosrc INTO v_membership
|
|
563
|
+
FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
|
|
564
|
+
WHERE n.nspname = 'auth' AND p.proname = 'user_tenant_ids' LIMIT 1;
|
|
565
|
+
IF v_membership IS NOT NULL
|
|
566
|
+
AND v_membership.prosrc ~* 'where\\s+false'
|
|
567
|
+
AND v_membership.prosrc !~* 'from\\s+\\w' THEN
|
|
568
|
+
v_offenders := v_offenders || 'auth.user_tenant_ids(uuid) [user_tenant_ids] is still the bootstrap stub returning the empty set — the strict resolver intersects to nothing, so the authenticated pool is silently deny-all';
|
|
569
|
+
END IF;
|
|
570
|
+
END IF;
|
|
571
|
+
|
|
572
|
+
-- ---------------------------------------------------------------------
|
|
573
|
+
-- 2. The positive rule over every authenticated-applicable policy.
|
|
574
|
+
-- ---------------------------------------------------------------------
|
|
575
|
+
FOR v_row IN
|
|
576
|
+
SELECT n.nspname AS schemaname,
|
|
577
|
+
c.relname AS tablename,
|
|
578
|
+
pol.polname AS policyname,
|
|
579
|
+
CASE pol.polcmd WHEN 'r' THEN 'SELECT' WHEN 'a' THEN 'INSERT'
|
|
580
|
+
WHEN 'w' THEN 'UPDATE' WHEN 'd' THEN 'DELETE'
|
|
581
|
+
ELSE 'ALL' END AS cmd,
|
|
582
|
+
pg_get_expr(pol.polqual, pol.polrelid) AS qual,
|
|
583
|
+
pg_get_expr(pol.polwithcheck, pol.polrelid) AS with_check
|
|
584
|
+
FROM pg_policy pol
|
|
585
|
+
JOIN pg_class c ON c.oid = pol.polrelid
|
|
586
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
587
|
+
WHERE n.nspname = ANY(p_schemas)
|
|
588
|
+
-- RESTRICTIVE policies are ANDed; they can only narrow access, so they
|
|
589
|
+
-- cannot be the source of a cross-tenant grant.
|
|
590
|
+
AND pol.polpermissive
|
|
591
|
+
AND (
|
|
592
|
+
EXISTS (SELECT 1 FROM unnest(pol.polroles) r WHERE r = 0)
|
|
593
|
+
OR (
|
|
594
|
+
EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'authenticated')
|
|
595
|
+
AND EXISTS (
|
|
596
|
+
SELECT 1 FROM unnest(pol.polroles) r
|
|
597
|
+
WHERE pg_has_role('authenticated', r, 'USAGE')
|
|
598
|
+
)
|
|
599
|
+
)
|
|
600
|
+
)
|
|
601
|
+
AND NOT (pol.polname = ANY(p_exempt_policies))
|
|
602
|
+
AND NOT ((c.relname || '.' || pol.polname) = ANY(p_exempt_policies))
|
|
603
|
+
AND NOT (c.relname = ANY(p_exempt_tables))
|
|
604
|
+
AND NOT ((n.nspname || '.' || c.relname) = ANY(p_exempt_tables))
|
|
605
|
+
ORDER BY c.relname, pol.polname
|
|
606
|
+
LOOP
|
|
607
|
+
-- A policy with NEITHER clause is permissive-everything.
|
|
608
|
+
IF v_row.qual IS NULL AND v_row.with_check IS NULL THEN
|
|
609
|
+
v_offenders := v_offenders || format(
|
|
610
|
+
'%s.%s [%s %s] is permissive with no tenant reference at all — every tenant, every row',
|
|
611
|
+
v_row.schemaname, v_row.tablename, v_row.policyname, v_row.cmd);
|
|
612
|
+
CONTINUE;
|
|
613
|
+
END IF;
|
|
614
|
+
|
|
615
|
+
-- EVERY clause present must resolve. A fenced read with an unfenced
|
|
616
|
+
-- WITH CHECK is a cross-tenant WRITE, which is what the refund incident
|
|
617
|
+
-- actually exercised.
|
|
618
|
+
IF v_row.qual IS NOT NULL
|
|
619
|
+
AND v_row.qual !~* '(auth\\s*\\.\\s*)?session_tenant_ids\\s*\\(' THEN
|
|
620
|
+
v_offenders := v_offenders || format('%s.%s [%s %s USING] %s :: %s',
|
|
621
|
+
v_row.schemaname, v_row.tablename, v_row.policyname, v_row.cmd,
|
|
622
|
+
auth._fence_reason(v_row.qual), v_row.qual);
|
|
623
|
+
END IF;
|
|
624
|
+
|
|
625
|
+
IF v_row.with_check IS NOT NULL
|
|
626
|
+
AND v_row.with_check !~* '(auth\\s*\\.\\s*)?session_tenant_ids\\s*\\(' THEN
|
|
627
|
+
v_offenders := v_offenders || format('%s.%s [%s %s WITH CHECK] %s :: %s',
|
|
628
|
+
v_row.schemaname, v_row.tablename, v_row.policyname, v_row.cmd,
|
|
629
|
+
auth._fence_reason(v_row.with_check), v_row.with_check);
|
|
630
|
+
END IF;
|
|
631
|
+
END LOOP;
|
|
632
|
+
|
|
633
|
+
-- ---------------------------------------------------------------------
|
|
634
|
+
-- 3. Table coverage. THE HOLE A POLICY-LEVEL CHECK CANNOT SEE: a table with
|
|
635
|
+
-- a tenant column and RLS switched off has no rows in pg_policy to
|
|
636
|
+
-- inspect, and under the canonical blanket grants it is readable AND
|
|
637
|
+
-- writable across every tenant — strictly more open than the union
|
|
638
|
+
-- predicate this release removes.
|
|
639
|
+
--
|
|
640
|
+
-- Discovered FROM THE CATALOG, never from a caller-supplied list. The
|
|
641
|
+
-- author who forgets ENABLE ROW LEVEL SECURITY is the same author who
|
|
642
|
+
-- forgets the list entry, in the same migration.
|
|
643
|
+
-- ---------------------------------------------------------------------
|
|
644
|
+
FOR v_row IN
|
|
645
|
+
SELECT n.nspname AS schemaname, c.relname AS tablename,
|
|
646
|
+
c.relkind::text AS relkind, c.relrowsecurity,
|
|
647
|
+
COALESCE((SELECT true FROM unnest(COALESCE(c.reloptions,'{}')) o
|
|
648
|
+
WHERE lower(replace(o,' ','')) = 'security_invoker=true'), false) AS security_invoker
|
|
649
|
+
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
650
|
+
WHERE c.relkind = ANY('{r,p,v,m,f}')
|
|
651
|
+
AND n.nspname = ANY(p_schemas)
|
|
652
|
+
AND NOT (c.relname = ANY(p_exempt_tables))
|
|
653
|
+
AND NOT ((n.nspname || '.' || c.relname) = ANY(p_exempt_tables))
|
|
654
|
+
AND EXISTS (SELECT 1 FROM pg_attribute a
|
|
655
|
+
WHERE a.attrelid = c.oid AND a.attnum > 0
|
|
656
|
+
AND NOT a.attisdropped AND a.attname = ANY(p_tenant_columns))
|
|
657
|
+
ORDER BY n.nspname, c.relname
|
|
658
|
+
LOOP
|
|
659
|
+
IF v_row.relkind IN ('v','m','f') THEN
|
|
660
|
+
-- A view with security_invoker=true executes as the CALLER, so the
|
|
661
|
+
-- underlying table's RLS applies and the view is genuinely safe.
|
|
662
|
+
IF v_row.relkind = 'v' AND v_row.security_invoker THEN CONTINUE; END IF;
|
|
663
|
+
v_offenders := v_offenders || format(
|
|
664
|
+
'%s.%s exposes a tenant column but cannot enforce RLS (relkind=%s) — a materialized/foreign relation has no row policies at all, and a view without security_invoker=true runs as its OWNER and bypasses the underlying table''s policies',
|
|
665
|
+
v_row.schemaname, v_row.tablename, v_row.relkind);
|
|
666
|
+
ELSIF NOT v_row.relrowsecurity THEN
|
|
667
|
+
v_offenders := v_offenders || format(
|
|
668
|
+
'%s.%s has a tenant column but ROW LEVEL SECURITY is NOT ENABLED — with the canonical blanket grants this table is readable and writable across EVERY tenant, and no policy is ever consulted',
|
|
669
|
+
v_row.schemaname, v_row.tablename);
|
|
670
|
+
END IF;
|
|
671
|
+
END LOOP;
|
|
672
|
+
|
|
673
|
+
IF array_length(v_offenders, 1) > 0 THEN
|
|
674
|
+
RAISE EXCEPTION 'tenant_fence: % authenticated-pool policy/helper defect(s). A user session must reach exactly ONE tenant — the one the edge resolved from the request — and nothing else, ever.%',
|
|
675
|
+
array_length(v_offenders, 1),
|
|
676
|
+
E'\\n - ' || array_to_string(v_offenders, E'\\n - ')
|
|
677
|
+
USING ERRCODE = 'P0001',
|
|
678
|
+
HINT = 'Regenerate policies with @nodii/db-rls withRls() / generateRlsPolicy(), which emit "<col> IN (SELECT auth.session_tenant_ids())". Nodii-staff cross-tenant access is a different path entirely: the platform_ops pool + withPlatformOpsContext.';
|
|
679
|
+
END IF;
|
|
680
|
+
END
|
|
681
|
+
$fence_check$;
|
|
682
|
+
GRANT EXECUTE ON FUNCTION auth._fence_reason(text) TO PUBLIC;
|
|
683
|
+
`;
|
|
684
|
+
/**
|
|
685
|
+
* PRECONDITION. Fails the migration BEFORE it drops anything if the resolver
|
|
686
|
+
* `auth.session_tenant_ids()` is absent.
|
|
687
|
+
*
|
|
688
|
+
* This library only REFERENCES the resolver; `@nodii/db-rls` installs it, and
|
|
689
|
+
* there is deliberately no package dependency between them (the published
|
|
690
|
+
* artifact carries no db-rls dep). So nothing in the module graph can enforce
|
|
691
|
+
* "upgrade db-rls first" — which makes this the enforcement point.
|
|
692
|
+
*
|
|
693
|
+
* Without it the failure is silent and destructive. The apply loop runs
|
|
694
|
+
* statements INDIVIDUALLY, not in one transaction, so on a database missing the
|
|
695
|
+
* resolver the DROP POLICY statements succeed and the CREATE POLICY statements
|
|
696
|
+
* fail with 42883 — leaving the tables RLS-enabled, `authenticated` holding its
|
|
697
|
+
* blanket CRUD grant, and ZERO policies. Deny-all, mid-migration, with nothing
|
|
698
|
+
* in the error to say the policies were dropped. Running this first turns that
|
|
699
|
+
* into a clean refusal with the old policies still in place.
|
|
700
|
+
*/
|
|
701
|
+
export const SESSION_RESOLVER_PRECONDITION_SQL = `DO $precondition$
|
|
702
|
+
BEGIN
|
|
703
|
+
IF to_regprocedure('auth.session_tenant_ids()') IS NULL THEN
|
|
704
|
+
RAISE EXCEPTION 'db_rls_resolver_missing: auth.session_tenant_ids() is not installed, so the RLS policies in this migration cannot be created'
|
|
705
|
+
USING ERRCODE = '42883',
|
|
706
|
+
HINT = 'Upgrade @nodii/db-rls and apply its auth schema (applyAuthSchemaMigrations, or vendor 0004_session_tenant_ids.sql) BEFORE upgrading this package. Postgres validates the function reference at CREATE POLICY time.';
|
|
707
|
+
END IF;
|
|
708
|
+
END
|
|
709
|
+
$precondition$;`;
|
|
384
710
|
/**
|
|
385
711
|
* Apply the DSAR migrations idempotently. Safe to call at every service boot.
|
|
386
712
|
*
|
|
@@ -388,6 +714,18 @@ export function getDsarMigrationSQL() {
|
|
|
388
714
|
* protocol; we split on `;` boundaries we control.
|
|
389
715
|
*/
|
|
390
716
|
export async function applyDsarMigrations(sql) {
|
|
717
|
+
// DELIVER the resolver + gate, then verify. Installing is the whole point:
|
|
718
|
+
// this runs at boot with owner privileges against the real database, and for
|
|
719
|
+
// several services it is the ONLY code that ever does — see
|
|
720
|
+
// SESSION_FENCE_BOOTSTRAP_SQL. The precondition below is retained as a
|
|
721
|
+
// POST-condition: if the install somehow did not take (an older db-rls
|
|
722
|
+
// vendored copy, a partially-applied blob, a permissions failure that did not
|
|
723
|
+
// raise), we still refuse BEFORE the DROP POLICY statements rather than
|
|
724
|
+
// half-migrating the table into a deny-all state with no policies at all.
|
|
725
|
+
for (const stmt of splitStatements(SESSION_FENCE_BOOTSTRAP_SQL)) {
|
|
726
|
+
await sql.unsafe(stmt);
|
|
727
|
+
}
|
|
728
|
+
await sql.unsafe(SESSION_RESOLVER_PRECONDITION_SQL);
|
|
391
729
|
const ddl = getDsarMigrationSQL();
|
|
392
730
|
const statements = splitStatements(ddl);
|
|
393
731
|
for (const stmt of statements) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/migrations/index.ts"],"names":[],"mappings":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AC;IACH,CAAC;IACD,IAAI,GAAG,CAAC,IAAI,EAAE;QAAE,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;IACrC,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/dist/orchestrator.d.ts
CHANGED
|
@@ -23,11 +23,19 @@ export interface BindOrchestratorOpts {
|
|
|
23
23
|
/** Defaults to `data_residency_holds`. */
|
|
24
24
|
holdsTableName?: string;
|
|
25
25
|
/**
|
|
26
|
-
* v0.
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
26
|
+
* v0.16.0 — opt OUT of the fail-CLOSED orchestrator-side hold gate.
|
|
27
|
+
*
|
|
28
|
+
* The pre-erase hold consult is fail-CLOSED BY DEFAULT: a SQL error during
|
|
29
|
+
* the consult BLOCKS the fan-out (returns `FAILED`) rather than proceeding.
|
|
30
|
+
* Per D202 + the platform fail-closed compliance-gate doctrine. Setting
|
|
31
|
+
* this `true` restores the pre-0.16.0 fail-OPEN behaviour and is a
|
|
32
|
+
* COMPLIANCE-GATE BYPASS. Default `false`.
|
|
33
|
+
*/
|
|
34
|
+
allowFailOpenHolds?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* @deprecated Since 0.16.0 fail-closed is the DEFAULT. `true` is a no-op;
|
|
37
|
+
* `false` THROWS {@link AmbiguousHoldFailureMode} at bind time — pass
|
|
38
|
+
* `allowFailOpenHolds: true` if you really want fail-open.
|
|
31
39
|
*/
|
|
32
40
|
failClosedHolds?: boolean;
|
|
33
41
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orchestrator.d.ts","sourceRoot":"","sources":["../src/orchestrator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"orchestrator.d.ts","sourceRoot":"","sources":["../src/orchestrator.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAEV,eAAe,EAEhB,MAAM,wBAAwB,CAAC;AAChC,OAAO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C;AAEH,QAAA,MAAM,UAAU,aAKd,CAAC;AAEH,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAEnE;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,gBAAgB,CAapE;AAkBD;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,CACrC,SAAS,EAAE,SAAS,EACpB,SAAS,EAAE,MAAM,EACjB,SAAS,SAAgC,GACxC,OAAO,CAAC,2BAA2B,GAAG,IAAI,CAAC,CAqC7C;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,oBAAoB,GAAG,eAAe,CAmf5E;AAED,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC"}
|
package/dist/orchestrator.js
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
// module (consumer wires this into `@nodii/saga`).
|
|
18
18
|
// 4. Aggregates per-sibling statuses into PARTIAL / COMPLETED / FAILED.
|
|
19
19
|
import { actionName } from "./audit-actions.js";
|
|
20
|
+
import { resolveFailClosedHolds } from "./hold-failure-mode.js";
|
|
20
21
|
import { isHeld } from "./holds.js";
|
|
21
22
|
import { CmkAdapterNotConfigured, CmkResolutionFailed, DEFAULT_ERASE_BLOCKING_HOLD_KINDS, } from "./types.js";
|
|
22
23
|
/**
|
|
@@ -122,6 +123,10 @@ export async function readAggregatorRow(sqlClient, requestId, tableName = "platf
|
|
|
122
123
|
* registered siblings. Spec § 5.5 + § 5.9.
|
|
123
124
|
*/
|
|
124
125
|
export function bindOrchestrator(opts) {
|
|
126
|
+
// 0.16.0 — resolve the hold-consult failure mode at BIND time. Fail-closed
|
|
127
|
+
// unless explicitly opted out; a deprecated `failClosedHolds: false` throws
|
|
128
|
+
// here (boot), not silently at request time.
|
|
129
|
+
const failClosedHolds = resolveFailClosedHolds("bindOrchestrator", opts);
|
|
125
130
|
const siblings = opts.siblingClients ?? {};
|
|
126
131
|
const aggTable = opts.aggregatorTableName ?? "platform_lifecycle_requests";
|
|
127
132
|
const eraseBlockingKinds = opts.eraseBlockingHoldKinds ?? DEFAULT_ERASE_BLOCKING_HOLD_KINDS;
|
|
@@ -331,8 +336,9 @@ export function bindOrchestrator(opts) {
|
|
|
331
336
|
}
|
|
332
337
|
}
|
|
333
338
|
catch (err) {
|
|
334
|
-
if (
|
|
335
|
-
// Fail-CLOSED (gap 3 + D202): hold-read error
|
|
339
|
+
if (failClosedHolds) {
|
|
340
|
+
// Fail-CLOSED (the 0.16.0 DEFAULT; gap 3 + D202): a hold-read error
|
|
341
|
+
// BLOCKS the erase fan-out.
|
|
336
342
|
const failResponse = {
|
|
337
343
|
request_id: requestId,
|
|
338
344
|
status: "FAILED",
|
|
@@ -344,7 +350,8 @@ export function bindOrchestrator(opts) {
|
|
|
344
350
|
await writeAggregator(request, rpcMethod, {}, "FAILED");
|
|
345
351
|
return failResponse;
|
|
346
352
|
}
|
|
347
|
-
/* fail-OPEN
|
|
353
|
+
/* fail-OPEN — reachable ONLY when the caller explicitly passed
|
|
354
|
+
`allowFailOpenHolds: true`. Holds table unreadable — proceed. */
|
|
348
355
|
}
|
|
349
356
|
}
|
|
350
357
|
// § 6.2 per_sibling_state: keyed by LOWERCASE sibling_id → object entry.
|