@rebasepro/server-postgres 0.0.1-canary.fc811d7 → 0.9.1-canary.0de22e0
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/README.md +21 -0
- package/dist/PostgresBackendDriver.d.ts +43 -2
- package/dist/PostgresBootstrapper.d.ts +17 -1
- package/dist/auth/services.d.ts +16 -0
- package/dist/collections/buildRegistry.d.ts +27 -0
- package/dist/connection.d.ts +21 -0
- package/dist/data-transformer.d.ts +9 -2
- package/dist/index.es.js +2502 -2624
- package/dist/index.es.js.map +1 -1
- package/dist/module-dir.d.ts +1 -0
- package/dist/schema/doctor.d.ts +1 -1
- package/dist/schema/introspect-db-logic.d.ts +0 -5
- package/dist/schema/introspect-db-naming.d.ts +10 -0
- package/dist/security/policy-drift.d.ts +46 -5
- package/dist/security/rls-enforcement.d.ts +28 -3
- package/dist/services/FetchService.d.ts +4 -24
- package/dist/services/PersistService.d.ts +27 -1
- package/dist/services/RelationService.d.ts +34 -1
- package/dist/services/channel-history.d.ts +118 -0
- package/dist/services/collection-helpers.d.ts +79 -14
- package/dist/services/dataService.d.ts +3 -1
- package/dist/services/index.d.ts +1 -1
- package/dist/services/realtimeService.d.ts +76 -2
- package/dist/services/row-pipeline.d.ts +63 -0
- package/package.json +15 -40
- package/src/PostgresBackendDriver.ts +183 -18
- package/src/PostgresBootstrapper.ts +86 -27
- package/src/auth/ensure-tables.ts +73 -11
- package/src/auth/services.ts +49 -19
- package/src/cli-helpers.ts +2 -20
- package/src/cli.ts +60 -0
- package/src/collections/buildRegistry.ts +59 -0
- package/src/connection.ts +61 -1
- package/src/data-transformer.ts +11 -9
- package/src/databasePoolManager.ts +2 -0
- package/src/module-dir.ts +7 -0
- package/src/schema/doctor-cli.ts +5 -1
- package/src/schema/doctor.ts +45 -20
- package/src/schema/generate-drizzle-schema-logic.ts +24 -29
- package/src/schema/generate-postgres-ddl-logic.ts +76 -28
- package/src/schema/introspect-db-inference.ts +1 -1
- package/src/schema/introspect-db-logic.ts +1 -10
- package/src/schema/introspect-db-naming.ts +15 -0
- package/src/schema/introspect-db.ts +19 -2
- package/src/schema/introspect-runtime.ts +1 -1
- package/src/security/anonymous-grants.test.ts +71 -0
- package/src/security/policy-drift.test.ts +153 -14
- package/src/security/policy-drift.ts +128 -10
- package/src/security/rls-enforcement.ts +67 -6
- package/src/services/BranchService.ts +42 -10
- package/src/services/FetchService.ts +65 -270
- package/src/services/PersistService.ts +130 -14
- package/src/services/RelationService.ts +153 -94
- package/src/services/channel-history.ts +343 -0
- package/src/services/collection-helpers.ts +164 -47
- package/src/services/dataService.ts +3 -2
- package/src/services/index.ts +1 -0
- package/src/services/realtimeService.ts +237 -28
- package/src/services/row-pipeline.ts +239 -0
- package/src/utils/drizzle-conditions.ts +13 -0
- package/src/websocket.ts +34 -12
- package/dist/chunk-DSJWtz9O.js +0 -40
- package/dist/schema/auth-default-policies.d.ts +0 -10
- package/dist/src-Eh-CZosp.js +0 -595
- package/dist/src-Eh-CZosp.js.map +0 -1
- package/src/schema/auth-default-policies.ts +0 -125
|
@@ -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"]
|
|
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
|
-
|
|
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>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { describe, expect, it, jest, afterEach } from "@jest/globals";
|
|
2
|
+
import { logger } from "@rebasepro/server";
|
|
3
|
+
|
|
4
|
+
import { warnOnAnonymousGrants } from "./rls-enforcement";
|
|
5
|
+
|
|
6
|
+
/** Capture what the operator would actually see at boot. */
|
|
7
|
+
function captureWarning(fn: () => void): string | null {
|
|
8
|
+
const spy = jest.spyOn(logger, "warn").mockImplementation(() => undefined as never);
|
|
9
|
+
try {
|
|
10
|
+
fn();
|
|
11
|
+
return spy.mock.calls.length > 0 ? String(spy.mock.calls[0][0]) : null;
|
|
12
|
+
} finally {
|
|
13
|
+
spy.mockRestore();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const collectionWith = (slug: string, using: string) => ({
|
|
18
|
+
slug,
|
|
19
|
+
securityRules: [{ name: "authenticated_access", operation: "all", using, withCheck: using }]
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => jest.restoreAllMocks());
|
|
23
|
+
|
|
24
|
+
describe("warnOnAnonymousGrants", () => {
|
|
25
|
+
// The rule found in a real app, across 8 collections of PII. It reads as
|
|
26
|
+
// "any logged-in user" and grants every anonymous visitor read and write.
|
|
27
|
+
const FROM_THE_WILD = "auth.uid() IS NOT NULL AND auth.uid() != 'anon'";
|
|
28
|
+
|
|
29
|
+
it("reports both mistakes in the rule from the wild", () => {
|
|
30
|
+
const warning = captureWarning(() => warnOnAnonymousGrants([collectionWith("talents", FROM_THE_WILD)]));
|
|
31
|
+
|
|
32
|
+
expect(warning).toContain("auth.uid() IS NOT NULL");
|
|
33
|
+
expect(warning).toContain("'anon' is a Supabase convention");
|
|
34
|
+
expect(warning).toContain("policy.authenticated()");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("groups by mistake and names every collection it appears on", () => {
|
|
38
|
+
const warning = captureWarning(() => warnOnAnonymousGrants(
|
|
39
|
+
["talents", "companies", "contact_messages"].map(s => collectionWith(s, FROM_THE_WILD))
|
|
40
|
+
));
|
|
41
|
+
|
|
42
|
+
expect(warning).toContain("talents");
|
|
43
|
+
expect(warning).toContain("companies");
|
|
44
|
+
expect(warning).toContain("contact_messages");
|
|
45
|
+
// Grouped, not repeated once per collection: the same habit across 8
|
|
46
|
+
// collections should not print the same paragraph 16 times.
|
|
47
|
+
expect(warning!.match(/Supabase convention/g)).toHaveLength(1);
|
|
48
|
+
expect(warning).toContain("3 rule(s)");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("catches the bare tautology, which carries no literal to notice", () => {
|
|
52
|
+
const warning = captureWarning(() => warnOnAnonymousGrants([collectionWith("talents", "auth.uid() IS NOT NULL")]));
|
|
53
|
+
|
|
54
|
+
expect(warning).toContain("auth.uid() IS NOT NULL");
|
|
55
|
+
expect(warning).not.toContain("Supabase convention");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("says nothing about rules that are actually correct", () => {
|
|
59
|
+
const warning = captureWarning(() => warnOnAnonymousGrants([
|
|
60
|
+
{ slug: "talents", securityRules: [{ name: "own_rows", operation: "all", ownerField: "owner_id" }] },
|
|
61
|
+
collectionWith("posts", `auth.uid() != 'anonymous'`),
|
|
62
|
+
{ slug: "public_page", securityRules: [{ name: "read", operation: "select", access: "public" }] }
|
|
63
|
+
] as never));
|
|
64
|
+
|
|
65
|
+
expect(warning).toBeNull();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("says nothing when no collection has rules at all", () => {
|
|
69
|
+
expect(captureWarning(() => warnOnAnonymousGrants([{ slug: "empty" }]))).toBeNull();
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -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 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 {
|
|
@@ -23,6 +23,17 @@ function dbWith(rows: Record<string, unknown>[]): Queryable {
|
|
|
23
23
|
return { query: async () => ({ rows: rows as never[] }) };
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
/** A pg_policies row matching an expected policy, clauses and all. */
|
|
27
|
+
function liveRow(p: PolicyRef, overrides: Record<string, unknown> = {}) {
|
|
28
|
+
return {
|
|
29
|
+
schemaname: p.schema, tablename: p.table, policyname: p.name, roles: p.roles, cmd: p.command,
|
|
30
|
+
// Postgres rewrites the text it stores; only presence is compared.
|
|
31
|
+
qual: p.hasUsing ? "(rewritten by postgres)" : null,
|
|
32
|
+
with_check: p.hasWithCheck ? "(rewritten by postgres)" : null,
|
|
33
|
+
...overrides
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
26
37
|
describe("parseExpectedPolicies", () => {
|
|
27
38
|
it("reads the DDL that db push actually applies", () => {
|
|
28
39
|
const ddl = generatePostgresPoliciesDdl([collection("authors")]);
|
|
@@ -38,11 +49,8 @@ describe("checkPolicyDrift", () => {
|
|
|
38
49
|
it("reports nothing when the database matches the collections", async () => {
|
|
39
50
|
const cols = [collection("authors")];
|
|
40
51
|
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
41
|
-
const live = expected.map((p) => ({
|
|
42
|
-
schemaname: p.schema, tablename: p.table, policyname: p.name, roles: p.roles, cmd: p.command
|
|
43
|
-
}));
|
|
44
52
|
|
|
45
|
-
const drift = await checkPolicyDrift(dbWith(
|
|
53
|
+
const drift = await checkPolicyDrift(dbWith(expected.map((p) => liveRow(p))), cols);
|
|
46
54
|
|
|
47
55
|
expect(hasDrift(drift)).toBe(false);
|
|
48
56
|
expect(formatPolicyDrift(drift)).toBe("");
|
|
@@ -62,8 +70,8 @@ describe("checkPolicyDrift", () => {
|
|
|
62
70
|
const cols = [collection("customers")];
|
|
63
71
|
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
64
72
|
const live = [
|
|
65
|
-
...expected.map((p) => (
|
|
66
|
-
{ schemaname: "public", tablename: "customers", policyname: "test_policy", roles: ["authenticated"], cmd: "ALL" }
|
|
73
|
+
...expected.map((p) => liveRow(p)),
|
|
74
|
+
{ schemaname: "public", tablename: "customers", policyname: "test_policy", roles: ["authenticated"], cmd: "ALL", qual: "true", with_check: null }
|
|
67
75
|
];
|
|
68
76
|
|
|
69
77
|
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
@@ -76,11 +84,8 @@ describe("checkPolicyDrift", () => {
|
|
|
76
84
|
it("flags a policy whose roles were changed underneath it as diverged", async () => {
|
|
77
85
|
const cols = [collection("orders")];
|
|
78
86
|
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
// Someone re-granted it to a role requests never run as.
|
|
82
|
-
roles: ["authenticated"], cmd: p.command
|
|
83
|
-
}));
|
|
87
|
+
// Someone re-granted it to a role requests never run as.
|
|
88
|
+
const live = expected.map((p) => liveRow(p, { roles: ["authenticated"] }));
|
|
84
89
|
|
|
85
90
|
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
86
91
|
|
|
@@ -89,9 +94,38 @@ describe("checkPolicyDrift", () => {
|
|
|
89
94
|
expect(formatPolicyDrift(drift)).toContain("Diverged");
|
|
90
95
|
});
|
|
91
96
|
|
|
97
|
+
it("flags a policy whose expression the database is missing entirely", async () => {
|
|
98
|
+
// A real production database had jobs.public_read_published stored as
|
|
99
|
+
// SELECT / {public} / USING NULL: every field this used to compare
|
|
100
|
+
// matched, drift reported zero, and the policy denied 100% of reads.
|
|
101
|
+
const cols = [collection("jobs")];
|
|
102
|
+
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
103
|
+
const select = expected.find((p) => p.command === "SELECT" && p.roles.includes("public"))!;
|
|
104
|
+
const live = expected.map((p) => (p === select ? liveRow(p, { qual: null }) : liveRow(p)));
|
|
105
|
+
|
|
106
|
+
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
107
|
+
|
|
108
|
+
expect(drift.diverged).toHaveLength(1);
|
|
109
|
+
expect(drift.diverged[0].differences.join(" ")).toContain("USING: expected an expression, database has none");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("does not mistake an expression Postgres rewrote for a missing one", async () => {
|
|
113
|
+
// The reason expression text is not compared: what comes back out of
|
|
114
|
+
// pg_policies never matches what went in.
|
|
115
|
+
const cols = [collection("posts")];
|
|
116
|
+
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
117
|
+
const live = expected.map((p) => liveRow(p, {
|
|
118
|
+
qual: p.hasUsing ? "((auth.uid() IS NOT NULL) AND ((auth.uid())::text <> 'anonymous'::text))" : null
|
|
119
|
+
}));
|
|
120
|
+
|
|
121
|
+
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
122
|
+
|
|
123
|
+
expect(hasDrift(drift)).toBe(false);
|
|
124
|
+
});
|
|
125
|
+
|
|
92
126
|
it("parses roles when the driver returns the raw {a,b} text form", async () => {
|
|
93
127
|
const cols = [collection("tags")];
|
|
94
|
-
const live = [{ schemaname: "public", tablename: "tags", policyname: "test_policy", roles: "{authenticated,anon}", cmd: "ALL" }];
|
|
128
|
+
const live = [{ schemaname: "public", tablename: "tags", policyname: "test_policy", roles: "{authenticated,anon}", cmd: "ALL", qual: "true", with_check: null }];
|
|
95
129
|
|
|
96
130
|
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
97
131
|
|
|
@@ -104,7 +138,7 @@ describe("checkPolicyDrift", () => {
|
|
|
104
138
|
// Note a collection with no securityRules is NOT this case — it still
|
|
105
139
|
// generates default (admin-only) policies.
|
|
106
140
|
const drift = await checkPolicyDrift(
|
|
107
|
-
dbWith([{ schemaname: "public", tablename: "x", policyname: "p", roles: ["public"], cmd: "ALL" }]),
|
|
141
|
+
dbWith([{ schemaname: "public", tablename: "x", policyname: "p", roles: ["public"], cmd: "ALL", qual: "true", with_check: null }]),
|
|
108
142
|
[]
|
|
109
143
|
);
|
|
110
144
|
|
|
@@ -120,3 +154,108 @@ describe("checkPolicyDrift", () => {
|
|
|
120
154
|
expect(drift.missing.length).toBeGreaterThan(0);
|
|
121
155
|
});
|
|
122
156
|
});
|
|
157
|
+
|
|
158
|
+
describe("isGeneratedPolicyName", () => {
|
|
159
|
+
it("recognises the generator's own shape", () => {
|
|
160
|
+
expect(isGeneratedPolicyName("documents_insert_a1b2c3d", "documents")).toBe(true);
|
|
161
|
+
// One rule spanning several operations appends the operation index.
|
|
162
|
+
expect(isGeneratedPolicyName("documents_update_a1b2c3d_1", "documents")).toBe(true);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("does not claim names a human could have written", () => {
|
|
166
|
+
expect(isGeneratedPolicyName("owner_access", "documents")).toBe(false);
|
|
167
|
+
expect(isGeneratedPolicyName("documents_default_admin_read", "documents")).toBe(false);
|
|
168
|
+
// Right shape, wrong table — belongs to something else.
|
|
169
|
+
expect(isGeneratedPolicyName("teams_insert_a1b2c3d", "documents")).toBe(false);
|
|
170
|
+
// A digest is 7 lowercase hex characters, nothing else.
|
|
171
|
+
expect(isGeneratedPolicyName("documents_insert_notahex", "documents")).toBe(false);
|
|
172
|
+
expect(isGeneratedPolicyName("documents_grant_a1b2c3d", "documents")).toBe(false);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe("dropOrphanedPolicies", () => {
|
|
177
|
+
/** Records the DDL issued so the test can assert on what was dropped. */
|
|
178
|
+
function recordingDb(rows: Record<string, unknown>[]) {
|
|
179
|
+
const executed: string[] = [];
|
|
180
|
+
const db: Queryable = {
|
|
181
|
+
query: async (text: string) => {
|
|
182
|
+
if (!/^SELECT/i.test(text)) executed.push(text);
|
|
183
|
+
return { rows: rows as never[] };
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
return { db, executed };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
it("drops the policy a rule edit superseded", async () => {
|
|
190
|
+
// The reported failure: tightening a rule renames its policy, and the
|
|
191
|
+
// permissive original stays live and keeps ORing itself back in.
|
|
192
|
+
const cols = [collection("documents")];
|
|
193
|
+
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
194
|
+
const stale = {
|
|
195
|
+
schemaname: "public", tablename: "documents", policyname: "documents_insert_dead1ee",
|
|
196
|
+
roles: ["public"], cmd: "INSERT", qual: null, with_check: "true"
|
|
197
|
+
};
|
|
198
|
+
const live = [...expected.map((p) => liveRow(p)), stale];
|
|
199
|
+
|
|
200
|
+
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
201
|
+
const { db, executed } = recordingDb(live);
|
|
202
|
+
const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
|
|
203
|
+
|
|
204
|
+
expect(dropped).toHaveLength(1);
|
|
205
|
+
expect(dropped[0].name).toBe("documents_insert_dead1ee");
|
|
206
|
+
expect(kept).toHaveLength(0);
|
|
207
|
+
expect(executed).toEqual([
|
|
208
|
+
'DROP POLICY IF EXISTS "documents_insert_dead1ee" ON "public"."documents"'
|
|
209
|
+
]);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("leaves a hand-written policy alone and reports it instead", async () => {
|
|
213
|
+
const cols = [collection("documents")];
|
|
214
|
+
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
215
|
+
const live = [
|
|
216
|
+
...expected.map((p) => liveRow(p)),
|
|
217
|
+
{ schemaname: "public", tablename: "documents", policyname: "ops_break_glass", roles: ["public"], cmd: "ALL", qual: "true", with_check: null }
|
|
218
|
+
];
|
|
219
|
+
|
|
220
|
+
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
221
|
+
const { db, executed } = recordingDb(live);
|
|
222
|
+
const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
|
|
223
|
+
|
|
224
|
+
expect(dropped).toHaveLength(0);
|
|
225
|
+
expect(kept.map((p) => p.name)).toEqual(["ops_break_glass"]);
|
|
226
|
+
expect(executed).toEqual([]);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("never touches a table the collections do not describe", async () => {
|
|
230
|
+
// Another application sharing the schema owns this table; a
|
|
231
|
+
// generator-shaped name there is coincidence, not our leftover.
|
|
232
|
+
const cols = [collection("documents")];
|
|
233
|
+
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
234
|
+
const live = [
|
|
235
|
+
...expected.map((p) => liveRow(p)),
|
|
236
|
+
{ schemaname: "public", tablename: "legacy", policyname: "legacy_select_a1b2c3d", roles: ["public"], cmd: "SELECT", qual: "true", with_check: null }
|
|
237
|
+
];
|
|
238
|
+
|
|
239
|
+
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
240
|
+
const { db, executed } = recordingDb(live);
|
|
241
|
+
const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
|
|
242
|
+
|
|
243
|
+
expect(dropped).toHaveLength(0);
|
|
244
|
+
expect(kept.map((p) => p.name)).toEqual(["legacy_select_a1b2c3d"]);
|
|
245
|
+
expect(executed).toEqual([]);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("does nothing when the database already matches", async () => {
|
|
249
|
+
const cols = [collection("documents")];
|
|
250
|
+
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
251
|
+
const live = expected.map((p) => liveRow(p));
|
|
252
|
+
|
|
253
|
+
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
254
|
+
const { db, executed } = recordingDb(live);
|
|
255
|
+
const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
|
|
256
|
+
|
|
257
|
+
expect(dropped).toHaveLength(0);
|
|
258
|
+
expect(kept).toHaveLength(0);
|
|
259
|
+
expect(executed).toEqual([]);
|
|
260
|
+
});
|
|
261
|
+
});
|
|
@@ -25,6 +25,10 @@ export interface PolicyRef {
|
|
|
25
25
|
roles: string[];
|
|
26
26
|
/** SELECT / INSERT / UPDATE / DELETE / ALL. */
|
|
27
27
|
command: string;
|
|
28
|
+
/** Whether a USING clause is present at all (not what it says). */
|
|
29
|
+
hasUsing: boolean;
|
|
30
|
+
/** Whether a WITH CHECK clause is present at all (not what it says). */
|
|
31
|
+
hasWithCheck: boolean;
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
export interface PolicyDrift {
|
|
@@ -40,18 +44,57 @@ export interface Queryable {
|
|
|
40
44
|
query<R>(text: string, values?: unknown[]): Promise<{ rows: R[] }>;
|
|
41
45
|
}
|
|
42
46
|
|
|
43
|
-
|
|
47
|
+
// The trailing group captures whichever clause follows the TO list, which is
|
|
48
|
+
// what tells us the clause is present.
|
|
49
|
+
const CREATE_POLICY = /CREATE POLICY "([^"]+)" ON "([^"]+)"\."([^"]+)"\s+AS (\w+)\s+FOR (\w+)\s+TO ([^\n]+?)(\s+USING\s*\(|\s+WITH CHECK\s*\(|;)/gi;
|
|
50
|
+
|
|
51
|
+
const WITH_CHECK_NEXT = /^\s+WITH CHECK\s*\(/i;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Index just past the `)` closing a clause whose `(` ends at `open`.
|
|
55
|
+
*
|
|
56
|
+
* Needed because a policy expression nests parens and can contain a quoted
|
|
57
|
+
* literal holding either character, so "find the next `)`" would stop early and
|
|
58
|
+
* miss the `WITH CHECK` that follows.
|
|
59
|
+
*/
|
|
60
|
+
function clauseEnd(ddl: string, open: number): number {
|
|
61
|
+
let depth = 1;
|
|
62
|
+
let inQuote = false;
|
|
63
|
+
for (let i = open; i < ddl.length; i++) {
|
|
64
|
+
const c = ddl[i];
|
|
65
|
+
if (inQuote) {
|
|
66
|
+
// '' is an escaped quote inside a string, not a close.
|
|
67
|
+
if (c === "'") {
|
|
68
|
+
if (ddl[i + 1] === "'") i++;
|
|
69
|
+
else inQuote = false;
|
|
70
|
+
}
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (c === "'") inQuote = true;
|
|
74
|
+
else if (c === "(") depth++;
|
|
75
|
+
else if (c === ")" && --depth === 0) return i + 1;
|
|
76
|
+
}
|
|
77
|
+
return ddl.length;
|
|
78
|
+
}
|
|
44
79
|
|
|
45
80
|
/** Parse the generated DDL rather than rebuilding the shape by hand. */
|
|
46
81
|
export function parseExpectedPolicies(ddl: string): PolicyRef[] {
|
|
47
82
|
const found: PolicyRef[] = [];
|
|
48
83
|
for (const m of ddl.matchAll(CREATE_POLICY)) {
|
|
49
|
-
const [, name, schema, table, , command, rolesRaw] = m;
|
|
84
|
+
const [, name, schema, table, , command, rolesRaw, clause] = m;
|
|
50
85
|
const roles = rolesRaw
|
|
51
86
|
.split(",")
|
|
52
87
|
.map((r) => r.trim().replace(/^"|"$/g, ""))
|
|
53
88
|
.filter(Boolean);
|
|
54
|
-
|
|
89
|
+
|
|
90
|
+
// The generator emits USING before WITH CHECK, so what follows the TO
|
|
91
|
+
// list settles USING; WITH CHECK is then whatever follows that clause.
|
|
92
|
+
const hasUsing = /USING/i.test(clause);
|
|
93
|
+
const hasWithCheck = hasUsing
|
|
94
|
+
? WITH_CHECK_NEXT.test(ddl.slice(clauseEnd(ddl, m.index + m[0].length)))
|
|
95
|
+
: /WITH CHECK/i.test(clause);
|
|
96
|
+
|
|
97
|
+
found.push({ schema, table, name, roles, command: command.toUpperCase(), hasUsing, hasWithCheck });
|
|
55
98
|
}
|
|
56
99
|
return found;
|
|
57
100
|
}
|
|
@@ -59,8 +102,9 @@ export function parseExpectedPolicies(ddl: string): PolicyRef[] {
|
|
|
59
102
|
async function readLivePolicies(client: Queryable, schemas: string[]): Promise<PolicyRef[]> {
|
|
60
103
|
const { rows } = await client.query<{
|
|
61
104
|
schemaname: string; tablename: string; policyname: string; roles: string[] | string; cmd: string;
|
|
105
|
+
qual: string | null; with_check: string | null;
|
|
62
106
|
}>(
|
|
63
|
-
`SELECT schemaname, tablename, policyname, roles, cmd
|
|
107
|
+
`SELECT schemaname, tablename, policyname, roles, cmd, qual, with_check
|
|
64
108
|
FROM pg_policies
|
|
65
109
|
WHERE schemaname = ANY($1)`,
|
|
66
110
|
[schemas]
|
|
@@ -74,7 +118,11 @@ async function readLivePolicies(client: Queryable, schemas: string[]): Promise<P
|
|
|
74
118
|
roles: Array.isArray(r.roles)
|
|
75
119
|
? r.roles
|
|
76
120
|
: String(r.roles ?? "").replace(/^\{|\}$/g, "").split(",").filter(Boolean),
|
|
77
|
-
command: (r.cmd ?? "ALL").toUpperCase()
|
|
121
|
+
command: (r.cmd ?? "ALL").toUpperCase(),
|
|
122
|
+
// Presence only. Postgres rewrites the text, but it does not invent or
|
|
123
|
+
// drop a clause: NULL here means the policy genuinely has none.
|
|
124
|
+
hasUsing: r.qual != null,
|
|
125
|
+
hasWithCheck: r.with_check != null
|
|
78
126
|
}));
|
|
79
127
|
}
|
|
80
128
|
|
|
@@ -85,11 +133,18 @@ const sameRoles = (a: string[], b: string[]) =>
|
|
|
85
133
|
/**
|
|
86
134
|
* Diff expected against live.
|
|
87
135
|
*
|
|
88
|
-
* Compares names, roles and
|
|
89
|
-
* *
|
|
90
|
-
* `with_check` when storing them (parenthesising, casting,
|
|
91
|
-
* so text comparison reports drift that does not exist, and
|
|
92
|
-
*
|
|
136
|
+
* Compares names, roles, command, and whether each clause exists — all exact
|
|
137
|
+
* values. Policy expression *text* is deliberately not compared: Postgres
|
|
138
|
+
* rewrites `qual`/`with_check` when storing them (parenthesising, casting,
|
|
139
|
+
* schema-qualifying), so text comparison reports drift that does not exist, and
|
|
140
|
+
* a check that cries wolf gets ignored.
|
|
141
|
+
*
|
|
142
|
+
* Presence is not text, though. A NULL `qual` is not a rewrite of an
|
|
143
|
+
* expression, it is the absence of one, and absence has no false-positive risk:
|
|
144
|
+
* either the generator emitted a clause or it did not. That distinction is worth
|
|
145
|
+
* the extra comparison — a production database was found with a SELECT policy
|
|
146
|
+
* whose `qual` was NULL, matching on every field this checked and denying 100%
|
|
147
|
+
* of reads. The same blindness would hide a policy that fails open.
|
|
93
148
|
*/
|
|
94
149
|
export async function checkPolicyDrift(
|
|
95
150
|
client: Queryable,
|
|
@@ -120,6 +175,13 @@ export async function checkPolicyDrift(
|
|
|
120
175
|
if (want.command !== got.command) {
|
|
121
176
|
differences.push(`command: expected ${want.command}, database has ${got.command}`);
|
|
122
177
|
}
|
|
178
|
+
for (const clause of ["USING", "WITH CHECK"] as const) {
|
|
179
|
+
const key = clause === "USING" ? "hasUsing" : "hasWithCheck";
|
|
180
|
+
if (want[key] === got[key]) continue;
|
|
181
|
+
differences.push(want[key]
|
|
182
|
+
? `${clause}: expected an expression, database has none — this policy matches no rows`
|
|
183
|
+
: `${clause}: expected none, database has an expression`);
|
|
184
|
+
}
|
|
123
185
|
if (differences.length > 0) drift.diverged.push({ expected: want, actual: got, differences });
|
|
124
186
|
}
|
|
125
187
|
|
|
@@ -130,6 +192,62 @@ export async function checkPolicyDrift(
|
|
|
130
192
|
return drift;
|
|
131
193
|
}
|
|
132
194
|
|
|
195
|
+
/**
|
|
196
|
+
* Does this name look like one the generator produced for this table?
|
|
197
|
+
*
|
|
198
|
+
* Unnamed rules compile to `<table>_<op>_<sha1[0:7]>` (plus `_<idx>` when one
|
|
199
|
+
* rule spans several operations), and the hash covers the rule's semantics — so
|
|
200
|
+
* *editing* a rule renames its policy. The policy under the old name is left
|
|
201
|
+
* behind by `db push`, which only DROPs the names it is about to CREATE, and
|
|
202
|
+
* Postgres ORs PERMISSIVE policies together: a superseded `USING (true)` keeps
|
|
203
|
+
* granting everything no matter how tight its replacement is.
|
|
204
|
+
*
|
|
205
|
+
* Matching the shape is what makes dropping them safe. A hand-written policy
|
|
206
|
+
* would have to collide with a 7-hex digest to be mistaken for generated one;
|
|
207
|
+
* a policy named anything else is left alone and merely reported, because a
|
|
208
|
+
* custom name is indistinguishable from one someone wrote in SQL on purpose.
|
|
209
|
+
*/
|
|
210
|
+
export function isGeneratedPolicyName(name: string, table: string): boolean {
|
|
211
|
+
return new RegExp(`^${table.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}_(select|insert|update|delete|all)_[0-9a-f]{7}(_\\d+)?$`)
|
|
212
|
+
.test(name);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export interface OrphanCleanup {
|
|
216
|
+
/** Superseded generated policies that were dropped. */
|
|
217
|
+
dropped: PolicyRef[];
|
|
218
|
+
/** Orphans left in place because their names are not generator-shaped. */
|
|
219
|
+
kept: PolicyRef[];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Drop the policies an earlier push superseded but never removed.
|
|
224
|
+
*
|
|
225
|
+
* Only touches tables the collections describe — a table with no expected
|
|
226
|
+
* policy is not ours to reconcile, and scanning by schema alone would sweep up
|
|
227
|
+
* policies belonging to something else sharing the database.
|
|
228
|
+
*/
|
|
229
|
+
export async function dropOrphanedPolicies(
|
|
230
|
+
client: Queryable,
|
|
231
|
+
drift: PolicyDrift,
|
|
232
|
+
collections: CollectionConfig[]
|
|
233
|
+
): Promise<OrphanCleanup> {
|
|
234
|
+
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(collections));
|
|
235
|
+
const managed = new Set(expected.map((p) => `${p.schema}.${p.table}`));
|
|
236
|
+
|
|
237
|
+
const cleanup: OrphanCleanup = { dropped: [], kept: [] };
|
|
238
|
+
for (const p of drift.orphaned) {
|
|
239
|
+
if (!managed.has(`${p.schema}.${p.table}`) || !isGeneratedPolicyName(p.name, p.table)) {
|
|
240
|
+
cleanup.kept.push(p);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
// Identifiers are quoted, and the name came from pg_policies rather than
|
|
244
|
+
// from user input, so it is already a valid identifier.
|
|
245
|
+
await client.query(`DROP POLICY IF EXISTS "${p.name}" ON "${p.schema}"."${p.table}"`);
|
|
246
|
+
cleanup.dropped.push(p);
|
|
247
|
+
}
|
|
248
|
+
return cleanup;
|
|
249
|
+
}
|
|
250
|
+
|
|
133
251
|
export const hasDrift = (d: PolicyDrift): boolean =>
|
|
134
252
|
d.missing.length > 0 || d.orphaned.length > 0 || d.diverged.length > 0;
|
|
135
253
|
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { sql as drizzleSql, SQL } from "drizzle-orm";
|
|
2
|
+
import { ANONYMOUS_USER_ID, PolicyExpression, SecurityRule } from "@rebasepro/types";
|
|
3
|
+
import { AnonymousGrantRisk, findAnonymousGrants, securityRuleToConditions } from "@rebasepro/common";
|
|
2
4
|
import { logger } from "@rebasepro/server";
|
|
3
5
|
|
|
4
6
|
/**
|
|
@@ -57,7 +59,7 @@ export interface ConnectionPosture {
|
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
export interface AuthContext {
|
|
60
|
-
|
|
62
|
+
uid: string;
|
|
61
63
|
/** Raw roles as carried on the user (strings or `{ id }` objects). */
|
|
62
64
|
roles: unknown[];
|
|
63
65
|
}
|
|
@@ -200,19 +202,21 @@ export async function ensureAppRole(run: RawSqlRunner, schemas: string[]): Promi
|
|
|
200
202
|
* treat `auth.uid() IS NULL` as the trusted server context, and `auth.uid()`
|
|
201
203
|
* is `NULLIF(current_setting('app.user_id'), '')` — so an EMPTY user id would
|
|
202
204
|
* be read as NULL and silently escalate a user request to server privileges.
|
|
203
|
-
* Coerce empty/blank ids to
|
|
204
|
-
* than trusting every caller (e.g. realtime subscription auth) to do it.
|
|
205
|
+
* Coerce empty/blank ids to `ANONYMOUS_USER_ID` here, at the single chokepoint,
|
|
206
|
+
* rather than trusting every caller (e.g. realtime subscription auth) to do it.
|
|
207
|
+
* That sentinel is exported from `@rebasepro/types` because it leaks into rule
|
|
208
|
+
* semantics: it is why `auth.uid() IS NOT NULL` is true for anonymous requests.
|
|
205
209
|
*/
|
|
206
210
|
export async function applyAuthContext(tx: SqlTx, auth: AuthContext, userRole?: string): Promise<void> {
|
|
207
|
-
const
|
|
211
|
+
const uid = typeof auth.uid === "string" && auth.uid.trim() !== "" ? auth.uid : ANONYMOUS_USER_ID;
|
|
208
212
|
const normalizedRoles = auth.roles.map((r: unknown) =>
|
|
209
213
|
typeof r === "string" ? r : (r as Record<string, unknown>)?.id ?? String(r)
|
|
210
214
|
);
|
|
211
215
|
await tx.execute(drizzleSql`
|
|
212
216
|
SELECT
|
|
213
|
-
set_config('app.user_id', ${
|
|
217
|
+
set_config('app.user_id', ${uid}, true),
|
|
214
218
|
set_config('app.user_roles', ${normalizedRoles.join(",")}, true),
|
|
215
|
-
set_config('app.jwt', ${JSON.stringify({ sub:
|
|
219
|
+
set_config('app.jwt', ${JSON.stringify({ sub: uid, roles: auth.roles })}, true)
|
|
216
220
|
`);
|
|
217
221
|
if (userRole) {
|
|
218
222
|
await tx.execute(drizzleSql.raw(`SET LOCAL ROLE ${quoteIdent(userRole)}`));
|
|
@@ -226,6 +230,63 @@ const FOREIGN_CONVENTION_ROLES: Record<string, string> = {
|
|
|
226
230
|
service_role: "Supabase"
|
|
227
231
|
};
|
|
228
232
|
|
|
233
|
+
/**
|
|
234
|
+
* Warn about rules that read as "signed-in users only" but admit anonymous
|
|
235
|
+
* callers — `auth.uid() IS NOT NULL`, or a comparison against another
|
|
236
|
+
* platform's magic user id such as `'anon'`.
|
|
237
|
+
*
|
|
238
|
+
* The sibling of {@link validatePolicyPgRoles}, for the more dangerous spelling
|
|
239
|
+
* of the same habit. A foreign `pgRoles` value makes a policy unreachable and
|
|
240
|
+
* the table reads empty — loud, and that guard throws. These do the opposite:
|
|
241
|
+
* the rule compiles to a grant, and nothing looks wrong until the data is
|
|
242
|
+
* already public.
|
|
243
|
+
*
|
|
244
|
+
* Warns rather than throws. Unlike an unreachable `pgRoles`, these rules are
|
|
245
|
+
* serving traffic today: refusing to boot would take an app offline to report a
|
|
246
|
+
* problem it already has, and on the read path it would take it offline
|
|
247
|
+
* *because* its data was exposed. Rewriting the author's SQL is not an option
|
|
248
|
+
* either — this is the escape hatch whose whole promise is that it means what it
|
|
249
|
+
* says. So: say so, loudly, and leave the rule alone.
|
|
250
|
+
*/
|
|
251
|
+
export function warnOnAnonymousGrants(
|
|
252
|
+
collections: { slug?: string; securityRules?: readonly SecurityRule[] }[]
|
|
253
|
+
): void {
|
|
254
|
+
// Grouped by the mistake, not by the rule: one habit typically repeats
|
|
255
|
+
// across every collection an author wrote, and a per-rule list would repeat
|
|
256
|
+
// the same paragraph dozens of times and get skimmed.
|
|
257
|
+
const byRisk = new Map<string, { risk: AnonymousGrantRisk; sites: string[] }>();
|
|
258
|
+
|
|
259
|
+
for (const collection of collections) {
|
|
260
|
+
for (const rule of collection.securityRules ?? []) {
|
|
261
|
+
const { usingExpr, withCheckExpr } = securityRuleToConditions(rule);
|
|
262
|
+
const risks = [usingExpr, withCheckExpr]
|
|
263
|
+
.filter((e): e is PolicyExpression => e !== null)
|
|
264
|
+
.flatMap(findAnonymousGrants);
|
|
265
|
+
|
|
266
|
+
for (const risk of risks) {
|
|
267
|
+
const key = `${risk.pattern}:${risk.detail}`;
|
|
268
|
+
const site = `${collection.slug ?? "(unnamed)"} → "${rule.name ?? "(unnamed rule)"}"`;
|
|
269
|
+
const entry = byRisk.get(key) ?? { risk, sites: [] };
|
|
270
|
+
if (!entry.sites.includes(site)) entry.sites.push(site);
|
|
271
|
+
byRisk.set(key, entry);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (byRisk.size === 0) return;
|
|
277
|
+
|
|
278
|
+
const problems = [...byRisk.values()].map(({ risk, sites }) =>
|
|
279
|
+
` • ${risk.explanation}\n ${sites.length} rule(s): ${sites.join(", ")}`
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
logger.warn(
|
|
283
|
+
`Security rules that read as a lockdown but grant access to anonymous requests. Every caller from a ` +
|
|
284
|
+
`client carries a user id ('${ANONYMOUS_USER_ID}' when nobody is signed in), so these clauses are ` +
|
|
285
|
+
`true for everyone:\n\n` +
|
|
286
|
+
problems.join("\n\n") + "\n"
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
229
290
|
/**
|
|
230
291
|
* Reject `pgRoles` that this server can never satisfy.
|
|
231
292
|
*
|