@rebasepro/server-postgres 0.9.1-canary.1d2d8b5 → 0.9.1-canary.29ed165

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.
Files changed (62) hide show
  1. package/README.md +21 -0
  2. package/dist/PostgresBackendDriver.d.ts +43 -2
  3. package/dist/PostgresBootstrapper.d.ts +17 -1
  4. package/dist/auth/services.d.ts +68 -52
  5. package/dist/collections/buildRegistry.d.ts +27 -0
  6. package/dist/connection.d.ts +21 -0
  7. package/dist/data-transformer.d.ts +9 -2
  8. package/dist/index.es.js +2060 -762
  9. package/dist/index.es.js.map +1 -1
  10. package/dist/schema/auth-bootstrap-sql.d.ts +1 -1
  11. package/dist/schema/auth-schema.d.ts +24 -24
  12. package/dist/schema/doctor.d.ts +1 -1
  13. package/dist/schema/introspect-db-logic.d.ts +0 -5
  14. package/dist/schema/introspect-db-naming.d.ts +10 -0
  15. package/dist/security/policy-drift.d.ts +54 -0
  16. package/dist/security/rls-enforcement.d.ts +2 -2
  17. package/dist/services/FetchService.d.ts +4 -24
  18. package/dist/services/PersistService.d.ts +9 -1
  19. package/dist/services/RelationService.d.ts +34 -1
  20. package/dist/services/channel-history.d.ts +118 -0
  21. package/dist/services/collection-helpers.d.ts +79 -14
  22. package/dist/services/dataService.d.ts +3 -1
  23. package/dist/services/index.d.ts +1 -1
  24. package/dist/services/realtimeService.d.ts +76 -2
  25. package/dist/services/row-pipeline.d.ts +63 -0
  26. package/package.json +12 -33
  27. package/src/PostgresBackendDriver.ts +183 -18
  28. package/src/PostgresBootstrapper.ts +80 -26
  29. package/src/auth/ensure-tables.ts +170 -28
  30. package/src/auth/services.ts +181 -150
  31. package/src/cli.ts +60 -0
  32. package/src/collections/buildRegistry.ts +59 -0
  33. package/src/connection.ts +61 -1
  34. package/src/data-transformer.ts +11 -9
  35. package/src/databasePoolManager.ts +2 -0
  36. package/src/schema/auth-bootstrap-sql.ts +7 -1
  37. package/src/schema/auth-schema.ts +13 -13
  38. package/src/schema/doctor.ts +45 -20
  39. package/src/schema/generate-drizzle-schema-logic.ts +24 -29
  40. package/src/schema/generate-postgres-ddl-logic.ts +76 -28
  41. package/src/schema/introspect-db-inference.ts +1 -1
  42. package/src/schema/introspect-db-logic.ts +1 -10
  43. package/src/schema/introspect-db-naming.ts +15 -0
  44. package/src/schema/introspect-db.ts +19 -2
  45. package/src/schema/introspect-runtime.ts +1 -1
  46. package/src/security/policy-drift.test.ts +152 -1
  47. package/src/security/policy-drift.ts +126 -4
  48. package/src/security/rls-enforcement.ts +11 -5
  49. package/src/services/BranchService.ts +42 -10
  50. package/src/services/FetchService.ts +65 -270
  51. package/src/services/PersistService.ts +62 -9
  52. package/src/services/RelationService.ts +153 -94
  53. package/src/services/channel-history.ts +343 -0
  54. package/src/services/collection-helpers.ts +164 -47
  55. package/src/services/dataService.ts +3 -2
  56. package/src/services/index.ts +1 -0
  57. package/src/services/realtimeService.ts +238 -29
  58. package/src/services/row-pipeline.ts +239 -0
  59. package/src/utils/drizzle-conditions.ts +13 -0
  60. package/src/websocket.ts +34 -12
  61. package/dist/schema/auth-default-policies.d.ts +0 -10
  62. package/src/schema/auth-default-policies.ts +0 -132
@@ -1,8 +1,6 @@
1
1
  import { CollectionConfig, NumberProperty, Property, Relation, RelationProperty, SecurityOperation, SecurityRule, StringProperty, isPostgresCollectionConfig, DateProperty, ArrayProperty, MapProperty, ReferenceProperty, VectorProperty, BinaryProperty } from "@rebasepro/types";
2
- import { getEnumVarName, getTableName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres } from "@rebasepro/common";
3
- import { toSnakeCase } from "@rebasepro/utils";
4
- import { createHash } from "crypto";
5
- import { getEffectiveSecurityRules } from "./auth-default-policies";
2
+ import { getEnumVarName, getTableName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres, getEffectiveSecurityRules, getInjectedSecurityRules, resolveJunctionSpecs, getJunctionSecurityRules, getJunctionCollectionConfig } from "@rebasepro/common";
3
+ import { toSnakeCase, getPolicyNamesForRule } from "@rebasepro/utils";
6
4
 
7
5
  // --- Helper Functions ---
8
6
 
@@ -44,22 +42,6 @@ const isIdProperty = (propName: string, prop: Property, collection: CollectionCo
44
42
  return !hasExplicitId && propName === "id";
45
43
  };
46
44
 
47
- const getPolicyNameHash = (rule: SecurityRule): string => {
48
- const data = JSON.stringify({
49
- a: rule.access,
50
- m: rule.mode,
51
- op: rule.operation,
52
- ops: rule.operations?.slice().sort(),
53
- own: rule.ownerField,
54
- rol: rule.roles?.slice().sort(),
55
- pg: rule.pgRoles?.slice().sort(),
56
- u: rule.using,
57
- w: rule.withCheck,
58
- c: rule.condition,
59
- ch: rule.check
60
- });
61
- return createHash("sha1").update(data).digest("hex").substring(0, 7);
62
- };
63
45
 
64
46
  type ResolveCollection = (slug: string) => CollectionConfig | undefined;
65
47
 
@@ -69,14 +51,10 @@ const generatePolicyDdl = (collection: CollectionConfig, rule: SecurityRule, res
69
51
  ? rule.operations
70
52
  : [rule.operation ?? "all"];
71
53
 
72
- const ruleHash = getPolicyNameHash(rule);
54
+ const policyNames = getPolicyNamesForRule(rule, tableName);
73
55
 
74
56
  return ops.map((op, opIdx) => {
75
- const policyName = rule.name
76
- ? (ops.length > 1 ? `${rule.name}_${op}` : rule.name)
77
- : `${tableName}_${op}_${ruleHash}${ops.length > 1 ? `_${opIdx}` : ""}`;
78
-
79
- return generateSinglePolicyDdl(collection, rule, op, policyName, resolveCollection);
57
+ return generateSinglePolicyDdl(collection, rule, op, policyNames[opIdx], resolveCollection);
80
58
  }).join("");
81
59
  };
82
60
 
@@ -248,6 +226,10 @@ export const generatePostgresDdl = async (
248
226
  });
249
227
  if (ddl.endsWith(";\n")) ddl += "\n";
250
228
 
229
+ // Junction policy derivation needs every declaring side of each junction,
230
+ // not just the first relation that reached it in the walk below.
231
+ const junctionSpecs = resolveJunctionSpecs(collections);
232
+
251
233
  const allTablesToGenerate = new Map<string, {
252
234
  collection: CollectionConfig,
253
235
  isJunction?: boolean,
@@ -283,6 +265,11 @@ export const generatePostgresDdl = async (
283
265
 
284
266
  // 3. Generate tables
285
267
  const fkStatements: string[] = [];
268
+ // Policies are emitted after every CREATE TABLE, like the FK constraints:
269
+ // a policy may reference other tables (a junction's derived policies always
270
+ // reference both endpoints; `policy.existsIn` references a join table), and
271
+ // CREATE POLICY validates those relations at creation time.
272
+ const policyStatements: string[] = [];
286
273
  for (const [tableName, {
287
274
  collection,
288
275
  isJunction,
@@ -315,6 +302,25 @@ export const generatePostgresDdl = async (
315
302
 
316
303
  fkStatements.push(`ALTER TABLE "${schema}"."${baseTableName}" ADD CONSTRAINT "${baseTableName}_${sourceColumn}_fkey" FOREIGN KEY ("${sourceColumn}") REFERENCES "${sourceSchema}"."${sourceTable}" ("${sourceId}") ON DELETE ${onDelete.toUpperCase()};`);
317
304
  fkStatements.push(`ALTER TABLE "${schema}"."${baseTableName}" ADD CONSTRAINT "${baseTableName}_${targetColumn}_fkey" FOREIGN KEY ("${targetColumn}") REFERENCES "${targetSchema}"."${targetTable}" ("${targetId}") ON DELETE ${onDelete.toUpperCase()};`);
305
+
306
+ if (options.includePolicies) {
307
+ // Junction tables are generated tables like any other: locked by
308
+ // default, with derived policies — reads follow the endpoints'
309
+ // visibility, writes follow the declaring side's update rules.
310
+ // Without this they were the one kind of generated table with no
311
+ // RLS at all, readable and writable by every signed-in user.
312
+ ddl += `ALTER TABLE "${schema}"."${baseTableName}" ENABLE ROW LEVEL SECURITY;\n`;
313
+ ddl += `\n`;
314
+
315
+ const spec = junctionSpecs.get(baseTableName);
316
+ if (spec) {
317
+ const junctionCollection = getJunctionCollectionConfig(spec);
318
+ const resolveCollection: ResolveCollection = (slug) => collections.find(c => c.slug === slug || getTableName(c) === slug);
319
+ getJunctionSecurityRules(spec).forEach((rule: SecurityRule) => {
320
+ policyStatements.push(generatePolicyDdl(junctionCollection, rule, resolveCollection));
321
+ });
322
+ }
323
+ }
318
324
  } else if (!isJunction) {
319
325
  ddl += `CREATE TABLE "${schema}"."${baseTableName}" (\n`;
320
326
  const columns: string[] = [];
@@ -436,9 +442,8 @@ export const generatePostgresDdl = async (
436
442
  if (securityRules.length > 0) {
437
443
  const resolveCollection: ResolveCollection = (slug) => collections.find(c => c.slug === slug || getTableName(c) === slug);
438
444
  securityRules.forEach((rule: SecurityRule) => {
439
- ddl += generatePolicyDdl(collection, rule, resolveCollection);
445
+ policyStatements.push(generatePolicyDdl(collection, rule, resolveCollection));
440
446
  });
441
- ddl += "\n";
442
447
  }
443
448
  }
444
449
  }
@@ -449,6 +454,12 @@ export const generatePostgresDdl = async (
449
454
  ddl += fkStatements.join("\n") + "\n\n";
450
455
  }
451
456
 
457
+ if (policyStatements.length > 0) {
458
+ ddl += "-- Row Level Security Policies\n";
459
+ ddl += policyStatements.join("");
460
+ ddl += "\n";
461
+ }
462
+
452
463
  return ddl;
453
464
  };
454
465
 
@@ -478,13 +489,50 @@ export const generatePostgresPoliciesDdl = (collections: CollectionConfig[]): st
478
489
  const securityRules = getEffectiveSecurityRules(collection);
479
490
  if (securityRules.length > 0) {
480
491
  const resolveCollection: ResolveCollection = (slug) => collections.find(c => c.slug === slug || getTableName(c) === slug);
492
+ const injectedNames = new Set(getInjectedSecurityRules(collection).map((rule) => rule.name));
493
+
481
494
  securityRules.forEach((rule: SecurityRule) => {
495
+ // Say which policies the author did not write. They are permissive,
496
+ // so they OR with the declared rules and widen the final ACL beyond
497
+ // what `securityRules` reads like — and re-appear after any manual
498
+ // DROP, because a push asserts the declared state.
499
+ if (rule.name && injectedNames.has(rule.name)) {
500
+ ddl += `-- Injected by Rebase (not from this collection's securityRules).\n`;
501
+ ddl += `-- Set \`disableDefaultPolicies: true\` on "${collection.slug}" to drop these and own its RLS outright.\n`;
502
+ }
482
503
  ddl += generatePolicyDdl(collection, rule, resolveCollection);
483
504
  });
484
505
  ddl += "\n";
485
506
  }
486
507
  }
487
508
 
509
+ // Junction tables are generated from `through` relations, not declared as
510
+ // collections, so the walk above never sees them. They get the same
511
+ // treatment as any generated table: locked by default, with derived
512
+ // policies — reads follow the endpoints, writes follow the declaring
513
+ // side's update rules.
514
+ const junctionSpecs = resolveJunctionSpecs(collections);
515
+ for (const spec of junctionSpecs.values()) {
516
+ ddl += `ALTER TABLE "${spec.schema}"."${spec.table}" ENABLE ROW LEVEL SECURITY;\n`;
517
+ ddl += `\n`;
518
+
519
+ const junctionRules = getJunctionSecurityRules(spec);
520
+ if (junctionRules.length === 0) continue;
521
+
522
+ const junctionCollection = getJunctionCollectionConfig(spec);
523
+ const resolveCollection: ResolveCollection = (slug) => collections.find(c => c.slug === slug || getTableName(c) === slug);
524
+ const declaringSlugs = spec.declaringSides.map(s => s.collection.slug).join('", "');
525
+
526
+ ddl += `-- Derived by Rebase for the junction "${spec.table}" (no collection declares it).\n`;
527
+ ddl += `-- Reads require both endpoint rows to be visible; writes follow the update\n`;
528
+ ddl += `-- rules of "${declaringSlugs}". Set \`disableDefaultPolicies: true\` on the\n`;
529
+ ddl += `-- declaring collection(s) to drop these and police the junction yourself.\n`;
530
+ junctionRules.forEach((rule: SecurityRule) => {
531
+ ddl += generatePolicyDdl(junctionCollection, rule, resolveCollection);
532
+ });
533
+ ddl += "\n";
534
+ }
535
+
488
536
  return ddl;
489
537
  };
490
538
 
@@ -1,4 +1,4 @@
1
- import { humanize } from "./introspect-db-logic";
1
+ import { humanize } from "./introspect-db-naming";
2
2
 
3
3
  export interface InferenceResult {
4
4
  propType?: string; // If the inference changes the base type
@@ -7,6 +7,7 @@
7
7
  * and consumed directly by tests.
8
8
  */
9
9
  import { inferPropertyFromData } from "./introspect-db-inference";
10
+ import { humanize } from "./introspect-db-naming";
10
11
 
11
12
  // ── Typed interfaces for SQL query results ────────────────────────────
12
13
 
@@ -117,16 +118,6 @@ export function singularize(word: string): string {
117
118
  return word;
118
119
  }
119
120
 
120
- /**
121
- * Convert a snake_case name to a human-readable Title Case label.
122
- * e.g. "created_at" -> "Created At", "customer_id" -> "Customer Id"
123
- */
124
- export function humanize(snakeName: string): string {
125
- return snakeName
126
- .replace(/_/g, " ")
127
- .replace(/\b\w/g, (c) => c.toUpperCase());
128
- }
129
-
130
121
  /**
131
122
  * Convert a snake_case table name to a camelCase + "Collection" variable name.
132
123
  * e.g. "company_token" -> "companyTokenCollection"
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Naming helpers shared by the introspection modules. These live apart from
3
+ * `introspect-db-logic.ts` because the inference pass needs them too, and
4
+ * importing them from there would close a cycle back through this module.
5
+ */
6
+
7
+ /**
8
+ * Convert a snake_case name to a human-readable Title Case label.
9
+ * e.g. "created_at" -> "Created At", "customer_id" -> "Customer Id"
10
+ */
11
+ export function humanize(snakeName: string): string {
12
+ return snakeName
13
+ .replace(/_/g, " ")
14
+ .replace(/\b\w/g, (c) => c.toUpperCase());
15
+ }
@@ -30,6 +30,7 @@ async function main() {
30
30
  "--force": Boolean,
31
31
  "--schema": String,
32
32
  "--data-inference": Boolean,
33
+ "--no-data-inference": Boolean,
33
34
  "-o": "--output",
34
35
  "-c": "--collections",
35
36
  "-f": "--force"
@@ -166,8 +167,14 @@ async function main() {
166
167
  logger.info(chalk.blue(`Found ${tablesMap.size} tables (including ${joinTables.size} detected join tables).`));
167
168
 
168
169
  let runDataInference = false;
169
- if (args["--data-inference"] !== undefined) {
170
+ if (args["--no-data-inference"]) {
171
+ runDataInference = false;
172
+ } else if (args["--data-inference"] !== undefined) {
170
173
  runDataInference = args["--data-inference"];
174
+ } else if (!process.stdin.isTTY) {
175
+ // No terminal to answer the question below (scaffolding scripts, CI,
176
+ // `rebase init --introspect`) — asking would hang forever.
177
+ logger.info(chalk.gray("Skipping data inference (non-interactive run; pass --data-inference to enable)."));
171
178
  } else {
172
179
  const rl = readline.createInterface({
173
180
  input: process.stdin,
@@ -236,7 +243,17 @@ async function main() {
236
243
  const merged = mergeIndexContent(existing, generatedFiles);
237
244
  fs.writeFileSync(indexPath, merged, "utf-8");
238
245
  } else {
239
- const indexContent = generateIndexContent(generatedFiles);
246
+ // --force replaces collections derived from the database, but the
247
+ // directory can also hold hand-written ones with no table in the
248
+ // introspected schema (the auth users collection lives in
249
+ // "rebase"). The backend discovers the whole directory, so an
250
+ // index listing only introspected tables would silently drop them
251
+ // from the admin UI while the API still served them.
252
+ const siblings = fs.readdirSync(outDir)
253
+ .filter(f => f.endsWith(".ts") && f !== "index.ts")
254
+ .map(f => f.replace(/\.ts$/, ""));
255
+ const allFiles = [...new Set([...generatedFiles, ...siblings])];
256
+ const indexContent = generateIndexContent(allFiles);
240
257
  fs.writeFileSync(indexPath, indexContent, "utf-8");
241
258
  }
242
259
  logger.info(chalk.green(` ✓ ${indexPath}`));
@@ -23,11 +23,11 @@ import {
23
23
  buildTablesMap,
24
24
  buildEnumMap,
25
25
  identifyJoinTables,
26
- humanize,
27
26
  singularize,
28
27
  mapPgType,
29
28
  getIconForTable
30
29
  } from "./introspect-db-logic";
30
+ import { humanize } from "./introspect-db-naming";
31
31
 
32
32
  export interface IntrospectedSchema {
33
33
  tablesMap: Map<string, TableMeta>;
@@ -1,7 +1,7 @@
1
1
  import { describe, expect, it } from "@jest/globals";
2
2
  import type { CollectionConfig } from "@rebasepro/types";
3
3
 
4
- import { checkPolicyDrift, parseExpectedPolicies, formatPolicyDrift, hasDrift, type PolicyRef, type Queryable } from "./policy-drift";
4
+ import { checkPolicyDrift, parseExpectedPolicies, formatPolicyDrift, hasDrift, dropOrphanedPolicies, isGeneratedPolicyName, type PolicyRef, type Queryable } from "./policy-drift";
5
5
  import { generatePostgresPoliciesDdl } from "../schema/generate-postgres-ddl-logic";
6
6
 
7
7
  function collection(slug: string): CollectionConfig {
@@ -123,6 +123,52 @@ describe("checkPolicyDrift", () => {
123
123
  expect(hasDrift(drift)).toBe(false);
124
124
  });
125
125
 
126
+ it("flags the pre-fix permissive tautology that every other check misses", async () => {
127
+ // A database pushed before the `policy.authenticated()` fix carries
128
+ // `auth.uid() IS NOT NULL` — true for anonymous visitors. Its name,
129
+ // roles, command and clause presence all match the corrected policy, so
130
+ // this is the only signal that catches it.
131
+ const cols = [collection("posts")];
132
+ const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
133
+ const live = expected.map((p) => liveRow(p, {
134
+ qual: p.hasUsing ? "(auth.uid() IS NOT NULL)" : null
135
+ }));
136
+
137
+ const drift = await checkPolicyDrift(dbWith(live), cols);
138
+
139
+ expect(drift.insecure.length).toBeGreaterThan(0);
140
+ expect(hasDrift(drift)).toBe(true);
141
+ expect(drift.diverged).toHaveLength(0); // nothing else notices
142
+ expect(formatPolicyDrift(drift)).toContain("anonymous");
143
+ expect(formatPolicyDrift(drift)).toContain("db push");
144
+ });
145
+
146
+ it("clears the corrected expression, in either literal spelling", async () => {
147
+ const cols = [collection("posts")];
148
+ const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
149
+ for (const guard of ["<> 'anonymous'::text", "<> 'anonymous'", "!= 'anonymous'"]) {
150
+ const live = expected.map((p) => liveRow(p, {
151
+ qual: p.hasUsing ? `((auth.uid() IS NOT NULL) AND ((auth.uid())::text ${guard}))` : null
152
+ }));
153
+ const drift = await checkPolicyDrift(dbWith(live), cols);
154
+ expect(drift.insecure).toHaveLength(0);
155
+ }
156
+ });
157
+
158
+ it("also flags the tautology in a WITH CHECK clause", async () => {
159
+ const cols = [collection("posts")];
160
+ const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
161
+ const live = expected.map((p) => liveRow(p, {
162
+ with_check: p.hasWithCheck ? "(auth.uid() IS NOT NULL)" : null
163
+ }));
164
+
165
+ const drift = await checkPolicyDrift(dbWith(live), cols);
166
+
167
+ const flagged = drift.insecure.some((i) => /WITH CHECK/.test(i.reason));
168
+ // Only assert when the fixture actually had a WITH CHECK policy to carry it.
169
+ if (expected.some((p) => p.hasWithCheck)) expect(flagged).toBe(true);
170
+ });
171
+
126
172
  it("parses roles when the driver returns the raw {a,b} text form", async () => {
127
173
  const cols = [collection("tags")];
128
174
  const live = [{ schemaname: "public", tablename: "tags", policyname: "test_policy", roles: "{authenticated,anon}", cmd: "ALL", qual: "true", with_check: null }];
@@ -154,3 +200,108 @@ describe("checkPolicyDrift", () => {
154
200
  expect(drift.missing.length).toBeGreaterThan(0);
155
201
  });
156
202
  });
203
+
204
+ describe("isGeneratedPolicyName", () => {
205
+ it("recognises the generator's own shape", () => {
206
+ expect(isGeneratedPolicyName("documents_insert_a1b2c3d", "documents")).toBe(true);
207
+ // One rule spanning several operations appends the operation index.
208
+ expect(isGeneratedPolicyName("documents_update_a1b2c3d_1", "documents")).toBe(true);
209
+ });
210
+
211
+ it("does not claim names a human could have written", () => {
212
+ expect(isGeneratedPolicyName("owner_access", "documents")).toBe(false);
213
+ expect(isGeneratedPolicyName("documents_default_admin_read", "documents")).toBe(false);
214
+ // Right shape, wrong table — belongs to something else.
215
+ expect(isGeneratedPolicyName("teams_insert_a1b2c3d", "documents")).toBe(false);
216
+ // A digest is 7 lowercase hex characters, nothing else.
217
+ expect(isGeneratedPolicyName("documents_insert_notahex", "documents")).toBe(false);
218
+ expect(isGeneratedPolicyName("documents_grant_a1b2c3d", "documents")).toBe(false);
219
+ });
220
+ });
221
+
222
+ describe("dropOrphanedPolicies", () => {
223
+ /** Records the DDL issued so the test can assert on what was dropped. */
224
+ function recordingDb(rows: Record<string, unknown>[]) {
225
+ const executed: string[] = [];
226
+ const db: Queryable = {
227
+ query: async (text: string) => {
228
+ if (!/^SELECT/i.test(text)) executed.push(text);
229
+ return { rows: rows as never[] };
230
+ }
231
+ };
232
+ return { db, executed };
233
+ }
234
+
235
+ it("drops the policy a rule edit superseded", async () => {
236
+ // The reported failure: tightening a rule renames its policy, and the
237
+ // permissive original stays live and keeps ORing itself back in.
238
+ const cols = [collection("documents")];
239
+ const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
240
+ const stale = {
241
+ schemaname: "public", tablename: "documents", policyname: "documents_insert_dead1ee",
242
+ roles: ["public"], cmd: "INSERT", qual: null, with_check: "true"
243
+ };
244
+ const live = [...expected.map((p) => liveRow(p)), stale];
245
+
246
+ const drift = await checkPolicyDrift(dbWith(live), cols);
247
+ const { db, executed } = recordingDb(live);
248
+ const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
249
+
250
+ expect(dropped).toHaveLength(1);
251
+ expect(dropped[0].name).toBe("documents_insert_dead1ee");
252
+ expect(kept).toHaveLength(0);
253
+ expect(executed).toEqual([
254
+ 'DROP POLICY IF EXISTS "documents_insert_dead1ee" ON "public"."documents"'
255
+ ]);
256
+ });
257
+
258
+ it("leaves a hand-written policy alone and reports it instead", async () => {
259
+ const cols = [collection("documents")];
260
+ const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
261
+ const live = [
262
+ ...expected.map((p) => liveRow(p)),
263
+ { schemaname: "public", tablename: "documents", policyname: "ops_break_glass", roles: ["public"], cmd: "ALL", qual: "true", with_check: null }
264
+ ];
265
+
266
+ const drift = await checkPolicyDrift(dbWith(live), cols);
267
+ const { db, executed } = recordingDb(live);
268
+ const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
269
+
270
+ expect(dropped).toHaveLength(0);
271
+ expect(kept.map((p) => p.name)).toEqual(["ops_break_glass"]);
272
+ expect(executed).toEqual([]);
273
+ });
274
+
275
+ it("never touches a table the collections do not describe", async () => {
276
+ // Another application sharing the schema owns this table; a
277
+ // generator-shaped name there is coincidence, not our leftover.
278
+ const cols = [collection("documents")];
279
+ const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
280
+ const live = [
281
+ ...expected.map((p) => liveRow(p)),
282
+ { schemaname: "public", tablename: "legacy", policyname: "legacy_select_a1b2c3d", roles: ["public"], cmd: "SELECT", qual: "true", with_check: null }
283
+ ];
284
+
285
+ const drift = await checkPolicyDrift(dbWith(live), cols);
286
+ const { db, executed } = recordingDb(live);
287
+ const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
288
+
289
+ expect(dropped).toHaveLength(0);
290
+ expect(kept.map((p) => p.name)).toEqual(["legacy_select_a1b2c3d"]);
291
+ expect(executed).toEqual([]);
292
+ });
293
+
294
+ it("does nothing when the database already matches", async () => {
295
+ const cols = [collection("documents")];
296
+ const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
297
+ const live = expected.map((p) => liveRow(p));
298
+
299
+ const drift = await checkPolicyDrift(dbWith(live), cols);
300
+ const { db, executed } = recordingDb(live);
301
+ const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
302
+
303
+ expect(dropped).toHaveLength(0);
304
+ expect(kept).toHaveLength(0);
305
+ expect(executed).toEqual([]);
306
+ });
307
+ });
@@ -29,6 +29,14 @@ export interface PolicyRef {
29
29
  hasUsing: boolean;
30
30
  /** Whether a WITH CHECK clause is present at all (not what it says). */
31
31
  hasWithCheck: boolean;
32
+ /**
33
+ * The live clause text, when read from `pg_policies`. Present only for live
34
+ * policies (the expected side is parsed from DDL and does not carry it).
35
+ * Used solely for the insecure-tautology scan, not for divergence — Postgres
36
+ * rewrites this text, so it is not safe to diff against expected.
37
+ */
38
+ qual?: string | null;
39
+ withCheck?: string | null;
32
40
  }
33
41
 
34
42
  export interface PolicyDrift {
@@ -38,6 +46,19 @@ export interface PolicyDrift {
38
46
  orphaned: PolicyRef[];
39
47
  /** Same policy name, different roles or command. */
40
48
  diverged: { expected: PolicyRef; actual: PolicyRef; differences: string[] }[];
49
+ /**
50
+ * A live policy whose expression is the known-permissive tautology
51
+ * `auth.uid() IS NOT NULL` — true for anonymous visitors too, because the
52
+ * user path coerces a blank id to the `'anonymous'` sentinel. This is what
53
+ * `policy.authenticated()` used to compile to, so a database pushed before
54
+ * that fix carries it, and neither the name, roles, command nor clause
55
+ * *presence* differs from the corrected policy — the only thing that changed
56
+ * is the expression text, which this checker otherwise (correctly) ignores.
57
+ * So it is the one drift that hides from every other check here.
58
+ *
59
+ * @see reason a sentence naming the clause and what to do.
60
+ */
61
+ insecure: { policy: PolicyRef; reason: string }[];
41
62
  }
42
63
 
43
64
  export interface Queryable {
@@ -122,10 +143,30 @@ async function readLivePolicies(client: Queryable, schemas: string[]): Promise<P
122
143
  // Presence only. Postgres rewrites the text, but it does not invent or
123
144
  // drop a clause: NULL here means the policy genuinely has none.
124
145
  hasUsing: r.qual != null,
125
- hasWithCheck: r.with_check != null
146
+ hasWithCheck: r.with_check != null,
147
+ qual: r.qual,
148
+ withCheck: r.with_check
126
149
  }));
127
150
  }
128
151
 
152
+ /**
153
+ * The permissive tautology `auth.uid() IS NOT NULL`, without the
154
+ * `<> 'anonymous'` guard that makes it mean "signed in".
155
+ *
156
+ * Whitespace varies with Postgres's rewrite, so match on a collapsed form. The
157
+ * guard clause (`<> 'anonymous'`, in any spelling) is what distinguishes the
158
+ * corrected policy from the stale one, so its presence clears the text.
159
+ */
160
+ function isPermissiveAuthTautology(clause: string | null | undefined): boolean {
161
+ if (!clause) return false;
162
+ const flat = clause.toLowerCase().replace(/\s+/g, " ");
163
+ if (!/auth\.uid\(\)\s*is not null/.test(flat)) return false;
164
+ // The fix appends `AND auth.uid() <> 'anonymous'`; Postgres may store the
165
+ // literal as `'anonymous'::text`. Either spelling means it is the corrected
166
+ // policy, not the tautology.
167
+ return !/<>\s*'anonymous'/.test(flat) && !/!=\s*'anonymous'/.test(flat);
168
+ }
169
+
129
170
  const keyOf = (p: PolicyRef) => `${p.schema}.${p.table}.${p.name}`;
130
171
  const sameRoles = (a: string[], b: string[]) =>
131
172
  a.length === b.length && [...a].sort().join(",") === [...b].sort().join(",");
@@ -154,13 +195,31 @@ export async function checkPolicyDrift(
154
195
  const schemas = [...new Set(expected.map((p) => p.schema))];
155
196
  // Nothing expected means nothing to reconcile against; scanning every
156
197
  // schema would report the whole database as orphaned.
157
- if (schemas.length === 0) return { missing: [], orphaned: [], diverged: [] };
198
+ if (schemas.length === 0) return { missing: [], orphaned: [], diverged: [], insecure: [] };
158
199
 
159
200
  const live = await readLivePolicies(client, schemas);
160
201
  const liveByKey = new Map(live.map((p) => [keyOf(p), p]));
161
202
  const expectedByKey = new Map(expected.map((p) => [keyOf(p), p]));
162
203
 
163
- const drift: PolicyDrift = { missing: [], orphaned: [], diverged: [] };
204
+ const drift: PolicyDrift = { missing: [], orphaned: [], diverged: [], insecure: [] };
205
+
206
+ // Scan every live policy for the permissive tautology. This is deliberately
207
+ // independent of the name-keyed diff below: a database pushed before the
208
+ // `authenticated()` fix matches its expected policy on name, roles, command
209
+ // and clause presence, so nothing else here would flag it.
210
+ for (const p of live) {
211
+ const clause = isPermissiveAuthTautology(p.qual)
212
+ ? "USING"
213
+ : isPermissiveAuthTautology(p.withCheck) ? "WITH CHECK" : null;
214
+ if (clause) {
215
+ drift.insecure.push({
216
+ policy: p,
217
+ reason: `${clause} is \`auth.uid() IS NOT NULL\`, which is true for anonymous ` +
218
+ `visitors too — this grants access to signed-out requests. It predates the ` +
219
+ `\`policy.authenticated()\` fix; re-run \`rebase db push\` to tighten it.`
220
+ });
221
+ }
222
+ }
164
223
 
165
224
  for (const [key, want] of expectedByKey) {
166
225
  const got = liveByKey.get(key);
@@ -192,8 +251,64 @@ export async function checkPolicyDrift(
192
251
  return drift;
193
252
  }
194
253
 
254
+ /**
255
+ * Does this name look like one the generator produced for this table?
256
+ *
257
+ * Unnamed rules compile to `<table>_<op>_<sha1[0:7]>` (plus `_<idx>` when one
258
+ * rule spans several operations), and the hash covers the rule's semantics — so
259
+ * *editing* a rule renames its policy. The policy under the old name is left
260
+ * behind by `db push`, which only DROPs the names it is about to CREATE, and
261
+ * Postgres ORs PERMISSIVE policies together: a superseded `USING (true)` keeps
262
+ * granting everything no matter how tight its replacement is.
263
+ *
264
+ * Matching the shape is what makes dropping them safe. A hand-written policy
265
+ * would have to collide with a 7-hex digest to be mistaken for generated one;
266
+ * a policy named anything else is left alone and merely reported, because a
267
+ * custom name is indistinguishable from one someone wrote in SQL on purpose.
268
+ */
269
+ export function isGeneratedPolicyName(name: string, table: string): boolean {
270
+ return new RegExp(`^${table.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}_(select|insert|update|delete|all)_[0-9a-f]{7}(_\\d+)?$`)
271
+ .test(name);
272
+ }
273
+
274
+ export interface OrphanCleanup {
275
+ /** Superseded generated policies that were dropped. */
276
+ dropped: PolicyRef[];
277
+ /** Orphans left in place because their names are not generator-shaped. */
278
+ kept: PolicyRef[];
279
+ }
280
+
281
+ /**
282
+ * Drop the policies an earlier push superseded but never removed.
283
+ *
284
+ * Only touches tables the collections describe — a table with no expected
285
+ * policy is not ours to reconcile, and scanning by schema alone would sweep up
286
+ * policies belonging to something else sharing the database.
287
+ */
288
+ export async function dropOrphanedPolicies(
289
+ client: Queryable,
290
+ drift: PolicyDrift,
291
+ collections: CollectionConfig[]
292
+ ): Promise<OrphanCleanup> {
293
+ const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(collections));
294
+ const managed = new Set(expected.map((p) => `${p.schema}.${p.table}`));
295
+
296
+ const cleanup: OrphanCleanup = { dropped: [], kept: [] };
297
+ for (const p of drift.orphaned) {
298
+ if (!managed.has(`${p.schema}.${p.table}`) || !isGeneratedPolicyName(p.name, p.table)) {
299
+ cleanup.kept.push(p);
300
+ continue;
301
+ }
302
+ // Identifiers are quoted, and the name came from pg_policies rather than
303
+ // from user input, so it is already a valid identifier.
304
+ await client.query(`DROP POLICY IF EXISTS "${p.name}" ON "${p.schema}"."${p.table}"`);
305
+ cleanup.dropped.push(p);
306
+ }
307
+ return cleanup;
308
+ }
309
+
195
310
  export const hasDrift = (d: PolicyDrift): boolean =>
196
- d.missing.length > 0 || d.orphaned.length > 0 || d.diverged.length > 0;
311
+ d.missing.length > 0 || d.orphaned.length > 0 || d.diverged.length > 0 || d.insecure.length > 0;
197
312
 
198
313
  /** Human-readable report; empty string when the database matches the config. */
199
314
  export function formatPolicyDrift(drift: PolicyDrift): string {
@@ -217,5 +332,12 @@ export function formatPolicyDrift(drift: PolicyDrift): string {
217
332
  for (const diff of d.differences) lines.push(` ${diff}`);
218
333
  }
219
334
  }
335
+ if (drift.insecure.length > 0) {
336
+ lines.push(" Insecure — a live policy grants access it should not:");
337
+ for (const i of drift.insecure) {
338
+ lines.push(` • ${i.policy.schema}.${i.policy.table} → "${i.policy.name}"`);
339
+ lines.push(` ${i.reason}`);
340
+ }
341
+ }
220
342
  return lines.join("\n");
221
343
  }