@protobase/server 0.58.7 → 0.58.8
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/assistant/query-runner/query-runner.d.ts +7 -2
- package/dist/assistant/query-runner/query-runner.js +8 -4
- package/dist/assistant/query-runner/runner-plan.js +4 -4
- package/dist/better-auth/auth-settings.d.ts +1 -0
- package/dist/better-auth/auth-tables.d.ts +13 -6
- package/dist/better-auth/auth-tables.js +9 -6
- package/dist/better-auth/create-auth.d.ts +1 -0
- package/dist/better-auth/store/resources-outside.d.ts +4 -1
- package/dist/better-auth/store/resources-outside.js +8 -3
- package/dist/better-auth/user-table.d.ts +6 -0
- package/dist/better-auth/user-table.js +3 -2
- package/dist/package.js +1 -1
- package/package.json +4 -4
|
@@ -6,16 +6,21 @@ export type TableName = {
|
|
|
6
6
|
schema?: string;
|
|
7
7
|
name: string;
|
|
8
8
|
};
|
|
9
|
+
/** The tables the role must never read: these, and every table in `schemas`, whatever its name. */
|
|
10
|
+
export type GuardedTables = {
|
|
11
|
+
tables: readonly TableName[];
|
|
12
|
+
schemas: readonly string[];
|
|
13
|
+
};
|
|
9
14
|
/**
|
|
10
15
|
* The SQL that makes the role the assistant's read-only queries run as, and its runner, and grants it exactly the
|
|
11
16
|
* resources' tables, never an auth table; `undefined` when the database is current.
|
|
12
17
|
*/
|
|
13
|
-
export declare const queryRunnerMigration: (db: Db, models: readonly ResourceModel[], authTables:
|
|
18
|
+
export declare const queryRunnerMigration: (db: Db, models: readonly ResourceModel[], authTables: GuardedTables) => Promise<string | undefined>;
|
|
14
19
|
/**
|
|
15
20
|
* Refuses to go on while the runner of the assistant's read-only queries is missing or out of date, or runs as a role
|
|
16
21
|
* that is not weaker than the app's: the app's role itself or one it is a member of, a superuser, a login role, or one
|
|
17
22
|
* that can read an auth table, or while the role may call a security-definer function that runs as a role able to
|
|
18
23
|
* read one. It checks the owner's privileges, not its name, so a database cloned under another name still passes. A resource added since the last migration is not checked: Postgres refuses to read its table until then.
|
|
19
24
|
*/
|
|
20
|
-
export declare const checkQueryRunner: (db: Db, authTables:
|
|
25
|
+
export declare const checkQueryRunner: (db: Db, authTables: GuardedTables) => Promise<void>;
|
|
21
26
|
//#endregion
|
|
@@ -8,6 +8,8 @@ const quoted = (name) => `"${name.replaceAll("\"", "\"\"")}"`;
|
|
|
8
8
|
const regclassText = ({ schema, name }) => schema ? `${quoted(schema)}.${quoted(name)}` : quoted(name);
|
|
9
9
|
const roleSql = sql.raw(queryRoleSql);
|
|
10
10
|
const rowsOf = async (db, query) => (await query.execute(db)).rows;
|
|
11
|
+
const guardedRelations = ({ tables, schemas }) => sql`select c.oid, c.relnamespace from pg_class c join pg_namespace n on n.oid = c.relnamespace
|
|
12
|
+
where c.oid in (select to_regclass(t.name) from unnest(${tables.map(regclassText)}::text[]) as t(name)) or n.nspname = any(${[...schemas]}::text[])`;
|
|
11
13
|
const resolved = (db, tables) => rowsOf(db, sql`select n.nspname as schema, c.relname as name from unnest(${tables.map(regclassText)}::text[]) with ordinality as t(name, n)
|
|
12
14
|
join pg_class c on c.oid = to_regclass(t.name) join pg_namespace n on n.oid = c.relnamespace order by t.n`);
|
|
13
15
|
const readRunner = async (db) => {
|
|
@@ -27,7 +29,8 @@ const readQueryRunner = async (db, models, authTables) => {
|
|
|
27
29
|
role,
|
|
28
30
|
runner: await readRunner(db),
|
|
29
31
|
resources: await resolved(db, models.map((model) => model.table)),
|
|
30
|
-
authTables: await resolved(db, authTables),
|
|
32
|
+
authTables: await resolved(db, authTables.tables),
|
|
33
|
+
guardedSchemas: [...authTables.schemas],
|
|
31
34
|
granted: {
|
|
32
35
|
tables,
|
|
33
36
|
schemas: schemas.map((found) => found.schema)
|
|
@@ -39,10 +42,11 @@ const readQueryRunner = async (db, models, authTables) => {
|
|
|
39
42
|
* resources' tables, never an auth table; `undefined` when the database is current.
|
|
40
43
|
*/
|
|
41
44
|
const queryRunnerMigration = async (db, models, authTables) => {
|
|
42
|
-
const { role, runner, resources, authTables: auth, granted } = await readQueryRunner(db, models, authTables);
|
|
45
|
+
const { role, runner, resources, authTables: auth, guardedSchemas, granted } = await readQueryRunner(db, models, authTables);
|
|
43
46
|
const plan = plannedGrants({
|
|
44
47
|
resources,
|
|
45
48
|
authTables: auth,
|
|
49
|
+
guardedSchemas,
|
|
46
50
|
granted
|
|
47
51
|
});
|
|
48
52
|
return role.exists && runner?.source === runnerBody && runner.definer && runner.owner === role.name && !runner.executable && plan.grants.length === 0 && plan.revokes.length === 0 ? void 0 : queryRunnerSql(plan);
|
|
@@ -53,7 +57,7 @@ const leakingFunctions = (db, authTables) => rowsOf(db, sql`with runner as (sele
|
|
|
53
57
|
from pg_proc p join pg_namespace n on n.oid = p.pronamespace, runner
|
|
54
58
|
where p.prosecdef and p.prorettype <> 'trigger'::regtype and p.proowner <> runner.role
|
|
55
59
|
and has_schema_privilege(runner.role, p.pronamespace, 'USAGE') and has_function_privilege(runner.role, p.oid, 'EXECUTE')
|
|
56
|
-
and exists (select from
|
|
60
|
+
and exists (select from (${guardedRelations(authTables)}) c
|
|
57
61
|
where has_schema_privilege(p.proowner, c.relnamespace, 'USAGE') and has_any_column_privilege(p.proowner, c.oid, 'SELECT'))
|
|
58
62
|
order by 1`);
|
|
59
63
|
/**
|
|
@@ -65,7 +69,7 @@ const leakingFunctions = (db, authTables) => rowsOf(db, sql`with runner as (sele
|
|
|
65
69
|
const checkQueryRunner = async (db, authTables) => {
|
|
66
70
|
const [runner] = await rowsOf(db, sql`select p.prosrc as source, p.prosecdef as definer,
|
|
67
71
|
(o.rolsuper or o.rolcanlogin or pg_has_role(p.proowner, current_user, 'MEMBER')) as strong,
|
|
68
|
-
exists (select from
|
|
72
|
+
exists (select from (${guardedRelations(authTables)}) c
|
|
69
73
|
where has_schema_privilege(p.proowner, c.relnamespace, 'USAGE') and has_any_column_privilege(p.proowner, c.oid, 'SELECT')) as reads_auth
|
|
70
74
|
from pg_proc p join pg_roles o on o.oid = p.proowner where p.oid = to_regprocedure(${queryRunnerSignature})`);
|
|
71
75
|
if (!runner || !runner.definer || runner.source !== runnerBody) throw refusal("is missing");
|
|
@@ -3,11 +3,11 @@ const same = (a, b) => a.schema === b.schema && a.name === b.name;
|
|
|
3
3
|
const unique = (items, equal) => items.filter((item, index) => items.findIndex((other) => equal(item, other)) === index);
|
|
4
4
|
/**
|
|
5
5
|
* What the query role must be given and what taken back so that it reads exactly the resources' tables, never an auth
|
|
6
|
-
* table, with usage on the schemas those are in. `resources` and `authTables` are the
|
|
7
|
-
* what the role holds now.
|
|
6
|
+
* table nor any table of a guarded schema, with usage on the schemas those are in. `resources` and `authTables` are the
|
|
7
|
+
* tables that exist, `granted` what the role holds now.
|
|
8
8
|
*/
|
|
9
|
-
const plannedGrants = ({ resources, authTables, granted }) => {
|
|
10
|
-
const tables = unique(resources.filter((table) => !authTables.some((auth) => same(table, auth))), same);
|
|
9
|
+
const plannedGrants = ({ resources, authTables, guardedSchemas, granted }) => {
|
|
10
|
+
const tables = unique(resources.filter((table) => !guardedSchemas.includes(table.schema) && !authTables.some((auth) => same(table, auth))), same);
|
|
11
11
|
const schemas = [...new Set(tables.map((table) => table.schema))];
|
|
12
12
|
return {
|
|
13
13
|
grants: [...schemas.filter((schema) => !granted.schemas.includes(schema)).map((schema) => ({
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { AdminAuth } from "./create-auth.js";
|
|
2
2
|
//#region src/better-auth/auth-tables.d.ts
|
|
3
|
+
/** A table as config names it: the search path decides when it names no schema. */
|
|
4
|
+
export type TableName = {
|
|
5
|
+
schema?: string;
|
|
6
|
+
name: string;
|
|
7
|
+
};
|
|
8
|
+
/** The tables the assistant's queries and the app's resources must never read: these, and every table in `schemas`. */
|
|
9
|
+
export type AuthStoreTables = {
|
|
10
|
+
tables: TableName[];
|
|
11
|
+
schemas: string[];
|
|
12
|
+
};
|
|
3
13
|
/**
|
|
4
14
|
* The admin store's tables, which the assistant's queries and the app's resources must never read: the user table and
|
|
5
|
-
* `storeTableNames`,
|
|
6
|
-
*
|
|
15
|
+
* `storeTableNames`, with `users.keyTable` the table under the user view too, such as Supabase's `auth.users`, whose
|
|
16
|
+
* password hashes the view leaves out but the table holds, and every table of `users.guardedSchemas`.
|
|
7
17
|
*/
|
|
8
|
-
export declare const authStoreTables: (auth: AdminAuth) =>
|
|
9
|
-
schema: string | undefined;
|
|
10
|
-
name: string;
|
|
11
|
-
}[];
|
|
18
|
+
export declare const authStoreTables: (auth: AdminAuth) => AuthStoreTables;
|
|
12
19
|
//#endregion
|
|
@@ -3,8 +3,8 @@ import { storeTableNames } from "./store/table-names.js";
|
|
|
3
3
|
//#region src/better-auth/auth-tables.ts
|
|
4
4
|
/**
|
|
5
5
|
* The admin store's tables, which the assistant's queries and the app's resources must never read: the user table and
|
|
6
|
-
* `storeTableNames`,
|
|
7
|
-
*
|
|
6
|
+
* `storeTableNames`, with `users.keyTable` the table under the user view too, such as Supabase's `auth.users`, whose
|
|
7
|
+
* password hashes the view leaves out but the table holds, and every table of `users.guardedSchemas`.
|
|
8
8
|
*/
|
|
9
9
|
const authStoreTables = (auth) => {
|
|
10
10
|
const { schema, users } = storeNames(auth);
|
|
@@ -12,10 +12,13 @@ const authStoreTables = (auth) => {
|
|
|
12
12
|
schema,
|
|
13
13
|
name
|
|
14
14
|
}));
|
|
15
|
-
return
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
return {
|
|
16
|
+
tables: users.keyTable ? [...own, {
|
|
17
|
+
schema: users.keyTable.schema,
|
|
18
|
+
name: users.keyTable.table
|
|
19
|
+
}] : own,
|
|
20
|
+
schemas: users.guardedSchemas
|
|
21
|
+
};
|
|
19
22
|
};
|
|
20
23
|
//#endregion
|
|
21
24
|
export { authStoreTables };
|
|
@@ -8962,6 +8962,7 @@ export declare const createAuth: (options: CreateAuthOptions, plugins?: AuthPlug
|
|
|
8962
8962
|
schema: string;
|
|
8963
8963
|
table: string;
|
|
8964
8964
|
} | undefined;
|
|
8965
|
+
guardedSchemas: string[];
|
|
8965
8966
|
};
|
|
8966
8967
|
socialProviders: string[];
|
|
8967
8968
|
/** The app's name it signs in and mails as: `PROTOBASE_APP_NAME`, else `appName`, else `Protobase`. */
|
|
@@ -2,6 +2,9 @@ import { AdminAuth } from "../create-auth.js";
|
|
|
2
2
|
import { ResourceModel } from "@protobase/schema";
|
|
3
3
|
import { Kysely } from "kysely";
|
|
4
4
|
//#region src/better-auth/store/resources-outside.d.ts
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* Refuses a resource over a table of the admin store: the user table, a feature's table or the record of migrations,
|
|
7
|
+
* or any table of a guarded schema (`users.guardedSchemas`), by the schema the catalogue finds the table in.
|
|
8
|
+
*/
|
|
6
9
|
export declare const checkResourcesOutsideAuthStore: (db: Kysely<any>, models: readonly ResourceModel[], auth: AdminAuth) => Promise<void>;
|
|
7
10
|
//#endregion
|
|
@@ -6,12 +6,17 @@ const quoted = (name) => `"${name.replaceAll("\"", "\"\"")}"`;
|
|
|
6
6
|
const resolve = async (db, tables) => (await sql`select t.n::int as n, ns.nspname as schema, c.relname as name, current_database() as database
|
|
7
7
|
from unnest(${tables.map(({ schema, name }) => schema ? `${quoted(schema)}.${quoted(name)}` : quoted(name))}::text[]) with ordinality as t(name, n)
|
|
8
8
|
join pg_class c on c.oid = to_regclass(t.name) join pg_namespace ns on ns.oid = c.relnamespace order by t.n`.execute(db)).rows;
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Refuses a resource over a table of the admin store: the user table, a feature's table or the record of migrations,
|
|
11
|
+
* or any table of a guarded schema (`users.guardedSchemas`), by the schema the catalogue finds the table in.
|
|
12
|
+
*/
|
|
10
13
|
const checkResourcesOutsideAuthStore = async (db, models, auth) => {
|
|
11
14
|
if (models.length === 0) return;
|
|
12
|
-
const
|
|
15
|
+
const { tables, schemas } = authStoreTables(auth);
|
|
16
|
+
const store = await resolve(storeDb(auth), tables);
|
|
13
17
|
const same = (a, b) => a.database === b.database && a.schema === b.schema && a.name === b.name;
|
|
14
|
-
const
|
|
18
|
+
const guarded = (table) => schemas.includes(table.schema) || store.some((own) => same(own, table));
|
|
19
|
+
const over = (await resolve(db, models.map((model) => model.table))).filter(guarded);
|
|
15
20
|
if (over.length === 0) return;
|
|
16
21
|
const named = over.map((table) => `${models[table.n - 1].name} (${table.schema}.${table.name})`).join(", ");
|
|
17
22
|
throw new Error(`Resources over the auth store's tables, which hold password hashes, sessions and tokens: ${named}. Remove them from the config; the auth store is read through Better Auth alone.`);
|
|
@@ -37,5 +37,11 @@ export type UserTableOptions = {
|
|
|
37
37
|
schema: string;
|
|
38
38
|
table: string;
|
|
39
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* Schemas the other system that made the user table keeps its sign-in data in, such as `['auth']` for Supabase Auth's
|
|
42
|
+
* identities, sessions, refresh tokens and two-factor secrets: every table in them, today's or one added later,
|
|
43
|
+
* counts among the store's tables. Name only a schema that holds none of the app's own tables.
|
|
44
|
+
*/
|
|
45
|
+
guardedSchemas?: string[];
|
|
40
46
|
};
|
|
41
47
|
//#endregion
|
|
@@ -17,7 +17,7 @@ const userFields = {
|
|
|
17
17
|
last_organization_id: "lastOrganizationId"
|
|
18
18
|
};
|
|
19
19
|
/** The user table's names, from `auth.users` option; unknown columns are an error. */
|
|
20
|
-
const userTable = ({ table = "user", columns = {}, metadata, keyTable } = {}) => {
|
|
20
|
+
const userTable = ({ table = "user", columns = {}, metadata, keyTable, guardedSchemas = [] } = {}) => {
|
|
21
21
|
const unknown = Object.keys(columns).find((name) => !(name in userFields));
|
|
22
22
|
if (unknown !== void 0) throw new Error(`users.columns names "${unknown}", which is not a column of the user table: ${Object.keys(userFields).join(", ")}`);
|
|
23
23
|
const column = (name) => columns[name] ?? name;
|
|
@@ -26,7 +26,8 @@ const userTable = ({ table = "user", columns = {}, metadata, keyTable } = {}) =>
|
|
|
26
26
|
column,
|
|
27
27
|
fieldNames: Object.fromEntries(Object.entries(userFields).map(([name, field]) => [field, column(name)])),
|
|
28
28
|
metadata,
|
|
29
|
-
keyTable
|
|
29
|
+
keyTable,
|
|
30
|
+
guardedSchemas
|
|
30
31
|
};
|
|
31
32
|
};
|
|
32
33
|
/** Better Auth's `user` option: the table and its core columns. */
|
package/dist/package.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/server",
|
|
3
|
-
"version": "0.58.
|
|
3
|
+
"version": "0.58.8",
|
|
4
4
|
"description": "The Protobase REST API as a Hono app, with Better Auth sign-in.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -42,9 +42,9 @@
|
|
|
42
42
|
"@better-auth/sso": "^1.7.12",
|
|
43
43
|
"@livekit/protocol": "1.53.1",
|
|
44
44
|
"@modelcontextprotocol/server": "^2.3.1",
|
|
45
|
-
"@protobase/layout": "0.58.
|
|
46
|
-
"@protobase/query": "0.58.
|
|
47
|
-
"@protobase/schema": "0.58.
|
|
45
|
+
"@protobase/layout": "0.58.8",
|
|
46
|
+
"@protobase/query": "0.58.8",
|
|
47
|
+
"@protobase/schema": "0.58.8",
|
|
48
48
|
"@react-email/render": "^2.1.0",
|
|
49
49
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
50
50
|
"@sentry/core": "^11.7.0",
|