@everystack/server 0.2.21 → 0.2.24
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 +681 -0
- package/README.md +5 -1
- package/jest-preset.js +63 -0
- package/package.json +23 -10
- package/src/db-doctor.ts +229 -0
- package/src/db.ts +100 -10
- package/src/plugin.ts +98 -3
- package/src/role-chain.ts +75 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The role chain — generated, idempotent SQL for the least-privilege login role and the
|
|
3
|
+
* application roles it switches between.
|
|
4
|
+
*
|
|
5
|
+
* This is what the `db:provision` action runs (as the operator/owner) at deploy time so a
|
|
6
|
+
* fresh database comes up with the credential split already in place — no hand-written
|
|
7
|
+
* `CREATE ROLE`, no `ALTER ROLE … PASSWORD` in a console. The password is set separately
|
|
8
|
+
* by `db:provision` from a deploy-generated value; this module only shapes the roles and
|
|
9
|
+
* their membership graph. Table grants and RLS policies stay in the app's migrations
|
|
10
|
+
* (they're schema-specific); this is the part that must exist before those grants resolve.
|
|
11
|
+
*
|
|
12
|
+
* See docs/plans/secure-by-default-database.md.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const IDENT = /^[a-z_][a-z0-9_]*$/;
|
|
16
|
+
|
|
17
|
+
export interface RoleChainOptions {
|
|
18
|
+
/**
|
|
19
|
+
* The LOGIN role the API connects as — least-privilege: LOGIN, NOINHERIT (memberships
|
|
20
|
+
* are SET ROLE-only, never ambient), NOBYPASSRLS, and no grants of its own.
|
|
21
|
+
* Default 'authenticator'.
|
|
22
|
+
*/
|
|
23
|
+
loginRole?: string;
|
|
24
|
+
/**
|
|
25
|
+
* The NOLOGIN application roles the login role may `SET ROLE` to, in order of privilege.
|
|
26
|
+
* Default ['anon', 'authenticated', 'admin'].
|
|
27
|
+
*/
|
|
28
|
+
appRoles?: string[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function assertIdent(name: string, what: string): void {
|
|
32
|
+
if (!IDENT.test(name)) {
|
|
33
|
+
throw new Error(`Invalid ${what}: ${JSON.stringify(name)} — must match ${IDENT}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Generate idempotent DDL that creates the application roles and the least-privilege login
|
|
39
|
+
* role, and wires the membership chain so the login role can `SET ROLE` to each. Safe to
|
|
40
|
+
* run on every deploy: each `CREATE ROLE` is `IF NOT EXISTS`-guarded, and the login role's
|
|
41
|
+
* attributes are re-asserted with `ALTER ROLE` so a role created the old way (e.g. without
|
|
42
|
+
* an explicit NOBYPASSRLS) is corrected. Does NOT set passwords — that's `db:provision`'s
|
|
43
|
+
* job, from a deploy-generated value.
|
|
44
|
+
*/
|
|
45
|
+
export function generateRoleChainSQL(options: RoleChainOptions = {}): string {
|
|
46
|
+
const loginRole = options.loginRole ?? 'authenticator';
|
|
47
|
+
const appRoles = options.appRoles ?? ['anon', 'authenticated', 'admin'];
|
|
48
|
+
|
|
49
|
+
assertIdent(loginRole, 'loginRole');
|
|
50
|
+
appRoles.forEach((r) => assertIdent(r, 'app role'));
|
|
51
|
+
|
|
52
|
+
const lines: string[] = [];
|
|
53
|
+
|
|
54
|
+
// Application roles — NOLOGIN; reached only via SET ROLE.
|
|
55
|
+
for (const role of appRoles) {
|
|
56
|
+
lines.push(
|
|
57
|
+
`DO $$ BEGIN IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '${role}') THEN CREATE ROLE ${role} NOLOGIN; END IF; END $$;`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// The least-privilege login role. Create if absent, then re-assert attributes so an
|
|
62
|
+
// existing role is brought into compliance (LOGIN, NOINHERIT, NOBYPASSRLS).
|
|
63
|
+
lines.push(
|
|
64
|
+
`DO $$ BEGIN IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '${loginRole}') THEN CREATE ROLE ${loginRole} LOGIN NOINHERIT NOBYPASSRLS; END IF; END $$;`,
|
|
65
|
+
);
|
|
66
|
+
lines.push(`ALTER ROLE ${loginRole} WITH LOGIN NOINHERIT NOBYPASSRLS;`);
|
|
67
|
+
|
|
68
|
+
// Membership chain — the login role may SET ROLE to each app role. NOINHERIT means it
|
|
69
|
+
// gains their privileges only when it explicitly switches, never ambiently.
|
|
70
|
+
for (const role of appRoles) {
|
|
71
|
+
lines.push(`GRANT ${role} TO ${loginRole};`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return lines.join('\n') + '\n';
|
|
75
|
+
}
|