@rebasepro/server-postgres 0.13.1-canary.g06dbe5b → 0.13.1-canary.g1822133
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/PostgresBootstrapper.d.ts +26 -0
- package/dist/auth/services.d.ts +2 -0
- package/dist/{auth-users-columns-CBEOeYqa.js → auth-users-columns-BfQHf9JE.js} +390 -26
- package/dist/auth-users-columns-BfQHf9JE.js.map +1 -0
- package/dist/{backup-service-Bww-Lg0s.js → backup-service-BH0Dzo_h.js} +2 -3
- package/dist/{backup-service-Bww-Lg0s.js.map → backup-service-BH0Dzo_h.js.map} +1 -1
- package/dist/cli-output.d.ts +34 -0
- package/dist/data_driver-ULAyJEi9.js.map +1 -1
- package/dist/ensure-collection-policies-8vuu-n4r.js +124 -0
- package/dist/ensure-collection-policies-8vuu-n4r.js.map +1 -0
- package/dist/{ensure-collection-tables-DzeTEvMv.js → ensure-collection-tables-CbvaGuVn.js} +3 -3
- package/dist/{ensure-collection-tables-DzeTEvMv.js.map → ensure-collection-tables-CbvaGuVn.js.map} +1 -1
- package/dist/index.es.js +824 -1059
- package/dist/index.es.js.map +1 -1
- package/dist/{rls-bootstrap-sql-Bpv3nUZo.js → rls-bootstrap-sql-69hYT8nr.js} +2 -2
- package/dist/{rls-bootstrap-sql-Bpv3nUZo.js.map → rls-bootstrap-sql-69hYT8nr.js.map} +1 -1
- package/dist/rls-enforcement-BJ_3wxwg.js +425 -0
- package/dist/rls-enforcement-BJ_3wxwg.js.map +1 -0
- package/dist/schema/doctor.d.ts +18 -0
- package/dist/schema/ensure-collection-policies.d.ts +33 -9
- package/dist/schema/introspect-db-inference.d.ts +8 -1
- package/dist/schema/introspect-db-logic.d.ts +49 -0
- package/dist/schema/introspect-db-project.d.ts +21 -0
- package/dist/security/policy-drift.d.ts +34 -0
- package/dist/services/PersistService.d.ts +21 -17
- package/dist/services/RelationService.d.ts +9 -87
- package/dist/services/RelationWriteService.d.ts +82 -0
- package/dist/services/collection-helpers.d.ts +16 -0
- package/dist/services/junction-writes.d.ts +82 -0
- package/dist/services/realtimeService.d.ts +33 -2
- package/dist/services/write-denial.d.ts +36 -0
- package/dist/{src-C_wvdMnl.js → src-DCdn3Val.js} +35 -3
- package/dist/src-DCdn3Val.js.map +1 -0
- package/dist/{websocket-D1qbmLZ2.js → websocket-C8ZqVBiV.js} +2 -2
- package/dist/{websocket-D1qbmLZ2.js.map → websocket-C8ZqVBiV.js.map} +1 -1
- package/package.json +6 -6
- package/src/PostgresBootstrapper.ts +81 -5
- package/src/auth/ensure-tables.ts +5 -5
- package/src/auth/services.ts +13 -0
- package/src/backup/backup-cli.ts +59 -57
- package/src/cli-errors.ts +6 -6
- package/src/cli-helpers.ts +4 -4
- package/src/cli-output.ts +43 -0
- package/src/cli.ts +155 -147
- package/src/collections/buildRegistry.ts +3 -1
- package/src/data-transformer.ts +22 -11
- package/src/history/ensure-history-table.ts +2 -2
- package/src/schema/doctor-cli.ts +2 -2
- package/src/schema/doctor.ts +41 -21
- package/src/schema/ensure-collection-policies.ts +99 -6
- package/src/schema/generate-drizzle-schema-logic.ts +22 -26
- package/src/schema/generate-drizzle-schema.ts +11 -10
- package/src/schema/generate-postgres-ddl.ts +14 -13
- package/src/schema/generated-schema-staleness.ts +7 -5
- package/src/schema/introspect-db-inference.ts +9 -2
- package/src/schema/introspect-db-logic.ts +193 -49
- package/src/schema/introspect-db-project.ts +78 -0
- package/src/schema/introspect-db.ts +42 -25
- package/src/schema/introspect-runtime.ts +14 -2
- package/src/security/policy-drift.test.ts +104 -3
- package/src/security/policy-drift.ts +129 -7
- package/src/security/rls-enforcement.ts +1 -1
- package/src/services/FetchService.ts +78 -7
- package/src/services/PersistService.ts +44 -41
- package/src/services/RelationService.ts +35 -737
- package/src/services/RelationWriteService.ts +653 -0
- package/src/services/cdc/trigger-cdc.ts +5 -1
- package/src/services/collection-helpers.ts +35 -3
- package/src/services/junction-writes.ts +295 -0
- package/src/services/pg-notify-listener.ts +1 -1
- package/src/services/realtimeService.ts +38 -39
- package/src/services/write-denial.ts +55 -0
- package/src/utils/drizzle-conditions.ts +69 -13
- package/dist/auth-users-columns-CBEOeYqa.js.map +0 -1
- package/dist/ensure-collection-policies-B_JMGa5K.js +0 -57
- package/dist/ensure-collection-policies-B_JMGa5K.js.map +0 -1
- package/dist/src-C_wvdMnl.js.map +0 -1
|
@@ -89,6 +89,32 @@ export interface PostgresDriverInternals {
|
|
|
89
89
|
* and this is the part that was wrong.
|
|
90
90
|
*/
|
|
91
91
|
export declare function resolveDriftCheckName(col: CollectionConfig, registeredTableNames: string[]): string;
|
|
92
|
+
/**
|
|
93
|
+
* Is this the local database `rebase init` scaffolds — i.e. the one case where
|
|
94
|
+
* "you are connected as a superuser" is not news?
|
|
95
|
+
*
|
|
96
|
+
* The scaffold's own `docker-compose.yml` sets `POSTGRES_USER: rebase_app`,
|
|
97
|
+
* which makes that role the cluster superuser, so the superuser advisory below
|
|
98
|
+
* was the only WARN a brand-new project ever saw and it was about a decision
|
|
99
|
+
* the tool had made for the developer.
|
|
100
|
+
*
|
|
101
|
+
* Of the two available fixes — provision a non-superuser table-owner role in
|
|
102
|
+
* the scaffold, or recognise the local shape and stay quiet — this is the
|
|
103
|
+
* second, because the first breaks the scaffold it is meant to improve: a
|
|
104
|
+
* non-superuser owner cannot `CREATE EXTENSION` (search collections need
|
|
105
|
+
* `pg_trgm`/`unaccent`, applied by `rebase db push` and again by the boot
|
|
106
|
+
* schema-ensure), so the very first `pnpm run db:push` on a scaffolded project
|
|
107
|
+
* with a search block would fail. Trading a working first run for a quieter log
|
|
108
|
+
* line is the wrong trade.
|
|
109
|
+
*
|
|
110
|
+
* The condition is deliberately narrow — a *non-production* process talking to
|
|
111
|
+
* a database on the loopback interface. A genuine production superuser
|
|
112
|
+
* connection still warns, and so does a non-production process pointed at a
|
|
113
|
+
* remote database (the usual "my dev machine writes to staging" mistake, where
|
|
114
|
+
* the advisory is exactly right). NODE_ENV alone would not do: the scaffold
|
|
115
|
+
* ships `NODE_ENV=development` and some deployments inherit it.
|
|
116
|
+
*/
|
|
117
|
+
export declare function isScaffoldedLocalDatabase(connectionString: string | undefined): boolean;
|
|
92
118
|
/**
|
|
93
119
|
* Default PostgreSQL bootstrapper.
|
|
94
120
|
*
|
package/dist/auth/services.d.ts
CHANGED
|
@@ -287,6 +287,7 @@ export declare class PostgresAuthRepository implements AuthRepository {
|
|
|
287
287
|
secretEncrypted: string;
|
|
288
288
|
}) | null>;
|
|
289
289
|
verifyMfaFactor(factorId: string): Promise<void>;
|
|
290
|
+
updateMfaFactorSecret(factorId: string, secretEncrypted: string): Promise<void>;
|
|
290
291
|
deleteMfaFactor(factorId: string, uid: string): Promise<void>;
|
|
291
292
|
createMfaChallenge(factorId: string, ipAddress?: string): Promise<MfaChallengeInfo>;
|
|
292
293
|
getMfaChallengeById(challengeId: string): Promise<MfaChallengeInfo | null>;
|
|
@@ -323,6 +324,7 @@ export declare class MfaService implements MfaRepository {
|
|
|
323
324
|
*/
|
|
324
325
|
claimMfaFactorCounter(factorId: string, counter: number): Promise<boolean>;
|
|
325
326
|
verifyMfaFactor(factorId: string): Promise<void>;
|
|
327
|
+
updateMfaFactorSecret(factorId: string, secretEncrypted: string): Promise<void>;
|
|
326
328
|
deleteMfaFactor(factorId: string, uid: string): Promise<void>;
|
|
327
329
|
createMfaChallenge(factorId: string, ipAddress?: string): Promise<MfaChallengeInfo>;
|
|
328
330
|
getMfaChallengeById(challengeId: string): Promise<MfaChallengeInfo | null>;
|
|
@@ -3,7 +3,7 @@ import "process";
|
|
|
3
3
|
__createRequire(import.meta.url);
|
|
4
4
|
import { l as __require, s as __commonJSMin } from "./connection-BuZ97wsr.js";
|
|
5
5
|
import { a as policy, i as ANONYMOUS_USER_IDS, r as ANONYMOUS_USER_ID } from "./data_driver-ULAyJEi9.js";
|
|
6
|
-
import { a as rewriteLegacyRlsFunctions, c as isPostgresCollectionConfig, d as getDataSourceCapabilities, f as
|
|
6
|
+
import { a as rewriteLegacyRlsFunctions, c as isPostgresCollectionConfig, d as getDataSourceCapabilities, f as ALL_WHERE_FILTER_OPS, g as toCanonicalOp, h as REST_TO_CANONICAL, i as RLS_UID_SQL, l as isRelationalCollectionConfig, m as NULL_OPS, p as CANONICAL_TO_REST, r as RLS_ROLES_SQL, s as getDeclaredSubcollections } from "./src-DCdn3Val.js";
|
|
7
7
|
import { createHash } from "node:crypto";
|
|
8
8
|
//#region ../types/src/types/entities.ts
|
|
9
9
|
/**
|
|
@@ -1412,6 +1412,73 @@ function toPostgresIdentifier(name) {
|
|
|
1412
1412
|
if (bytes.byteLength <= 63) return name;
|
|
1413
1413
|
return new TextDecoder("utf-8").decode(bytes.subarray(0, 63)).replace(/�+$/, "");
|
|
1414
1414
|
}
|
|
1415
|
+
/**
|
|
1416
|
+
* The API name a database column is served under.
|
|
1417
|
+
*
|
|
1418
|
+
* The wire name of a field is its property key, and Rebase's property keys are
|
|
1419
|
+
* camelCase — `displayName`, `createdAt`, `photoURL`. Columns are snake_case,
|
|
1420
|
+
* because an unquoted Postgres identifier folds to lower case and a camelCase
|
|
1421
|
+
* column is therefore reachable only as `"authorId"` forever: in hand-written
|
|
1422
|
+
* SQL, in psql, in an RLS policy body, in a dump, and in every third-party tool
|
|
1423
|
+
* that ever touches the database. So the two conventions are both right, and
|
|
1424
|
+
* this is the function that crosses between them.
|
|
1425
|
+
*
|
|
1426
|
+
* It exists because two sources of field names never crossed: a foreign key
|
|
1427
|
+
* derived from a relation (`author_id`) and a column read back by introspection
|
|
1428
|
+
* (`user_id`) both landed on the wire under their column name, while every
|
|
1429
|
+
* hand-authored collection next to them used camelCase. One API, two
|
|
1430
|
+
* conventions, and no rule a caller could infer from outside — those names are
|
|
1431
|
+
* also the `where` and `orderBy` keys, so it was not a matter of taste.
|
|
1432
|
+
*
|
|
1433
|
+
* Rules, in the order they matter:
|
|
1434
|
+
*
|
|
1435
|
+
* - **A name with no separator is returned unchanged.** `photoURL` stays
|
|
1436
|
+
* `photoURL` and `id` stays `id`. Lower-casing a single token is what makes
|
|
1437
|
+
* a "camelCase" helper destructive — `camelCase("photoURL")` is `photourl` —
|
|
1438
|
+
* and this function is applied to names that are *already* keys.
|
|
1439
|
+
* - **Each following segment keeps its own casing** apart from an upper-cased
|
|
1440
|
+
* first letter, so `photo_URL` → `photoURL` rather than `photoUrl`.
|
|
1441
|
+
* - **The result may still not be a JavaScript identifier.** `2fa_enabled`
|
|
1442
|
+
* becomes `2faEnabled`, which is a perfectly good object key and still needs
|
|
1443
|
+
* quoting where one is written into generated source.
|
|
1444
|
+
*
|
|
1445
|
+
* Not the inverse of {@link toSnakeCase}: `toSnakeCase` tokenises on case
|
|
1446
|
+
* boundaries and would turn `photoURL` into `photo_url`. Round-tripping is not
|
|
1447
|
+
* a property either function promises, which is why a column name that a
|
|
1448
|
+
* property maps explicitly is always read off `columnName` rather than derived.
|
|
1449
|
+
*/
|
|
1450
|
+
function toWireKey(columnName) {
|
|
1451
|
+
if (!columnName) return columnName;
|
|
1452
|
+
const segments = columnName.split(/[-_ ]+/).filter(Boolean);
|
|
1453
|
+
if (segments.length <= 1) return columnName;
|
|
1454
|
+
return segments.map((segment, index) => index === 0 ? segment.charAt(0).toLowerCase() + segment.slice(1) : segment.charAt(0).toUpperCase() + segment.slice(1)).join("");
|
|
1455
|
+
}
|
|
1456
|
+
/**
|
|
1457
|
+
* The first candidate key not already used, or a numbered fallback.
|
|
1458
|
+
*
|
|
1459
|
+
* Introspection turns a set of column names into a set of object keys, and the
|
|
1460
|
+
* mapping is not injective: `user_id` and `userId` are two columns and one
|
|
1461
|
+
* {@link toWireKey}, and two foreign keys can strip to the same relation name.
|
|
1462
|
+
* A duplicate key in a generated object literal is a TypeScript error, so the
|
|
1463
|
+
* whole collection stops compiling — and a duplicate key in a `Record` built at
|
|
1464
|
+
* runtime is worse, because it silently drops a column instead.
|
|
1465
|
+
*
|
|
1466
|
+
* The numbered tail is what makes this total: a function that returns a key it
|
|
1467
|
+
* cannot guarantee is free has only moved the duplicate one line down.
|
|
1468
|
+
*
|
|
1469
|
+
* Structurally typed on `has` so a `Map` of emitted blocks and a `Set` of taken
|
|
1470
|
+
* names both satisfy it. Lives here, in the package both introspection
|
|
1471
|
+
* producers and the admin's table import can reach, because they must resolve a
|
|
1472
|
+
* collision the same way or one database describes itself three ways.
|
|
1473
|
+
*/
|
|
1474
|
+
function firstFreeKey(candidates, taken) {
|
|
1475
|
+
for (const candidate of candidates) if (!taken.has(candidate)) return candidate;
|
|
1476
|
+
const base = candidates[candidates.length - 1];
|
|
1477
|
+
for (let suffix = 2;; suffix++) {
|
|
1478
|
+
const candidate = `${base}_${suffix}`;
|
|
1479
|
+
if (!taken.has(candidate)) return candidate;
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1415
1482
|
//#endregion
|
|
1416
1483
|
//#region ../common/src/util/entities.ts
|
|
1417
1484
|
/**
|
|
@@ -1876,6 +1943,48 @@ function getColumnName(fullColumn) {
|
|
|
1876
1943
|
return fullColumn.includes(".") ? fullColumn.split(".").pop() : fullColumn;
|
|
1877
1944
|
}
|
|
1878
1945
|
/**
|
|
1946
|
+
* The field key a database column is served and addressed under.
|
|
1947
|
+
*
|
|
1948
|
+
* A column has two names and they are not the same name. `author_id` is what
|
|
1949
|
+
* Postgres stores; `authorId` is the key on the JSON row, the key in the
|
|
1950
|
+
* generated Drizzle table, and the key a caller writes in `where` and
|
|
1951
|
+
* `orderBy`. Every place that starts from a column and has to reach a row, a
|
|
1952
|
+
* Drizzle table or a payload goes through here, so there is one answer rather
|
|
1953
|
+
* than one per call site — the two that disagreed put `displayName` and
|
|
1954
|
+
* `author_id` on the same API.
|
|
1955
|
+
*
|
|
1956
|
+
* A declared property is the authority when there is one, because its key *is*
|
|
1957
|
+
* the wire name and `columnName` is the only thing that ever renamed the
|
|
1958
|
+
* column:
|
|
1959
|
+
*
|
|
1960
|
+
* 1. an explicit `columnName` equal to this column;
|
|
1961
|
+
* 2. a property whose key is literally the column (an author who wrote
|
|
1962
|
+
* `author_id:` meant `author_id` on the wire, and gets it);
|
|
1963
|
+
* 3. a property whose key snake-cases to the column, which is the default
|
|
1964
|
+
* mapping — `authorId` → `author_id`.
|
|
1965
|
+
*
|
|
1966
|
+
* With no property in the way — a foreign key derived from a relation, which
|
|
1967
|
+
* usually has none — the name is derived: {@link toWireKey}.
|
|
1968
|
+
*
|
|
1969
|
+
* Note the fallback is *not* the column verbatim. That was the old behaviour
|
|
1970
|
+
* and it is precisely the defect: a derived foreign key reached the wire under
|
|
1971
|
+
* its column name while every hand-authored field beside it was camelCase.
|
|
1972
|
+
*/
|
|
1973
|
+
function fieldKeyForColumn(collection, column) {
|
|
1974
|
+
const properties = collection?.properties;
|
|
1975
|
+
if (properties) {
|
|
1976
|
+
for (const [key, prop] of Object.entries(properties)) {
|
|
1977
|
+
const columnName = prop?.columnName;
|
|
1978
|
+
if (typeof columnName === "string" && columnName === column) return key;
|
|
1979
|
+
}
|
|
1980
|
+
for (const key of Object.keys(properties)) {
|
|
1981
|
+
if (key === column) return key;
|
|
1982
|
+
if (toSnakeCase(key) === column) return key;
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
return toWireKey(column);
|
|
1986
|
+
}
|
|
1987
|
+
/**
|
|
1879
1988
|
* Look up a relation by key with forgiving normalization.
|
|
1880
1989
|
*
|
|
1881
1990
|
* `resolveCollectionRelations` stores each relation under a single canonical
|
|
@@ -2115,11 +2224,32 @@ function sqlToPolicy(sql) {
|
|
|
2115
2224
|
* is the more dangerous spelling, because it inverts a rule instead of
|
|
2116
2225
|
* emptying a table.
|
|
2117
2226
|
*/
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2227
|
+
/**
|
|
2228
|
+
* A `Map`, not an object literal.
|
|
2229
|
+
*
|
|
2230
|
+
* As `Record<string, string>` this was indexed with a literal taken straight
|
|
2231
|
+
* out of a policy, so every key on `Object.prototype` answered: a rule
|
|
2232
|
+
* comparing `rebase.uid()` to `"valueOf"`, `"toString"`, `"constructor"` or
|
|
2233
|
+
* `"hasOwnProperty"` found a truthy "platform" and reported an anonymous-grant
|
|
2234
|
+
* risk that does not exist — with the matched function interpolated into the
|
|
2235
|
+
* explanation as the platform's name. A security warning that fires on
|
|
2236
|
+
* innocent input is worse than none: it is what teaches people to skip the
|
|
2237
|
+
* warnings that are real.
|
|
2238
|
+
*
|
|
2239
|
+
* Same shape as the prototype-pollution class swept out of `setIn`, `getIn`,
|
|
2240
|
+
* `mergeDeep` and `unflattenObject` — a data-derived key reaching a plain
|
|
2241
|
+
* object. Found by a property test, on the input `"valueOf"`.
|
|
2242
|
+
*/
|
|
2243
|
+
var FOREIGN_CONVENTION_UIDS = /* @__PURE__ */ new Map([
|
|
2244
|
+
["anon", "Supabase"],
|
|
2245
|
+
["authenticated", "Supabase"],
|
|
2246
|
+
["service_role", "Supabase"]
|
|
2247
|
+
]);
|
|
2248
|
+
/**
|
|
2249
|
+
* The same foreign literals, as a pattern for SQL that could not be parsed
|
|
2250
|
+
* back into structure.
|
|
2251
|
+
*/
|
|
2252
|
+
var FOREIGN_UID_LITERAL_SQL = new RegExp(String.raw`rebase\.uid\(\)\s*=\s*'(${[...FOREIGN_CONVENTION_UIDS.keys()].join("|")})'`, "i");
|
|
2123
2253
|
/**
|
|
2124
2254
|
* `rebase.uid() IS NOT NULL` in raw SQL, the clause that is always true.
|
|
2125
2255
|
*
|
|
@@ -2166,17 +2296,27 @@ function findAnonymousGrants(expr) {
|
|
|
2166
2296
|
case "existsIn":
|
|
2167
2297
|
visit(e.where);
|
|
2168
2298
|
return;
|
|
2169
|
-
case "raw":
|
|
2299
|
+
case "raw": {
|
|
2170
2300
|
if (UID_NOT_NULL.test(e.sql)) found.push({
|
|
2171
2301
|
pattern: "uid-not-null",
|
|
2172
2302
|
detail: e.sql,
|
|
2173
2303
|
explanation: `\`rebase.uid() IS NOT NULL\` is true for every request that came from a client, including anonymous ones — they carry '${ANONYMOUS_USER_ID}', not NULL. Use \`condition: policy.authenticated()\` to mean "signed in".`
|
|
2174
2304
|
});
|
|
2305
|
+
const foreign = FOREIGN_UID_LITERAL_SQL.exec(e.sql);
|
|
2306
|
+
if (foreign) {
|
|
2307
|
+
const literal = foreign[1];
|
|
2308
|
+
found.push({
|
|
2309
|
+
pattern: "foreign-uid-literal",
|
|
2310
|
+
detail: literal,
|
|
2311
|
+
explanation: `'${literal}' is a ${FOREIGN_CONVENTION_UIDS.get(literal)} convention. Rebase reports an anonymous request as '${ANONYMOUS_USER_ID}', so comparing against '${literal}' passes for every caller. Use \`condition: policy.authenticated()\` to mean "signed in".`
|
|
2312
|
+
});
|
|
2313
|
+
}
|
|
2175
2314
|
return;
|
|
2315
|
+
}
|
|
2176
2316
|
case "compare": {
|
|
2177
2317
|
const literal = [e.left, e.right].find((o) => o.kind === "literal");
|
|
2178
2318
|
if (!(e.left.kind === "authUid" || e.right.kind === "authUid") || typeof literal?.value !== "string") return;
|
|
2179
|
-
const platform = FOREIGN_CONVENTION_UIDS
|
|
2319
|
+
const platform = FOREIGN_CONVENTION_UIDS.get(literal.value);
|
|
2180
2320
|
if (!platform) return;
|
|
2181
2321
|
found.push({
|
|
2182
2322
|
pattern: "foreign-uid-literal",
|
|
@@ -2451,11 +2591,28 @@ function getIdPropertyName$1(collection) {
|
|
|
2451
2591
|
*
|
|
2452
2592
|
* Collections that opt out via `disableDefaultPolicies` are returned unchanged.
|
|
2453
2593
|
*/
|
|
2594
|
+
/**
|
|
2595
|
+
* The restrictive write gate for an auth collection.
|
|
2596
|
+
*
|
|
2597
|
+
* Restrictive, so it is ANDed with everything else: whatever an author's
|
|
2598
|
+
* permissive rules allow, a write to this table still has to satisfy this too.
|
|
2599
|
+
* It is the only thing standing between "users may edit their own row" and
|
|
2600
|
+
* "users may grant themselves any role".
|
|
2601
|
+
*/
|
|
2602
|
+
function adminWriteGate(tableName) {
|
|
2603
|
+
return {
|
|
2604
|
+
name: `${tableName}_require_admin_write`,
|
|
2605
|
+
mode: "restrictive",
|
|
2606
|
+
operations: [...DEFAULT_GUARDED_OPS],
|
|
2607
|
+
condition: SERVER_OR_ADMIN_EXPR$1,
|
|
2608
|
+
check: SERVER_OR_ADMIN_EXPR$1
|
|
2609
|
+
};
|
|
2610
|
+
}
|
|
2454
2611
|
function getEffectiveSecurityRules(collection) {
|
|
2455
2612
|
const explicit = [...collection.securityRules ?? []];
|
|
2456
|
-
if (isPostgresCollectionConfig(collection) && collection.disableDefaultPolicies) return explicit;
|
|
2457
2613
|
const tableName = getTableName(collection);
|
|
2458
2614
|
const injected = [];
|
|
2615
|
+
if (isPostgresCollectionConfig(collection) && collection.disableDefaultPolicies) return isAuthCollection$1(collection) ? [...explicit, adminWriteGate(tableName)] : explicit;
|
|
2459
2616
|
injected.push({
|
|
2460
2617
|
name: `${tableName}_default_admin_read`,
|
|
2461
2618
|
operations: ["select"],
|
|
@@ -2473,13 +2630,7 @@ function getEffectiveSecurityRules(collection) {
|
|
|
2473
2630
|
operations: ["select"],
|
|
2474
2631
|
condition: policy.compare(policy.field(getIdPropertyName$1(collection)), "eq", policy.authUid())
|
|
2475
2632
|
});
|
|
2476
|
-
injected.push(
|
|
2477
|
-
name: `${tableName}_require_admin_write`,
|
|
2478
|
-
mode: "restrictive",
|
|
2479
|
-
operations: [...DEFAULT_GUARDED_OPS],
|
|
2480
|
-
condition: SERVER_OR_ADMIN_EXPR$1,
|
|
2481
|
-
check: SERVER_OR_ADMIN_EXPR$1
|
|
2482
|
-
});
|
|
2633
|
+
injected.push(adminWriteGate(tableName));
|
|
2483
2634
|
}
|
|
2484
2635
|
return [...explicit, ...injected];
|
|
2485
2636
|
}
|
|
@@ -3993,6 +4144,17 @@ async function collectAllPages(find, params, label = "collection") {
|
|
|
3993
4144
|
* @module
|
|
3994
4145
|
*/
|
|
3995
4146
|
/**
|
|
4147
|
+
* Escape a value for the wire format: `\` → `\\`, `,` → `\,`, `(` → `\(`,
|
|
4148
|
+
* `)` → `\)`.
|
|
4149
|
+
*/
|
|
4150
|
+
/**
|
|
4151
|
+
* The wire spelling of an empty list.
|
|
4152
|
+
*
|
|
4153
|
+
* A lone backslash: unproducible by {@link escapeWireValue}, which doubles
|
|
4154
|
+
* every backslash it emits, so it cannot collide with any real item.
|
|
4155
|
+
*/
|
|
4156
|
+
var EMPTY_LIST_TOKEN = "\\";
|
|
4157
|
+
/**
|
|
3996
4158
|
* Unescape a wire-format value.
|
|
3997
4159
|
*
|
|
3998
4160
|
* **Conservative**, and deliberately so: only the four sequences
|
|
@@ -4041,7 +4203,199 @@ function splitListItems(inner) {
|
|
|
4041
4203
|
items.push(unescapeWireValue(current));
|
|
4042
4204
|
return items;
|
|
4043
4205
|
}
|
|
4044
|
-
|
|
4206
|
+
/**
|
|
4207
|
+
* Operator tables as `Map`s, because the key comes off the wire.
|
|
4208
|
+
*
|
|
4209
|
+
* Indexed as plain objects, every `Object.prototype` member answered: a query
|
|
4210
|
+
* string of `?f=valueOf.x` found a truthy "operator" — the inherited function —
|
|
4211
|
+
* and `deserializeTuple` returned it *as the operator*, so a function object
|
|
4212
|
+
* travelled on into the compilers in place of a `WhereFilterOp`. The guard one
|
|
4213
|
+
* line below (`if (!canonicalOp)`) reads as though it rejects anything unknown,
|
|
4214
|
+
* and does not: `Object.prototype` is not unknown to a plain object.
|
|
4215
|
+
*
|
|
4216
|
+
* Same shape as the prototype-key defects swept out of `setIn`, `getIn`,
|
|
4217
|
+
* `mergeDeep`, `unflattenObject` and `FOREIGN_CONVENTION_UIDS`.
|
|
4218
|
+
*/
|
|
4219
|
+
var REST_OP_LOOKUP = new Map(Object.entries(REST_TO_CANONICAL));
|
|
4220
|
+
new Map(Object.entries(CANONICAL_TO_REST));
|
|
4221
|
+
/** The operator spellings a rejection lists back to the caller. */
|
|
4222
|
+
var VALID_OPERATOR_LIST = ALL_WHERE_FILTER_OPS.join(", ");
|
|
4223
|
+
/**
|
|
4224
|
+
* A filter condition named an operator this dialect does not have.
|
|
4225
|
+
*
|
|
4226
|
+
* ## Why this throws, rather than returning a typed rejection
|
|
4227
|
+
*
|
|
4228
|
+
* `deserializeFilter` is the *shared* codec: the REST ingress
|
|
4229
|
+
* (`packages/server/src/api/rest/query-parser.ts`), the browser SDK and the
|
|
4230
|
+
* admin panel (`buildRebaseData.ts`) all decode through it. Two constraints
|
|
4231
|
+
* follow.
|
|
4232
|
+
*
|
|
4233
|
+
* - It cannot throw the server's `ApiError`. `@rebasepro/common` does not
|
|
4234
|
+
* depend on `@rebasepro/server` (the dependency runs the other way), and a
|
|
4235
|
+
* browser client has no error handler to render an `ApiError` with. So the
|
|
4236
|
+
* rejection is this plain `Error` subclass, whose `message` reads correctly
|
|
4237
|
+
* wherever it surfaces — a rejected promise in an app, a 400 body over HTTP.
|
|
4238
|
+
* - It cannot be a returned rejection *value*. Every caller assigns the result
|
|
4239
|
+
* straight into a query it is about to run; a sentinel that none of them
|
|
4240
|
+
* check would be ignored, which is exactly the silently-wrong-filter failure
|
|
4241
|
+
* this exists to stop. Throwing is also what this file already does for the
|
|
4242
|
+
* sibling cases — `serializeTuple` on an unknown canonical operator,
|
|
4243
|
+
* `deserializeLogicalCondition` past the nesting bound — and the REST parser
|
|
4244
|
+
* already converts the latter into a 400.
|
|
4245
|
+
*
|
|
4246
|
+
* `statusCode`, `code` and `details` are carried as fields because the server's
|
|
4247
|
+
* Hono error handler duck-types those off any thrown error: a decode path that
|
|
4248
|
+
* forgets to convert still answers 400 with the canonical envelope instead of a
|
|
4249
|
+
* 500 that says "An unexpected error occurred". `query-parser.ts` converts
|
|
4250
|
+
* explicitly all the same — that is the path the contract is stated on, and an
|
|
4251
|
+
* incidental 400 is not a contract.
|
|
4252
|
+
*/
|
|
4253
|
+
var UnknownFilterOperatorError = class extends Error {
|
|
4254
|
+
/** The field the condition was written against. */
|
|
4255
|
+
field;
|
|
4256
|
+
/** The operator string as it arrived, verbatim. */
|
|
4257
|
+
operator;
|
|
4258
|
+
/** Every operator this dialect accepts, in canonical spelling. */
|
|
4259
|
+
validOperators = ALL_WHERE_FILTER_OPS;
|
|
4260
|
+
/** See the class docblock: read by the server's error handler. */
|
|
4261
|
+
statusCode = 400;
|
|
4262
|
+
code = "UNKNOWN_FILTER_OPERATOR";
|
|
4263
|
+
details;
|
|
4264
|
+
constructor(field, operator) {
|
|
4265
|
+
super(`Unknown filter operator '${operator}' on field '${field}'. Valid operators: ${VALID_OPERATOR_LIST}`);
|
|
4266
|
+
this.name = "UnknownFilterOperatorError";
|
|
4267
|
+
this.field = field;
|
|
4268
|
+
this.operator = operator;
|
|
4269
|
+
this.details = {
|
|
4270
|
+
field,
|
|
4271
|
+
operator,
|
|
4272
|
+
validOperators: ALL_WHERE_FILTER_OPS
|
|
4273
|
+
};
|
|
4274
|
+
}
|
|
4275
|
+
};
|
|
4276
|
+
/**
|
|
4277
|
+
* Two to three characters of ASCII punctuation and nothing else — the shape
|
|
4278
|
+
* every symbolic operator has (`==`, `>=`, `<>`, `~~`, `!!`, `>>`, `===`), and
|
|
4279
|
+
* one a column value effectively never has.
|
|
4280
|
+
*
|
|
4281
|
+
* Two characters minimum on purpose. A *single* punctuation character is a
|
|
4282
|
+
* perfectly ordinary value — `{ grade: ["-", "+"] }` is a two-item list, not a
|
|
4283
|
+
* condition — and the only single-character operator anyone actually mistypes
|
|
4284
|
+
* is `=`, which is named separately below. `<` and `>` need no special case:
|
|
4285
|
+
* they are real operators and resolve.
|
|
4286
|
+
*/
|
|
4287
|
+
var SYMBOLIC_OPERATOR = /^[^\p{L}\p{N}\s]{2,3}$/u;
|
|
4288
|
+
/** Lowercase, strip everything that is not a letter or digit. */
|
|
4289
|
+
function normalizeOperatorName(op) {
|
|
4290
|
+
return op.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
4291
|
+
}
|
|
4292
|
+
/**
|
|
4293
|
+
* Every real operator name with its case and separators removed, so a
|
|
4294
|
+
* respelling of one — `arrayContains`, `not_in`, `NOT-LIKE`, `isNull` — is
|
|
4295
|
+
* recognised as an attempt at an operator rather than read as a value.
|
|
4296
|
+
*
|
|
4297
|
+
* These are rejected rather than accepted: admitting a second spelling of an
|
|
4298
|
+
* operator would leave two wire spellings of one thing, and the rejection
|
|
4299
|
+
* message names the one that works.
|
|
4300
|
+
*/
|
|
4301
|
+
var RESPELLED_OPERATORS = new Set([...ALL_WHERE_FILTER_OPS, ...Object.keys(REST_TO_CANONICAL)].map(normalizeOperatorName));
|
|
4302
|
+
/**
|
|
4303
|
+
* Operator names *other* query dialects use, which this one does not have.
|
|
4304
|
+
*
|
|
4305
|
+
* This list is curated, and deliberately so. For a word-shaped string there is
|
|
4306
|
+
* no rule that separates "an operator the caller guessed" from "a value that
|
|
4307
|
+
* happens to be a word": `{ tags: ["a", "b"] }` has to keep meaning a two-item
|
|
4308
|
+
* `in` list, so the codec cannot simply refuse every unrecognised word in
|
|
4309
|
+
* position 0. The line is therefore drawn by name, and only around names whose
|
|
4310
|
+
* use as an operator is far more likely than their use as one of two sibling
|
|
4311
|
+
* values. `contains` is the motivating case — the first thing a developer
|
|
4312
|
+
* reaches for, and until now it compiled to `title IN ('contains', 'Hell')`.
|
|
4313
|
+
*
|
|
4314
|
+
* Genuinely ambiguous single words (`any`, `all`, `exists`, `search`, `not`)
|
|
4315
|
+
* are left off: as operators they are rare, and as enum values they are common.
|
|
4316
|
+
* Everywhere else the tie goes to *rejecting*, because a 400 naming the
|
|
4317
|
+
* supported set costs the caller one round trip, and the alternative — which is
|
|
4318
|
+
* what every name on this list used to produce — is a query that runs, returns
|
|
4319
|
+
* rows, and is wrong.
|
|
4320
|
+
*/
|
|
4321
|
+
var NEAR_MISS_OPERATORS = /* @__PURE__ */ new Set([
|
|
4322
|
+
"contains",
|
|
4323
|
+
"notcontains",
|
|
4324
|
+
"doesnotcontain",
|
|
4325
|
+
"doesnotcontains",
|
|
4326
|
+
"includes",
|
|
4327
|
+
"notincludes",
|
|
4328
|
+
"startswith",
|
|
4329
|
+
"notstartswith",
|
|
4330
|
+
"beginswith",
|
|
4331
|
+
"startingwith",
|
|
4332
|
+
"endswith",
|
|
4333
|
+
"notendswith",
|
|
4334
|
+
"matches",
|
|
4335
|
+
"notmatches",
|
|
4336
|
+
"regex",
|
|
4337
|
+
"regexp",
|
|
4338
|
+
"between",
|
|
4339
|
+
"notbetween",
|
|
4340
|
+
"equals",
|
|
4341
|
+
"notequals",
|
|
4342
|
+
"equalto",
|
|
4343
|
+
"isequalto",
|
|
4344
|
+
"isnotequalto",
|
|
4345
|
+
"greaterthan",
|
|
4346
|
+
"greaterthanorequal",
|
|
4347
|
+
"greaterthanorequalto",
|
|
4348
|
+
"lessthan",
|
|
4349
|
+
"lessthanorequal",
|
|
4350
|
+
"lessthanorequalto",
|
|
4351
|
+
"isempty",
|
|
4352
|
+
"isnotempty",
|
|
4353
|
+
"oneof",
|
|
4354
|
+
"noneof",
|
|
4355
|
+
"anyof",
|
|
4356
|
+
"allof",
|
|
4357
|
+
"null",
|
|
4358
|
+
"isnullorempty"
|
|
4359
|
+
]);
|
|
4360
|
+
/**
|
|
4361
|
+
* Was this string *meant* as an operator?
|
|
4362
|
+
*
|
|
4363
|
+
* Only consulted after {@link toCanonicalOp} has already failed to resolve it,
|
|
4364
|
+
* so a `true` here is always a rejection.
|
|
4365
|
+
*/
|
|
4366
|
+
function isOperatorShaped(op) {
|
|
4367
|
+
if (op === "=") return true;
|
|
4368
|
+
if (SYMBOLIC_OPERATOR.test(op)) return true;
|
|
4369
|
+
const normalized = normalizeOperatorName(op);
|
|
4370
|
+
if (!normalized) return false;
|
|
4371
|
+
return RESPELLED_OPERATORS.has(normalized) || NEAR_MISS_OPERATORS.has(normalized);
|
|
4372
|
+
}
|
|
4373
|
+
/**
|
|
4374
|
+
* Read a `[op, value]` tuple, if that is what this is.
|
|
4375
|
+
*
|
|
4376
|
+
* Three outcomes, and the middle one is the defect this function exists for:
|
|
4377
|
+
*
|
|
4378
|
+
* - the operator resolves (canonical *or* REST spelling) → the canonical tuple;
|
|
4379
|
+
* - the operator does not resolve but was plainly meant as one → throw;
|
|
4380
|
+
* - it does not look like an operator at all → `undefined`, and the caller
|
|
4381
|
+
* falls back to reading the array as a list of values.
|
|
4382
|
+
*
|
|
4383
|
+
* The old test was `toCanonicalOp(raw[0]) === raw[0]`, i.e. canonical spelling
|
|
4384
|
+
* only, with *everything else* — including every REST short-code — dropping
|
|
4385
|
+
* through to `["in", raw]`. So the operator string itself became a value in a
|
|
4386
|
+
* membership test: `["!!", "Hello"]` compiled to `title IN ('!!','Hello')`,
|
|
4387
|
+
* which matches, and the caller got back rows their filter was written to
|
|
4388
|
+
* exclude. `["eq", "active"]` had the same shape of failure.
|
|
4389
|
+
*/
|
|
4390
|
+
function readTuple(field, raw) {
|
|
4391
|
+
if (!Array.isArray(raw) || raw.length !== 2) return void 0;
|
|
4392
|
+
const [op, value] = raw;
|
|
4393
|
+
if (typeof op !== "string") return void 0;
|
|
4394
|
+
const canonical = toCanonicalOp(op);
|
|
4395
|
+
if (canonical) return [canonical, value];
|
|
4396
|
+
if (op.includes(".")) return void 0;
|
|
4397
|
+
if (isOperatorShaped(op)) throw new UnknownFilterOperatorError(field, op);
|
|
4398
|
+
}
|
|
4045
4399
|
/**
|
|
4046
4400
|
* Parse a single PostgREST dot-string into a `[WhereFilterOp, unknown]` tuple.
|
|
4047
4401
|
*
|
|
@@ -4058,10 +4412,13 @@ function deserializeSingle(raw) {
|
|
|
4058
4412
|
if (dotIndex === -1) return ["==", raw];
|
|
4059
4413
|
const prefix = raw.substring(0, dotIndex);
|
|
4060
4414
|
const rest = raw.substring(dotIndex + 1);
|
|
4061
|
-
const canonicalOp = REST_OP_LOOKUP
|
|
4415
|
+
const canonicalOp = REST_OP_LOOKUP.get(prefix);
|
|
4062
4416
|
if (!canonicalOp) return ["==", raw];
|
|
4063
4417
|
if (NULL_OPS.has(canonicalOp)) return [canonicalOp, null];
|
|
4064
|
-
if (rest.startsWith("(") && rest.endsWith(")"))
|
|
4418
|
+
if (rest.startsWith("(") && rest.endsWith(")")) {
|
|
4419
|
+
const inner = rest.slice(1, -1);
|
|
4420
|
+
return [canonicalOp, inner === EMPTY_LIST_TOKEN ? [] : splitListItems(inner)];
|
|
4421
|
+
}
|
|
4065
4422
|
return [canonicalOp, rest];
|
|
4066
4423
|
}
|
|
4067
4424
|
/**
|
|
@@ -4076,20 +4433,27 @@ function deserializeSingle(raw) {
|
|
|
4076
4433
|
*
|
|
4077
4434
|
* deserializeFilter({ age: ["gte.18", "lt.65"] })
|
|
4078
4435
|
* // → { age: [[">=", "18"], ["<", "65"]] }
|
|
4436
|
+
*
|
|
4437
|
+
* @throws {UnknownFilterOperatorError} when a condition names an operator this
|
|
4438
|
+
* dialect does not have. See that class for why a rejection here is a throw.
|
|
4079
4439
|
*/
|
|
4080
4440
|
function deserializeFilter(query) {
|
|
4081
4441
|
const result = {};
|
|
4082
4442
|
for (const [field, raw] of Object.entries(query)) {
|
|
4083
4443
|
if (raw === void 0) continue;
|
|
4084
|
-
|
|
4085
|
-
|
|
4444
|
+
const tuple = readTuple(field, raw);
|
|
4445
|
+
if (tuple) {
|
|
4446
|
+
result[field] = tuple;
|
|
4086
4447
|
continue;
|
|
4087
4448
|
}
|
|
4088
4449
|
if (Array.isArray(raw)) {
|
|
4089
4450
|
if (raw.length === 0) continue;
|
|
4090
|
-
if (Array.isArray(raw[0])
|
|
4091
|
-
|
|
4092
|
-
|
|
4451
|
+
if (Array.isArray(raw[0])) {
|
|
4452
|
+
const tuples = raw.map((item) => readTuple(field, item));
|
|
4453
|
+
if (tuples.every((t) => t !== void 0)) {
|
|
4454
|
+
result[field] = tuples;
|
|
4455
|
+
continue;
|
|
4456
|
+
}
|
|
4093
4457
|
}
|
|
4094
4458
|
if (raw.length === 1) result[field] = typeof raw[0] === "string" ? deserializeSingle(raw[0]) : ["==", raw[0]];
|
|
4095
4459
|
else if (typeof raw[0] === "string" && raw[0].includes(".")) result[field] = raw.map((r) => typeof r === "string" ? deserializeSingle(r) : ["==", r]);
|
|
@@ -5066,6 +5430,6 @@ function isAuthCollection(collection) {
|
|
|
5066
5430
|
return typeof auth === "object" && auth !== null && auth.enabled === true;
|
|
5067
5431
|
}
|
|
5068
5432
|
//#endregion
|
|
5069
|
-
export {
|
|
5433
|
+
export { DEFAULT_ONE_OF_TYPE as $, getEnumVarName as A, createRelationRefWithData as B, getEffectiveSecurityRules as C, fieldKeyForColumn as D, findAnonymousGrants as E, getDeclaredPrimaryKeys as F, legacyForeignKeyName as G, updateDateAutoValues as H, isAddressableId as I, getPolicyNamesForRule as J, toPostgresIdentifier as K, parseIdValues as L, getTableVarName as M, resolveCollectionRelations as N, findRelation as O, buildCompositeId as P, toSnakeCase as Q, sortCollectionsBySlug as R, resolveJunctionSpecs as S, securityRuleToConditions as T, firstFreeKey as U, normalizeToEntityRelation as V, generateForeignKeyName as W, mergeDeep as X, isPrototypePollutingKey as Y, camelCase as Z, CollectionRegistry as _, SEARCH_STAMP_PREFIX as a, getJunctionCollectionConfig as b, assertSearchIsPostgresOnly as c, searchColumnStamps as d, DEFAULT_ONE_OF_VALUE as et, searchExtensionStatements as f, buildSdkData as g, visibleColumnProjection as h, isAuthCollection as i, getTableName as j, getColumnName as k, buildSearchColumnSpec as l, searchIndexStatements as m, authUsersColumnDefinition as n, isManyToMany as nt, SEARCH_TEXT_FN as o, searchHelperFunctions as p, toWireKey as q, authUsersColumnSql as r, Vector as rt, SEARCH_UNACCENT_FN as s, AUTH_USERS_COLUMNS as t, hasForeignKeyOnTarget as tt, hiddenColumnsOption as u, relationalCollections as v, policyToPostgres as w, getJunctionSecurityRules as x, resolveStringColumnLength as y, createRelationRef as z };
|
|
5070
5434
|
|
|
5071
|
-
//# sourceMappingURL=auth-users-columns-
|
|
5435
|
+
//# sourceMappingURL=auth-users-columns-BfQHf9JE.js.map
|