@pgxsinkit/contracts 0.2.7 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/apply-strategy.d.ts +13 -1
- package/dist/apply-strategy.d.ts.map +1 -1
- package/dist/config.d.ts +256 -87
- package/dist/config.d.ts.map +1 -1
- package/dist/convergence-model.d.ts +2 -2
- package/dist/convergence-model.d.ts.map +1 -1
- package/dist/event-stream.d.ts +1 -1
- package/dist/fingerprint.d.ts +31 -11
- package/dist/fingerprint.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +480 -202
- package/dist/index.js.map +16 -14
- package/dist/predicate.d.ts +162 -0
- package/dist/predicate.d.ts.map +1 -0
- package/dist/projection.d.ts +2 -2
- package/dist/projection.d.ts.map +1 -1
- package/dist/registry-diff.d.ts.map +1 -1
- package/dist/registry-invariant.d.ts +26 -14
- package/dist/registry-invariant.d.ts.map +1 -1
- package/dist/registry.d.ts +74 -48
- package/dist/registry.d.ts.map +1 -1
- package/dist/sql-role-guard.d.ts +1 -1
- package/dist/stream-envelope.d.ts +84 -0
- package/dist/stream-envelope.d.ts.map +1 -0
- package/dist/supabase-rls.d.ts +32 -36
- package/dist/supabase-rls.d.ts.map +1 -1
- package/package.json +3 -2
- package/skills/registry-authoring/SKILL.md +185 -185
- package/skills/registry-authoring/references/storage-engine-and-build.md +29 -0
- package/src/apply-strategy.ts +23 -4
- package/src/config.ts +340 -116
- package/src/convergence-model.ts +2 -2
- package/src/event-stream.ts +1 -1
- package/src/fingerprint.ts +76 -14
- package/src/index.ts +2 -0
- package/src/predicate.ts +274 -0
- package/src/projection.ts +6 -2
- package/src/registry-diff.ts +15 -4
- package/src/registry-invariant.ts +34 -19
- package/src/registry.ts +324 -78
- package/src/sql-role-guard.ts +1 -1
- package/src/stream-envelope.ts +91 -0
- package/src/supabase-rls.ts +76 -74
package/src/sql-role-guard.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* The single role-existence guard used by every pgxsinkit-emitted GRANT/REVOKE.
|
|
3
3
|
*
|
|
4
4
|
* pgxsinkit artifacts are portable: the same migration runs on a Supabase-shaped cluster (where
|
|
5
|
-
* `anon`/`authenticated`/`service_role` exist), on a plain Postgres, and on a
|
|
5
|
+
* `anon`/`authenticated`/`service_role` exist), on a plain Postgres, and on a pgwasm test lane
|
|
6
6
|
* (where none of them do). A bare `REVOKE … FROM anon` is a hard error where the role is absent, so
|
|
7
7
|
* every role-scoped grant/revoke is wrapped in a `DO $$ … IF EXISTS (SELECT 1 FROM pg_roles …)`
|
|
8
8
|
* block and simply does nothing there.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The change envelope carried on every Circuits table and shape stream.
|
|
3
|
+
*
|
|
4
|
+
* Restated rather than imported from `@electric-circuits/protocol`, for the reasons given on
|
|
5
|
+
* {@link Predicate}: this is the wire contract pgxsinkit commits to, the protocol package is alpha,
|
|
6
|
+
* and a published library should not put an alpha peer in its public types. The engine's serde
|
|
7
|
+
* definitions are authoritative; any divergence is a bug here.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** A scalar cell value on the wire. */
|
|
11
|
+
export type StreamValue = string | number | boolean | null;
|
|
12
|
+
|
|
13
|
+
/** A row is a flat map of column name to value. */
|
|
14
|
+
export type StreamRow = Record<string, StreamValue>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The engine's **complete** shape-stream vocabulary — two verbs, not four.
|
|
18
|
+
*
|
|
19
|
+
* `insert`/`update` are the *input* side: what the replication ingestor parses out of Postgres. They
|
|
20
|
+
* never reach a shape stream. Every row the engine emits goes out as `upsert` (`output.rs`
|
|
21
|
+
* `translate_output`/`agg_envelope`) and every eviction as key-only `delete`
|
|
22
|
+
* (`translate_output`/`delete_envelopes`) — the engine states the row's new value rather than
|
|
23
|
+
* claiming anything about what preceded it, because a shape row can enter a subscriber's view for
|
|
24
|
+
* reasons that are not a Postgres INSERT (a subquery flip, a scope grant, a backfill replay).
|
|
25
|
+
*
|
|
26
|
+
* Listing the input verbs here as if they might arrive is what produced a dead `"update"` branch in
|
|
27
|
+
* the translator and a comment asserting `upsert` was backfill-only. Both were wrong.
|
|
28
|
+
*/
|
|
29
|
+
export type StreamOperation = "upsert" | "delete";
|
|
30
|
+
|
|
31
|
+
export interface StreamEnvelope {
|
|
32
|
+
/** The table name — the collection discriminator. */
|
|
33
|
+
type: string;
|
|
34
|
+
/** The stringified primary key. Composite keys are already joined by the engine. */
|
|
35
|
+
key: string;
|
|
36
|
+
/** Present for insert/update/upsert; **absent for delete**, which is key-only. */
|
|
37
|
+
value?: StreamRow;
|
|
38
|
+
headers: {
|
|
39
|
+
operation: StreamOperation;
|
|
40
|
+
txid?: string;
|
|
41
|
+
/** Stamped by the durable-streams server on read; never sent by a producer. */
|
|
42
|
+
offset?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Postgres commit LSN (`"HI/LO"` hex), stamped by the engine on **live** shape envelopes.
|
|
45
|
+
*
|
|
46
|
+
* **Absent on backfill rows**, and absent in library (no-Postgres) mode. That absence is why
|
|
47
|
+
* ADR-0056 floors catch-up on the stream OFFSET rather than on this: a frontier that only some
|
|
48
|
+
* envelopes can advance is not a frontier.
|
|
49
|
+
*/
|
|
50
|
+
lsn?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Position of the change within its transaction, stamped by the replication ingestor on
|
|
53
|
+
* **table**-stream envelopes so the engine's own tailer can skip at-least-once duplicates.
|
|
54
|
+
* **Not present on shape streams**, which is what we read — recorded so nobody builds
|
|
55
|
+
* deduplication on a field that will never arrive.
|
|
56
|
+
*/
|
|
57
|
+
seq?: number;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A delete envelope carries no row body, so an eviction discloses nothing but the key. */
|
|
62
|
+
export function isDeleteEnvelope(envelope: StreamEnvelope): boolean {
|
|
63
|
+
return envelope.headers.operation === "delete";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* One change as the APPLIER consumes it, after an envelope has been resolved against its table.
|
|
68
|
+
*
|
|
69
|
+
* Distinct from {@link StreamEnvelope} on purpose. An envelope is the wire form — key-only on a
|
|
70
|
+
* delete, headers carrying transport metadata. This is what the apply path needs instead: a value
|
|
71
|
+
* that is always present (a delete's is its reconstructed primary key), and the transport metadata
|
|
72
|
+
* dropped.
|
|
73
|
+
*
|
|
74
|
+
* The operation set is {@link StreamOperation}'s, unchanged. The applier used to narrow the wire's
|
|
75
|
+
* four verbs down to three DML verbs; there are only two verbs to carry now, and inventing a DML
|
|
76
|
+
* distinction the engine never made is what the translator got wrong.
|
|
77
|
+
*
|
|
78
|
+
* `value` is `Record<string, unknown>` rather than {@link StreamRow} because the applier handles
|
|
79
|
+
* values the wire never carries — a `bigint` id kept as a string for precision, a JSON column
|
|
80
|
+
* arriving as a parsed object — and narrowing here would only push casts into the codecs.
|
|
81
|
+
*/
|
|
82
|
+
export type SyncRow = Record<string, unknown>;
|
|
83
|
+
|
|
84
|
+
export type SyncOperation = StreamOperation;
|
|
85
|
+
|
|
86
|
+
export interface SyncChange<T extends SyncRow = SyncRow> {
|
|
87
|
+
/** The stringified primary key, as the stream named it. */
|
|
88
|
+
key: string;
|
|
89
|
+
value: T;
|
|
90
|
+
headers: { operation: SyncOperation } & Record<string, unknown>;
|
|
91
|
+
}
|
package/src/supabase-rls.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { and, eq, getTableName, or, sql, type AnyColumn, type SQL } from "drizzle-orm";
|
|
2
2
|
import { PgDialect, pgPolicy, type AnyPgTable, type PgRole } from "drizzle-orm/pg-core";
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { buildOwnershipShapePredicate, DENY_ALL_PREDICATE, type JwtClaims } from "./config";
|
|
5
|
+
import { p, type Predicate } from "./predicate";
|
|
5
6
|
|
|
6
7
|
// Render a typed fragment to inline-literal SQL text (CREATE POLICY DDL cannot carry `$n` binds).
|
|
7
8
|
// The tier-② discipline for the text builders below: values enter as typed `${value}` interpolations
|
|
@@ -12,8 +13,8 @@ function renderInlineSql(fragment: SQL): string {
|
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
// Walk a dotted claim path defensively (unverified claims may be any shape): anything that is not a
|
|
15
|
-
// plain object on the way down yields `undefined` rather than throwing. Shared by every
|
|
16
|
-
//
|
|
16
|
+
// plain object on the way down yields `undefined` rather than throwing. Shared by every read-path
|
|
17
|
+
// predicate mirror below — each family's mirror sits beside its own policies and reads exactly the
|
|
17
18
|
// claim the policy's SQL reads.
|
|
18
19
|
function readClaimsPath(claims: JwtClaims | null, path: string[]): unknown {
|
|
19
20
|
let current: unknown = claims;
|
|
@@ -63,7 +64,7 @@ export type SupabaseOwnerOrAdminNativePoliciesOptions = {
|
|
|
63
64
|
subjectCastType?: string;
|
|
64
65
|
/**
|
|
65
66
|
* Path to the roles array in the claims. Defaults to {@link adminRolesClaimPath}; pass the SAME value to
|
|
66
|
-
* {@link resolveOwnerOrAdminAccess} / {@link
|
|
67
|
+
* {@link resolveOwnerOrAdminAccess} / {@link buildOwnerOrAdminShapePredicate} so both surfaces read one claim.
|
|
67
68
|
*/
|
|
68
69
|
adminRolesClaimPath?: readonly string[];
|
|
69
70
|
};
|
|
@@ -120,8 +121,9 @@ function buildOwnerOrAdminPolicyName(tableName: string, command: SupabaseOwnerOr
|
|
|
120
121
|
|
|
121
122
|
// ---------------------------------------------------------------------------
|
|
122
123
|
// Shared predicate leaves (used by both policy families). RLS is the *write* path
|
|
123
|
-
// (Postgres), so
|
|
124
|
-
// which
|
|
124
|
+
// (Postgres), so these render to SQL text with columns qualified — unlike the read-path
|
|
125
|
+
// mirrors, which carry the column objects themselves in a predicate AST and render no SQL
|
|
126
|
+
// at all. The only bits that stay raw `sql` are genuinely-Postgres
|
|
125
127
|
// expressions with no Drizzle operator: the JWT-subject `(select current_setting(...)::type)`
|
|
126
128
|
// and the admin-roles `EXISTS`. Literal values use `eq(col, value).inlineParams()` so the
|
|
127
129
|
// value is inlined into the DDL — a bare `$n` is something `CREATE POLICY` cannot carry.
|
|
@@ -274,13 +276,13 @@ export const supabaseOwnerOrAdminDefaults = {
|
|
|
274
276
|
} as const;
|
|
275
277
|
|
|
276
278
|
// ---------------------------------------------------------------------------
|
|
277
|
-
//
|
|
278
|
-
// to reach the same verdict in JS, from the same claims: the policy's `OR` admin branch becomes "no
|
|
279
|
-
// filter at all" (every row streams), and its owner branch becomes the ownership shape
|
|
279
|
+
// Read-path mirror of the owner-or-admin policies. The read path never runs RLS, so the control plane
|
|
280
|
+
// has to reach the same verdict in JS, from the same claims: the policy's `OR` admin branch becomes "no
|
|
281
|
+
// filter at all" (every row streams), and its owner branch becomes the ownership shape predicate. One
|
|
280
282
|
// declaration — the owner column plus the admin role name — two enforcement surfaces, so a row can
|
|
281
283
|
// never be writable-but-unreadable (or the reverse).
|
|
282
284
|
//
|
|
283
|
-
// SELECT only, by construction: a shape
|
|
285
|
+
// SELECT only, by construction: a shape predicate filters a read stream, so it mirrors the `select`
|
|
284
286
|
// policy. INSERT/UPDATE/DELETE remain the policies' business.
|
|
285
287
|
// ---------------------------------------------------------------------------
|
|
286
288
|
|
|
@@ -326,20 +328,17 @@ export function resolveOwnerOrAdminAccess(
|
|
|
326
328
|
}
|
|
327
329
|
|
|
328
330
|
/**
|
|
329
|
-
* The
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
* {@link buildOwnershipShapeWhere}, i.e. their own rows, or the {@link DENY_ALL} sentinel when there is
|
|
333
|
-
* no subject. Returning the sentinel *by reference* is what lets a `customWhere` built on this probe as
|
|
334
|
-
* claims-dependent (`isClaimsDependentRowFilter`).
|
|
331
|
+
* The owner-or-admin **predicate** — the native-path counterpart to
|
|
332
|
+
* the owner-or-admin `select` policy, from the same declaration. An admin gets `null` (no filter,
|
|
333
|
+
* every row — the policy's bypass branch); anyone else gets {@link buildOwnershipShapePredicate}.
|
|
335
334
|
*/
|
|
336
|
-
export function
|
|
335
|
+
export function buildOwnerOrAdminShapePredicate(
|
|
337
336
|
ownerColumn: AnyColumn,
|
|
338
337
|
claims: JwtClaims | null,
|
|
339
338
|
options: OwnerOrAdminAccessOptions = {},
|
|
340
|
-
):
|
|
339
|
+
): Predicate | null {
|
|
341
340
|
const access = resolveOwnerOrAdminAccess(claims, options);
|
|
342
|
-
return access.admin ? null :
|
|
341
|
+
return access.admin ? null : buildOwnershipShapePredicate(ownerColumn, access.subject);
|
|
343
342
|
}
|
|
344
343
|
|
|
345
344
|
// ---------------------------------------------------------------------------
|
|
@@ -360,11 +359,12 @@ export function buildOwnerOrAdminShapeWhere(
|
|
|
360
359
|
// leaves stay `sql`: the `IN (subquery)` containment (Drizzle's `inArray` cannot wrap a raw subquery)
|
|
361
360
|
// and the `(select current_setting(...)::type)` JWT-subject expression (wrapped in a scalar subquery —
|
|
362
361
|
// the Supabase per-statement-eval RLS perf idiom). Columns serialize qualified
|
|
363
|
-
// (`"work_items"."workspace_id"`) — fine for Postgres RLS
|
|
362
|
+
// (`"work_items"."workspace_id"`) — fine for Postgres RLS, which is the write path; the read mirror
|
|
363
|
+
// builds a predicate AST from the same column objects and serializes no SQL at all.
|
|
364
364
|
//
|
|
365
|
-
// The `select` half of this family is mirrored on the
|
|
366
|
-
// {@link
|
|
367
|
-
// RLS, so the two surfaces are generated from one declaration and must agree.
|
|
365
|
+
// The `select` half of this family is mirrored on the read path by
|
|
366
|
+
// {@link buildMembershipShapePredicate}, built from the same column declarations — the sync engine cannot
|
|
367
|
+
// read RLS, so the two surfaces are generated from one declaration and must agree.
|
|
368
368
|
// ---------------------------------------------------------------------------
|
|
369
369
|
|
|
370
370
|
type MembershipPolicyKind = "select" | "insert" | "update" | "delete";
|
|
@@ -519,26 +519,26 @@ export function buildSupabaseMembershipNativePolicies(options: SupabaseMembershi
|
|
|
519
519
|
}
|
|
520
520
|
|
|
521
521
|
// ---------------------------------------------------------------------------
|
|
522
|
-
//
|
|
523
|
-
// re-derive the same visible set in the shape
|
|
522
|
+
// Read-path mirror of the membership policies. The read path never runs RLS, so the control plane must
|
|
523
|
+
// re-derive the same visible set in the shape predicate, from the SAME Drizzle columns the policies were
|
|
524
524
|
// built from — one declaration, two enforcement surfaces, so a row can never be readable-but-unwritable
|
|
525
525
|
// (or the reverse) through a rename or a typo.
|
|
526
526
|
//
|
|
527
527
|
// SELECT only, by construction: the mirror reproduces the family's `select` predicate — plain
|
|
528
528
|
// membership of the container. The owner/manager branches and the write-state gate are WRITE-side
|
|
529
|
-
// concerns (INSERT/UPDATE/DELETE) and are deliberately absent here; a shape
|
|
529
|
+
// concerns (INSERT/UPDATE/DELETE) and are deliberately absent here; a shape predicate filters a read
|
|
530
530
|
// stream and has nothing to gate.
|
|
531
531
|
//
|
|
532
|
-
// The two surfaces
|
|
532
|
+
// The two surfaces express the SAME containment differently, on purpose:
|
|
533
533
|
// - RLS (`membershipMatch`) renders `= ANY(ARRAY(select …))` — the InitPlan/bitmap-index-scan
|
|
534
534
|
// discipline Postgres needs for a runtime-resolved set (see that function's comment).
|
|
535
|
-
// -
|
|
536
|
-
//
|
|
537
|
-
// Same columns, two
|
|
535
|
+
// - The mirror emits `p.in(col, p.subquery(…))` — an AST node the engine plans itself. There is no
|
|
536
|
+
// Postgres planner on this side to appease, and no SQL text for anything to lex.
|
|
537
|
+
// Same columns, two forms — that is exactly what the mirror is for.
|
|
538
538
|
// ---------------------------------------------------------------------------
|
|
539
539
|
|
|
540
540
|
/**
|
|
541
|
-
* The columns {@link
|
|
541
|
+
* The columns {@link buildMembershipShapePredicate} needs: the SELECT-relevant subset of
|
|
542
542
|
* {@link SupabaseMembershipPredicateColumns}. The write-only fields (owner, manager role, subject cast,
|
|
543
543
|
* write gate, policy role) are accepted but ignored, so the **same declaration object** you hand to
|
|
544
544
|
* {@link buildSupabaseMembershipNativePolicies} can be handed to the read mirror verbatim.
|
|
@@ -550,24 +550,28 @@ export type SupabaseMembershipShapeColumns = Pick<
|
|
|
550
550
|
Partial<SupabaseMembershipNativePoliciesOptions>;
|
|
551
551
|
|
|
552
552
|
/**
|
|
553
|
-
* The
|
|
554
|
-
*
|
|
555
|
-
* is a member of. Columns are referenced **bare** via {@link c} (Electric's grammar requires plain
|
|
556
|
-
* column refs) yet still through the real Drizzle column objects, so the reference is rename-safe; the
|
|
557
|
-
* subject rides as a typed interpolation and becomes a bound param once `buildRowFilterShape`
|
|
558
|
-
* serializes it (never a hand-escaped literal). The subquery is **self-contained** (uncorrelated) — it
|
|
559
|
-
* gets its own `FROM`, so the membership table's bare column names resolve to it.
|
|
553
|
+
* The membership **predicate** — the read-path mirror of the membership `select` policy, from the
|
|
554
|
+
* same column declarations.
|
|
560
555
|
*
|
|
561
|
-
*
|
|
562
|
-
*
|
|
556
|
+
* The containment becomes a first-class {@link InSubqueryPredicate} rather than SQL text, which buys
|
|
557
|
+
* something the text form could not: the engine maintains the inner set incrementally and **shares it
|
|
558
|
+
* across every shape referencing the same subquery**, so an entitlement-shaped membership costs one
|
|
559
|
+
* index rather than one per shape. The subquery is uncorrelated, as the engine requires — the inner
|
|
560
|
+
* `where` reads the membership table's own columns only.
|
|
563
561
|
*/
|
|
564
|
-
export function
|
|
562
|
+
export function buildMembershipShapePredicate(
|
|
563
|
+
columns: SupabaseMembershipShapeColumns,
|
|
564
|
+
claims: JwtClaims | null,
|
|
565
|
+
): Predicate {
|
|
565
566
|
const subject = readSubjectClaim(claims);
|
|
566
567
|
if (subject === null) {
|
|
567
|
-
return
|
|
568
|
+
return DENY_ALL_PREDICATE;
|
|
568
569
|
}
|
|
569
570
|
|
|
570
|
-
return
|
|
571
|
+
return p.in(
|
|
572
|
+
columns.containerColumn,
|
|
573
|
+
p.subquery(columns.membershipContainerColumn, p.eq(columns.membershipSubjectColumn, subject)),
|
|
574
|
+
);
|
|
571
575
|
}
|
|
572
576
|
|
|
573
577
|
// ---------------------------------------------------------------------------
|
|
@@ -589,10 +593,10 @@ export function buildMembershipShapeWhere(columns: SupabaseMembershipShapeColumn
|
|
|
589
593
|
// regression-guard the InitPlan discipline (see the `rls-read-load` perf track);
|
|
590
594
|
// never ship it.
|
|
591
595
|
//
|
|
592
|
-
// The same grant set is mirrored on the
|
|
593
|
-
//
|
|
594
|
-
// resolves the ids from the claims in JS and
|
|
595
|
-
// cannot read RLS, so the two surfaces are generated from one declaration and must agree.
|
|
596
|
+
// The same grant set is mirrored on the read path by {@link resolveGrantScopeAccess} (or
|
|
597
|
+
// {@link resolveGrantScopeIds} for the id set alone) + {@link buildGrantScopeShapePredicate}: the control
|
|
598
|
+
// plane resolves the ids from the claims in JS and compiles them into the shape's predicate — the sync
|
|
599
|
+
// engine cannot read RLS, so the two surfaces are generated from one declaration and must agree.
|
|
596
600
|
// ---------------------------------------------------------------------------
|
|
597
601
|
|
|
598
602
|
type GrantScopePolicyKind = "select" | "insert" | "update" | "delete";
|
|
@@ -607,7 +611,7 @@ const defaultBypassScopeKind = "platform";
|
|
|
607
611
|
* An unconditional bypass declaration: any grant whose `role` ∈ `roleValues` and whose `scope.kind` =
|
|
608
612
|
* `scopeKind` (default `"platform"`) confers access to every row — e.g. a platform-scoped
|
|
609
613
|
* `platform_admin`. Hand the SAME object to the policy builder and to
|
|
610
|
-
* {@link resolveGrantScopeAccess} / {@link
|
|
614
|
+
* {@link resolveGrantScopeAccess} / {@link buildGrantScopeAccessShapePredicate}.
|
|
611
615
|
*/
|
|
612
616
|
export type GrantScopeBypassOptions = {
|
|
613
617
|
roleValues: string[];
|
|
@@ -635,7 +639,7 @@ export type SupabaseGrantScopePredicateColumns = GrantScopeClaimOptions & {
|
|
|
635
639
|
* `bypass.roleValues` and whose `scope.kind` = `bypass.scopeKind` (default "platform") grants all
|
|
636
640
|
* rows — e.g. a platform-scoped `platform_admin`. The bypass is an **uncorrelated** EXISTS, so it
|
|
637
641
|
* stays InitPlan-hoisted in both the correct and naive forms. Its read-path twin is
|
|
638
|
-
* {@link
|
|
642
|
+
* {@link buildGrantScopeAccessShapePredicate} (bypass → `null`, i.e. no shape filter at all).
|
|
639
643
|
*/
|
|
640
644
|
bypass?: GrantScopeBypassOptions;
|
|
641
645
|
};
|
|
@@ -753,13 +757,13 @@ export function buildSupabaseGrantScopeNativePolicies(options: SupabaseGrantScop
|
|
|
753
757
|
}
|
|
754
758
|
|
|
755
759
|
// ---------------------------------------------------------------------------
|
|
756
|
-
//
|
|
757
|
-
// resolve the same visible scope-id set from the claims in JS and
|
|
758
|
-
//
|
|
760
|
+
// Read-path mirror of the grant-scope policy. The read path never runs RLS, so the control plane must
|
|
761
|
+
// resolve the same visible scope-id set from the claims in JS and emit it as a literal `in` predicate.
|
|
762
|
+
// These helpers are the read-path counterpart to the policy above — one declaration,
|
|
759
763
|
// two enforcement surfaces, derived from the same grant data so they cannot drift.
|
|
760
764
|
//
|
|
761
765
|
// The policy's OR bypass branch has a mirror too: {@link resolveGrantScopeAccess} reports it and
|
|
762
|
-
// {@link
|
|
766
|
+
// {@link buildGrantScopeAccessShapePredicate} turns it into `null` — no shape filter at all, every row
|
|
763
767
|
// streams — exactly as the `exists (…)` OR-branch makes the scope test irrelevant on the write side.
|
|
764
768
|
// Without it a bypass grant would be enforced on writes yet invisible on reads.
|
|
765
769
|
// ---------------------------------------------------------------------------
|
|
@@ -804,20 +808,20 @@ export function resolveGrantScopeIds(claims: JwtClaims | null, options: GrantSco
|
|
|
804
808
|
}
|
|
805
809
|
|
|
806
810
|
/**
|
|
807
|
-
* The
|
|
808
|
-
*
|
|
809
|
-
*
|
|
810
|
-
*
|
|
811
|
-
*
|
|
811
|
+
* The grant-scope **predicate** — the read-path mirror of the grant-scope `select` policy.
|
|
812
|
+
* An empty id set denies all rows ({@link DENY_ALL_PREDICATE}), mirroring the policy returning no rows.
|
|
813
|
+
*
|
|
814
|
+
* An `OR` of equalities, not an `IN`: the engine's `In` leaf takes a *subquery*, and a grant set lives
|
|
815
|
+
* in the JWT rather than in a table, so there is nothing to select from. This is not a workaround —
|
|
816
|
+
* `OR(=v…)` is exactly what the engine's own SQL parser desugars `IN (list)` to, so the two forms
|
|
817
|
+
* compile to the same nodes.
|
|
812
818
|
*/
|
|
813
|
-
export function
|
|
819
|
+
export function buildGrantScopeShapePredicate(scopeColumn: AnyColumn, ids: string[]): Predicate {
|
|
814
820
|
if (ids.length === 0) {
|
|
815
|
-
return
|
|
821
|
+
return DENY_ALL_PREDICATE;
|
|
816
822
|
}
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
sql`, `,
|
|
820
|
-
)})`;
|
|
823
|
+
const equalities = ids.map((id) => p.eq(scopeColumn, id));
|
|
824
|
+
return equalities.length === 1 ? equalities[0]! : p.or(...equalities);
|
|
821
825
|
}
|
|
822
826
|
|
|
823
827
|
/** The full grant-scope declaration the read mirror needs: the claim options plus the optional bypass. */
|
|
@@ -879,7 +883,7 @@ function hasGrantScopeBypass(claims: JwtClaims | null, options: GrantScopeAccess
|
|
|
879
883
|
* options object you gave {@link buildSupabaseGrantScopeNativePolicies} and a bypass grant cannot be
|
|
880
884
|
* enforced on writes yet invisible on reads.
|
|
881
885
|
*
|
|
882
|
-
* Never throws on malformed claims (they reach a `
|
|
886
|
+
* Never throws on malformed claims (they reach a `customPredicate` unverified in shape) — a wrong shape
|
|
883
887
|
* simply confers nothing. A malformed `grantsClaimPath`/`scopeIdField` **option** is still a loud error.
|
|
884
888
|
*/
|
|
885
889
|
export function resolveGrantScopeAccess(claims: JwtClaims | null, options: GrantScopeAccessOptions): GrantScopeAccess {
|
|
@@ -890,18 +894,16 @@ export function resolveGrantScopeAccess(claims: JwtClaims | null, options: Grant
|
|
|
890
894
|
}
|
|
891
895
|
|
|
892
896
|
/**
|
|
893
|
-
* The
|
|
894
|
-
* `select` policy
|
|
895
|
-
*
|
|
896
|
-
*
|
|
897
|
-
* reference* when the set is empty (which is what lets a `customWhere` built on this probe as
|
|
898
|
-
* claims-dependent). Use {@link buildGrantScopeShapeWhere} directly only when there is no bypass to mirror.
|
|
897
|
+
* The grant-scope **predicate**, bypass included — the native-path counterpart to
|
|
898
|
+
* the grant-scope `select` policy, from the same declaration. A caller holding a bypass grant
|
|
899
|
+
* gets `null` (no filter, every row); anyone else gets {@link buildGrantScopeShapePredicate} over
|
|
900
|
+
* their resolved ids.
|
|
899
901
|
*/
|
|
900
|
-
export function
|
|
902
|
+
export function buildGrantScopeAccessShapePredicate(
|
|
901
903
|
scopeColumn: AnyColumn,
|
|
902
904
|
claims: JwtClaims | null,
|
|
903
905
|
options: GrantScopeAccessOptions,
|
|
904
|
-
):
|
|
906
|
+
): Predicate | null {
|
|
905
907
|
const access = resolveGrantScopeAccess(claims, options);
|
|
906
|
-
return access.bypass ? null :
|
|
908
|
+
return access.bypass ? null : buildGrantScopeShapePredicate(scopeColumn, access.ids);
|
|
907
909
|
}
|