@rebasepro/server-postgres 0.29.0 → 0.29.2
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/{BranchService-JiYJ_n9E.js → BranchService-X9R505mP.js} +3 -3
- package/dist/{BranchService-JiYJ_n9E.js.map → BranchService-X9R505mP.js.map} +1 -1
- package/dist/PostgresBackendDriver.d.ts +90 -12
- package/dist/PostgresBootstrapper.d.ts +34 -0
- package/dist/auth/ensure-tables.d.ts +2 -2
- package/dist/auth/schema-version.d.ts +5 -5
- package/dist/auth/services.d.ts +9 -9
- package/dist/{backup-cli-D_aKXPyX.js → backup-cli-CwiJEp_J.js} +3 -3
- package/dist/{backup-cli-D_aKXPyX.js.map → backup-cli-CwiJEp_J.js.map} +1 -1
- package/dist/{boot-ddl-D4W9o9xE.js → boot-ddl-CbZj9ovZ.js} +2 -2
- package/dist/{boot-ddl-D4W9o9xE.js.map → boot-ddl-CbZj9ovZ.js.map} +1 -1
- package/dist/{cli-console-DPdZCWTi.js → cli-console-DDkNetCz.js} +6 -6
- package/dist/{cli-console-DPdZCWTi.js.map → cli-console-DDkNetCz.js.map} +1 -1
- package/dist/{cli-errors-D8s4z_3g.js → cli-errors-C73Qy7WJ.js} +49 -6
- package/dist/{cli-errors-D8s4z_3g.js.map → cli-errors-C73Qy7WJ.js.map} +1 -1
- package/dist/cli.js +209 -53
- package/dist/cli.js.map +1 -1
- package/dist/{connection-COuS5Fm-.js → connection-C1zXTPJq.js} +7 -3
- package/dist/{connection-COuS5Fm-.js.map → connection-C1zXTPJq.js.map} +1 -1
- package/dist/data-transformer.d.ts +2 -2
- package/dist/database-health.d.ts +2 -1
- package/dist/database-preflight-DIauc8i5.js +184 -0
- package/dist/database-preflight-DIauc8i5.js.map +1 -0
- package/dist/{databasePoolManager-7hXVurmG.js → databasePoolManager-DIjzlTK4.js} +2 -2
- package/dist/{databasePoolManager-7hXVurmG.js.map → databasePoolManager-DIjzlTK4.js.map} +1 -1
- package/dist/db-subcommands.d.ts +1 -1
- package/dist/{deferred-index-build-F2GWu009.js → deferred-index-build-DDdnjaOW.js} +25 -9
- package/dist/deferred-index-build-DDdnjaOW.js.map +1 -0
- package/dist/{doctor-_g2tHRtG.js → doctor-DwMXljiq.js} +11 -11
- package/dist/doctor-DwMXljiq.js.map +1 -0
- package/dist/{ensure-collection-policies-DUpWJb5D.js → ensure-collection-policies-DJsOMpaI.js} +23 -13
- package/dist/ensure-collection-policies-DJsOMpaI.js.map +1 -0
- package/dist/{ensure-collection-tables-BK--gDo6.js → ensure-collection-tables-h1mUO7Uu.js} +18 -10
- package/dist/ensure-collection-tables-h1mUO7Uu.js.map +1 -0
- package/dist/{ensure-tables-Bx1DYarz.js → ensure-tables-CUqYgMvo.js} +4 -4
- package/dist/ensure-tables-CUqYgMvo.js.map +1 -0
- package/dist/{enum-labels-kept-DyFE4Gaq.js → enum-labels-kept-z7ustOl_.js} +3 -3
- package/dist/{enum-labels-kept-DyFE4Gaq.js.map → enum-labels-kept-z7ustOl_.js.map} +1 -1
- package/dist/{generate-drizzle-schema-taNTu3xv.js → generate-drizzle-schema-BIUqhSzh.js} +2 -2
- package/dist/{generate-drizzle-schema-taNTu3xv.js.map → generate-drizzle-schema-BIUqhSzh.js.map} +1 -1
- package/dist/{generate-drizzle-schema-logic-DpFA1UVd.js → generate-drizzle-schema-logic-BBXjxm5z.js} +3 -3
- package/dist/{generate-drizzle-schema-logic-DpFA1UVd.js.map → generate-drizzle-schema-logic-BBXjxm5z.js.map} +1 -1
- package/dist/{generate-files-DbwU8Btx.js → generate-files-BkiRFoV1.js} +3 -3
- package/dist/{generate-files-DbwU8Btx.js.map → generate-files-BkiRFoV1.js.map} +1 -1
- package/dist/{generate-postgres-ddl-logic-ISVgsP34.js → generate-postgres-ddl-logic-G9PvjHrr.js} +10 -5
- package/dist/{generate-postgres-ddl-logic-ISVgsP34.js.map → generate-postgres-ddl-logic-G9PvjHrr.js.map} +1 -1
- package/dist/{generated-schema-diff-4lOC0g1_.js → generated-schema-diff-D2soazQU.js} +18 -7
- package/dist/generated-schema-diff-D2soazQU.js.map +1 -0
- package/dist/history/HistoryService.d.ts +2 -3
- package/dist/history/ensure-history-table.d.ts +8 -3
- package/dist/index.d.ts +2 -1
- package/dist/index.es.js +22 -14
- package/dist/index.es.js.map +1 -1
- package/dist/interfaces.d.ts +24 -2
- package/dist/{introspect-db-logic-DgQVzccW.js → introspect-db-logic-DWqAEURB.js} +7 -5
- package/dist/introspect-db-logic-DWqAEURB.js.map +1 -0
- package/dist/{migrate-baseline-BeepEphh.js → migrate-baseline-CveNQli3.js} +166 -460
- package/dist/migrate-baseline-CveNQli3.js.map +1 -0
- package/dist/{migrate-policy-stamps-cYMJ_9Ha.js → migrate-policy-stamps-C4qbQ0jK.js} +42 -13
- package/dist/migrate-policy-stamps-C4qbQ0jK.js.map +1 -0
- package/dist/{open-rebase-CTNBmmH8.js → open-rebase-Cvid3iwR.js} +1813 -689
- package/dist/open-rebase-Cvid3iwR.js.map +1 -0
- package/dist/open-rebase.d.ts +7 -2
- package/dist/{pg-error-utils-BLOVXK67.js → pg-error-utils-80uTIHQs.js} +2 -2
- package/dist/{pg-error-utils-BLOVXK67.js.map → pg-error-utils-80uTIHQs.js.map} +1 -1
- package/dist/pipelined-transaction.d.ts +24 -37
- package/dist/{plan-schema-CMNWvqIa.js → plan-schema-CMhNYEYF.js} +109 -14
- package/dist/plan-schema-CMhNYEYF.js.map +1 -0
- package/dist/{policy-drift-IjD2AUD-.js → policy-drift-Bx6663KD.js} +29 -6
- package/dist/policy-drift-Bx6663KD.js.map +1 -0
- package/dist/push-index-adoption-C-TpM445.js +22 -0
- package/dist/push-index-adoption-C-TpM445.js.map +1 -0
- package/dist/{push-index-prebuild-CIvajIrL.js → push-index-prebuild-xbpgZEJK.js} +4 -4
- package/dist/{push-index-prebuild-CIvajIrL.js.map → push-index-prebuild-xbpgZEJK.js.map} +1 -1
- package/dist/{push-kept-defaults-CLbsuSmo.js → push-kept-defaults-D-JEv0Gn.js} +2 -2
- package/dist/{push-kept-defaults-CLbsuSmo.js.map → push-kept-defaults-D-JEv0Gn.js.map} +1 -1
- package/dist/{push-legacy-renames-DeW_HTLJ.js → push-legacy-renames-7UKspspl.js} +4 -4
- package/dist/{push-legacy-renames-DeW_HTLJ.js.map → push-legacy-renames-7UKspspl.js.map} +1 -1
- package/dist/{push-policy-plan--oRXOj4X.js → push-policy-plan-BOw5PtU8.js} +14 -8
- package/dist/push-policy-plan-BOw5PtU8.js.map +1 -0
- package/dist/read-routing.d.ts +10 -30
- package/dist/{render-ddl-CzVcbbR1.js → render-ddl-Di-fTGti.js} +40 -10
- package/dist/render-ddl-Di-fTGti.js.map +1 -0
- package/dist/request-role-cli-Bxxskru9.js +260 -0
- package/dist/request-role-cli-Bxxskru9.js.map +1 -0
- package/dist/request-role-cli.d.ts +86 -0
- package/dist/request-role-migration-BCW4v0rq.js +150 -0
- package/dist/request-role-migration-BCW4v0rq.js.map +1 -0
- package/dist/rls-bootstrap-sql-CxszeSc7.js +534 -0
- package/dist/rls-bootstrap-sql-CxszeSc7.js.map +1 -0
- package/dist/rls-enforcement-CyUCPDH8.js +1195 -0
- package/dist/rls-enforcement-CyUCPDH8.js.map +1 -0
- package/dist/rls-settings-BCGMIig3.js +231 -0
- package/dist/rls-settings-BCGMIig3.js.map +1 -0
- package/dist/schema/doctor-cli.js +145 -7
- package/dist/schema/doctor-cli.js.map +1 -1
- package/dist/schema/doctor-database.d.ts +27 -0
- package/dist/schema/drizzle-ddl.d.ts +2 -2
- package/dist/schema/ensure-collection-policies.d.ts +15 -3
- package/dist/schema/generate-drizzle-schema.js +1 -1
- package/dist/schema/generate-postgres-ddl-logic.d.ts +5 -1
- package/dist/schema/generate-postgres-ddl.js +1 -1
- package/dist/schema/index-adoption.d.ts +99 -0
- package/dist/schema/introspect-db-logic.d.ts +2 -1
- package/dist/schema/introspect-db.js +3 -3
- package/dist/schema/migrate-policy-stamps.d.ts +19 -1
- package/dist/schema/plan/diff-plan.d.ts +14 -2
- package/dist/schema/plan/render-ddl.d.ts +17 -3
- package/dist/schema/plan/tenant-keep-trigger.d.ts +9 -0
- package/dist/schema/plan/types.d.ts +17 -0
- package/dist/schema/plan/updated-at-trigger.d.ts +1 -1
- package/dist/schema/plan/view-names.d.ts +6 -0
- package/dist/schema/push-index-adoption.d.ts +10 -0
- package/dist/schema/push-policy-plan.d.ts +5 -1
- package/dist/schema/rls-bootstrap-sql.d.ts +34 -0
- package/dist/schema/search-column.d.ts +2 -2
- package/dist/search-column-BM-GV6vH.js.map +1 -1
- package/dist/security/caller-sql-columns.d.ts +60 -0
- package/dist/security/caller-sql.d.ts +8 -0
- package/dist/security/database-preflight.d.ts +24 -0
- package/dist/security/policy-drift.d.ts +19 -1
- package/dist/security/request-role-migration.d.ts +36 -0
- package/dist/security/rls-enforcement.d.ts +277 -21
- package/dist/security/rls-settings.d.ts +210 -0
- package/dist/services/FetchService.d.ts +33 -8
- package/dist/services/PersistService.d.ts +75 -4
- package/dist/services/RelationService.d.ts +1 -1
- package/dist/services/channel-bus/PostgresChannelBus.d.ts +2 -2
- package/dist/services/channel-bus/index.d.ts +2 -2
- package/dist/services/channel-history.d.ts +2 -2
- package/dist/services/channel-presence.d.ts +3 -3
- package/dist/services/dataService.d.ts +8 -2
- package/dist/services/realtimeService.d.ts +21 -5
- package/dist/services/socket-liveness.d.ts +6 -1
- package/dist/services/view-access.d.ts +2 -2
- package/dist/types.d.ts +1 -1
- package/dist/utils/drizzle-conditions.d.ts +12 -0
- package/dist/utils/positional-params.d.ts +40 -0
- package/dist/view-names-Dscp6--B.js +24 -0
- package/dist/view-names-Dscp6--B.js.map +1 -0
- package/dist/{websocket-D2y88dOa.js → websocket-BSYV_KSw.js} +3 -3
- package/dist/websocket-BSYV_KSw.js.map +1 -0
- package/dist/websocket.d.ts +14 -3
- package/package.json +7 -7
- package/dist/deferred-index-build-F2GWu009.js.map +0 -1
- package/dist/doctor-_g2tHRtG.js.map +0 -1
- package/dist/ensure-collection-policies-DUpWJb5D.js.map +0 -1
- package/dist/ensure-collection-tables-BK--gDo6.js.map +0 -1
- package/dist/ensure-tables-Bx1DYarz.js.map +0 -1
- package/dist/generated-schema-diff-4lOC0g1_.js.map +0 -1
- package/dist/introspect-db-logic-DgQVzccW.js.map +0 -1
- package/dist/migrate-baseline-BeepEphh.js.map +0 -1
- package/dist/migrate-policy-stamps-cYMJ_9Ha.js.map +0 -1
- package/dist/open-rebase-CTNBmmH8.js.map +0 -1
- package/dist/plan-schema-CMNWvqIa.js.map +0 -1
- package/dist/policy-drift-IjD2AUD-.js.map +0 -1
- package/dist/push-policy-plan--oRXOj4X.js.map +0 -1
- package/dist/render-ddl-CzVcbbR1.js.map +0 -1
- package/dist/rls-enforcement-jZTv1yci.js +0 -534
- package/dist/rls-enforcement-jZTv1yci.js.map +0 -1
- package/dist/websocket-D2y88dOa.js.map +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { SQL } from "drizzle-orm";
|
|
2
2
|
import { SecurityRule } from "@rebasepro/types";
|
|
3
3
|
import { REBASE_USER_ROLE } from "@rebasepro/common";
|
|
4
|
+
import { type GrantsMode, type ProvisionRoleMode, type RequestIdentity, type RlsSettings } from "./rls-settings.js";
|
|
4
5
|
/**
|
|
5
6
|
* Unified RLS enforcement — the "user context vs server context" model.
|
|
6
7
|
*
|
|
@@ -117,10 +118,14 @@ export interface AuthContext {
|
|
|
117
118
|
claims?: Record<string, unknown>;
|
|
118
119
|
}
|
|
119
120
|
/**
|
|
120
|
-
* Take every privilege the
|
|
121
|
-
* gets when RLS cannot be switched on for it, so it is unreachable
|
|
122
|
-
* unprotected. Quoted exactly: a mixed-case adopted table
|
|
123
|
-
* lower case names a table that does not exist.
|
|
121
|
+
* Take every privilege the request roles hold on one table back — what a
|
|
122
|
+
* table gets when RLS cannot be switched on for it, so it is unreachable
|
|
123
|
+
* rather than unprotected. Quoted exactly: a mixed-case adopted table
|
|
124
|
+
* (`"User"`) folded to lower case names a table that does not exist.
|
|
125
|
+
*
|
|
126
|
+
* Every request role, as this database recorded them (`rls.requestRole`), and
|
|
127
|
+
* each only if it exists: one statement, for the boot's policies step and its
|
|
128
|
+
* final posture check alike, without either being handed the roles.
|
|
124
129
|
*/
|
|
125
130
|
export declare const revokeUserRoleSql: (schema: string, table: string) => string;
|
|
126
131
|
/**
|
|
@@ -182,11 +187,117 @@ export declare function warnOnRoleSchemaCollision(run: RawSqlRunner): Promise<vo
|
|
|
182
187
|
* under-reporting costs the entire authorization model.
|
|
183
188
|
*/
|
|
184
189
|
export declare function detectConnectionPosture(run: RawSqlRunner, schemas?: string[]): Promise<ConnectionPosture>;
|
|
190
|
+
/**
|
|
191
|
+
* Raised when `rebase_user` cannot be provisioned, adopted or switched to —
|
|
192
|
+
* with the SQL that fixes it in {@link instructions}.
|
|
193
|
+
*
|
|
194
|
+
* Its own class so a caller that prints the instructions itself (the CLI) can
|
|
195
|
+
* tell they are already in the message.
|
|
196
|
+
*/
|
|
197
|
+
export declare class AppRoleSetupError extends Error {
|
|
198
|
+
readonly instructions: string;
|
|
199
|
+
constructor(problem: string, instructions: string);
|
|
200
|
+
}
|
|
185
201
|
/**
|
|
186
202
|
* Human-actionable instructions for when the connection cannot provision the
|
|
187
|
-
* user role itself (no CREATEROLE and role not pre-created by the platform)
|
|
203
|
+
* user role itself (no CREATEROLE and role not pre-created by the platform),
|
|
204
|
+
* or holds it without the right to switch to it.
|
|
205
|
+
*
|
|
206
|
+
* Every statement is safe to re-run and the same text works on Postgres 15 and
|
|
207
|
+
* 16+, so it can be pasted whole whatever state the database is in.
|
|
208
|
+
* `connectionRole` is the role the server connects as; without one, a
|
|
209
|
+
* placeholder Postgres will refuse until it is replaced.
|
|
210
|
+
*/
|
|
211
|
+
export declare function appRoleSetupInstructions(connectionRole: string | undefined, schemas: string[], role?: string, mode?: GrantsMode): string;
|
|
212
|
+
/**
|
|
213
|
+
* Refuse a `rebase_user` that would not restrict anything.
|
|
214
|
+
*
|
|
215
|
+
* The role is cluster-wide and found by name, so the one a boot adopts may
|
|
216
|
+
* have been made by anybody — an earlier tool, a hand-written script, another
|
|
217
|
+
* application on the same cluster. Every authenticated request runs as it, so
|
|
218
|
+
* an attribute or a membership that lifts RLS lifts it from all of them, and
|
|
219
|
+
* nothing would say so: requests succeed, with every row.
|
|
220
|
+
*
|
|
221
|
+
* - LOGIN: anyone holding its password connects as the request role directly.
|
|
222
|
+
* - SUPERUSER, BYPASSRLS: RLS does not apply to it at all.
|
|
223
|
+
* - A membership in a role that owns tables here, or is itself a login,
|
|
224
|
+
* superuser or BYPASSRLS role: a member inheriting an owner is the owner for
|
|
225
|
+
* every policy check, and one that may `SET ROLE` to a privileged role is a
|
|
226
|
+
* statement away from it. (`GRANT app TO rebase_user` for `GRANT rebase_user
|
|
227
|
+
* TO app` is the easy way to get here.)
|
|
228
|
+
*
|
|
229
|
+
* A plain NOLOGIN group role is left alone: `validatePolicyPgRoles` sends a
|
|
230
|
+
* project with `pgRoles` to exactly that grant.
|
|
231
|
+
*/
|
|
232
|
+
export declare function assertAppRoleRestricted(run: RawSqlRunner, role?: string): Promise<void>;
|
|
233
|
+
/**
|
|
234
|
+
* Run {@link roleSwitchProbe}, and on a refusal throw an
|
|
235
|
+
* {@link AppRoleSetupError} carrying the SQL that grants the switch.
|
|
236
|
+
*
|
|
237
|
+
* For a boot that adopts the role without provisioning it as much as for one
|
|
238
|
+
* that just did: either way the next thing that happens is a request running
|
|
239
|
+
* as it.
|
|
240
|
+
*/
|
|
241
|
+
export declare function proveRoleSwitch(run: RawSqlRunner, schemas: string[], connectionRole?: string, role?: string): Promise<void>;
|
|
242
|
+
/** The `rebase.schema_meta` key that records which roles requests run as. */
|
|
243
|
+
export declare const REQUEST_ROLES_KEY = "request_roles";
|
|
244
|
+
/**
|
|
245
|
+
* The request roles this database recorded on an earlier provisioning boot, or
|
|
246
|
+
* `undefined` when none has — every database provisioned before the setting
|
|
247
|
+
* existed, which ran as `rebase_user`.
|
|
248
|
+
*
|
|
249
|
+
* Recorded in the database rather than only configured because a role belongs
|
|
250
|
+
* to the cluster and holds grants here: a later boot configured with another
|
|
251
|
+
* name would provision a second role and leave the first one holding every
|
|
252
|
+
* grant. Reading the record is how that boot notices.
|
|
253
|
+
*/
|
|
254
|
+
export declare function readRecordedRequestRoles(run: RawSqlRunner): Promise<string[] | undefined>;
|
|
255
|
+
/**
|
|
256
|
+
* Record `roles` as this database's request roles, creating `rebase.schema_meta`
|
|
257
|
+
* if nothing has yet. The table is one of Rebase's internal tables, so it is
|
|
258
|
+
* taken back from the request roles here, as its every creator does.
|
|
259
|
+
*/
|
|
260
|
+
export declare function recordRequestRoles(run: RawSqlRunner, roles: readonly string[]): Promise<void>;
|
|
261
|
+
/**
|
|
262
|
+
* Refuse a boot configured with request roles other than the ones this
|
|
263
|
+
* database recorded. The same set in any order is the same.
|
|
264
|
+
*
|
|
265
|
+
* Nothing recorded means `rebase_user`, as every database from before the
|
|
266
|
+
* setting ran — but only where `rebase_user` actually holds grants here. A
|
|
267
|
+
* database it has never been granted on (a fresh one) has nothing to strand,
|
|
268
|
+
* and a new role is simply recorded.
|
|
188
269
|
*/
|
|
189
|
-
export declare function
|
|
270
|
+
export declare function assertRecordedRequestRoles(configured: readonly string[], recorded: readonly string[] | undefined, defaultRoleHoldsGrants: boolean): void;
|
|
271
|
+
/** Whether `role` holds any table privilege in `schemas` — something a new role would strand. */
|
|
272
|
+
export declare function roleHoldsTableGrants(run: RawSqlRunner, schemas: readonly string[], role?: string): Promise<boolean>;
|
|
273
|
+
/**
|
|
274
|
+
* Login roles other than this connection that can switch into `role`, and so
|
|
275
|
+
* can pass this database's policies as whichever user they like: the settings the
|
|
276
|
+
* policies read can be set by any session.
|
|
277
|
+
*
|
|
278
|
+
* Roles belong to the cluster, so this is how two projects on one cluster
|
|
279
|
+
* sharing `rebase_user` shows up from inside either database. Superusers are
|
|
280
|
+
* left out — they can become any role, and nothing here changes that.
|
|
281
|
+
*/
|
|
282
|
+
export declare function findOtherRoleMembers(run: RawSqlRunner, role: string): Promise<string[]>;
|
|
283
|
+
/** How {@link ensureAppRole} provisions: which role, and what it may create and grant. */
|
|
284
|
+
export interface EnsureAppRoleOptions {
|
|
285
|
+
/** The request role. Default `rebase_user`. */
|
|
286
|
+
role?: string;
|
|
287
|
+
/** `"never"`: check the role and the membership, create neither. */
|
|
288
|
+
provisionRole?: ProvisionRoleMode;
|
|
289
|
+
/** What the grant tier grants — see {@link ensureAppRoleGrants}. */
|
|
290
|
+
grants?: GrantsMode;
|
|
291
|
+
/** The tables Rebase serves, for `grants: "managed"`. */
|
|
292
|
+
managedTables?: readonly ManagedTable[];
|
|
293
|
+
/** Roles granted to the request role `WITH INHERIT TRUE` (PostgreSQL 16+). */
|
|
294
|
+
inheritRoles?: readonly string[];
|
|
295
|
+
}
|
|
296
|
+
/** A table Rebase serves, by schema and name. */
|
|
297
|
+
export interface ManagedTable {
|
|
298
|
+
schema: string;
|
|
299
|
+
table: string;
|
|
300
|
+
}
|
|
190
301
|
/**
|
|
191
302
|
* Idempotently provision the `rebase_user` role, membership for the current
|
|
192
303
|
* connection role, and DML grants (+ default privileges for future tables)
|
|
@@ -200,10 +311,13 @@ export declare function appRoleSetupInstructions(connectionRole: string, schemas
|
|
|
200
311
|
* RLS still filters every row: these grants only make the tables *reachable*
|
|
201
312
|
* by the role; the policies decide which rows/commands actually pass.
|
|
202
313
|
*
|
|
203
|
-
* Throws with precise setup instructions when the
|
|
204
|
-
* connection cannot create it
|
|
314
|
+
* Throws {@link AppRoleSetupError}, with precise setup instructions, when the
|
|
315
|
+
* role is missing and the connection cannot create it, when an existing role
|
|
316
|
+
* would not restrict anything, and when the connection cannot switch to it.
|
|
317
|
+
* Boot fails then, rather than serving an API whose every signed-in request is
|
|
318
|
+
* refused — or, worse, allowed.
|
|
205
319
|
*/
|
|
206
|
-
export declare function ensureAppRole(run: RawSqlRunner, schemas: string[]): Promise<void>;
|
|
320
|
+
export declare function ensureAppRole(run: RawSqlRunner, schemas: string[], options?: EnsureAppRoleOptions): Promise<void>;
|
|
207
321
|
/**
|
|
208
322
|
* The framework's internal tables in `schemas` that the request role can
|
|
209
323
|
* reach — `schema.table`, sorted — when nothing is left to take it back off.
|
|
@@ -221,7 +335,135 @@ export declare function ensureAppRole(run: RawSqlRunner, schemas: string[]): Pro
|
|
|
221
335
|
* gets. A table with RLS on is somebody's collection by that same rule as the
|
|
222
336
|
* sweep's, and is left out.
|
|
223
337
|
*/
|
|
224
|
-
export declare function findExposedInternalTables(run: RawSqlRunner, schemas: string[]): Promise<string[]>;
|
|
338
|
+
export declare function findExposedInternalTables(run: RawSqlRunner, schemas: string[], role?: string): Promise<string[]>;
|
|
339
|
+
/**
|
|
340
|
+
* Tables collections are served from that the user role holds no privilege on
|
|
341
|
+
* and this connection cannot grant it, because another role owns them.
|
|
342
|
+
*
|
|
343
|
+
* The grant tier passes these over — it could only fail on them — so on a
|
|
344
|
+
* database another application shares, a collection over that application's
|
|
345
|
+
* table boots and then refuses every request with "permission denied". This
|
|
346
|
+
* names them, with the owner who can run the grant. Refusing rather than
|
|
347
|
+
* widening, so the boot warns instead of stopping: the rest of the project
|
|
348
|
+
* works, and an introspected project serves every table in the schema,
|
|
349
|
+
* whoever owns it.
|
|
350
|
+
*/
|
|
351
|
+
export declare function findUngrantableTables(run: RawSqlRunner, tables: readonly ManagedTable[], role?: string): Promise<{
|
|
352
|
+
name: string;
|
|
353
|
+
owner: string;
|
|
354
|
+
grant: string;
|
|
355
|
+
}[]>;
|
|
356
|
+
/** What {@link ensureAppRoleGrants} grants, and to whom. */
|
|
357
|
+
export interface AppRoleGrantOptions {
|
|
358
|
+
/** The request role. Default `rebase_user`. */
|
|
359
|
+
role?: string;
|
|
360
|
+
/**
|
|
361
|
+
* `"schema"` (default): every table and sequence in the schemas that this
|
|
362
|
+
* connection may grant on, and default privileges for later ones.
|
|
363
|
+
* `"managed"`: only {@link managedTables} and the sequences they own, no
|
|
364
|
+
* default privileges. `"none"`: nothing, not even the revoke sweep.
|
|
365
|
+
*/
|
|
366
|
+
mode?: GrantsMode;
|
|
367
|
+
managedTables?: readonly ManagedTable[];
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Grant each of `inheritRoles` to the request role `WITH INHERIT TRUE`, so a
|
|
371
|
+
* policy written `TO` one of them — Supabase's `authenticated`, say — applies
|
|
372
|
+
* to requests.
|
|
373
|
+
*
|
|
374
|
+
* PostgreSQL 16 and newer only. From 16 inheritance is an option of each
|
|
375
|
+
* grant, so the request role takes on these roles' privileges and stays
|
|
376
|
+
* NOINHERIT towards every other membership. Before 16 the only lever is the
|
|
377
|
+
* role's own INHERIT attribute, which would hand it every role it belongs to;
|
|
378
|
+
* that is refused rather than done.
|
|
379
|
+
*/
|
|
380
|
+
export declare function ensureInheritedRoles(run: RawSqlRunner, role: string, inheritRoles: readonly string[]): Promise<void>;
|
|
381
|
+
/**
|
|
382
|
+
* Provision the role admin requests run as for one request role:
|
|
383
|
+
* `<role>_admin`, inheriting `role` — its grants, and every policy written
|
|
384
|
+
* for it — and {@link REBASE_ADMIN_ROLE}, with the connection able to switch
|
|
385
|
+
* to it. The role itself goes through {@link ensureAppRole} (created NOLOGIN
|
|
386
|
+
* NOSUPERUSER NOBYPASSRLS, checked as restricted when found, the switch
|
|
387
|
+
* proved), with no grants of its own: everything it can reach, it reaches as
|
|
388
|
+
* `role` does.
|
|
389
|
+
*
|
|
390
|
+
* Inheriting is per grant from PostgreSQL 16 (`WITH INHERIT TRUE`); before
|
|
391
|
+
* that it is the role's own `INHERIT` attribute, harmless here because this
|
|
392
|
+
* role belongs to exactly the two roles granted below.
|
|
393
|
+
*/
|
|
394
|
+
export declare function ensureAdminRequestRole(run: RawSqlRunner, schemas: string[], options: {
|
|
395
|
+
role: string;
|
|
396
|
+
adminRole: string;
|
|
397
|
+
provisionRole?: ProvisionRoleMode;
|
|
398
|
+
}): Promise<void>;
|
|
399
|
+
/**
|
|
400
|
+
* Let the connection role itself pass the server-or-admin policies, by
|
|
401
|
+
* inheriting {@link REBASE_ADMIN_ROLE} — which is what `TO public` gave it.
|
|
402
|
+
*
|
|
403
|
+
* It matters where the connection is subject to RLS: always, when it owns
|
|
404
|
+
* none of the tables (requests then run as the connection itself, so the
|
|
405
|
+
* server context and admins depend on it), and on any table it does not own
|
|
406
|
+
* otherwise. Best effort: a connection that cannot be granted the role is
|
|
407
|
+
* told what to run, and its server-context and admin reads of those tables are
|
|
408
|
+
* refused — closed, never wider — until someone does.
|
|
409
|
+
*/
|
|
410
|
+
export declare function ensureConnectionInheritsAdminRole(run: RawSqlRunner, options: {
|
|
411
|
+
provision: boolean;
|
|
412
|
+
}): Promise<void>;
|
|
413
|
+
/**
|
|
414
|
+
* The request identity a boot hands the driver and the realtime refetch, with
|
|
415
|
+
* the admin switch on when every admin request role is ready: provisioned
|
|
416
|
+
* here, or — on a boot that provisions nothing — found existing, inheriting
|
|
417
|
+
* what it should and switchable. Otherwise admin requests run as the request
|
|
418
|
+
* role, as before, and the boot says why: under `adminPolicies: "adminRole"`,
|
|
419
|
+
* policies already written `TO rebase_admin` then refuse them.
|
|
420
|
+
*/
|
|
421
|
+
export declare function settleAdminRequestRoles(run: RawSqlRunner, schemas: string[], settings: RlsSettings, options: {
|
|
422
|
+
provision: boolean;
|
|
423
|
+
}): Promise<RequestIdentity>;
|
|
424
|
+
/**
|
|
425
|
+
* Whether policies written `TO rebase_admin` reach every caller they are for on
|
|
426
|
+
* this connection, read from the catalogue without changing anything: the role
|
|
427
|
+
* exists, the connection passes it (exempt from RLS, or inheriting it — the
|
|
428
|
+
* server context on tables it does not own), and each of `adminRoles` exists,
|
|
429
|
+
* inherits its request role and `rebase_admin`, and the connection can switch
|
|
430
|
+
* into it. The state {@link settleAdminRequestRoles} and
|
|
431
|
+
* {@link ensureConnectionInheritsAdminRole} leave when both succeed.
|
|
432
|
+
*
|
|
433
|
+
* With `existingOnly`, an admin role that does not exist is passed over, but at
|
|
434
|
+
* least one must be ready: for a role list read back from the database, which
|
|
435
|
+
* does not say which role is an anonymous one with no admin counterpart.
|
|
436
|
+
*
|
|
437
|
+
* Anything it cannot read answers `false`, and `false` means `TO public`: the
|
|
438
|
+
* shape every caller passes, as before the admin role existed.
|
|
439
|
+
*/
|
|
440
|
+
export declare function adminSwitchLive(run: RawSqlRunner, adminRoles: ReadonlyMap<string, string>, options?: {
|
|
441
|
+
existingOnly?: boolean;
|
|
442
|
+
}): Promise<boolean>;
|
|
443
|
+
/**
|
|
444
|
+
* The admin switch for a boot whose requests switch roles: settle the admin
|
|
445
|
+
* request roles and the connection's `rebase_admin`, then say whether the
|
|
446
|
+
* switch is live — which is what decides where the policies step writes the
|
|
447
|
+
* server-or-admin policies. `TO rebase_admin` only when it is: written there
|
|
448
|
+
* while admin requests still run as the request role, they refuse every admin
|
|
449
|
+
* caller, `dataAsAdmin` included, as an empty table. Otherwise `TO public`, the
|
|
450
|
+
* documented fallback, which the request role passes.
|
|
451
|
+
*/
|
|
452
|
+
export declare function settleAdminSwitch(run: RawSqlRunner, schemas: string[], settings: RlsSettings, options: {
|
|
453
|
+
provision: boolean;
|
|
454
|
+
}): Promise<{
|
|
455
|
+
request: RequestIdentity;
|
|
456
|
+
adminPoliciesLive: boolean;
|
|
457
|
+
}>;
|
|
458
|
+
/**
|
|
459
|
+
* Where `db push` and `rebase doctor` expect the server-or-admin policies, for
|
|
460
|
+
* the request roles this database records (or the configured ones): `TO
|
|
461
|
+
* rebase_admin` only when {@link adminSwitchLive} says a server on this
|
|
462
|
+
* connection would have the switch live, `TO public` otherwise — the same
|
|
463
|
+
* answer a boot reaches, without provisioning anything. Settings that do not
|
|
464
|
+
* resolve answer `false`.
|
|
465
|
+
*/
|
|
466
|
+
export declare function adminPoliciesLiveForCli(run: RawSqlRunner): Promise<boolean>;
|
|
225
467
|
/**
|
|
226
468
|
* The grant tier of {@link ensureAppRole} on its own: `USAGE` on each existing
|
|
227
469
|
* schema in `schemas`, DML on its tables and `USAGE, SELECT` on its
|
|
@@ -235,20 +477,27 @@ export declare function findExposedInternalTables(run: RawSqlRunner, schemas: st
|
|
|
235
477
|
* table's `pg_class` row whether or not the ACL changes, which invalidates
|
|
236
478
|
* every table's cached plans on every connection, and the internal-table sweep
|
|
237
479
|
* ran 23 `REVOKE`s per schema. A boot against a database whose grants already
|
|
238
|
-
* hold now reads one catalogue query and writes nothing
|
|
239
|
-
*
|
|
480
|
+
* hold now reads one catalogue query and writes nothing.
|
|
481
|
+
*
|
|
482
|
+
* Tables and sequences are granted by name, the ones that are missing it and
|
|
483
|
+
* that this connection may grant on — not `ON ALL … IN SCHEMA`, which also
|
|
484
|
+
* tries every relation another role owns. On a schema shared with another
|
|
485
|
+
* application that statement fails for a table this connection holds no
|
|
486
|
+
* privilege on, or only warns for one it holds without grant option, and either
|
|
487
|
+
* way the schema never reads as granted. The ACLs this leaves are the ones the
|
|
488
|
+
* schema-wide statement left on everything it could reach.
|
|
240
489
|
*
|
|
241
490
|
* Table owners (the expected non-superuser posture) can always grant on their
|
|
242
491
|
* own objects, so this needs no extra privilege.
|
|
243
492
|
*/
|
|
244
|
-
export declare function ensureAppRoleGrants(run: RawSqlRunner, schemas: string[]): Promise<void>;
|
|
493
|
+
export declare function ensureAppRoleGrants(run: RawSqlRunner, schemas: string[], options?: AppRoleGrantOptions): Promise<void>;
|
|
245
494
|
/**
|
|
246
495
|
* Rebase's own RLS-off tables in `schema` that a `revokeInternalTableAccess`
|
|
247
496
|
* sweep would change — the user role holds a privilege on them, or their ACL
|
|
248
497
|
* has never been written. One read; the sweep it narrows is one `DO` per
|
|
249
498
|
* table name.
|
|
250
499
|
*/
|
|
251
|
-
export declare function internalTablesGrantedToUserRole(run: RawSqlRunner, schema: string): Promise<string[]>;
|
|
500
|
+
export declare function internalTablesGrantedToUserRole(run: RawSqlRunner, schema: string, role?: string): Promise<string[]>;
|
|
252
501
|
/**
|
|
253
502
|
* Apply the authenticated context to a transaction: the `app.*` GUCs that RLS
|
|
254
503
|
* policies read via `rebase.uid()` / `rebase.roles()` / `rebase.jwt()`, and — when
|
|
@@ -271,7 +520,7 @@ export declare function internalTablesGrantedToUserRole(run: RawSqlRunner, schem
|
|
|
271
520
|
* That sentinel is exported from `@rebasepro/types` because it leaks into rule
|
|
272
521
|
* semantics: it is why `rebase.uid() IS NOT NULL` is true for anonymous requests.
|
|
273
522
|
*/
|
|
274
|
-
export declare function applyAuthContext(tx: SqlTx, auth: AuthContext, userRole?: string): Promise<void>;
|
|
523
|
+
export declare function applyAuthContext(tx: SqlTx, auth: AuthContext, userRole?: string | RequestIdentity): Promise<void>;
|
|
275
524
|
/**
|
|
276
525
|
* {@link applyAuthContext} as one statement and its values, for a transaction
|
|
277
526
|
* that sends it in the same batch as its first query
|
|
@@ -279,7 +528,7 @@ export declare function applyAuthContext(tx: SqlTx, auth: AuthContext, userRole?
|
|
|
279
528
|
* — what a pooled connection could not carry at connect — set ahead of the
|
|
280
529
|
* caller's, in the same statement.
|
|
281
530
|
*/
|
|
282
|
-
export declare function authContextStatement(auth: AuthContext, userRole?: string, before?: readonly (readonly [string, string])[]): {
|
|
531
|
+
export declare function authContextStatement(auth: AuthContext, userRole?: string | RequestIdentity, before?: readonly (readonly [string, string])[]): {
|
|
283
532
|
text: string;
|
|
284
533
|
values: string[];
|
|
285
534
|
};
|
|
@@ -330,8 +579,14 @@ export declare function warnOnLegacyRlsFunctions(collections: {
|
|
|
330
579
|
* no data, so the mistake survives review and ships.
|
|
331
580
|
*
|
|
332
581
|
* Requests run as `rebase_user`, so a policy is only reachable if it targets
|
|
333
|
-
* `public` or a role `rebase_user`
|
|
334
|
-
* error worth failing the boot for.
|
|
582
|
+
* `public` or a role whose privileges `rebase_user` has. Anything else is a
|
|
583
|
+
* configuration error worth failing the boot for.
|
|
584
|
+
*
|
|
585
|
+
* Privileges, not membership: Postgres applies a policy `TO r` to a role that
|
|
586
|
+
* has `r`'s privileges, which a member only gets by inheriting them, and
|
|
587
|
+
* `rebase_user` is `NOINHERIT`. This used to ask `pg_has_role(…, 'MEMBER')`, so
|
|
588
|
+
* `GRANT authenticated TO rebase_user` — the grant its own hint suggested —
|
|
589
|
+
* passed here and still read every collection as empty.
|
|
335
590
|
*/
|
|
336
591
|
export declare function validatePolicyPgRoles(run: RawSqlRunner, collections: {
|
|
337
592
|
slug?: string;
|
|
@@ -340,6 +595,7 @@ export declare function validatePolicyPgRoles(run: RawSqlRunner, collections: {
|
|
|
340
595
|
pgRoles?: readonly string[];
|
|
341
596
|
}[];
|
|
342
597
|
}[],
|
|
343
|
-
/** The role requests actually run as: `rebase_user`
|
|
344
|
-
* privileged enough to switch, otherwise the
|
|
345
|
-
|
|
598
|
+
/** The role(s) requests actually run as: `rebase_user` (or the `rls.requestRole`
|
|
599
|
+
* roles) when the connection is privileged enough to switch, otherwise the
|
|
600
|
+
* connection role itself. With several, a role is usable when any reaches it. */
|
|
601
|
+
requestRoles?: string | readonly string[]): Promise<void>;
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import type { AuthContext } from "./rls-enforcement.js";
|
|
2
|
+
/**
|
|
3
|
+
* The escape hatches for the request role — every choice Rebase makes about
|
|
4
|
+
* the roles, grants, policy targets and session settings that row-level
|
|
5
|
+
* security runs on, as a setting.
|
|
6
|
+
*
|
|
7
|
+
* Each one defaults to what Rebase did before it existed, so a project that
|
|
8
|
+
* sets nothing gets exactly that. The point is the other values: a database
|
|
9
|
+
* someone already runs has its own roles, its own grants and sometimes its own
|
|
10
|
+
* policies, and Rebase should fit them rather than demand a database of its
|
|
11
|
+
* own. See docs/design/rebase-user-on-existing-databases.md.
|
|
12
|
+
*
|
|
13
|
+
* Set in code on the driver (`rls`), and — for the ones an operator may need to
|
|
14
|
+
* change per deployment — from the environment, which wins, as it does for the
|
|
15
|
+
* realtime settings. Function-valued settings are code only.
|
|
16
|
+
*/
|
|
17
|
+
export interface PostgresRlsConfig {
|
|
18
|
+
/**
|
|
19
|
+
* The role signed-in and anonymous requests run as. Default `"rebase_user"`.
|
|
20
|
+
*
|
|
21
|
+
* - A name: every request runs as it. Give each project on a shared cluster
|
|
22
|
+
* its own, because a role belongs to the whole cluster and any login that
|
|
23
|
+
* can switch into it passes this database's policies.
|
|
24
|
+
* - `{ anonymous, signedIn }`: a caller with no session runs as one, anyone
|
|
25
|
+
* signed in (guests and `rebase.dataAsAdmin` included) as the other —
|
|
26
|
+
* the `anon` / `authenticated` split of Supabase and PostgREST.
|
|
27
|
+
* - A function of the request's identity, for anything else. Declare every
|
|
28
|
+
* name it can return in {@link requestRoles}; a name outside that list
|
|
29
|
+
* fails the request.
|
|
30
|
+
*
|
|
31
|
+
* Env: `REBASE_RLS_REQUEST_ROLE` (a name).
|
|
32
|
+
*/
|
|
33
|
+
requestRole?: string | RequestRoleMap | ((auth: AuthContext) => string);
|
|
34
|
+
/**
|
|
35
|
+
* Every role a function-valued {@link requestRole} can return, so they can
|
|
36
|
+
* be provisioned, granted, recorded and checked. Derived for the other forms.
|
|
37
|
+
*/
|
|
38
|
+
requestRoles?: readonly string[];
|
|
39
|
+
/**
|
|
40
|
+
* Whether Rebase creates the request role and the membership that lets the
|
|
41
|
+
* connection switch into it. `"auto"` (default) does; `"never"` leaves both
|
|
42
|
+
* to the operator — roles managed in Terraform, a DBA who creates every role
|
|
43
|
+
* by hand — and only checks them, failing the boot with the SQL to run.
|
|
44
|
+
*
|
|
45
|
+
* Env: `REBASE_RLS_PROVISION_ROLE`.
|
|
46
|
+
*/
|
|
47
|
+
provisionRole?: ProvisionRoleMode;
|
|
48
|
+
/**
|
|
49
|
+
* What Rebase grants the request role.
|
|
50
|
+
*
|
|
51
|
+
* - `"schema"` (default): every table and sequence in the schemas it serves
|
|
52
|
+
* that the connection may grant on, plus default privileges so tables
|
|
53
|
+
* created later are covered.
|
|
54
|
+
* - `"managed"`: only the tables Rebase serves — collections, junctions, its
|
|
55
|
+
* own row-secured tables — and their sequences; no default privileges.
|
|
56
|
+
* For a schema shared with tables Rebase should not reach.
|
|
57
|
+
* - `"none"`: nothing. Grants are yours; a table you have not granted is
|
|
58
|
+
* refused, never widened.
|
|
59
|
+
*
|
|
60
|
+
* Env: `REBASE_RLS_GRANTS`.
|
|
61
|
+
*/
|
|
62
|
+
grants?: GrantsMode;
|
|
63
|
+
/**
|
|
64
|
+
* Whom permissive policies apply to. `"public"` (default) is every role
|
|
65
|
+
* with a grant on the table. `"requestRole"` narrows them to the request
|
|
66
|
+
* role(s), so another application's role reading the same table gets
|
|
67
|
+
* nothing from Rebase's policies. Restrictive policies always apply to
|
|
68
|
+
* everyone: they can only take access away.
|
|
69
|
+
*
|
|
70
|
+
* Env: `REBASE_RLS_POLICY_TARGET`.
|
|
71
|
+
*/
|
|
72
|
+
policyTarget?: PolicyTargetMode;
|
|
73
|
+
/**
|
|
74
|
+
* Whom the policies that only the server context or an admin can pass apply
|
|
75
|
+
* to — the injected `<table>_default_admin_*` baseline, and any permissive
|
|
76
|
+
* rule whose every clause needs one of the two, such as `roles: ["admin"]`.
|
|
77
|
+
*
|
|
78
|
+
* - `"adminRole"` (default): `TO rebase_admin`, a role that holds nothing
|
|
79
|
+
* and cannot log in. Admin requests run as `<request role>_admin`, which
|
|
80
|
+
* inherits the request role and `rebase_admin`; a signed-in user's plan
|
|
81
|
+
* never sees those policies, so an owner-scoped list or count uses the
|
|
82
|
+
* owner index instead of scanning the table.
|
|
83
|
+
* - `"public"`: `TO public`, every request as the request role — what
|
|
84
|
+
* Rebase did before. For a database where `rebase_admin` cannot be
|
|
85
|
+
* created, or a connection that is subject to RLS and cannot be granted it.
|
|
86
|
+
*
|
|
87
|
+
* Env: `REBASE_RLS_ADMIN_POLICIES`.
|
|
88
|
+
*/
|
|
89
|
+
adminPolicies?: AdminPoliciesMode;
|
|
90
|
+
/**
|
|
91
|
+
* `false` drops the injected server-or-admin baseline from every
|
|
92
|
+
* collection, as `disableDefaultPolicies` does for one. Default `true`.
|
|
93
|
+
* A collection's own `disableDefaultPolicies` still decides for it.
|
|
94
|
+
*/
|
|
95
|
+
defaultPolicies?: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Roles whose policies should apply to requests: each is granted to the
|
|
98
|
+
* request role(s) `WITH INHERIT TRUE`, so a policy written `TO
|
|
99
|
+
* authenticated` applies. PostgreSQL 16 and newer only — before 16 the only
|
|
100
|
+
* way is to make the request role inherit from every role it belongs to,
|
|
101
|
+
* which this refuses to do.
|
|
102
|
+
*/
|
|
103
|
+
inheritRoles?: readonly string[];
|
|
104
|
+
/**
|
|
105
|
+
* The session settings a request carries its identity in, besides the
|
|
106
|
+
* `app.*` settings Rebase's own policies read (always set).
|
|
107
|
+
*
|
|
108
|
+
* - `"rebase"` (default): nothing more.
|
|
109
|
+
* - `"supabase"`: also `request.jwt.claims`, `request.jwt.claim.sub` and
|
|
110
|
+
* `request.jwt.claim.role`, so policies written with Supabase's
|
|
111
|
+
* `auth.uid()` / `auth.jwt()` see the caller.
|
|
112
|
+
* - A function returning `[name, value]` pairs, for any other convention.
|
|
113
|
+
*
|
|
114
|
+
* Env: `REBASE_RLS_IDENTITY` (a named form).
|
|
115
|
+
*/
|
|
116
|
+
identity?: IdentityMode | ((auth: AuthContext, role: string) => readonly (readonly [string, string])[]);
|
|
117
|
+
/**
|
|
118
|
+
* What to do when a login other than this connection can switch into the
|
|
119
|
+
* request role — which lets it pass this database's policies as anyone.
|
|
120
|
+
* `"warn"` (default), `"refuse"` to fail the boot, `"ignore"` for a role
|
|
121
|
+
* shared on purpose.
|
|
122
|
+
*
|
|
123
|
+
* Env: `REBASE_RLS_SHARED_ROLE`.
|
|
124
|
+
*/
|
|
125
|
+
sharedRole?: SharedRoleMode;
|
|
126
|
+
}
|
|
127
|
+
export interface RequestRoleMap {
|
|
128
|
+
/** The role a caller with no session runs as. */
|
|
129
|
+
anonymous: string;
|
|
130
|
+
/** The role every signed-in caller runs as, guests included. */
|
|
131
|
+
signedIn: string;
|
|
132
|
+
}
|
|
133
|
+
export type ProvisionRoleMode = "auto" | "never";
|
|
134
|
+
export type GrantsMode = "schema" | "managed" | "none";
|
|
135
|
+
export type PolicyTargetMode = "public" | "requestRole";
|
|
136
|
+
export type AdminPoliciesMode = "adminRole" | "public";
|
|
137
|
+
export type IdentityMode = "rebase" | "supabase";
|
|
138
|
+
export type SharedRoleMode = "warn" | "refuse" | "ignore";
|
|
139
|
+
/**
|
|
140
|
+
* How one request runs: the role to switch to, and any session settings beyond
|
|
141
|
+
* `app.*`. What the driver and the realtime refetch hand to `applyAuthContext`.
|
|
142
|
+
*/
|
|
143
|
+
export interface RequestIdentity {
|
|
144
|
+
roleFor(auth: AuthContext): string;
|
|
145
|
+
settingsFor(auth: AuthContext, role: string): readonly (readonly [string, string])[];
|
|
146
|
+
/**
|
|
147
|
+
* The role a request holding the `admin` role runs as instead of `role`
|
|
148
|
+
* (`<role>_admin`, under `adminPolicies: "adminRole"`), or `undefined` to
|
|
149
|
+
* run it as `role`. Set by the boot once those roles are known to exist and
|
|
150
|
+
* be switchable — see `settleAdminRequestRoles`. The identity settings are
|
|
151
|
+
* still `role`'s: the switch changes which policies apply, not who asks.
|
|
152
|
+
*/
|
|
153
|
+
adminRoleFor?(role: string): string | undefined;
|
|
154
|
+
}
|
|
155
|
+
/** {@link PostgresRlsConfig}, resolved: every setting decided, every name checked. */
|
|
156
|
+
export interface RlsSettings {
|
|
157
|
+
/** Every role a request can run as, the default first. */
|
|
158
|
+
roles: readonly string[];
|
|
159
|
+
/** The role a request runs as when the setting is a single name — the first of {@link roles} otherwise. */
|
|
160
|
+
defaultRole: string;
|
|
161
|
+
request: RequestIdentity;
|
|
162
|
+
provisionRole: ProvisionRoleMode;
|
|
163
|
+
grants: GrantsMode;
|
|
164
|
+
policyTarget: PolicyTargetMode;
|
|
165
|
+
adminPolicies: AdminPoliciesMode;
|
|
166
|
+
/**
|
|
167
|
+
* Under `adminPolicies: "adminRole"`, each request role an admin request can
|
|
168
|
+
* arrive as, mapped to the role it switches to instead — every role but the
|
|
169
|
+
* anonymous one of a `{ anonymous, signedIn }` pair. Empty under `"public"`.
|
|
170
|
+
*/
|
|
171
|
+
adminRoles: ReadonlyMap<string, string>;
|
|
172
|
+
defaultPolicies: boolean;
|
|
173
|
+
inheritRoles: readonly string[];
|
|
174
|
+
/**
|
|
175
|
+
* The request roles {@link inheritRoles} are granted to: the one role, or
|
|
176
|
+
* only the signed-in one of a `{ anonymous, signedIn }` pair — granting
|
|
177
|
+
* `authenticated` to `anon` would hand every caller with no session the
|
|
178
|
+
* signed-in policies.
|
|
179
|
+
*/
|
|
180
|
+
inheritTo: readonly string[];
|
|
181
|
+
sharedRole: SharedRoleMode;
|
|
182
|
+
/** Every setting that is not at its default, as `name=value`, for the boot line. */
|
|
183
|
+
nonDefaults: readonly string[];
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* The driver's `rls` block with the environment applied over it — what
|
|
187
|
+
* `postgresDriverConfigFromEnv` does for every other setting. A variable that
|
|
188
|
+
* is set wins over the code; the rest of the block is kept as written.
|
|
189
|
+
*/
|
|
190
|
+
export declare function rlsConfigFromEnv(config: PostgresRlsConfig | undefined, read: (name: string) => string | undefined): PostgresRlsConfig | undefined;
|
|
191
|
+
/**
|
|
192
|
+
* Decide every setting from the driver's `rls` block (environment already
|
|
193
|
+
* applied — {@link rlsConfigFromEnv}). Throws on a value it cannot use: a
|
|
194
|
+
* mistyped mode or an unusable role name fails the boot, not the first request.
|
|
195
|
+
*/
|
|
196
|
+
export declare function resolveRlsSettings(config: PostgresRlsConfig | undefined): RlsSettings;
|
|
197
|
+
/**
|
|
198
|
+
* What each non-default setting hands to the operator, said once at boot. A
|
|
199
|
+
* hatch that takes a safety job away from Rebase says which job it took.
|
|
200
|
+
*/
|
|
201
|
+
export declare function rlsHandoverNotes(settings: RlsSettings): string[];
|
|
202
|
+
/**
|
|
203
|
+
* The request settings for a command that runs outside the server — `db push`,
|
|
204
|
+
* `db migrate`, a restore — which sees the database declaration and the
|
|
205
|
+
* environment but not the backend's code. The roles are the environment's or
|
|
206
|
+
* the declaration's when either names them; otherwise the ones a server boot
|
|
207
|
+
* recorded in this database (which covers a role configured in code);
|
|
208
|
+
* otherwise the default.
|
|
209
|
+
*/
|
|
210
|
+
export declare function cliRlsSettings(recordedRoles: readonly string[] | undefined, env?: NodeJS.ProcessEnv): RlsSettings;
|
|
@@ -33,6 +33,22 @@ import { type ReadCallContextProvider } from "./read-scope.js";
|
|
|
33
33
|
export declare function numericAggregateAliases(aggregates: DriverAggregate[], properties: Record<string, {
|
|
34
34
|
type?: string;
|
|
35
35
|
}>): Set<string>;
|
|
36
|
+
/**
|
|
37
|
+
* What `fingerprint()` computes over a table's row versions, from their tuple
|
|
38
|
+
* headers alone: no column is read, decoded or turned into text.
|
|
39
|
+
*
|
|
40
|
+
* Every row version has the xmin of the transaction that wrote it: an insert
|
|
41
|
+
* or an update adds a new one, a delete takes one away. The count and the xmin
|
|
42
|
+
* sum alone are linear, and cancel: a transaction that deletes one row,
|
|
43
|
+
* updates another and inserts a third keeps both whenever the two xmins it
|
|
44
|
+
* removed add up to twice its own, which two concurrent sessions reach. So
|
|
45
|
+
* each version also adds a hash of its xmin, which no such arithmetic
|
|
46
|
+
* cancels: a write only ever adds versions under its own new xid, so the
|
|
47
|
+
* multiset of xmins after it can never equal the one before. The xmin alone,
|
|
48
|
+
* and not the ctid beside it, because a materialized view's collection is a
|
|
49
|
+
* view over its stored rows that passes their `xmin` through and no `ctid`.
|
|
50
|
+
*/
|
|
51
|
+
export declare function rowVersionsFingerprint(xmin: SQL): SQL;
|
|
36
52
|
export declare class FetchService {
|
|
37
53
|
private db;
|
|
38
54
|
private registry;
|
|
@@ -113,6 +129,12 @@ export declare class FetchService {
|
|
|
113
129
|
* Delegates to DrizzleConditionBuilder.buildFilterConditions
|
|
114
130
|
*/
|
|
115
131
|
buildFilterConditions<M extends Record<string, unknown>>(filter: FilterValues<Extract<keyof M, string>>, table: PgTable<any>, collectionPath: string): SQL[];
|
|
132
|
+
/**
|
|
133
|
+
* A filter and a logical group as conditions, compiled as a read compiles
|
|
134
|
+
* them: for a write whose rows the filter chooses (`updateWhere`,
|
|
135
|
+
* `deleteWhere`).
|
|
136
|
+
*/
|
|
137
|
+
buildWriteConditions(filter: FilterValues<string> | undefined, logical: LogicalCondition | undefined, table: PgTable<any>, collectionPath: string): SQL[];
|
|
116
138
|
/**
|
|
117
139
|
* Resolves the correct Drizzle column for sorting.
|
|
118
140
|
* Automatically maps owning relation property keys to their underlying foreign key column.
|
|
@@ -132,7 +154,7 @@ export declare class FetchService {
|
|
|
132
154
|
* `_score` is only meaningful for a collection that declared a `search`
|
|
133
155
|
* block *and* for a request that carried a search string — ranking rows
|
|
134
156
|
* against no query ranks them all at zero. Outside those two conditions it
|
|
135
|
-
* is an unknown field and gets the same 400 as
|
|
157
|
+
* is an unknown field and gets the same 400 as every other typo, which is the
|
|
136
158
|
* behaviour that matters: a sort that is silently dropped returns 200 with
|
|
137
159
|
* rows in arbitrary order, and paging over that repeats and skips rows.
|
|
138
160
|
*/
|
|
@@ -691,13 +713,16 @@ export declare class FetchService {
|
|
|
691
713
|
* `toRestRow`. For a row the statement itself wrote, with nothing written
|
|
692
714
|
* after it, `RETURNING` those same columns is that row — the same values
|
|
693
715
|
* through the same column decoders, in the same order, rendered the same
|
|
694
|
-
* way.
|
|
695
|
-
*
|
|
696
|
-
*
|
|
697
|
-
*
|
|
698
|
-
*
|
|
699
|
-
*
|
|
700
|
-
*
|
|
716
|
+
* way. One thing narrows the read and not the statement, and keeps the
|
|
717
|
+
* read: a `beforeQuery` adds conditions to it (and runs application code),
|
|
718
|
+
* so a row outside them is no answer today, and would be one from
|
|
719
|
+
* `RETURNING`.
|
|
720
|
+
*
|
|
721
|
+
* Per-field read access does not: the read selects the same columns and
|
|
722
|
+
* withholds what the caller may not read when it renders the row
|
|
723
|
+
* (`toRestRow` → `stripUnreadable`), which is the render here too — so an
|
|
724
|
+
* `excludeFromApi` column is written, and not answered, without a read
|
|
725
|
+
* back per row.
|
|
701
726
|
*
|
|
702
727
|
* Row-level security is the same either way. `INSERT … RETURNING` holds
|
|
703
728
|
* the new row to the table's SELECT policies and refuses one they hide
|