@filelayer/core 0.3.0

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.
Files changed (69) hide show
  1. package/CHANGELOG.md +338 -0
  2. package/LICENSE +202 -0
  3. package/MIGRATIONS.md +328 -0
  4. package/NOTICE +37 -0
  5. package/README.md +343 -0
  6. package/SEMANTICS.md +729 -0
  7. package/dist/authz.d.ts +524 -0
  8. package/dist/authz.d.ts.map +1 -0
  9. package/dist/authz.js +889 -0
  10. package/dist/authz.js.map +1 -0
  11. package/dist/db.d.ts +145 -0
  12. package/dist/db.d.ts.map +1 -0
  13. package/dist/db.js +217 -0
  14. package/dist/db.js.map +1 -0
  15. package/dist/delivery.d.ts +293 -0
  16. package/dist/delivery.d.ts.map +1 -0
  17. package/dist/delivery.js +519 -0
  18. package/dist/delivery.js.map +1 -0
  19. package/dist/errors.d.ts +16 -0
  20. package/dist/errors.d.ts.map +1 -0
  21. package/dist/errors.js +21 -0
  22. package/dist/errors.js.map +1 -0
  23. package/dist/filelayer.d.ts +542 -0
  24. package/dist/filelayer.d.ts.map +1 -0
  25. package/dist/filelayer.js +1360 -0
  26. package/dist/filelayer.js.map +1 -0
  27. package/dist/index.d.ts +8 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +8 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/simple.d.ts +297 -0
  32. package/dist/simple.d.ts.map +1 -0
  33. package/dist/simple.js +492 -0
  34. package/dist/simple.js.map +1 -0
  35. package/dist/storage.d.ts +269 -0
  36. package/dist/storage.d.ts.map +1 -0
  37. package/dist/storage.js +700 -0
  38. package/dist/storage.js.map +1 -0
  39. package/dist/store.d.ts +432 -0
  40. package/dist/store.d.ts.map +1 -0
  41. package/dist/store.js +862 -0
  42. package/dist/store.js.map +1 -0
  43. package/package.json +77 -0
  44. package/schema.sql +1190 -0
  45. package/src/authz.ts +1398 -0
  46. package/src/db.ts +271 -0
  47. package/src/delivery.ts +737 -0
  48. package/src/errors.ts +24 -0
  49. package/src/filelayer.ts +1836 -0
  50. package/src/index.ts +7 -0
  51. package/src/simple.ts +666 -0
  52. package/src/storage.ts +917 -0
  53. package/src/store.ts +1072 -0
  54. package/test/delivery.test.ts +0 -0
  55. package/test/group-subjects.test.ts +1072 -0
  56. package/test/helpers.ts +65 -0
  57. package/test/listing.test.ts +689 -0
  58. package/test/local-s3.d.mts +33 -0
  59. package/test/local-s3.mjs +400 -0
  60. package/test/persistence.test.ts +953 -0
  61. package/test/regression.test.ts +619 -0
  62. package/test/s3-live.test.ts +322 -0
  63. package/test/security.test.ts +1652 -0
  64. package/test/semantics.test.ts +888 -0
  65. package/test/storage.test.ts +437 -0
  66. package/test/tiers.test.ts +432 -0
  67. package/test/vault-example.test.ts +302 -0
  68. package/tsconfig.build.json +29 -0
  69. package/tsconfig.json +19 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AAEH,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAoFrD,4EAA4E;AAC5E,MAAM,UAAU,UAAU,CACxB,CAAiB;IAEjB,OAAO,OAAO,CAAC,CAAC,UAAU,KAAK,UAAU,CAAC;AAC5C,CAAC;AAED,MAAM,UAAU,OAAO,CACrB,CAAiB;IAEjB,OAAO,OAAO,CAAC,CAAC,IAAI,KAAK,UAAU,CAAC;AACtC,CAAC;AAED,gFAAgF;AAChF,iBAAiB;AACjB,gFAAgF;AAEhF,yDAAyD;AACzD,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,MAAkC,EAClC,KAAK,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI;IAEzB,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,CAAC;IAClC,MAAM,MAAM,GAAiB,EAAE,CAAC;I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IAAI,CAAC;IAC/B,IAAI,OAAO,GAAkB,IAAI,CAAC;IAClC,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;QAC9B,IAAI,GAAG,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC5B,OAAO,GAAG,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IACpC,CAAC;IAAC,MAAM,CAAC;QACP,sCAAsC;IACxC,CAAC;IACD,MAAM,GAAG,GAAG,IAAI,KAAK,CACnB,WAAW,EAAE,YAAY,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC,OAAO,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CACjG,CAAC;IACD,GAA2D,CAAC,MAAM,GAAG,IAAI,CAAC;IAC1E,GAA2D,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;IACjF,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,432 @@
1
+ /**
2
+ * STORE LAYER
3
+ *
4
+ * Implements `AuthzDeps` (see authz.ts) against PostgreSQL.
5
+ *
6
+ * The authorization grant lookups below MUST read through the `live_grant`
7
+ * view, never `file_grant`. The liveness predicate -- not revoked, not expired,
8
+ * under cap, AND the same for every ancestor of the grant -- is expressed
9
+ * exactly once, in the schema, and every caller inherits it. If a future query
10
+ * here reaches for `file_grant` directly it has silently opted out of
11
+ * revocation, expiry and delegation control, which is P4, which is the product.
12
+ * The view is the enforcement point, not a convenience.
13
+ *
14
+ * The one deliberate exception is `findGrantBySecret`, which reads
15
+ * `file_grant`. It exists so that a denial can be ATTRIBUTED and EXPLAINED in
16
+ * the audit log, and the engine never uses its result to allow anything. It is
17
+ * named to make that obvious and it is the only such query in the file.
18
+ *
19
+ * ------------------------------------------------------------------------
20
+ * TRUST BOUNDARY. EVERY METHOD ON `PostgresStore` IS UNAUTHORIZED.
21
+ * ------------------------------------------------------------------------
22
+ * This class is the engine's DEPENDENCY surface (`AuthzDeps`), not an API.
23
+ * `getFile`, `getActorGrants`, `listAuthorizedFiles`, `listAudit`,
24
+ * `verifyAuditChain` and `consumeDownload` all take resource ids and no
25
+ * principal, because the engine has already established standing before it
26
+ * calls them -- that is the division of labour. They are the same shape as the
27
+ * defect fixed in `Filelayer.getFileRecord`, and they are safe only because
28
+ * they are not reachable from the developer's surface.
29
+ *
30
+ * The rule that keeps that true, stated so it can be enforced in review:
31
+ * `@filelayer/sdk` -- the only thing a customer touches in a hosted deployment
32
+ * -- MUST NOT re-export `PostgresStore`, `Filelayer.store`, or `store.db`. A
33
+ * developer holding `store.db` holds arbitrary SQL over every tenant in the
34
+ * hosted database, which is precisely the connection a hosted deployment exists
35
+ * to take away from them.
36
+ */
37
+ import type { AuditInput, AuthzDeps, Capability, FileRef, GrantRow, ListedFile, ListQuery, OrgRole } from './authz.ts';
38
+ import type { Queryable } from './db.ts';
39
+ import { type MembershipCell } from './authz.ts';
40
+ export interface FileRow extends FileRef {
41
+ name: string;
42
+ contentType: string;
43
+ sizeBytes: number | null;
44
+ storageProvider: string;
45
+ storageKey: string;
46
+ createdAt: Date;
47
+ }
48
+ export interface AuditRow {
49
+ id: number;
50
+ orgId: string | null;
51
+ occurredAt: Date;
52
+ action: string;
53
+ decision: 'allow' | 'deny';
54
+ reason: string | null;
55
+ actorId: string | null;
56
+ fileId: string | null;
57
+ grantId: string | null;
58
+ ip: string | null;
59
+ userAgent: string | null;
60
+ context: Record<string, unknown>;
61
+ prevHash: string | null;
62
+ hash: string;
63
+ }
64
+ /**
65
+ * PGlite returns text[] as a JS array already; node-postgres does too. This
66
+ * guard exists because a raw text round-trip ('{read,write}') would otherwise
67
+ * silently produce a single-element array containing the literal braces, and a
68
+ * capability check against that string would deny everything -- fail closed,
69
+ * but confusingly.
70
+ */
71
+ export declare function toCapabilities(v: Capability[] | string): Capability[];
72
+ /**
73
+ * The project every row belongs to when nobody named one. Mirrors the row
74
+ * inserted by schema.sql; see the PROJECT section there for why it exists.
75
+ */
76
+ export declare const DEFAULT_PROJECT_ID = "00000000-0000-0000-0000-0000000f11e1";
77
+ export interface StoreOptions {
78
+ /**
79
+ * The customer application this store speaks for (P8).
80
+ *
81
+ * In a hosted deployment the API layer resolves a project from the request's
82
+ * API key and constructs a store bound to it. A bound store CANNOT see a
83
+ * file, an org or a listing belonging to any other project: the scope is
84
+ * applied in the SQL, not checked afterwards, so there is no branch to forget.
85
+ *
86
+ * This is defence in depth, not the primary control -- the primary control is
87
+ * the composite foreign keys in schema.sql, which make a cross-project
88
+ * reference unrepresentable. It is here because it also bounds audit-chain
89
+ * flooding: a caller who guesses an org UUID belonging to a DIFFERENT project
90
+ * cannot reach that org's audit chain at all, because `orgExists` returns
91
+ * false and the denial goes to the system chain.
92
+ *
93
+ * `null` means unscoped, which is correct for a single-project deployment and
94
+ * for the control plane. It is not the default.
95
+ */
96
+ projectId?: string | null;
97
+ }
98
+ export declare class PostgresStore implements AuthzDeps {
99
+ readonly db: Queryable;
100
+ /** Null means unscoped. See StoreOptions. */
101
+ readonly projectId: string | null;
102
+ constructor(db: Queryable, opts?: StoreOptions);
103
+ /**
104
+ * The same store, bound to a different connection -- in practice, to an open
105
+ * transaction.
106
+ *
107
+ * This is what puts the audit write in the same transaction as the mutation
108
+ * it records. The engine calls `deps.audit()`; if `deps` is a store bound to
109
+ * the transaction, the event and the mutation commit or roll back together,
110
+ * and `audit_append()`'s advisory lock is held across BOTH rather than across
111
+ * one autocommit statement. `projectId` rides along, so a transactional store
112
+ * cannot accidentally become an unscoped one.
113
+ */
114
+ withDb(db: Queryable): PostgresStore;
115
+ now(): Date;
116
+ getFile(fileId: string): Promise<FileRef | null>;
117
+ /**
118
+ * "Is there an org here that this store may speak about?"
119
+ *
120
+ * A soft-deleted org is not one, and neither is an org whose PROJECT is
121
+ * soft-deleted -- deleting a customer must not leave their tenants
122
+ * addressable. Nor is an org in a different project.
123
+ *
124
+ * The return value decides which audit chain a denial is written to, so this
125
+ * predicate is also the thing that keeps a probe at another project's org id
126
+ * off that org's chain.
127
+ */
128
+ orgExists(orgId: string): Promise<boolean>;
129
+ /**
130
+ * Membership confers nothing when the org, the project, or the ACTOR
131
+ * has been soft-deleted (P7).
132
+ *
133
+ * The actor clause is the one that was missing. `deleted_at` existed on
134
+ * `actor` and nothing read it, so a "deleted" user kept every role-derived
135
+ * capability they had -- which made the column decorative and made the answer
136
+ * to "what does deleting a user do?" depend on which method you asked.
137
+ */
138
+ getMembership(orgId: string, actorId: string): Promise<OrgRole | null>;
139
+ countOwners(orgId: string): Promise<number>;
140
+ /**
141
+ * live_grant only.
142
+ *
143
+ * THE ORDER BY IS LOAD-BEARING, and its absence was a latent defect found
144
+ * while adding the group subject types. `resolveStanding` attributes an allow
145
+ * to the FIRST grant in this result that supplies the requested capability,
146
+ * and on the delegation path that grant becomes the new grant's
147
+ * `parent_grant_id` -- so it decides the ceiling the attenuation trigger will
148
+ * measure the child against. With no ORDER BY, Postgres returned heap order,
149
+ * which is not a defined order at all: it changes with page layout, with
150
+ * VACUUM, and (this is how it surfaced) with the width of the row. A
151
+ * principal holding several grants on one file could therefore have a
152
+ * `share()` call succeed or fail depending on physical storage.
153
+ *
154
+ * Oldest-first makes the choice deterministic and makes it the sensible one:
155
+ * the authority you were given first is the one you delegate from, and it is
156
+ * the same rule `getAnonymousGrant` already applies. See the note on the
157
+ * residual union-vs-parent gap in `authorizeShare`.
158
+ */
159
+ getActorGrants(fileId: string, actorId: string): Promise<GrantRow[]>;
160
+ /**
161
+ * live_grant only. THE GROUP-GRANT PATH (RFC-001).
162
+ *
163
+ * ONE JOIN. Not a fan-out table, not a cached member list, not a
164
+ * materialized view -- a join against `membership`, evaluated on this
165
+ * request. That is the whole implementation of "membership changes must
166
+ * change access on the next request, without recomputation": there is
167
+ * nothing to recompute, because nothing was ever computed. Adding a member
168
+ * makes the join match; removing one makes it stop matching; no `file_grant`
169
+ * row is read, written, or even looked at during either operation. The test
170
+ * that asserts this counts grant rows before and after a join and a leave.
171
+ *
172
+ * The membership half carries the same three liveness clauses `getMembership`
173
+ * applies -- org, project and actor not soft-deleted -- because a group grant
174
+ * must not confer through a membership that confers nothing on its own. The
175
+ * SUBJECT ORG's own deletion is handled one level down, inside
176
+ * `grant_scope_is_live` (I2), so it binds `consume_download` and the
177
+ * attenuation trigger as well and not merely this query.
178
+ *
179
+ * `subject_min_role` is compared against the held role by tuple membership
180
+ * in the DERIVED cell list -- see `membershipCellSql` -- so the threshold is
181
+ * not restated in SQL here or in `listAuthorizedFiles`.
182
+ */
183
+ getGroupGrants(fileId: string, actorId: string): Promise<GrantRow[]>;
184
+ /** live_grant only. Lookup is by hash; the plaintext never reaches the DB. */
185
+ findLiveGrantBySecret(secretHash: string): Promise<GrantRow | null>;
186
+ /**
187
+ * ATTRIBUTION ONLY -- never an authorization input.
188
+ *
189
+ * Reads `file_grant`, so it sees revoked, expired, exhausted and
190
+ * ancestor-dead grants. The engine calls it exclusively on paths that have
191
+ * already denied, in order to name the grant in the audit event and record
192
+ * WHY the link stopped working. Any future use of this result to grant
193
+ * access is a P4 violation and should fail review.
194
+ */
195
+ findGrantBySecret(secretHash: string): Promise<GrantRow | null>;
196
+ isDescendantOf(ancestorId: string, grantId: string): Promise<boolean>;
197
+ /** live_grant only. */
198
+ getAnonymousGrant(fileId: string): Promise<GrantRow | null>;
199
+ /**
200
+ * THE SET FORM OF THE DECISION.
201
+ *
202
+ * One query, one round trip, for the same predicate `authorize()` evaluates
203
+ * per file. Read it against `resolveStanding()` in authz.ts -- the three OR
204
+ * branches are the three sources of authority, in the same order, with the
205
+ * same conditions:
206
+ *
207
+ * 1. org role -> membership x the DERIVED role-cell list
208
+ * 2. actor grant -> live_grant, subject_type='actor', subject_id=caller
209
+ * 3. anonymous -> the EARLIEST live anonymous grant, read only
210
+ *
211
+ * ...and the lifecycle gate is the DERIVED lifecycle-cell list.
212
+ *
213
+ * The role and lifecycle tuples are not written here. They are generated by
214
+ * `listPredicate()` from `fileCapabilities()` and `lifecycleDenial()`, so
215
+ * changing the role model changes this query automatically. The tuples are
216
+ * still validated against the enum vocabulary before interpolation, because a
217
+ * generated string that reaches SQL unchecked is a generated string that can
218
+ * be made to reach SQL unchecked.
219
+ *
220
+ * `now` is passed in rather than read from the database so that the point
221
+ * check and the set query cannot disagree about what time it is. Grant
222
+ * liveness still uses the database clock, via `live_grant`, in BOTH paths.
223
+ *
224
+ * Ordering is (created_at, id) ascending -- total, because id is unique --
225
+ * and pagination is keyset, not OFFSET, so a concurrent insert cannot make a
226
+ * row skip a page.
227
+ */
228
+ listAuthorizedFiles(q: ListQuery): Promise<ListedFile[]>;
229
+ /**
230
+ * Link secrets are 256 bits of CSPRNG output, so they are not guessable and
231
+ * not enumerable; an unsalted SHA-256 is the correct primitive here (it must
232
+ * be deterministic to be indexable, and there is no low-entropy input to
233
+ * protect). Passwords are the opposite case and use scrypt below.
234
+ */
235
+ hashSecret(secret: string): Promise<string>;
236
+ hashPassword(password: string): Promise<string>;
237
+ verifyPassword(password: string, hash: string): Promise<boolean>;
238
+ /**
239
+ * Hash chain. Each event commits to its predecessor in the same chain, where
240
+ * a chain is an org -- or the SYSTEM chain, `org_id IS NULL`, for decisions
241
+ * with no tenant to attribute them to.
242
+ *
243
+ * The digest now covers EVERY forensically relevant column, not the
244
+ * seven it used to cover. `reason`, `grant_id`, `ip`, `user_agent` and
245
+ * `context` are exactly the fields an incident responder relies on and
246
+ * exactly the fields an attacker would rewrite; leaving them outside the
247
+ * commitment made the chain decorative for the questions that matter.
248
+ *
249
+ * JSON encoding rather than string concatenation because concatenation is
250
+ * ambiguous: ('ab','c') and ('a','bc') would hash identically and a forger
251
+ * could shift field boundaries. `context` is canonicalised with sorted keys
252
+ * at every level, because jsonb does not preserve key order and the digest
253
+ * must survive the round trip through the database.
254
+ *
255
+ * CONCURRENCY -- fixed. This used to be a SELECT of the last hash
256
+ * followed by an INSERT, from here, in two statements -- atomic only under a
257
+ * single writer, and a chain FORK under two. It is now one call to
258
+ * `audit_append()`, which takes `pg_advisory_xact_lock` on the chain, reads
259
+ * the predecessor and inserts, all inside one statement and therefore one
260
+ * transaction. See the long comment on that function in schema.sql, including
261
+ * what PGlite can and cannot prove about it.
262
+ *
263
+ * The canonical encoding still lives here and only here: this method computes
264
+ * everything in the digest input EXCEPT the predecessor hash (which it cannot
265
+ * know without racing) and hands it over as `hash_tail`. The database
266
+ * prepends the predecessor it read under the lock. `verifyAuditChain` then
267
+ * recomputes the entire digest in TypeScript on read, so the SQL side and the
268
+ * TypeScript side are checked against each other by every chain verification
269
+ * in the suite.
270
+ *
271
+ * CHAIN FLOODING -- AND WHY IT IS NOT FIXED HERE.
272
+ *
273
+ * An unauthenticated caller who guesses an org UUID can cause denials to be
274
+ * appended to that tenant's chain. Under the per-chain lock above that is
275
+ * worse than storage growth: the tenant's own requests each take the same
276
+ * lock on the way through their audit write, so a flood is a latency attack
277
+ * on that tenant's whole request path, not merely noise in their log. (That
278
+ * interaction is new with the per-chain lock and is worth stating on its own.)
279
+ *
280
+ * It still does not belong in this function:
281
+ *
282
+ * - P5 says every decision is recorded, INCLUDING denials, because denials
283
+ * are the security-relevant events. Any in-engine mitigation is a rule for
284
+ * DROPPING audit events, and a log with a "we stop recording under load"
285
+ * clause is worthless in exactly the incident it exists for.
286
+ * - The engine cannot distinguish a flood from reconnaissance. They are the
287
+ * same request; only the rate differs, and rate is not observable from
288
+ * inside a single decision.
289
+ * - Admission control is the layer that can see it, and it belongs above the
290
+ * engine.
291
+ *
292
+ * WHAT IS FIXED HERE: the blast radius. `orgExists` is project-scoped, so a
293
+ * caller can only reach a tenant chain inside a project they are already
294
+ * authenticated for; probes at every other org id land on the system chain.
295
+ * The exposure goes from "any internet caller can degrade any tenant" to "an
296
+ * authenticated customer can degrade their own tenant" -- a quota question.
297
+ * That reduction is asserted in test/semantics.test.ts.
298
+ *
299
+ * WHAT REMAINS AN OPERATIONAL REQUIREMENT ON THE API LAYER. Stated as a
300
+ * requirement, not a hope, and repeated in SEMANTICS.md:
301
+ *
302
+ * R1. Every request carries a project credential; no project, no engine.
303
+ * R2. Rate limit per (project, source address) BEFORE the engine runs.
304
+ * R3. Cap per-project audit append rate and shed with 429 -- never by
305
+ * dropping a decision that was actually made.
306
+ * R4. Alert on SYSTEM-chain append rate. That chain is where unattributable
307
+ * probes go, so its rate is the enumeration signal.
308
+ */
309
+ audit(event: AuditInput): Promise<void>;
310
+ listAudit(orgId: string | null, filter?: {
311
+ decision?: 'allow' | 'deny';
312
+ fileId?: string;
313
+ actorId?: string;
314
+ action?: string;
315
+ limit?: number;
316
+ }): Promise<AuditRow[]>;
317
+ /**
318
+ * Full chain replay. Returns the first inconsistency found, if any.
319
+ * Pass `null` to verify the system chain.
320
+ */
321
+ verifyAuditChain(orgId: string | null): Promise<AuditChainResult>;
322
+ /**
323
+ * The conditional write *is* the reservation, and it charges the whole
324
+ * ancestor chain (see `consume_download` in schema.sql). Never read the
325
+ * counter and then decide -- see the "naive" comparison in the test suite for
326
+ * what that costs.
327
+ *
328
+ * The function now always returns exactly one row, and this reads
329
+ * `granted === true` rather than defaulting a missing value. Both halves of
330
+ * that had to change: an explicit `(false, 0)` row makes the SQL side
331
+ * unambiguous, and an explicit `=== true` makes the TypeScript side fail
332
+ * closed even if the SQL side is ever replaced with something that returns
333
+ * nothing at all.
334
+ */
335
+ consumeDownload(grantId: string): Promise<{
336
+ granted: boolean;
337
+ remaining: number | null;
338
+ }>;
339
+ recordUsage(orgId: string, kind: 'authz' | 'read' | 'write', bytes?: number): Promise<void>;
340
+ /**
341
+ * Distinct actors who own a file on a given day. `usage_daily` counts events;
342
+ * this counts PEOPLE, which is the quantity authorization load actually
343
+ * tracks -- the graph walk behind every decision grows with the number of
344
+ * distinct holders, not with how many bytes they hold. Idempotent per
345
+ * (org, day, actor).
346
+ */
347
+ recordFileOwner(orgId: string, actorId: string): Promise<void>;
348
+ }
349
+ /**
350
+ * A page is bounded so that one request cannot be turned into an unbounded
351
+ * scan, and so that the single audit event's `fileIds` array stays bounded too.
352
+ */
353
+ export declare const LIST_MAX_LIMIT = 200;
354
+ export declare const LIST_DEFAULT_LIMIT = 50;
355
+ /**
356
+ * `(floor, held role) IN ((...),(...))` -- the group-grant threshold, generated
357
+ * from the derived cells rather than written as `m.role >= g.subject_min_role`.
358
+ *
359
+ * WHY NOT JUST `>=`. Postgres would happily compare two `org_role` values by
360
+ * their DECLARATION ORDER in the enum, and it would be correct today. It would
361
+ * also be a second, independent statement of the role ordering, sitting in SQL,
362
+ * agreeing with `ROLE_RANK` in authz.ts only by coincidence -- and reordering
363
+ * the enum (or inserting a value into the middle of it, which
364
+ * `ALTER TYPE ... ADD VALUE BEFORE` permits) would silently change who can read
365
+ * what, with no test failing. Generating the pairs from `roleMeets()` keeps one
366
+ * definition of the ordering, in TypeScript, where the point check reads it.
367
+ *
368
+ * A `role` grant with no floor is not representable (the CHECK requires one)
369
+ * and an `org` grant's NULL floor reads as 'viewer', which is what the coalesce
370
+ * says.
371
+ *
372
+ * Returns `false` when the cell list is empty, for the same reason
373
+ * `roleCellSql` returns null: `IN ()` is a syntax error, and a generator that
374
+ * crashes on a fail-CLOSED input is a generator someone will "fix".
375
+ */
376
+ export declare function membershipCellSql(cells: readonly MembershipCell[]): string;
377
+ export interface AuditChainResult {
378
+ valid: boolean;
379
+ checked: number;
380
+ brokenAt?: number;
381
+ problem?: 'prev_hash_mismatch' | 'hash_mismatch';
382
+ }
383
+ export interface AuditHashInput {
384
+ prevHash: string | null;
385
+ orgId: string | null;
386
+ occurredAt: Date;
387
+ action: string;
388
+ decision: string;
389
+ actorId: string | null;
390
+ fileId: string | null;
391
+ reason?: string | null;
392
+ grantId?: string | null;
393
+ ip?: string | null;
394
+ userAgent?: string | null;
395
+ context?: Record<string, unknown> | null;
396
+ }
397
+ /**
398
+ * Canonical JSON: object keys sorted at every depth, so a value that has been
399
+ * through jsonb (which does not preserve insertion order) hashes identically to
400
+ * the value that was written.
401
+ */
402
+ export declare function canonicalJson(v: unknown): unknown;
403
+ /**
404
+ * Everything in the digest input EXCEPT the predecessor hash, as the tail of a
405
+ * JSON array -- i.e. `orgId,...,context]`, with no leading bracket.
406
+ *
407
+ * This split is what lets `audit_append()` (schema.sql) take the chain lock,
408
+ * read the predecessor and compute the digest in ONE statement without any of
409
+ * the canonical encoding being duplicated in SQL. The database performs one
410
+ * concatenation:
411
+ *
412
+ * '[' || to_json(prev_hash) || ',' || tail
413
+ *
414
+ * `prevHash` is the first element of the array for exactly this reason, and
415
+ * moving it would silently break the chain -- which is why the ordering is
416
+ * asserted by a test rather than left to a comment.
417
+ */
418
+ export declare function auditHashTail(input: Omit<AuditHashInput, 'prevHash'>): string;
419
+ export declare function auditHash(input: AuditHashInput): string;
420
+ export declare function isUuid(v: unknown): v is string;
421
+ /**
422
+ * Accept only what `inet` will take back out unchanged.
423
+ *
424
+ * Two reasons this is not paranoia. First, `host(inet)` is what the chain
425
+ * verifier reads, so any value Postgres would normalise (a CIDR suffix, for
426
+ * instance) would silently break tamper-evidence for every subsequent event.
427
+ * Second, an unparseable address would raise on INSERT, and since the audit
428
+ * write is on the critical path of every request, that turns a malformed
429
+ * `X-Forwarded-For` header into an outage.
430
+ */
431
+ export declare function normalizeIp(v: string | null | undefined): string | null;
432
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAIH,OAAO,KAAK,EACV,UAAU,EACV,SAAS,EACT,UAAU,EACV,OAAO,EAEP,QAAQ,EAER,UAAU,EAEV,SAAS,EACT,OAAO,EACR,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,OAAO,EAAmB,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;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