@everystack/model 0.4.15 → 0.4.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@everystack/model",
3
- "version": "0.4.15",
3
+ "version": "0.4.16",
4
4
  "description": "Declare a table's schema, validation, and authorization once — compiled to migrations, RLS, and handler config",
5
5
  "license": "AGPL-3.0-only",
6
6
  "publishConfig": {
package/src/can.ts CHANGED
@@ -138,6 +138,80 @@ export function isPublicReadAbility(ability: Ability): boolean {
138
138
  && (!ability.condition.role || ability.condition.role === 'anon');
139
139
  }
140
140
 
141
+ /**
142
+ * Does this ability hand out DELETE?
143
+ *
144
+ * `manage` is "all actions" and compiles to the admin-bypass ALL policy, so it carries DELETE
145
+ * even though nothing spells it. Counting only the literal `delete` verb would leave the
146
+ * soft-delete hazard one grant over — which is exactly the shape `@everystack/auth`'s own
147
+ * user model has: `deletedAt`, no public read, no `can('delete')`, and an admin `manage` that
148
+ * can still destroy the row.
149
+ */
150
+ export function isDeleteAbility(ability: Ability): boolean {
151
+ return ability.action === 'delete' || ability.action === 'manage';
152
+ }
153
+
154
+ /**
155
+ * Does a `privileges` map hand out DELETE?
156
+ *
157
+ * The second spelling of a reachable DELETE, and the one that slipped the first version of
158
+ * this rule. `can()` decides a grant AND a policy together; `privileges` is the key for a
159
+ * grant that has no policy — which is exactly what `db:pull` renders when it finds a live
160
+ * DELETE grant no policy covers. On a `rls: false` table that grant is unrestricted, so the
161
+ * table can be destroyed row by row without a single `can('delete')` in the model.
162
+ *
163
+ * Takes the map rather than a descriptor because both callers already hold one in the same
164
+ * shape (a model's `privileges`, and `db:pull`'s `DerivedAbilities.privileges`), so unlike the
165
+ * ability axis this one needs no second representation and cannot drift at all.
166
+ */
167
+ export function privilegesGrantDelete(privileges: Record<string, readonly string[]> | undefined): boolean {
168
+ return Object.values(privileges ?? {}).some((privs) => privs.includes('DELETE'));
169
+ }
170
+
171
+ /** The three facts the soft-delete rule turns on. See {@link needsSoftDeleteDeclaration}. */
172
+ export interface SoftDeleteFacts {
173
+ /** A `deletedAt` / `deleted_at` field is present. */
174
+ hasDeletedAtField: boolean;
175
+ /** Some ability is a PUBLIC read — see {@link isPublicReadAbility}. */
176
+ hasPublicRead: boolean;
177
+ /**
178
+ * A DELETE can reach this table — via an ability ({@link isDeleteAbility}) or via a
179
+ * `privileges` grant ({@link privilegesGrantDelete}). The SPELLING of the grant was never
180
+ * load-bearing; what matters is that rows can be destroyed.
181
+ */
182
+ grantsDelete: boolean;
183
+ }
184
+
185
+ /**
186
+ * THE rule deciding when `softDelete` must be declared rather than defaulted.
187
+ *
188
+ * `softDelete` changes observable behaviour on TWO axes, and the refusal has to cover both:
189
+ *
190
+ * - **visibility** — on a model with a public read, `true` AND-s `deleted_at IS NULL` into the
191
+ * anon policy. What a caller with no session can see.
192
+ * - **durability** — on ANY model that can be deleted from, `true` makes the handler mark
193
+ * `deleted_at` instead of removing the row. What a DELETE destroys.
194
+ *
195
+ * The guard originally asked only the visibility question, so a model with an owner-scoped
196
+ * read and a `delete` ability sailed past it and silently defaulted to `false`. That is how
197
+ * `uploads` and `image_variants` began hard-deleting rows, orphaning their S3 objects forever
198
+ * — on those models `softDelete: true` adds no policy predicate at all, so visibility was
199
+ * never the axis that mattered.
200
+ *
201
+ * Deliberately NOT "any model with a `deletedAt` field": where nothing reads publicly and
202
+ * nothing deletes, the flag provably changes neither SQL nor handler behaviour, and refusing
203
+ * there would be ceremony charged to every brownfield pull for no hazard.
204
+ *
205
+ * Stated over FACTS, not over a descriptor, because it has two callers holding two
206
+ * representations: `defineModel` has structured {@link Ability} objects, and `db:pull`'s
207
+ * renderer only ever holds rendered source text. Both extract the same three facts and share
208
+ * this one boolean, so the two surfaces cannot drift on the rule itself — the failure that
209
+ * already bit this guard once, when the refusal and the renderer disagreed about `role: 'anon'`.
210
+ */
211
+ export function needsSoftDeleteDeclaration(facts: SoftDeleteFacts): boolean {
212
+ return facts.hasDeletedAtField && (facts.hasPublicRead || facts.grantsDelete);
213
+ }
214
+
141
215
  /** Declare one authorization rule. */
142
216
  export function can(action: Action, condition: AbilityCondition = {}): Ability {
143
217
  return { action, condition };
package/src/index.ts CHANGED
@@ -20,7 +20,8 @@ export type { TableConstraint } from './constraints';
20
20
  export { sql } from './raw-sql';
21
21
  export type { RawSql } from './raw-sql';
22
22
 
23
- export { can, isColumnAbility, isPublicReadAbility } from './can';
23
+ export { can, isColumnAbility, isPublicReadAbility, isDeleteAbility, needsSoftDeleteDeclaration, privilegesGrantDelete } from './can';
24
+ export type { SoftDeleteFacts } from './can';
24
25
  export type { Ability, AbilityCondition, Action } from './can';
25
26
 
26
27
  export { belongsTo, hasMany } from './relations';
package/src/model.ts CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
  import type { FieldBuilder } from './field';
15
15
  import type { Ability } from './can';
16
- import { isPublicReadAbility } from './can';
16
+ import { isPublicReadAbility, isDeleteAbility, needsSoftDeleteDeclaration, privilegesGrantDelete } from './can';
17
17
  import type { Relation } from './relations';
18
18
  import type { TableConstraint } from './constraints';
19
19
  import type { RawSql } from './raw-sql';
@@ -125,9 +125,27 @@ export interface ModelDefinition {
125
125
  */
126
126
  privileges?: Record<string, TablePrivilege[]>;
127
127
  /**
128
- * `true` excludes soft-deleted rows (`deleted_at IS NULL`) from the public read policies
129
- * and from the generic data API. **Required — not inferred — on any model that has a
130
- * `deletedAt` field and a public read.**
128
+ * What a soft delete MEANS on this table. It drives two different machines, and both
129
+ * consequences are real — a model declaring it should expect all of this:
130
+ *
131
+ * - **The generic data API** (`deriveHandlerConfig` → `createHandler`): `DELETE` becomes
132
+ * `UPDATE SET deleted_at = now()` instead of removing the row; and soft-deleted rows are
133
+ * then filtered out of GET (for EVERY role, unless `?deleted=include|only`), out of
134
+ * embedded relation reads, and out of UPDATE. This half applies to every model, whatever
135
+ * its audience.
136
+ * - **RLS**: `deleted_at IS NULL` is AND-ed into the PUBLIC read policies. This half applies
137
+ * only where there is a public read — on an owner-scoped or admin-only model the compiled
138
+ * contract is byte-identical either way (pinned in cli's `public-read-pin.test.ts`).
139
+ *
140
+ * **Required — not inferred — on any model with a `deletedAt` field that either has a public
141
+ * read OR grants DELETE** (`can('delete')`, or `can('manage')`, which is "all actions"). See
142
+ * {@link needsSoftDeleteDeclaration}, the one rule this and `db:pull` share.
143
+ *
144
+ * The guard used to ask only about the public read, which is the visibility axis. That let a
145
+ * model with an owner-scoped read and a `delete` ability default to `false` and begin
146
+ * destroying rows — with an S3 purge job keyed on `deleted_at IS NOT NULL`, that orphaned
147
+ * every file it should have cleaned up. On those models the flag adds no policy at all, so
148
+ * visibility was never the axis that mattered.
131
149
  *
132
150
  * It used to be inferred from the field's mere presence. A column named `deleted_at` is
133
151
  * not a statement of visibility intent: it is carried for audit, for app-layer filtering,
@@ -345,9 +363,12 @@ export function defineModel(table: string, def: ModelDefinition): ModelDescripto
345
363
  // The soft-delete guard used to be inferred from the field's presence, which meant a model
346
364
  // could gain — or, once the inference was removed, silently LOSE — a predicate that decides
347
365
  // what anonymous users can see, with nothing in any diff to review. Neither default is safe,
348
- // so there is no default: on the exact set of models where the answer changes behaviour (a
349
- // `deletedAt` field plus a public read), it must be declared. `softDelete: true` is what the
350
- // inference used to do.
366
+ // so there is no default: on the exact set of models where the answer changes behaviour, it
367
+ // must be declared. `softDelete: true` is what the inference used to do.
368
+ //
369
+ // That set is NOT "has a public read". It is the union of two axes — see
370
+ // `needsSoftDeleteDeclaration`, which is the whole rule and is shared with db:pull so the two
371
+ // surfaces cannot drift on it.
351
372
  // `isPublicReadAbility`, not a local copy: this refusal and db:pull's renderer were asking
352
373
  // the same question — "can a caller with no session see these rows?" — and answering it
353
374
  // differently. The renderer already counted `role: 'anon'`; this refusal did not, so a table
@@ -356,15 +377,31 @@ export function defineModel(table: string, def: ModelDefinition): ModelDescripto
356
377
  // spelled. Explicit `softDelete: false` remains the one-line answer, and pull writes that
357
378
  // line into every model it renders, so a pulled checkout keeps loading.
358
379
  const hasPublicRead = (def.abilities ?? []).some(isPublicReadAbility);
380
+ // Either spelling of a reachable DELETE. `privileges` matters most on an `rls: false` table,
381
+ // where the grant is unrestricted and no policy stands between it and every row.
382
+ const grantsDelete = (def.abilities ?? []).some(isDeleteAbility) || privilegesGrantDelete(def.privileges);
359
383
  // Both spellings of the field key. The guard used to test `'deletedAt' in def.fields`
360
384
  // literally, so a model keying the column `deleted_at` — the shape a snake_case brownfield
361
385
  // schema pulls as — carried a soft-delete column past a guard that never looked at it.
362
386
  const softDeleteField = 'deletedAt' in def.fields || 'deleted_at' in def.fields;
363
- if (softDeleteField && hasPublicRead && def.softDelete === undefined) {
387
+ if (needsSoftDeleteDeclaration({ hasDeletedAtField: softDeleteField, hasPublicRead, grantsDelete }) && def.softDelete === undefined) {
388
+ // DURABILITY FIRST. The old message called this "a visibility decision", which is false
389
+ // for exactly the models that broke: with no public read, `softDelete: true` adds no
390
+ // policy predicate at all. Naming the wrong consequence teaches the wrong fix, so each
391
+ // axis is stated only when it actually fired.
392
+ const why = grantsDelete
393
+ ? `grants DELETE (via can('delete'), can('manage') — which is "all actions" — or a privileges DELETE grant), `
394
+ + `so it must declare softDelete. `
395
+ + `This is a durability decision: softDelete: true makes a DELETE mark deleted_at instead of destroying the row; `
396
+ + `softDelete: false makes DELETE permanent.`
397
+ : `has a public read, so it must declare softDelete.`;
398
+ const visibility = hasPublicRead
399
+ ? ` It is also a visibility decision: softDelete: true excludes soft-deleted rows from the public read policies `
400
+ + `(what the compiler used to infer from the field's presence); softDelete: false leaves them readable.`
401
+ : '';
364
402
  throw new Error(
365
- `model '${table}': has a 'deletedAt' field and a public read, so it must declare softDelete. `
366
- + `softDelete: true excludes soft-deleted rows from the public read policies (what the compiler used to infer from the field's presence); `
367
- + `softDelete: false leaves them readable. This is a visibility decision, so it is declared, not guessed.`,
403
+ `model '${table}': has a 'deletedAt' field and ${why}${visibility} `
404
+ + `What a DELETE destroys is declared, not guessed — add softDelete: true or softDelete: false.`,
368
405
  );
369
406
  }
370
407