@everystack/cli 0.4.98 → 0.4.99

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.
@@ -0,0 +1,132 @@
1
+ /**
2
+ * A role that can write a table but holds no SELECT grant on it.
3
+ *
4
+ * `can('update', { role: 'moderator' })` alone compiles `GRANT UPDATE` and an UPDATE policy for
5
+ * moderator, but no SELECT grant. PostgreSQL needs SELECT to read the columns a WHERE or RETURNING
6
+ * names, so every `UPDATE t ... WHERE id = $1` by moderator fails with "permission denied for
7
+ * table t". The ability reads as working and is dead. The same holds for DELETE ... WHERE and
8
+ * INSERT ... RETURNING (measured: __tests__/integration/select-grant-advisory.test.ts).
9
+ *
10
+ * The rule reads the COMPILED grants (compileTableContract), not the abilities, so it judges what
11
+ * the database will hold: `manage` carries its SELECT, a bare `can('read')` covers anon and
12
+ * authenticated only, an owner read is authenticated's table SELECT, and a column-scoped SELECT
13
+ * counts. Two exceptions: a role with any ability carrying a raw `sql:` condition is skipped (a
14
+ * hand-written predicate is outside what this rule judges), and a write granted only through the
15
+ * model's `privileges:` is left to findNakedGrants, which owns that case.
16
+ *
17
+ * The rule cannot see role membership: a role that inherits SELECT from another role it is a
18
+ * member of is still reported, since no model declares memberships.
19
+ *
20
+ * The advice is a read SCOPED to the rows the write touches, never broader than the write: for an
21
+ * owner write a role-wide `can('read', { role })` would be a USING (true) policy for every
22
+ * signed-in user. A role-wide read is offered only for a role-wide write.
23
+ *
24
+ * Known limits: a table with `rls: false` whose `privileges:` grant a write but no SELECT is
25
+ * skipped here (findNakedGrants owns `privileges:` grants and does not name the missing SELECT);
26
+ * and a write scoped by `where:` alone gets the generic advice, which can be wider than the write.
27
+ *
28
+ * Advisory only — db:check reports it as a warning and never fails on it.
29
+ */
30
+
31
+ import type { Ability, ModelDescriptor } from '@everystack/model';
32
+ import { compileTableContract } from './authz-compile';
33
+ import type { TableContract } from './authz-contract';
34
+
35
+ export type WriteVerb = 'create' | 'delete' | 'update';
36
+
37
+ export interface SelectGrantGap {
38
+ /** `schema.table`. */
39
+ table: string;
40
+ role: string;
41
+ /** The ability verb whose grant is unusable without SELECT. */
42
+ verb: WriteVerb;
43
+ message: string;
44
+ }
45
+
46
+ /** Ability verb -> the privilege it compiles to, in report order. */
47
+ const WRITE_PRIVILEGES: ReadonlyArray<[WriteVerb, string]> = [
48
+ ['create', 'INSERT'],
49
+ ['delete', 'DELETE'],
50
+ ['update', 'UPDATE'],
51
+ ];
52
+
53
+ /** The statement shape that needs SELECT, per privilege. INSERT has no WHERE. */
54
+ const NEEDS_SELECT: Record<string, string> = {
55
+ INSERT: 'INSERT ... RETURNING',
56
+ DELETE: 'DELETE ... WHERE/RETURNING',
57
+ UPDATE: 'UPDATE ... WHERE/RETURNING',
58
+ };
59
+
60
+ /** Does the role hold this privilege on the table, table-wide or on any column? */
61
+ function holds(contract: TableContract, role: string, privilege: string): boolean {
62
+ return (contract.grants[role] ?? []).includes(privilege)
63
+ || Boolean(contract.columnGrants?.[role]?.[privilege]?.length);
64
+ }
65
+
66
+ /** The grantee an ability lands on — what compileGrants keys it under. */
67
+ function granteeOf(role: string | undefined): string {
68
+ if (!role) return 'authenticated';
69
+ return role.toLowerCase() === 'public' ? 'PUBLIC' : role;
70
+ }
71
+
72
+ /** The table privileges an ability compiles to (`manage` is all four). */
73
+ function privilegesOf(action: Ability['action']): string[] {
74
+ switch (action) {
75
+ case 'manage': return ['DELETE', 'INSERT', 'SELECT', 'UPDATE'];
76
+ case 'read': return ['SELECT'];
77
+ case 'create': return ['INSERT'];
78
+ case 'update': return ['UPDATE'];
79
+ case 'delete': return ['DELETE'];
80
+ default: return [];
81
+ }
82
+ }
83
+
84
+ /** The abilities whose grant puts `privilege` on `role` — none means it came from `privileges:`. */
85
+ function sources(model: ModelDescriptor, role: string, privilege: string): Ability[] {
86
+ return model.abilities.filter((a) => granteeOf(a.condition.role) === role && privilegesOf(a.action).includes(privilege));
87
+ }
88
+
89
+ /** The read to suggest: scoped like the write when the write is owner/via-scoped, else generic. */
90
+ function remedy(role: string, from: readonly Ability[]): string {
91
+ const scoped = from.find((a) => a.condition.owner || a.condition.via);
92
+ const lead = `Add a read ability for ${role} scoped to the rows it may touch`;
93
+ if (!scoped) {
94
+ return `${lead} (for example can('read', { owner: '<column>' }) for an owner write, `
95
+ + `or can('read', { role: '${role}' }) for a role-wide one).`;
96
+ }
97
+ const keys = [
98
+ ...(scoped.condition.role ? [`role: '${scoped.condition.role}'`] : []),
99
+ scoped.condition.owner ? `owner: '${scoped.condition.owner}'` : `via: '${scoped.condition.via}'`,
100
+ ];
101
+ return `${lead}, for example can('read', { ${keys.join(', ')} }).`;
102
+ }
103
+
104
+ export function findSelectGrantGaps(models: readonly ModelDescriptor[]): SelectGrantGap[] {
105
+ const out: SelectGrantGap[] = [];
106
+ for (const model of models) {
107
+ const contract = compileTableContract(model);
108
+ const suppressed = new Set(
109
+ model.abilities.filter((a) => a.condition.sql != null).map((a) => granteeOf(a.condition.role)),
110
+ );
111
+ const roles = new Set([...Object.keys(contract.grants), ...Object.keys(contract.columnGrants ?? {})]);
112
+ for (const role of [...roles].sort()) {
113
+ if (suppressed.has(role)) continue;
114
+ // A PUBLIC SELECT reaches every role, so it satisfies any of them.
115
+ if (holds(contract, role, 'SELECT') || holds(contract, 'PUBLIC', 'SELECT')) continue;
116
+ for (const [verb, privilege] of WRITE_PRIVILEGES) {
117
+ if (!holds(contract, role, privilege)) continue;
118
+ const from = sources(model, role, privilege);
119
+ if (from.length === 0) continue;
120
+ out.push({
121
+ table: contract.table,
122
+ role,
123
+ verb,
124
+ message: `${contract.table}: ${role} can ${privilege} but has no SELECT grant; `
125
+ + `${NEEDS_SELECT[privilege]} by ${role} fails with "permission denied for table ${model.table}". `
126
+ + remedy(role, from),
127
+ });
128
+ }
129
+ }
130
+ }
131
+ return out;
132
+ }
@@ -24,6 +24,8 @@
24
24
 
25
25
  import { invokeAction, lambdaSessionRunner } from './aws';
26
26
  import { pollTaskUntilStopped } from './task-poll';
27
+ import type { BackupLiveRead } from './apply-execute';
28
+ import type { AuthzManifest } from './authz-manifest';
27
29
 
28
30
  /** The verdict on a completed dump's fingerprint pair. Pure — the heart of the sandwich. */
29
31
  export type SandwichVerdict = { ok: true } | { ok: false; reason: string };
@@ -46,10 +48,14 @@ export function judgeSandwich(fpBefore: string, fpAfter: string): SandwichVerdic
46
48
  }
47
49
 
48
50
  export interface StageBackupDeps {
49
- /** Read the live base-schema fingerprint. Called twice, bracketing the dump. */
50
- readFingerprint: () => Promise<string>;
51
- /** Dispatch `db:backup` with the operator-computed fingerprint. */
52
- dispatch: (fingerprint: string) => Promise<{ runId: string; taskArn: string; id: string; key: string; warning?: string }>;
51
+ /**
52
+ * Read the live base-schema fingerprint and the target's origin commit, in one session. Called
53
+ * twice, bracketing the dump; the sandwich judges the fingerprints, and the OPENING read is what
54
+ * the dispatch carries.
55
+ */
56
+ readLive: () => Promise<BackupLiveRead>;
57
+ /** Dispatch `db:backup` with the operator-computed fingerprint and origin commit. */
58
+ dispatch: (live: BackupLiveRead) => Promise<{ runId: string; taskArn: string; id: string; key: string; warning?: string }>;
53
59
  /**
54
60
  * Wait for the task to stop; resolve ok, or a human reason it did not.
55
61
  *
@@ -59,6 +65,13 @@ export interface StageBackupDeps {
59
65
  awaitTask: (ids: { runId: string; taskArn: string; id: string }) => Promise<{ ok: true; bytes?: number; stampedFingerprint?: string } | { ok: false; reason: string }>;
60
66
  /** Delete a retracted artifact (the ops Lambda's db:backup:retract). */
61
67
  retract: (key: string) => Promise<void>;
68
+ /**
69
+ * Stamp the declared-authz manifest onto the finished backup (the ops Lambda's
70
+ * db:backup:stamp-authz). Optional: only a caller that rendered a manifest supplies it. Runs only
71
+ * on a PROVEN backup, and its failure is a warning, never a failed backup — the dump is valid, the
72
+ * manifest is metadata layered on top, and a restore treats a missing one as "no manifest".
73
+ */
74
+ stampAuthz?: (key: string) => Promise<void>;
62
75
  /** Progress output. Optional so tests stay quiet. */
63
76
  onStep?: (message: string) => void;
64
77
  }
@@ -78,18 +91,19 @@ export async function runStageBackup(deps: StageBackupDeps): Promise<StageBackup
78
91
  const step = deps.onStep ?? (() => {});
79
92
 
80
93
  step('Reading the live schema fingerprint (before the dump)...');
81
- let fpBefore: string;
94
+ let before: BackupLiveRead;
82
95
  try {
83
- fpBefore = await deps.readFingerprint();
96
+ before = await deps.readLive();
84
97
  } catch (err: any) {
85
98
  // Refuse BEFORE dispatching: a dump whose fingerprint cannot be proven is not a safety net, and
86
99
  // taking one anyway would burn the dump window and still leave nothing trustworthy.
87
100
  return { ok: false, reason: `the schema fingerprint could not be read, so no backup was dispatched: ${err?.message ?? String(err)}` };
88
101
  }
102
+ const fpBefore = before.fingerprint;
89
103
 
90
104
  let dispatched: { runId: string; taskArn: string; id: string; key: string; warning?: string };
91
105
  try {
92
- dispatched = await deps.dispatch(fpBefore);
106
+ dispatched = await deps.dispatch(before);
93
107
  } catch (err: any) {
94
108
  return { ok: false, reason: `the backup would not dispatch: ${err?.message ?? String(err)}` };
95
109
  }
@@ -127,7 +141,7 @@ export async function runStageBackup(deps: StageBackupDeps): Promise<StageBackup
127
141
  step('Re-reading the schema fingerprint (after the dump)...');
128
142
  let fpAfter: string;
129
143
  try {
130
- fpAfter = await deps.readFingerprint();
144
+ fpAfter = (await deps.readLive()).fingerprint;
131
145
  } catch (err: any) {
132
146
  // The dump exists but is unproven. Do NOT retract — an unreadable fingerprint is not evidence
133
147
  // the schema moved, and silently destroying a good backup is the worse error.
@@ -146,7 +160,16 @@ export async function runStageBackup(deps: StageBackupDeps): Promise<StageBackup
146
160
  const unverified = finished.stampedFingerprint === undefined
147
161
  ? 'the task did not report the fingerprint it stamped, so the artifact could not be verified against what the operator sent (the result-ledger write is best-effort)'
148
162
  : undefined;
149
- const warning = [dispatched.warning, unverified].filter(Boolean).join('; ') || undefined;
163
+ let unstamped: string | undefined;
164
+ if (deps.stampAuthz) {
165
+ step('Recording the declared authz manifest on the backup...');
166
+ try {
167
+ await deps.stampAuthz(dispatched.key);
168
+ } catch (err: any) {
169
+ unstamped = `the backup is valid, but its authz manifest was NOT recorded (${err?.message ?? String(err)}) — a restore from it cannot converge authz from the manifest; take another backup if you need one`;
170
+ }
171
+ }
172
+ const warning = [dispatched.warning, unverified, unstamped].filter(Boolean).join('; ') || undefined;
150
173
  return { ok: true, id: dispatched.id, key: dispatched.key, bytes: finished.bytes, fingerprint: fpBefore, warning };
151
174
  }
152
175
 
@@ -178,22 +201,31 @@ export function stageBackupDeps(
178
201
  config: { region: string },
179
202
  opsFn: string,
180
203
  stage: string | undefined,
181
- hooks: { onStep?: (message: string) => void; onDispatch?: (d: { runId: string; taskArn: string; id: string; warning?: string }) => void } = {},
204
+ hooks: {
205
+ onStep?: (message: string) => void;
206
+ onDispatch?: (d: { runId: string; taskArn: string; id: string; warning?: string }) => void;
207
+ /** The declared-authz manifest to stamp onto the finished backup (db:backup renders it). */
208
+ authzManifest?: AuthzManifest;
209
+ } = {},
182
210
  ): StageBackupDeps {
211
+ const { authzManifest } = hooks;
183
212
  return {
184
213
  // BOTH ends of the sandwich read over this ONE closure, so they cannot land on different lanes.
185
214
  // That matters: a before-read over a direct session and an after-read over the two-invoke
186
215
  // ops-Lambda blend could manufacture a false mismatch — or, worse, a false match. The symmetry
187
216
  // is structural here rather than asserted; if a caller ever needs to override one end, it has to
188
217
  // override both.
189
- readFingerprint: async () => {
190
- // The SAME helper db:apply's concurrency lock uses — that shared definition is the point.
191
- const { liveBaseFingerprint } = await import('./apply-execute');
192
- return (await liveBaseFingerprint(lambdaSessionRunner(config.region, opsFn))).hash;
218
+ readLive: async () => {
219
+ // The SAME one-session read db:apply's concurrency lock and descent proof use — the
220
+ // fingerprint and the ledger anchor come from one snapshot, one round trip.
221
+ const { readBackupLive } = await import('./apply-execute');
222
+ return readBackupLive(lambdaSessionRunner(config.region, opsFn));
193
223
  },
194
- dispatch: async (fingerprint) => {
224
+ dispatch: async ({ fingerprint, originCommit }) => {
225
+ // `originCommit` is sent as read: a string, `null` (proven none), or left out when the ledger
226
+ // read failed (JSON drops undefined), so the artifact never claims what the read did not prove.
195
227
  const dispatched: any = await invokeAction(config.region, opsFn, 'db:backup', {
196
- stage, actor: process.env.USER ?? null, fingerprint,
228
+ stage, actor: process.env.USER ?? null, fingerprint, originCommit,
197
229
  });
198
230
  if (dispatched?.error) throw new Error(dispatched.error);
199
231
  hooks.onDispatch?.(dispatched);
@@ -224,6 +256,15 @@ export function stageBackupDeps(
224
256
  if (res?.error) throw new Error(res.error);
225
257
  if (!res?.retracted) throw new Error(`the ops function did not confirm the retraction (returned ${JSON.stringify(res)})`);
226
258
  },
259
+ // Sent AFTER the dump, never in the dispatch: the task job is one ECS env override capped at
260
+ // 8192 chars, which a real manifest outgrows. Scoped server-side like the retraction.
261
+ ...(authzManifest ? {
262
+ stampAuthz: async (key: string) => {
263
+ const res: any = await invokeAction(config.region, opsFn, 'db:backup:stamp-authz', { key, stage, authzManifest });
264
+ if (res?.error) throw new Error(res.error);
265
+ if (!res?.stamped) throw new Error(`the ops function did not confirm the stamp (returned ${JSON.stringify(res)}) — deploy a server build that ships db:backup:stamp-authz`);
266
+ },
267
+ } : {}),
227
268
  onStep: hooks.onStep,
228
269
  };
229
270
  }