@rebasepro/server-postgres 0.9.1-canary.f0ac103 → 0.9.1-canary.fd3754b
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/PostgresBackendDriver.d.ts +2 -25
- package/dist/PostgresBootstrapper.d.ts +0 -10
- package/dist/auth/services.d.ts +0 -16
- package/dist/chunk-DSJWtz9O.js +40 -0
- package/dist/connection.d.ts +0 -21
- package/dist/data-transformer.d.ts +2 -9
- package/dist/index.es.js +2604 -2007
- package/dist/index.es.js.map +1 -1
- package/dist/schema/auth-default-policies.d.ts +10 -0
- package/dist/security/policy-drift.d.ts +5 -16
- package/dist/security/rls-enforcement.d.ts +2 -27
- package/dist/services/FetchService.d.ts +24 -4
- package/dist/services/PersistService.d.ts +1 -27
- package/dist/services/RelationService.d.ts +1 -34
- package/dist/services/collection-helpers.d.ts +14 -79
- package/dist/services/dataService.d.ts +1 -3
- package/dist/services/index.d.ts +1 -1
- package/dist/services/realtimeService.d.ts +0 -7
- package/dist/src-Eh-CZosp.js +595 -0
- package/dist/src-Eh-CZosp.js.map +1 -0
- package/package.json +17 -15
- package/src/PostgresBackendDriver.ts +13 -127
- package/src/PostgresBootstrapper.ts +26 -68
- package/src/auth/ensure-tables.ts +11 -73
- package/src/auth/services.ts +19 -49
- package/src/cli-helpers.ts +20 -2
- package/src/connection.ts +1 -61
- package/src/data-transformer.ts +9 -11
- package/src/databasePoolManager.ts +0 -2
- package/src/schema/auth-default-policies.ts +125 -0
- package/src/schema/doctor-cli.ts +1 -5
- package/src/schema/generate-drizzle-schema-logic.ts +29 -24
- package/src/schema/generate-postgres-ddl-logic.ts +28 -76
- package/src/schema/introspect-db.ts +2 -19
- package/src/security/policy-drift.test.ts +14 -48
- package/src/security/policy-drift.ts +10 -72
- package/src/security/rls-enforcement.ts +3 -64
- package/src/services/BranchService.ts +10 -42
- package/src/services/FetchService.ts +270 -65
- package/src/services/PersistService.ts +14 -130
- package/src/services/RelationService.ts +94 -153
- package/src/services/collection-helpers.ts +47 -164
- package/src/services/dataService.ts +2 -3
- package/src/services/index.ts +0 -1
- package/src/services/realtimeService.ts +19 -40
- package/src/utils/drizzle-conditions.ts +0 -13
- package/src/websocket.ts +1 -4
- package/dist/collections/buildRegistry.d.ts +0 -27
- package/dist/services/row-pipeline.d.ts +0 -63
- package/src/collections/buildRegistry.ts +0 -59
- package/src/services/row-pipeline.ts +0 -215
package/src/cli-helpers.ts
CHANGED
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import { execSync } from "child_process";
|
|
4
|
-
import { pathToFileURL } from "url";
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
5
5
|
import chalk from "chalk";
|
|
6
6
|
import { logger } from "@rebasepro/server";
|
|
7
7
|
import type { CollectionConfig, Relation } from "@rebasepro/types";
|
|
8
|
-
|
|
8
|
+
|
|
9
|
+
const getHelpersDirname = () => {
|
|
10
|
+
try {
|
|
11
|
+
return eval("__dirname");
|
|
12
|
+
} catch {
|
|
13
|
+
const err = new Error();
|
|
14
|
+
const stackLine = err.stack?.split("\n")[1] || "";
|
|
15
|
+
const match = stackLine.match(/\((.*?):\d+:\d+\)/) || stackLine.match(/at (.*?):\d+:\d+/);
|
|
16
|
+
if (match && match[1]) {
|
|
17
|
+
let cleanPath = match[1];
|
|
18
|
+
if (cleanPath.startsWith("file://")) {
|
|
19
|
+
cleanPath = fileURLToPath(cleanPath);
|
|
20
|
+
}
|
|
21
|
+
return path.dirname(cleanPath);
|
|
22
|
+
}
|
|
23
|
+
return process.cwd();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const __helpersDirname = getHelpersDirname();
|
|
9
27
|
|
|
10
28
|
|
|
11
29
|
|
package/src/connection.ts
CHANGED
|
@@ -27,68 +27,11 @@ const DEFAULT_POOL: Required<PostgresPoolConfig> = {
|
|
|
27
27
|
max: 20,
|
|
28
28
|
idleTimeoutMillis: 30_000,
|
|
29
29
|
connectionTimeoutMillis: 10_000,
|
|
30
|
-
|
|
31
|
-
// statement_timeout. When the client timer fires first, node-postgres
|
|
32
|
-
// abandons the in-flight statement but keeps the connection — inside a
|
|
33
|
-
// transaction that leaves the tx open (and any pending ROLLBACK is
|
|
34
|
-
// spliced out of the client queue before it ever reaches the wire), so
|
|
35
|
-
// the pooled connection is returned still in-transaction with its RLS
|
|
36
|
-
// GUCs set. The server abort (SQLSTATE 57014) is the clean path; the
|
|
37
|
-
// client timeout is only a backstop for a dead network.
|
|
38
|
-
queryTimeout: 60_000,
|
|
30
|
+
queryTimeout: 30_000,
|
|
39
31
|
statementTimeout: 30_000,
|
|
40
32
|
keepAlive: true
|
|
41
33
|
};
|
|
42
34
|
|
|
43
|
-
/** ReadyForQuery status byte: `I` idle, `T` in transaction, `E` failed transaction. */
|
|
44
|
-
const TX_IDLE = "I";
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Destroy pool clients that are released while still inside a transaction.
|
|
48
|
-
*
|
|
49
|
-
* pg-pool returns a client to the idle list whenever `release()` is called
|
|
50
|
-
* without an error — even if the connection is still mid-transaction (status
|
|
51
|
-
* `T`/`E`). That happens in practice: drizzle's pool transaction releases in
|
|
52
|
-
* a `finally` after attempting ROLLBACK, and if the ROLLBACK itself fails
|
|
53
|
-
* (e.g. it was queued behind a statement that hit the client-side
|
|
54
|
-
* query_timeout), the client goes back dirty. The next checkout then runs
|
|
55
|
-
* its statements inside the zombie transaction — with the previous request's
|
|
56
|
-
* `app.*` RLS GUCs still applied, which turns unrelated queries into
|
|
57
|
-
* RLS-scoped ones (observed in production as registration failing with
|
|
58
|
-
* SQLSTATE 42501 under a leaked anonymous context).
|
|
59
|
-
*
|
|
60
|
-
* pg-pool emits `release` before it consults its private `_expired` set, so
|
|
61
|
-
* marking the client expired here makes `_release()` destroy it instead of
|
|
62
|
-
* pooling it. Both `client._txStatus` (pg ≥ 8.16) and `pool._expired` are
|
|
63
|
-
* private APIs — feature-detect and fall back to loud logging so an upstream
|
|
64
|
-
* change degrades to observability, never to silent corruption.
|
|
65
|
-
*/
|
|
66
|
-
export function guardPoolAgainstDirtyRelease(pool: Pool, label: string): void {
|
|
67
|
-
pool.on("release", (err: Error | undefined, client: unknown) => {
|
|
68
|
-
if (err) return; // errored clients are already destroyed by pg-pool
|
|
69
|
-
const txStatus = (client as { _txStatus?: string | null })?._txStatus;
|
|
70
|
-
if (typeof txStatus !== "string" || txStatus === TX_IDLE) return;
|
|
71
|
-
|
|
72
|
-
// pg-pool keeps expired clients in a WeakSet (a plain object works too
|
|
73
|
-
// if upstream ever changes it — duck-type on add/has).
|
|
74
|
-
const expired = (pool as unknown as { _expired?: { add(c: object): unknown; has(c: object): boolean } })._expired;
|
|
75
|
-
if (expired && typeof expired.add === "function" && typeof expired.has === "function" && client && typeof client === "object") {
|
|
76
|
-
expired.add(client);
|
|
77
|
-
logger.error(
|
|
78
|
-
`[${label}] Client released back to the pool while still in a transaction ` +
|
|
79
|
-
`(status '${txStatus}') — destroying it so the open transaction and its ` +
|
|
80
|
-
`session state (RLS GUCs) cannot leak into the next request.`
|
|
81
|
-
);
|
|
82
|
-
} else {
|
|
83
|
-
logger.error(
|
|
84
|
-
`[${label}] Client released mid-transaction (status '${txStatus}') but the ` +
|
|
85
|
-
`pool's internal expiry set is unavailable (pg-pool internals changed?). ` +
|
|
86
|
-
`The connection may leak its open transaction into subsequent requests.`
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
35
|
/**
|
|
93
36
|
* Create a Drizzle-backed Postgres connection with a production-grade
|
|
94
37
|
* connection pool.
|
|
@@ -132,7 +75,6 @@ export function createPostgresDatabaseConnection(
|
|
|
132
75
|
logger.warn("[pg-pool] Connection timeout detected — pool will auto-retry");
|
|
133
76
|
}
|
|
134
77
|
});
|
|
135
|
-
guardPoolAgainstDirtyRelease(pool, "pg-pool");
|
|
136
78
|
|
|
137
79
|
// Create drizzle instance — pass schema when available to enable db.query relational API
|
|
138
80
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
@@ -176,7 +118,6 @@ export function createDirectDatabaseConnection(
|
|
|
176
118
|
pool.on("error", (err) => {
|
|
177
119
|
logger.error("[pg-direct-pool] Unexpected pool error", { detail: err.message });
|
|
178
120
|
});
|
|
179
|
-
guardPoolAgainstDirtyRelease(pool, "pg-direct-pool");
|
|
180
121
|
|
|
181
122
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
182
123
|
|
|
@@ -216,7 +157,6 @@ export function createReadReplicaConnection(
|
|
|
216
157
|
pool.on("error", (err) => {
|
|
217
158
|
logger.error("[pg-replica-pool] Unexpected pool error", { detail: err.message });
|
|
218
159
|
});
|
|
219
|
-
guardPoolAgainstDirtyRelease(pool, "pg-replica-pool");
|
|
220
160
|
|
|
221
161
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
222
162
|
|
package/src/data-transformer.ts
CHANGED
|
@@ -20,19 +20,12 @@ import { logger } from "@rebasepro/server";
|
|
|
20
20
|
export interface SerializedEntityData {
|
|
21
21
|
/** Scalar column values ready for INSERT/UPDATE. */
|
|
22
22
|
scalarData: Record<string, unknown>;
|
|
23
|
-
/**
|
|
24
|
-
* Inverse relation updates that must be applied to target tables.
|
|
25
|
-
*
|
|
26
|
-
* No address here: the row being written does not know its own. These are
|
|
27
|
-
* applied by `PersistService`, which addresses them with the id it holds —
|
|
28
|
-
* the one it was given for an update, or the one the INSERT returned — and
|
|
29
|
-
* that is the authority. This item used to carry a `currentId` derived from
|
|
30
|
-
* the *input* values, which nothing ever read.
|
|
31
|
-
*/
|
|
23
|
+
/** Inverse relation updates that must be applied to target tables. */
|
|
32
24
|
inverseRelationUpdates: Array<{
|
|
33
25
|
relationKey: string;
|
|
34
26
|
relation: Relation;
|
|
35
27
|
newValue: unknown;
|
|
28
|
+
currentId?: string | number;
|
|
36
29
|
}>;
|
|
37
30
|
/** JoinPath relation updates that require multi-hop writes. */
|
|
38
31
|
joinPathRelationUpdates: Array<{
|
|
@@ -112,6 +105,7 @@ joinPathRelationUpdates: [] };
|
|
|
112
105
|
relationKey: string;
|
|
113
106
|
relation: Relation;
|
|
114
107
|
newValue: unknown;
|
|
108
|
+
currentId?: string | number;
|
|
115
109
|
}> = [];
|
|
116
110
|
const joinPathRelationUpdates: Array<{
|
|
117
111
|
relationKey: string;
|
|
@@ -152,10 +146,12 @@ joinPathRelationUpdates: [] };
|
|
|
152
146
|
} else if (relation.direction === "inverse" && relation.foreignKeyOnTarget) {
|
|
153
147
|
// Inverse relation: Need to update the target table's FK
|
|
154
148
|
const serializedValue = serializePropertyToServer(effectiveValue, property);
|
|
149
|
+
const pks = getPrimaryKeys(collection, registry!);
|
|
155
150
|
inverseRelationUpdates.push({
|
|
156
151
|
relationKey: key,
|
|
157
152
|
relation,
|
|
158
|
-
newValue: serializedValue
|
|
153
|
+
newValue: serializedValue,
|
|
154
|
+
currentId: (row.id as string | number | undefined) || buildCompositeId(row, pks)
|
|
159
155
|
});
|
|
160
156
|
// Don't add the original relation property to the result
|
|
161
157
|
continue;
|
|
@@ -174,10 +170,12 @@ joinPathRelationUpdates: [] };
|
|
|
174
170
|
});
|
|
175
171
|
} else {
|
|
176
172
|
// Many inverse joinPath: capture as inverse relation update
|
|
173
|
+
const pks = getPrimaryKeys(collection, registry!);
|
|
177
174
|
inverseRelationUpdates.push({
|
|
178
175
|
relationKey: key,
|
|
179
176
|
relation,
|
|
180
|
-
newValue: serializedValue
|
|
177
|
+
newValue: serializedValue,
|
|
178
|
+
currentId: (row.id as string | number | undefined) || buildCompositeId(row, pks)
|
|
181
179
|
});
|
|
182
180
|
}
|
|
183
181
|
// Don't add the original relation property to the result
|
|
@@ -2,7 +2,6 @@ import { Pool } from "pg";
|
|
|
2
2
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
3
3
|
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
4
4
|
import { logger } from "@rebasepro/server";
|
|
5
|
-
import { guardPoolAgainstDirtyRelease } from "./connection";
|
|
6
5
|
|
|
7
6
|
export class DatabasePoolManager {
|
|
8
7
|
private pools: Map<string, Pool> = new Map();
|
|
@@ -51,7 +50,6 @@ export class DatabasePoolManager {
|
|
|
51
50
|
pool.on("error", (err) => {
|
|
52
51
|
logger.error(`[DatabasePoolManager] Unexpected error on idle client for db ${databaseName}`, { error: err });
|
|
53
52
|
});
|
|
54
|
-
guardPoolAgainstDirtyRelease(pool, `pg-pool:${databaseName}`);
|
|
55
53
|
|
|
56
54
|
this.pools.set(databaseName, pool);
|
|
57
55
|
return pool;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { CollectionConfig, SecurityRule, SecurityOperation, AuthCollectionConfig, PolicyExpression, isPostgresCollectionConfig, policy } from "@rebasepro/types";
|
|
2
|
+
import { getTableName } from "@rebasepro/common";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Default RLS policies injected by the schema generator.
|
|
6
|
+
*
|
|
7
|
+
* Rebase's enforcement model is unified: authenticated (user-context) requests
|
|
8
|
+
* run under the restricted `rebase_user` role, so Postgres RLS binds *every*
|
|
9
|
+
* statement — reads and writes. A collection's `securityRules` are the whole
|
|
10
|
+
* authorization model. The server context (auth flows, migrations,
|
|
11
|
+
* `dataAsAdmin`) runs as the owner and bypasses RLS.
|
|
12
|
+
*
|
|
13
|
+
* Because RLS default-denies, every collection is **locked by default**: with
|
|
14
|
+
* no rules, only the server context and admins can touch it. The generator
|
|
15
|
+
* injects that safe baseline:
|
|
16
|
+
*
|
|
17
|
+
* **For every collection**
|
|
18
|
+
* 1. A permissive **server-or-admin SELECT** grant.
|
|
19
|
+
* 2. A permissive **server-or-admin write** grant (insert/update/delete).
|
|
20
|
+
*
|
|
21
|
+
* Author `securityRules` are permissive and OR together, so explicit rules only
|
|
22
|
+
* *broaden* access from this locked baseline (e.g. "users read/write their own
|
|
23
|
+
* rows").
|
|
24
|
+
*
|
|
25
|
+
* **For auth collections additionally**
|
|
26
|
+
* 3. A permissive **self SELECT** grant (`id = auth.uid()`), so users can read
|
|
27
|
+
* their own row (profile, session bootstrap) without every app re-declaring
|
|
28
|
+
* it.
|
|
29
|
+
* 4. A **restrictive** admin write gate. Restrictive policies are AND'd with
|
|
30
|
+
* every other policy, so a write is rejected unless the caller is an admin
|
|
31
|
+
* (or the server context) — even if the author also wrote a permissive rule
|
|
32
|
+
* such as "a user may edit their own row". Without this, a permissive owner
|
|
33
|
+
* rule would let a user change their own `roles`.
|
|
34
|
+
*
|
|
35
|
+
* The server context is recognised as `auth.uid() IS NULL` — the built-in flows
|
|
36
|
+
* that run without a user (signup, migrations) set no user GUC — which also
|
|
37
|
+
* lets the owner connection satisfy these policies even under FORCE RLS.
|
|
38
|
+
*
|
|
39
|
+
* Opt out with `disableDefaultPolicies: true` to take full responsibility for
|
|
40
|
+
* the collection's RLS.
|
|
41
|
+
*/
|
|
42
|
+
// Expressed structurally (not as raw SQL) so the admin UI can evaluate it
|
|
43
|
+
// exactly — the framework's most security-critical policies must be reflected
|
|
44
|
+
// precisely, not left as un-evaluable raw clauses. Compiles to
|
|
45
|
+
// `auth.uid() IS NULL OR (string_to_array(auth.roles(), ',') && ARRAY['admin'])`.
|
|
46
|
+
const SERVER_OR_ADMIN_EXPR: PolicyExpression = policy.or(
|
|
47
|
+
policy.not(policy.authenticated()),
|
|
48
|
+
policy.rolesOverlap(["admin"])
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
/** Write operations that must be admin-gated by default on auth collections. */
|
|
52
|
+
const DEFAULT_GUARDED_OPS: SecurityOperation[] = ["insert", "update", "delete"];
|
|
53
|
+
|
|
54
|
+
/** Whether a collection is flagged as an authentication collection. */
|
|
55
|
+
function isAuthCollection(collection: CollectionConfig): boolean {
|
|
56
|
+
const auth = collection.auth;
|
|
57
|
+
return auth === true || (typeof auth === "object" && (auth as AuthCollectionConfig)?.enabled === true);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The property marked as the row id (falls back to `id`). */
|
|
61
|
+
function getIdPropertyName(collection: CollectionConfig): string {
|
|
62
|
+
for (const [name, prop] of Object.entries(collection.properties ?? {})) {
|
|
63
|
+
if (prop && typeof prop === "object" && "isId" in prop && (prop as { isId?: unknown }).isId) {
|
|
64
|
+
return name;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return "id";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Returns the security rules that should be applied to a collection: the
|
|
72
|
+
* author's explicit `securityRules` plus the framework defaults described in
|
|
73
|
+
* the module doc (baseline server/admin read for all collections; self-read
|
|
74
|
+
* and the admin write gate for auth collections).
|
|
75
|
+
*
|
|
76
|
+
* Collections that opt out via `disableDefaultPolicies` are returned unchanged.
|
|
77
|
+
*/
|
|
78
|
+
export function getEffectiveSecurityRules(collection: CollectionConfig): SecurityRule[] {
|
|
79
|
+
const explicit = [...((isPostgresCollectionConfig(collection) ? collection.securityRules : undefined) ?? [])];
|
|
80
|
+
|
|
81
|
+
if (collection.disableDefaultPolicies) {
|
|
82
|
+
return explicit;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const tableName = getTableName(collection);
|
|
86
|
+
const injected: SecurityRule[] = [];
|
|
87
|
+
|
|
88
|
+
// Baseline read + write: the server context and admins can always operate.
|
|
89
|
+
// RLS default-denies under the user role, so without these a rule-less
|
|
90
|
+
// collection would be locked to everyone — including the admin studio.
|
|
91
|
+
// Author rules are permissive and broaden access from here.
|
|
92
|
+
injected.push({
|
|
93
|
+
name: `${tableName}_default_admin_read`,
|
|
94
|
+
operations: ["select"],
|
|
95
|
+
condition: SERVER_OR_ADMIN_EXPR
|
|
96
|
+
});
|
|
97
|
+
injected.push({
|
|
98
|
+
name: `${tableName}_default_admin_write`,
|
|
99
|
+
operations: [...DEFAULT_GUARDED_OPS],
|
|
100
|
+
condition: SERVER_OR_ADMIN_EXPR,
|
|
101
|
+
check: SERVER_OR_ADMIN_EXPR
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
if (isAuthCollection(collection)) {
|
|
105
|
+
// Self-read: a user can always read their own row.
|
|
106
|
+
injected.push({
|
|
107
|
+
name: `${tableName}_default_self_read`,
|
|
108
|
+
operations: ["select"],
|
|
109
|
+
condition: policy.compare(policy.field(getIdPropertyName(collection)), "eq", policy.authUid())
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Restrictive gate: AND'd with all other policies, so no permissive rule
|
|
113
|
+
// (e.g. an owner "edit your own row" rule) can let a non-admin change
|
|
114
|
+
// privileged columns like `roles`.
|
|
115
|
+
injected.push({
|
|
116
|
+
name: `${tableName}_require_admin_write`,
|
|
117
|
+
mode: "restrictive",
|
|
118
|
+
operations: [...DEFAULT_GUARDED_OPS],
|
|
119
|
+
condition: SERVER_OR_ADMIN_EXPR,
|
|
120
|
+
check: SERVER_OR_ADMIN_EXPR
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return [...explicit, ...injected];
|
|
125
|
+
}
|
package/src/schema/doctor-cli.ts
CHANGED
|
@@ -8,7 +8,7 @@ import chalk from "chalk";
|
|
|
8
8
|
import fs from "fs";
|
|
9
9
|
import { runDoctor, loadCollections } from "./doctor";
|
|
10
10
|
import { checkPolicyDrift, formatPolicyDrift, hasDrift } from "../security/policy-drift";
|
|
11
|
-
import { validatePolicyPgRoles
|
|
11
|
+
import { validatePolicyPgRoles } from "../security/rls-enforcement";
|
|
12
12
|
import { logger } from "@rebasepro/server";
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -41,10 +41,6 @@ async function runPolicyChecks(collectionsPath: string, databaseUrl?: string): P
|
|
|
41
41
|
logger.error(chalk.red(err instanceof Error ? err.message : String(err)));
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
// A rule that reads as a lockdown but is true for every caller compiles
|
|
45
|
-
// to a grant. Report it without booting a server.
|
|
46
|
-
warnOnAnonymousGrants(collections as never);
|
|
47
|
-
|
|
48
44
|
const drift = await checkPolicyDrift(pool as never, collections);
|
|
49
45
|
logger.info("");
|
|
50
46
|
if (hasDrift(drift)) {
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { CollectionConfig, NumberProperty, Property, Relation, RelationProperty, SecurityOperation, SecurityRule, StringProperty, isPostgresCollectionConfig, DateProperty, ArrayProperty, MapProperty, ReferenceProperty, VectorProperty, BinaryProperty } from "@rebasepro/types";
|
|
2
2
|
import { getPrimaryKeys } from "../services/collection-helpers";
|
|
3
|
-
import { getEnumVarName, getTableName, getTableVarName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres
|
|
4
|
-
import { toSnakeCase
|
|
3
|
+
import { getEnumVarName, getTableName, getTableVarName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres } from "@rebasepro/common";
|
|
4
|
+
import { toSnakeCase } from "@rebasepro/utils";
|
|
5
|
+
import { createHash } from "crypto";
|
|
5
6
|
import { logger } from "@rebasepro/server";
|
|
7
|
+
import { getEffectiveSecurityRules } from "./auth-default-policies";
|
|
6
8
|
// --- Helper Functions ---
|
|
7
9
|
|
|
8
10
|
/**
|
|
@@ -313,6 +315,22 @@ const wrapSql = (clause: string): string => `sql\`${clause}\``;
|
|
|
313
315
|
/**
|
|
314
316
|
* Generates a deterministic hash based on the rule configuration.
|
|
315
317
|
*/
|
|
318
|
+
const getPolicyNameHash = (rule: SecurityRule): string => {
|
|
319
|
+
const data = JSON.stringify({
|
|
320
|
+
a: rule.access,
|
|
321
|
+
m: rule.mode,
|
|
322
|
+
op: rule.operation,
|
|
323
|
+
ops: rule.operations?.slice().sort(),
|
|
324
|
+
own: rule.ownerField,
|
|
325
|
+
rol: rule.roles?.slice().sort(),
|
|
326
|
+
pg: rule.pgRoles?.slice().sort(),
|
|
327
|
+
u: rule.using,
|
|
328
|
+
w: rule.withCheck,
|
|
329
|
+
c: rule.condition,
|
|
330
|
+
ch: rule.check
|
|
331
|
+
});
|
|
332
|
+
return createHash("sha1").update(data).digest("hex").substring(0, 7);
|
|
333
|
+
};
|
|
316
334
|
|
|
317
335
|
/**
|
|
318
336
|
* Generates Drizzle pgPolicy() calls from a declarative SecurityRule definition.
|
|
@@ -333,11 +351,15 @@ const generatePolicyCode = (collection: CollectionConfig, rule: SecurityRule, in
|
|
|
333
351
|
? rule.operations
|
|
334
352
|
: [rule.operation ?? "all"];
|
|
335
353
|
|
|
336
|
-
const
|
|
354
|
+
const ruleHash = getPolicyNameHash(rule);
|
|
337
355
|
|
|
338
356
|
// Generate one pgPolicy per operation
|
|
339
357
|
return ops.map((op, opIdx) => {
|
|
340
|
-
|
|
358
|
+
const policyName = rule.name
|
|
359
|
+
? (ops.length > 1 ? `${rule.name}_${op}` : rule.name)
|
|
360
|
+
: `${tableName}_${op}_${ruleHash}${ops.length > 1 ? `_${opIdx}` : ""}`;
|
|
361
|
+
|
|
362
|
+
return generateSinglePolicyCode(collection, rule, op, policyName, resolveCollection);
|
|
341
363
|
}).join("");
|
|
342
364
|
};
|
|
343
365
|
|
|
@@ -545,10 +567,6 @@ export const generateSchema = async (collections: CollectionConfig[], stripPolic
|
|
|
545
567
|
});
|
|
546
568
|
schemaContent += "\n";
|
|
547
569
|
|
|
548
|
-
// Junction policy derivation needs every declaring side of each junction,
|
|
549
|
-
// not just the first relation that reached it in the walk below.
|
|
550
|
-
const junctionSpecs = resolveJunctionSpecs(collections);
|
|
551
|
-
|
|
552
570
|
// 2. Identify all tables (collections and junction tables only)
|
|
553
571
|
for (const collection of collections) {
|
|
554
572
|
const tableName = getTableName(collection);
|
|
@@ -605,22 +623,9 @@ export const generateSchema = async (collections: CollectionConfig[], stripPolic
|
|
|
605
623
|
schemaContent += `export const ${tableVarName} = ${tableCreator}(\"${baseTableName}\", {\n`;
|
|
606
624
|
schemaContent += ` ${sourceColumn}: ${sourceColType}(\"${sourceColumn}\").notNull().references(() => ${getTableVarName(getTableName(sourceCollection))}.${sourceId}, ${refOptions}),\n`;
|
|
607
625
|
schemaContent += ` ${targetColumn}: ${targetColType}(\"${targetColumn}\").notNull().references(() => ${getTableVarName(getTableName(targetCollection))}.${targetId}, ${refOptions}),\n`;
|
|
608
|
-
schemaContent += "}, (table) => (
|
|
609
|
-
schemaContent += ` primaryKey({ columns: [table.${sourceColumn}, table.${targetColumn}] })
|
|
610
|
-
|
|
611
|
-
// Junctions are generated tables like any other: locked by default,
|
|
612
|
-
// with derived policies (reads follow the endpoints, writes follow
|
|
613
|
-
// the declaring side's update rules). RLS is enabled regardless of
|
|
614
|
-
// policy stripping — a bare junction must default-deny, not fail open.
|
|
615
|
-
const junctionSpec = junctionSpecs.get(baseTableName);
|
|
616
|
-
if (!stripPolicies && junctionSpec) {
|
|
617
|
-
const junctionCollection = getJunctionCollectionConfig(junctionSpec);
|
|
618
|
-
const resolveCollection: ResolveCollection = (slug) => collections.find(c => c.slug === slug || getTableName(c) === slug);
|
|
619
|
-
getJunctionSecurityRules(junctionSpec).forEach((rule: SecurityRule, idx: number) => {
|
|
620
|
-
schemaContent += generatePolicyCode(junctionCollection, rule, idx, resolveCollection);
|
|
621
|
-
});
|
|
622
|
-
}
|
|
623
|
-
schemaContent += "])).enableRLS();\n\n";
|
|
626
|
+
schemaContent += "}, (table) => ({\n";
|
|
627
|
+
schemaContent += ` pk: primaryKey({ columns: [table.${sourceColumn}, table.${targetColumn}] })\n`;
|
|
628
|
+
schemaContent += "}));\n\n";
|
|
624
629
|
} else if (!isJunction) {
|
|
625
630
|
const schema = isPostgresCollectionConfig(collection) ? collection.schema : undefined;
|
|
626
631
|
const tableCreator = schema ? `${schema}Schema.table` : "pgTable";
|
|
@@ -1,6 +1,8 @@
|
|
|
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
|
|
3
|
-
import { toSnakeCase
|
|
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";
|
|
4
6
|
|
|
5
7
|
// --- Helper Functions ---
|
|
6
8
|
|
|
@@ -42,6 +44,22 @@ const isIdProperty = (propName: string, prop: Property, collection: CollectionCo
|
|
|
42
44
|
return !hasExplicitId && propName === "id";
|
|
43
45
|
};
|
|
44
46
|
|
|
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
|
+
};
|
|
45
63
|
|
|
46
64
|
type ResolveCollection = (slug: string) => CollectionConfig | undefined;
|
|
47
65
|
|
|
@@ -51,10 +69,14 @@ const generatePolicyDdl = (collection: CollectionConfig, rule: SecurityRule, res
|
|
|
51
69
|
? rule.operations
|
|
52
70
|
: [rule.operation ?? "all"];
|
|
53
71
|
|
|
54
|
-
const
|
|
72
|
+
const ruleHash = getPolicyNameHash(rule);
|
|
55
73
|
|
|
56
74
|
return ops.map((op, opIdx) => {
|
|
57
|
-
|
|
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);
|
|
58
80
|
}).join("");
|
|
59
81
|
};
|
|
60
82
|
|
|
@@ -226,10 +248,6 @@ export const generatePostgresDdl = async (
|
|
|
226
248
|
});
|
|
227
249
|
if (ddl.endsWith(";\n")) ddl += "\n";
|
|
228
250
|
|
|
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
|
-
|
|
233
251
|
const allTablesToGenerate = new Map<string, {
|
|
234
252
|
collection: CollectionConfig,
|
|
235
253
|
isJunction?: boolean,
|
|
@@ -265,11 +283,6 @@ export const generatePostgresDdl = async (
|
|
|
265
283
|
|
|
266
284
|
// 3. Generate tables
|
|
267
285
|
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[] = [];
|
|
273
286
|
for (const [tableName, {
|
|
274
287
|
collection,
|
|
275
288
|
isJunction,
|
|
@@ -302,25 +315,6 @@ export const generatePostgresDdl = async (
|
|
|
302
315
|
|
|
303
316
|
fkStatements.push(`ALTER TABLE "${schema}"."${baseTableName}" ADD CONSTRAINT "${baseTableName}_${sourceColumn}_fkey" FOREIGN KEY ("${sourceColumn}") REFERENCES "${sourceSchema}"."${sourceTable}" ("${sourceId}") ON DELETE ${onDelete.toUpperCase()};`);
|
|
304
317
|
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
|
-
}
|
|
324
318
|
} else if (!isJunction) {
|
|
325
319
|
ddl += `CREATE TABLE "${schema}"."${baseTableName}" (\n`;
|
|
326
320
|
const columns: string[] = [];
|
|
@@ -442,8 +436,9 @@ export const generatePostgresDdl = async (
|
|
|
442
436
|
if (securityRules.length > 0) {
|
|
443
437
|
const resolveCollection: ResolveCollection = (slug) => collections.find(c => c.slug === slug || getTableName(c) === slug);
|
|
444
438
|
securityRules.forEach((rule: SecurityRule) => {
|
|
445
|
-
|
|
439
|
+
ddl += generatePolicyDdl(collection, rule, resolveCollection);
|
|
446
440
|
});
|
|
441
|
+
ddl += "\n";
|
|
447
442
|
}
|
|
448
443
|
}
|
|
449
444
|
}
|
|
@@ -454,12 +449,6 @@ export const generatePostgresDdl = async (
|
|
|
454
449
|
ddl += fkStatements.join("\n") + "\n\n";
|
|
455
450
|
}
|
|
456
451
|
|
|
457
|
-
if (policyStatements.length > 0) {
|
|
458
|
-
ddl += "-- Row Level Security Policies\n";
|
|
459
|
-
ddl += policyStatements.join("");
|
|
460
|
-
ddl += "\n";
|
|
461
|
-
}
|
|
462
|
-
|
|
463
452
|
return ddl;
|
|
464
453
|
};
|
|
465
454
|
|
|
@@ -489,50 +478,13 @@ export const generatePostgresPoliciesDdl = (collections: CollectionConfig[]): st
|
|
|
489
478
|
const securityRules = getEffectiveSecurityRules(collection);
|
|
490
479
|
if (securityRules.length > 0) {
|
|
491
480
|
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
|
-
|
|
494
481
|
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
|
-
}
|
|
503
482
|
ddl += generatePolicyDdl(collection, rule, resolveCollection);
|
|
504
483
|
});
|
|
505
484
|
ddl += "\n";
|
|
506
485
|
}
|
|
507
486
|
}
|
|
508
487
|
|
|
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
|
-
|
|
536
488
|
return ddl;
|
|
537
489
|
};
|
|
538
490
|
|
|
@@ -30,7 +30,6 @@ async function main() {
|
|
|
30
30
|
"--force": Boolean,
|
|
31
31
|
"--schema": String,
|
|
32
32
|
"--data-inference": Boolean,
|
|
33
|
-
"--no-data-inference": Boolean,
|
|
34
33
|
"-o": "--output",
|
|
35
34
|
"-c": "--collections",
|
|
36
35
|
"-f": "--force"
|
|
@@ -167,14 +166,8 @@ async function main() {
|
|
|
167
166
|
logger.info(chalk.blue(`Found ${tablesMap.size} tables (including ${joinTables.size} detected join tables).`));
|
|
168
167
|
|
|
169
168
|
let runDataInference = false;
|
|
170
|
-
if (args["--
|
|
171
|
-
runDataInference = false;
|
|
172
|
-
} else if (args["--data-inference"] !== undefined) {
|
|
169
|
+
if (args["--data-inference"] !== undefined) {
|
|
173
170
|
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)."));
|
|
178
171
|
} else {
|
|
179
172
|
const rl = readline.createInterface({
|
|
180
173
|
input: process.stdin,
|
|
@@ -243,17 +236,7 @@ async function main() {
|
|
|
243
236
|
const merged = mergeIndexContent(existing, generatedFiles);
|
|
244
237
|
fs.writeFileSync(indexPath, merged, "utf-8");
|
|
245
238
|
} else {
|
|
246
|
-
|
|
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);
|
|
239
|
+
const indexContent = generateIndexContent(generatedFiles);
|
|
257
240
|
fs.writeFileSync(indexPath, indexContent, "utf-8");
|
|
258
241
|
}
|
|
259
242
|
logger.info(chalk.green(` ✓ ${indexPath}`));
|