@formancy/server 0.3.0 → 0.5.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.
@@ -1,1504 +0,0 @@
1
- import { createHash, randomBytes, randomUUID } from "node:crypto";
2
- import { auditedBy, authenticateApiKey, authenticateLocal, can, collectAbandonedFiles, createApiKey, createLocalUser, createSubmission, decodeSolution, exportCsv, healthOf, listForms, listSubmissions, listVersions, mintChallenge, offerUpload, publishForm, replayDelivery, resolveForm, resumeDraft, saveDraft, setFormAccess, startDraft, verifySolution } from "@formancy/server-core";
3
- import Fastify from "fastify";
4
- import rateLimit from "@fastify/rate-limit";
5
- import { hash, verify } from "@node-rs/argon2";
6
- import { SignJWT, jwtVerify } from "jose";
7
- import { and, desc, eq, inArray, lt, lte, max, ne } from "drizzle-orm";
8
- import { drizzle } from "drizzle-orm/postgres-js";
9
- import { integer, jsonb, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
10
- import { createReadStream } from "node:fs";
11
- import { mkdir, rm, stat, writeFile } from "node:fs/promises";
12
- import { dirname, join, resolve, sep } from "node:path";
13
- //#region src/headers.ts
14
- /**
15
- * The header a client declares what it was looking at with.
16
- *
17
- * One name, read by the submission route and by the publish plugin: a
18
- * submission says which version it rendered, and a publish says which version it
19
- * opened. Two spellings of one header is a bug nobody sees until a client sends
20
- * the other one.
21
- */
22
- const SCHEMA_HASH_HEADER = "x-formancy-schema-hash";
23
- //#endregion
24
- //#region src/routes/publish.ts
25
- /**
26
- * Publishing a form, as a plugin of its own.
27
- *
28
- * One route family per plugin, which is Fastify's own unit and what `app.ts`'s
29
- * size budget named as the seam. It moved when the budget refused the next thing
30
- * added to it rather than letting the number drift.
31
- */
32
- async function publishRoutes(app, { deps, requires }) {
33
- app.post("/forms", { preHandler: requires("form.publish") }, async (request, reply) => {
34
- const body = request.body;
35
- if (body === null || typeof body.path !== "string" || body.path === "" || body.schema === void 0) return reply.code(400).send({
36
- error: "bad_request",
37
- message: "Body needs { path, schema }."
38
- });
39
- const basedOn = request.headers[SCHEMA_HASH_HEADER];
40
- const outcome = await publishForm(deps, {
41
- path: body.path,
42
- schema: body.schema,
43
- actor: request.actor,
44
- ...typeof basedOn === "string" && basedOn !== "" ? { basedOnSchemaHash: basedOn } : {}
45
- });
46
- if (!outcome.ok) switch (outcome.kind) {
47
- case "invalid_schema": return reply.code(422).send({
48
- error: outcome.kind,
49
- errors: outcome.errors
50
- });
51
- case "unsafe_pattern": return reply.code(422).send({
52
- error: outcome.kind,
53
- patterns: outcome.patterns
54
- });
55
- case "invalid_logic": return reply.code(422).send({
56
- error: outcome.kind,
57
- message: outcome.message
58
- });
59
- case "unknown_options_source": return reply.code(422).send({
60
- error: outcome.kind,
61
- sources: outcome.sources
62
- });
63
- case "already_published": return reply.code(409).send({
64
- error: outcome.kind,
65
- version: outcome.version
66
- });
67
- case "version_changed": return reply.code(409).send({
68
- error: "FORM_VERSION_CHANGED",
69
- current: outcome.current === void 0 ? void 0 : {
70
- version: outcome.current.version,
71
- schemaHash: outcome.current.schemaHash,
72
- schema: outcome.current.schema
73
- }
74
- });
75
- }
76
- return reply.code(201).send({
77
- version: outcome.version,
78
- schemaHash: outcome.schemaHash,
79
- ...outcome.warnings.length > 0 ? { warnings: outcome.warnings } : {}
80
- });
81
- });
82
- }
83
- //#endregion
84
- //#region src/auth-runtime.ts
85
- /**
86
- * The real cryptography, confined to the composition root: argon2id for
87
- * secrets at rest, an HMAC-signed short-lived access token for sessions, and
88
- * a CSPRNG for API keys. server-core never sees any of this — it is handed
89
- * hash/verify/random functions and stays deterministic under test.
90
- *
91
- * HS256 rather than EdDSA for the session token, deliberately for this phase:
92
- * one secret in one env var is the self-hosting story a single-container
93
- * deployment can operate, and the signing scheme is an implementation detail
94
- * of a token that lives fifteen minutes. Revisit with key rotation.
95
- */
96
- function realSecretHashing() {
97
- return {
98
- hashSecret: (secret) => hash(secret),
99
- verifySecret: async (secret, digest) => {
100
- try {
101
- return await verify(digest, secret);
102
- } catch {
103
- return false;
104
- }
105
- }
106
- };
107
- }
108
- function realRandomToken() {
109
- return randomBytes(30).toString("base64url");
110
- }
111
- const TOKEN_LIFETIME = "15m";
112
- function createSessionTokens(secret) {
113
- if (secret.length < 32) throw new Error("FORMANCY_AUTH_SECRET must be at least 32 characters. Generate one: openssl rand -base64 33");
114
- const key = new TextEncoder().encode(secret);
115
- return {
116
- async issue(actor) {
117
- return new SignJWT({
118
- role: actor.role,
119
- kind: actor.kind
120
- }).setProtectedHeader({ alg: "HS256" }).setSubject(actor.id).setIssuedAt().setExpirationTime(TOKEN_LIFETIME).sign(key);
121
- },
122
- async verify(token) {
123
- try {
124
- const { payload } = await jwtVerify(token, key, { algorithms: ["HS256"] });
125
- const role = payload["role"];
126
- const kind = payload["kind"];
127
- if (typeof payload.sub !== "string" || role !== "admin" && role !== "editor" && role !== "viewer" || kind !== "user" && kind !== "apiKey") return;
128
- return {
129
- id: payload.sub,
130
- role,
131
- kind
132
- };
133
- } catch {
134
- return;
135
- }
136
- }
137
- };
138
- }
139
- //#endregion
140
- //#region src/app.ts
141
- /** Base64 JSON, the shape an ALTCHA client already produces. */
142
- const CHALLENGE_HEADER = "x-formancy-challenge";
143
- /** Proves the bearer started this draft. Lower-case: Fastify normalises. */
144
- const DRAFT_TOKEN_HEADER = "x-formancy-draft-token";
145
- const API_KEY_HEADER = "x-formancy-api-key";
146
- /**
147
- * The HTTP surface, split into two planes.
148
- *
149
- * PUBLIC — what a person filling a form needs: resolve, submit, drafts. These
150
- * stay unauthenticated by design; anonymous-submission hardening is its own
151
- * upcoming step and is opt-in per form.
152
- *
153
- * MANAGEMENT — everything an operator does: publish, catalog, submissions,
154
- * export, users, keys. Authenticated via a session token (login) or an API
155
- * key header, authorized through server-core's one-table can().
156
- *
157
- * This file is the composition root, so THIS is where ambient reality enters:
158
- * randomUUID, the real clock, argon2 and the CSPRNG. Everything below stays
159
- * injected and replayable.
160
- */
161
- async function createApp(storage, options) {
162
- const app = Fastify({
163
- logger: false,
164
- bodyLimit: options.bodyLimitBytes ?? 262144
165
- });
166
- const maxFileBytes = options.maxFileBytes ?? 10485760;
167
- const fileStore = options.fileStore;
168
- app.addContentTypeParser("*", {
169
- parseAs: "buffer",
170
- bodyLimit: maxFileBytes
171
- }, (_request, body, done) => {
172
- done(null, body);
173
- });
174
- const submissionLimit = options.submissionRateLimit ?? {
175
- max: 30,
176
- timeWindowMs: 6e4
177
- };
178
- const loginLimit = options.loginRateLimit ?? {
179
- max: 10,
180
- timeWindowMs: 6e4
181
- };
182
- const challengeSecret = options.challengeSecret;
183
- await app.register(rateLimit, {
184
- global: false,
185
- max: submissionLimit.max,
186
- timeWindow: submissionLimit.timeWindowMs
187
- });
188
- const deps = {
189
- storage,
190
- newId: () => randomUUID(),
191
- nowIso: () => (/* @__PURE__ */ new Date()).toISOString(),
192
- draftSecret: options.authSecret,
193
- ...options.optionsSources === void 0 ? {} : { optionsSources: options.optionsSources },
194
- capabilities: {
195
- now: () => Date.now(),
196
- today: () => (/* @__PURE__ */ new Date()).toISOString().slice(0, 10),
197
- random: () => Math.random()
198
- }
199
- };
200
- const authDeps = {
201
- storage,
202
- newId: deps.newId,
203
- nowIso: deps.nowIso,
204
- randomToken: realRandomToken,
205
- ...realSecretHashing()
206
- };
207
- const sessions = createSessionTokens(options.authSecret);
208
- if (options.bootstrapAdmin !== void 0) {
209
- if (await storage.getUserByEmail(options.bootstrapAdmin.email) === void 0) {
210
- const outcome = await createLocalUser(authDeps, {
211
- ...options.bootstrapAdmin,
212
- role: "admin"
213
- });
214
- if (!outcome.ok) throw new Error(`Bootstrap admin rejected: ${outcome.message}`);
215
- }
216
- }
217
- async function actorOf(request) {
218
- const bearer = request.headers.authorization;
219
- if (typeof bearer === "string" && bearer.startsWith("Bearer ")) return sessions.verify(bearer.slice(7));
220
- const apiKey = request.headers[API_KEY_HEADER];
221
- if (typeof apiKey === "string" && apiKey !== "") {
222
- const outcome = await authenticateApiKey(authDeps, apiKey);
223
- return outcome.ok ? outcome.actor : void 0;
224
- }
225
- }
226
- /** 401 without an identity, 403 with one that lacks the permission — the
227
- * difference tells a client whether to log in or to give up. */
228
- function requires(action) {
229
- return async (request, reply) => {
230
- const actor = await actorOf(request);
231
- if (actor === void 0) return reply.code(401).send({ error: "unauthenticated" });
232
- if (!can(actor, action)) return reply.code(403).send({
233
- error: "forbidden",
234
- action
235
- });
236
- request.actor = actor;
237
- };
238
- }
239
- /**
240
- * Append an audit row for something that has already happened.
241
- *
242
- * Never throws. An export that succeeded and an audit row that did not is
243
- * bad; an export that 500s because the audit row failed, after the rows have
244
- * already been written to the response, is worse and helps nobody. So the
245
- * failure is logged loudly at error level and the request stands.
246
- *
247
- * For a mutation that HAS a transaction, do not use this — pass the entry
248
- * into that call so the two commit together. `insertSubmission` is currently
249
- * the only one.
250
- */
251
- async function audit(request, draft) {
252
- const actor = request.actor;
253
- try {
254
- await deps.storage.recordAudit({
255
- id: deps.newId(),
256
- at: deps.nowIso(),
257
- requestId: request.id,
258
- ...auditedBy(actor, draft)
259
- });
260
- } catch (error) {
261
- request.log.error({
262
- err: error,
263
- action: draft.action
264
- }, "audit row could not be written");
265
- }
266
- }
267
- app.post("/auth/login", { config: { rateLimit: {
268
- max: loginLimit.max,
269
- timeWindow: loginLimit.timeWindowMs
270
- } } }, async (request, reply) => {
271
- const body = request.body;
272
- if (body === null || typeof body.email !== "string" || typeof body.password !== "string") return reply.code(400).send({
273
- error: "bad_request",
274
- message: "Body needs { email, password }."
275
- });
276
- const outcome = await authenticateLocal(authDeps, {
277
- email: body.email,
278
- password: body.password
279
- });
280
- if (!outcome.ok) {
281
- await audit(request, {
282
- action: "auth.login.failed",
283
- subject: body.email
284
- });
285
- return reply.code(401).send({ error: "invalid_credentials" });
286
- }
287
- await audit(request, {
288
- action: "auth.login",
289
- subject: body.email,
290
- actorKind: "user",
291
- actorId: outcome.actor.id
292
- });
293
- return reply.send({
294
- token: await sessions.issue(outcome.actor),
295
- role: outcome.actor.role
296
- });
297
- });
298
- app.post("/users", { preHandler: requires("user.create") }, async (request, reply) => {
299
- const body = request.body;
300
- if (body === null || typeof body.email !== "string" || typeof body.password !== "string" || body.role !== "admin" && body.role !== "editor" && body.role !== "viewer") return reply.code(400).send({
301
- error: "bad_request",
302
- message: "Body needs { email, password, role }."
303
- });
304
- const outcome = await createLocalUser(authDeps, {
305
- email: body.email,
306
- password: body.password,
307
- role: body.role
308
- });
309
- if (!outcome.ok) return reply.code(422).send({
310
- error: "rejected",
311
- message: outcome.message
312
- });
313
- return reply.code(201).send({ id: outcome.id });
314
- });
315
- app.post("/api-keys", { preHandler: requires("apiKey.create") }, async (request, reply) => {
316
- const body = request.body;
317
- if (body === null || typeof body.name !== "string" || body.role !== "admin" && body.role !== "editor" && body.role !== "viewer") return reply.code(400).send({
318
- error: "bad_request",
319
- message: "Body needs { name, role }."
320
- });
321
- const outcome = await createApiKey(authDeps, {
322
- name: body.name,
323
- role: body.role
324
- });
325
- if (!outcome.ok) return reply.code(422).send({
326
- error: "rejected",
327
- message: outcome.message
328
- });
329
- return reply.code(201).send({
330
- id: outcome.id,
331
- secret: outcome.secret
332
- });
333
- });
334
- await app.register(publishRoutes, {
335
- deps,
336
- requires
337
- });
338
- app.put("/f/:path/access", { preHandler: requires("form.publish") }, async (request, reply) => {
339
- const { path } = request.params;
340
- const body = request.body;
341
- const submit = body?.submit;
342
- if (submit !== "authenticated" && submit !== "public") return reply.code(400).send({
343
- error: "invalid_access",
344
- message: "submit must be \"authenticated\" or \"public\"."
345
- });
346
- const origins = body?.allowedOrigins;
347
- if (origins !== void 0 && !(Array.isArray(origins) && origins.every((o) => typeof o === "string"))) return reply.code(400).send({
348
- error: "invalid_access",
349
- message: "allowedOrigins must be an array of strings, or absent for no allowlist."
350
- });
351
- if (!(await setFormAccess(deps, {
352
- path,
353
- submit,
354
- ...origins === void 0 ? {} : { allowedOrigins: origins }
355
- })).ok) return reply.code(404).send({ error: "unknown_form" });
356
- await audit(request, {
357
- action: "form.access.changed",
358
- subject: path,
359
- detail: {
360
- submit,
361
- allowedOrigins: origins === void 0 ? "unchanged" : origins.join(" ")
362
- }
363
- });
364
- return reply.code(204).send();
365
- });
366
- /**
367
- * The log itself.
368
- *
369
- * Admin only, and reading it is NOT audited. A log that records its own
370
- * reads grows without bound from a dashboard that polls it, and the entry
371
- * would say nothing an access log does not — the events worth recording
372
- * are the ones that touch somebody else's data.
373
- */
374
- app.get("/audit", { preHandler: requires("user.create") }, async (request, reply) => {
375
- const raw = request.query?.limit;
376
- const asked = typeof raw === "string" ? Number.parseInt(raw, 10) : 100;
377
- const limit = Number.isFinite(asked) ? Math.min(Math.max(asked, 1), 500) : 100;
378
- return reply.send({ entries: await deps.storage.listAudit(limit) });
379
- });
380
- /**
381
- * Webhook health, and the deliveries that died.
382
- *
383
- * The reason the breaker keeps its counters on the row rather than in the
384
- * worker's memory. A self-hoster has no operations team watching a
385
- * dashboard, so a destination refusing deliveries since Tuesday has to be
386
- * answerable from the product — otherwise it is found when somebody asks
387
- * why the CRM has no leads this week.
388
- */
389
- app.get("/webhooks", { preHandler: requires("form.publish") }, async (_request, reply) => {
390
- const now = new Date(deps.nowIso());
391
- const hooks = await deps.storage.listWebhooks();
392
- return reply.send({ webhooks: hooks.map((hook) => healthOf(hook, now)) });
393
- });
394
- app.get("/deliveries/dead", { preHandler: requires("form.publish") }, async (request, reply) => {
395
- const raw = request.query?.limit;
396
- const asked = typeof raw === "string" ? Number.parseInt(raw, 10) : 50;
397
- const limit = Number.isFinite(asked) ? Math.min(Math.max(asked, 1), 200) : 50;
398
- const dead = await deps.storage.deadDeliveries(limit);
399
- return reply.send({ deliveries: dead.map((delivery) => ({
400
- id: delivery.id,
401
- webhookId: delivery.webhookId,
402
- submissionId: delivery.submissionId,
403
- eventId: delivery.eventId,
404
- attempt: delivery.attempt,
405
- lastError: delivery.lastError
406
- })) });
407
- });
408
- app.post("/deliveries/:id/replay", { preHandler: requires("form.publish") }, async (request, reply) => {
409
- const { id } = request.params;
410
- const outcome = await replayDelivery({
411
- storage: deps.storage,
412
- now: () => new Date(deps.nowIso())
413
- }, id);
414
- if (!outcome.ok) return outcome.kind === "unknown" ? reply.code(404).send({ error: "unknown_delivery" }) : reply.code(409).send({
415
- error: "not_dead",
416
- state: outcome.state,
417
- message: "Only a dead delivery can be replayed. A pending one is already queued, and a delivered one would be sent twice."
418
- });
419
- await audit(request, {
420
- action: "delivery.replayed",
421
- subject: id,
422
- detail: { webhookId: outcome.delivery.webhookId }
423
- });
424
- return reply.code(202).send({
425
- id,
426
- state: outcome.delivery.state
427
- });
428
- });
429
- /**
430
- * A puzzle for an anonymous visitor to solve before submitting.
431
- *
432
- * Public because the form is: requiring a session to obtain a challenge for
433
- * an unauthenticated submission would be a circle. It is cheap to mint and
434
- * stateless, so handing one out costs this server a hash.
435
- */
436
- app.get("/f/:path/challenge", { config: { rateLimit: {
437
- max: submissionLimit.max,
438
- timeWindow: submissionLimit.timeWindowMs
439
- } } }, async (request, reply) => {
440
- if (challengeSecret === void 0) return reply.code(404).send({ error: "challenge_not_enabled" });
441
- const { path } = request.params;
442
- if (await deps.storage.getFormByPath(path) === void 0) return reply.code(404).send({ error: "unknown_form" });
443
- return reply.send(await mintChallenge({
444
- secret: challengeSecret,
445
- nowSeconds: Math.floor(new Date(deps.nowIso()).getTime() / 1e3)
446
- }));
447
- });
448
- /**
449
- * Check a submitted solution, and spend it.
450
- *
451
- * Returns the refusal to send, or undefined when it passed. The spend is
452
- * separate from the verification and comes last: the stateless checks say
453
- * the solution is correct, and only the database can say it has not been
454
- * used — a correct solution stays correct, so without this one puzzle
455
- * would buy a thousand submissions.
456
- */
457
- async function checkChallenge(request) {
458
- const header = request.headers[CHALLENGE_HEADER];
459
- if (typeof header !== "string" || header === "") return {
460
- error: "challenge_required",
461
- message: `This form needs a solved challenge. Ask GET /f/:path/challenge for one and return it in ${CHALLENGE_HEADER}.`
462
- };
463
- const solution = decodeSolution(header);
464
- if (solution === void 0) return {
465
- error: "challenge_malformed",
466
- message: "The challenge header is not base64 JSON."
467
- };
468
- const nowSeconds = Math.floor(new Date(deps.nowIso()).getTime() / 1e3);
469
- const verified = await verifySolution(challengeSecret, solution, nowSeconds);
470
- if (!verified.ok) return {
471
- error: `challenge_${verified.reason}`,
472
- message: "The challenge was not solved. Ask for a new one and try again."
473
- };
474
- if (!await deps.storage.spendChallenge(verified.challenge, (/* @__PURE__ */ new Date(verified.expiresAtSeconds * 1e3)).toISOString())) return {
475
- error: "challenge_spent",
476
- message: "That challenge has already been used. Each one is good for one submission."
477
- };
478
- }
479
- app.get("/forms", { preHandler: requires("form.read") }, async (_request, reply) => {
480
- return reply.send({ forms: await listForms(deps) });
481
- });
482
- app.get("/f/:path/versions", { preHandler: requires("form.read") }, async (request, reply) => {
483
- const { path } = request.params;
484
- const versions = await listVersions(deps, path);
485
- if (versions === void 0) return reply.code(404).send({ error: "unknown_form" });
486
- return reply.send({ versions });
487
- });
488
- app.get("/f/:path/submissions", { preHandler: requires("submission.read") }, async (request, reply) => {
489
- const { path } = request.params;
490
- const listed = await listSubmissions(deps, path);
491
- if (listed === void 0) return reply.code(404).send({ error: "unknown_form" });
492
- await audit(request, {
493
- action: "submission.read",
494
- subject: path,
495
- detail: { count: listed.length }
496
- });
497
- return reply.send({ submissions: listed });
498
- });
499
- app.get("/f/:path/submissions/export.csv", { preHandler: requires("submission.export") }, async (request, reply) => {
500
- const { path } = request.params;
501
- const csv = await exportCsv(deps, path);
502
- if (csv === void 0) return reply.code(404).send({ error: "unknown_form" });
503
- await audit(request, {
504
- action: "submission.exported",
505
- subject: path,
506
- detail: { bytes: Buffer.byteLength(csv, "utf8") }
507
- });
508
- return reply.header("content-type", "text/csv; charset=utf-8").header("content-disposition", `attachment; filename="${path}-submissions.csv"`).send(csv);
509
- });
510
- /**
511
- * Fetch a published form. The one public route deliberately NOT rate-limited.
512
- *
513
- * Every other unauthenticated route is: submitting, starting a draft, writing
514
- * one, reading one, minting a challenge, asking for an upload target. Those are
515
- * writes, or guesses, or work somebody can demand of the server. This is the
516
- * read every visitor has to make before they can do anything at all.
517
- *
518
- * Limiting it by IP would refuse the form to real people sharing an address —
519
- * an office, a school, a phone network behind CGNAT — and the failure would
520
- * look like the form being broken rather than like a limit. That is a worse
521
- * outcome than the cheap read it would prevent, and the read is cached by
522
- * `schemaHash` anyway.
523
- *
524
- * Written down because the asymmetry is deliberate and looks like an omission.
525
- */
526
- app.get("/f/:path", async (request, reply) => {
527
- const { path } = request.params;
528
- const resolved = await resolveForm(deps, path);
529
- if (resolved === void 0) return reply.code(404).send({ error: "unknown_form" });
530
- return reply.send({
531
- version: resolved.version,
532
- schemaHash: resolved.schemaHash,
533
- schema: resolved.schema
534
- });
535
- });
536
- /**
537
- * Start a draft, and get the only key to it.
538
- *
539
- * The public plane is anonymous, so a draft has no account behind it. It was
540
- * previously addressed by an id the CALLER chose, with no check on either
541
- * route: anybody who knew or guessed an id could read a stranger's part-filled
542
- * form, and overwrite it — after which that person submits the substituted
543
- * content under their own name and nothing says otherwise
544
- * ([0062](../../../docs/decisions/0062-a-draft-carries-its-own-key.md)).
545
- *
546
- * So the server picks the id and signs it, and the token comes back exactly
547
- * once. Losing it means losing the draft, which is the correct trade for the
548
- * alternative.
549
- */
550
- app.post("/f/:path/drafts", { config: { rateLimit: {
551
- max: submissionLimit.max,
552
- timeWindow: submissionLimit.timeWindowMs
553
- } } }, async (request, reply) => {
554
- const { path } = request.params;
555
- const started = await startDraft(deps, { path });
556
- if (started === void 0) return reply.code(404).send({ error: "unknown_form" });
557
- return reply.code(201).send(started);
558
- });
559
- app.put("/f/:path/drafts/:draftId", { config: { rateLimit: {
560
- max: submissionLimit.max,
561
- timeWindow: submissionLimit.timeWindowMs
562
- } } }, async (request, reply) => {
563
- const { path, draftId } = request.params;
564
- const token = request.headers[DRAFT_TOKEN_HEADER];
565
- if (typeof token !== "string") return reply.code(401).send({ error: "draft_token_required" });
566
- const saved = await saveDraft(deps, {
567
- path,
568
- draftId,
569
- token,
570
- data: request.body ?? {}
571
- });
572
- if (saved === void 0) return reply.code(404).send({ error: "unknown_form" });
573
- if (!saved.saved) return reply.code(403).send({ error: "draft_token_invalid" });
574
- return reply.send({ version: saved.version });
575
- });
576
- app.get("/f/:path/drafts/:draftId", { config: { rateLimit: {
577
- max: submissionLimit.max,
578
- timeWindow: submissionLimit.timeWindowMs
579
- } } }, async (request, reply) => {
580
- const { path, draftId } = request.params;
581
- const token = request.headers[DRAFT_TOKEN_HEADER];
582
- if (typeof token !== "string") return reply.code(401).send({ error: "draft_token_required" });
583
- const resumed = await resumeDraft(deps, {
584
- path,
585
- draftId,
586
- token
587
- });
588
- if (resumed === void 0) return reply.code(404).send({ error: "unknown_draft" });
589
- return reply.send(resumed);
590
- });
591
- /**
592
- * Ask where to put a file.
593
- *
594
- * Refusing here, before a byte is sent, is the difference between rejecting
595
- * a 2 GB upload and receiving one first. The reply carries the id the
596
- * submission will reference and the URL to PUT the bytes to.
597
- */
598
- app.post("/f/:path/files", { config: { rateLimit: {
599
- max: submissionLimit.max,
600
- timeWindow: submissionLimit.timeWindowMs
601
- } } }, async (request, reply) => {
602
- if (fileStore === void 0) return reply.code(501).send({
603
- error: "uploads_unavailable",
604
- message: "This deployment has no file store configured, so it cannot accept uploads."
605
- });
606
- const { path } = request.params;
607
- const resolved = await resolveForm(deps, path);
608
- if (resolved === void 0) return reply.code(404).send({ error: "unknown_form" });
609
- const form = await storage.getFormByPath(path);
610
- if (form === void 0) return reply.code(404).send({ error: "unknown_form" });
611
- const actor = await actorOf(request);
612
- const origin = request.headers.origin;
613
- const body = request.body ?? {};
614
- const field = typeof body.field === "string" ? body.field : void 0;
615
- const name = typeof body.name === "string" ? body.name : void 0;
616
- const size = typeof body.size === "number" ? body.size : void 0;
617
- const contentType = typeof body.contentType === "string" ? body.contentType : "application/octet-stream";
618
- if (field === void 0 || name === void 0 || size === void 0) return reply.code(400).send({
619
- error: "invalid_request",
620
- message: "Send the field, the name and the size of the file you want to upload."
621
- });
622
- if (size > maxFileBytes) return reply.code(413).send({
623
- error: "too_large",
624
- message: `This deployment accepts files up to ${String(maxFileBytes)} bytes.`
625
- });
626
- const outcome = await offerUpload({
627
- storage,
628
- now: () => /* @__PURE__ */ new Date(),
629
- newId: () => randomUUID()
630
- }, {
631
- schema: resolved.schema,
632
- form,
633
- actor: actor === void 0 ? "anonymous" : "authenticated",
634
- origin: typeof origin === "string" ? origin : void 0,
635
- field,
636
- name,
637
- size,
638
- contentType
639
- });
640
- if (!outcome.ok) {
641
- const status = outcome.reason === "not_allowed" ? 403 : outcome.reason === "too_large" ? 413 : 400;
642
- return reply.code(status).send({ error: outcome.reason });
643
- }
644
- return reply.code(201).send({
645
- id: outcome.file.id,
646
- name: outcome.file.name,
647
- size: outcome.file.size,
648
- contentType: outcome.file.contentType,
649
- storageKey: outcome.file.storageKey,
650
- uploadUrl: `/f/${encodeURIComponent(path)}/files/${outcome.file.id}`
651
- });
652
- });
653
- /** Receive the bytes for a file that was offered. */
654
- app.put("/f/:path/files/:id", async (request, reply) => {
655
- if (fileStore === void 0) return reply.code(501).send({ error: "uploads_unavailable" });
656
- const { path, id } = request.params;
657
- const form = await storage.getFormByPath(path);
658
- if (form === void 0) return reply.code(404).send({ error: "unknown_form" });
659
- const file = await storage.getFile(id);
660
- if (file === void 0 || file.formId !== form.id) return reply.code(404).send({ error: "unknown_file" });
661
- if (file.state !== "offered") return reply.code(409).send({ error: "already_uploaded" });
662
- const bytes = request.body;
663
- if (!Buffer.isBuffer(bytes)) return reply.code(400).send({ error: "no_body" });
664
- if (bytes.byteLength > maxFileBytes) return reply.code(413).send({ error: "too_large" });
665
- if (bytes.byteLength !== file.size) return reply.code(400).send({
666
- error: "size_mismatch",
667
- message: `This upload was offered as ${String(file.size)} bytes and ${String(bytes.byteLength)} arrived.`
668
- });
669
- await fileStore.put(file.storageKey, bytes);
670
- await storage.updateFile({
671
- ...file,
672
- state: "stored"
673
- });
674
- return reply.code(204).send();
675
- });
676
- /**
677
- * Serve a file back.
678
- *
679
- * Always as an attachment, never inline, and `nosniff` alongside it. Stored
680
- * cross-site scripting through an uploaded HTML or SVG file is the most
681
- * commonly exploited vulnerability in this product category, and the real
682
- * answer is a separate hostname — which a single-container deployment does
683
- * not have. Forcing a download is the accommodation, and it is the one place
684
- * this deployment model genuinely costs something.
685
- */
686
- app.get("/f/:path/files/:id", async (request, reply) => {
687
- if (fileStore === void 0) return reply.code(501).send({ error: "uploads_unavailable" });
688
- const { path, id } = request.params;
689
- if (await actorOf(request) === void 0) return reply.code(401).send({ error: "unauthenticated" });
690
- const form = await storage.getFormByPath(path);
691
- const file = await storage.getFile(id);
692
- if (form === void 0 || file === void 0 || file.formId !== form.id) return reply.code(404).send({ error: "unknown_file" });
693
- const stream = await fileStore.open(file.storageKey);
694
- if (stream === void 0) return reply.code(404).send({ error: "unknown_file" });
695
- return reply.header("content-type", "application/octet-stream").header("x-content-type-options", "nosniff").header("content-security-policy", "default-src 'none'; sandbox").header("content-disposition", `attachment; filename="${file.name.replace(/["\r\n]/g, "")}"`).send(stream);
696
- });
697
- app.post("/f/:path/submissions", { config: { rateLimit: {
698
- max: submissionLimit.max,
699
- timeWindow: submissionLimit.timeWindowMs
700
- } } }, async (request, reply) => {
701
- const { path } = request.params;
702
- const declaredSchemaHash = request.headers[SCHEMA_HASH_HEADER];
703
- if (typeof declaredSchemaHash !== "string" || declaredSchemaHash === "") return reply.code(400).send({
704
- error: "missing_schema_hash",
705
- message: `Send the schema hash you rendered in the ${SCHEMA_HASH_HEADER} header.`
706
- });
707
- const actor = await actorOf(request);
708
- const origin = request.headers.origin;
709
- if (challengeSecret !== void 0 && actor === void 0) {
710
- const refusal = await checkChallenge(request);
711
- if (refusal !== void 0) return reply.code(400).send(refusal);
712
- }
713
- const outcome = await createSubmission(deps, {
714
- path,
715
- declaredSchemaHash,
716
- data: request.body ?? {},
717
- actor: actor === void 0 ? "anonymous" : "authenticated",
718
- ...typeof origin === "string" ? { origin } : {}
719
- });
720
- if (outcome.ok) return reply.code(201).send({
721
- id: outcome.id,
722
- data: outcome.canonicalData
723
- });
724
- switch (outcome.kind) {
725
- case "unknown_form": return reply.code(404).send({ error: "unknown_form" });
726
- case "version_changed": return reply.code(409).send({
727
- error: "FORM_VERSION_CHANGED",
728
- current: outcome.current === void 0 ? void 0 : {
729
- version: outcome.current.version,
730
- schemaHash: outcome.current.schemaHash,
731
- schema: outcome.current.schema
732
- }
733
- });
734
- case "invalid": return reply.code(422).send({
735
- error: "invalid",
736
- errors: outcome.errors
737
- });
738
- case "forbidden": return reply.code(403).send({ error: "forbidden" });
739
- case "source_unavailable": return reply.code(503).send({
740
- error: "source_unavailable",
741
- source: outcome.source
742
- });
743
- }
744
- });
745
- return app;
746
- }
747
- //#endregion
748
- //#region src/db.ts
749
- /**
750
- * The thin slice's tables. Three rules from the design spec are enforced HERE,
751
- * in the database, because application code can be bypassed and these must not
752
- * be:
753
- *
754
- * - published versions are immutable (a trigger refuses UPDATE),
755
- * - a submission binds to its exact version by real foreign key,
756
- * - that key is ON DELETE RESTRICT, so orphaning a submission from the schema
757
- * that produced it is structurally impossible.
758
- */
759
- const forms = pgTable("forms", {
760
- id: uuid("id").primaryKey(),
761
- path: text("path").notNull().unique(),
762
- currentVersionId: uuid("current_version_id"),
763
- accessSubmit: text("access_submit").notNull().default("authenticated"),
764
- allowedOrigins: text("allowed_origins").array(),
765
- createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow()
766
- });
767
- const formVersions = pgTable("form_versions", {
768
- id: uuid("id").primaryKey(),
769
- formId: uuid("form_id").notNull().references(() => forms.id),
770
- version: integer("version").notNull(),
771
- schema: jsonb("schema").notNull(),
772
- schemaHash: text("schema_hash").notNull(),
773
- createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow()
774
- });
775
- const users = pgTable("users", {
776
- id: uuid("id").primaryKey(),
777
- email: text("email").notNull().unique(),
778
- passwordHash: text("password_hash").notNull(),
779
- role: text("role").notNull(),
780
- createdAt: timestamp("created_at", { withTimezone: true }).notNull()
781
- });
782
- const apiKeys = pgTable("api_keys", {
783
- id: uuid("id").primaryKey(),
784
- name: text("name").notNull(),
785
- prefix: text("prefix").notNull(),
786
- secretHash: text("secret_hash").notNull(),
787
- role: text("role").notNull(),
788
- createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
789
- revokedAt: timestamp("revoked_at", { withTimezone: true })
790
- });
791
- const drafts = pgTable("drafts", {
792
- id: text("id").notNull(),
793
- formId: uuid("form_id").notNull().references(() => forms.id),
794
- formVersionId: uuid("form_version_id").notNull().references(() => formVersions.id),
795
- data: jsonb("data").notNull(),
796
- updatedAt: timestamp("updated_at", { withTimezone: true }).notNull()
797
- });
798
- const webhooks = pgTable("webhooks", {
799
- id: uuid("id").primaryKey(),
800
- formId: uuid("form_id").notNull().references(() => forms.id),
801
- url: text("url").notNull(),
802
- secret: text("secret").notNull(),
803
- consecutiveFailures: integer("consecutive_failures").notNull().default(0),
804
- openedAt: timestamp("opened_at", { withTimezone: true })
805
- });
806
- /**
807
- * The outbox. A row here and the submission that caused it are written by the
808
- * same transaction, which is what makes "accepted" and "will be delivered" one
809
- * decision rather than two.
810
- */
811
- const deliveries = pgTable("deliveries", {
812
- id: uuid("id").primaryKey(),
813
- webhookId: uuid("webhook_id").notNull(),
814
- submissionId: uuid("submission_id").notNull(),
815
- eventId: uuid("event_id").notNull(),
816
- body: text("body").notNull(),
817
- attempt: integer("attempt").notNull().default(0),
818
- nextAttemptAt: timestamp("next_attempt_at", { withTimezone: true }).notNull(),
819
- state: text("state").notNull().default("pending"),
820
- lastError: text("last_error")
821
- });
822
- const spentChallenges = pgTable("spent_challenges", {
823
- challenge: text("challenge").primaryKey(),
824
- expiresAt: timestamp("expires_at", { withTimezone: true }).notNull()
825
- });
826
- const auditLog = pgTable("audit_log", {
827
- id: uuid("id").primaryKey(),
828
- at: timestamp("at", { withTimezone: true }).notNull(),
829
- action: text("action").notNull(),
830
- actorKind: text("actor_kind"),
831
- actorId: text("actor_id"),
832
- subject: text("subject"),
833
- requestId: text("request_id"),
834
- detail: jsonb("detail")
835
- });
836
- const files = pgTable("files", {
837
- id: uuid("id").primaryKey(),
838
- formId: uuid("form_id").notNull(),
839
- name: text("name").notNull(),
840
- size: integer("size").notNull(),
841
- contentType: text("content_type").notNull(),
842
- storageKey: text("storage_key").notNull(),
843
- state: text("state").notNull().default("offered"),
844
- createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
845
- submissionId: uuid("submission_id")
846
- });
847
- const submissions = pgTable("submissions", {
848
- id: uuid("id").primaryKey(),
849
- formId: uuid("form_id").notNull().references(() => forms.id),
850
- formVersionId: uuid("form_version_id").notNull().references(() => formVersions.id, { onDelete: "restrict" }),
851
- data: jsonb("data").notNull(),
852
- submittedAt: timestamp("submitted_at", { withTimezone: true }).notNull()
853
- });
854
- /**
855
- * Bootstrap DDL for the thin slice — drizzle-kit migrations arrive with the
856
- * lifecycle work; a self-hosted skeleton that creates its own tables on first
857
- * start is worth more right now than a migration pipeline.
858
- */
859
- async function bootstrapSchema(sql) {
860
- await sql`
861
- CREATE TABLE IF NOT EXISTS forms (
862
- id uuid PRIMARY KEY,
863
- path text NOT NULL UNIQUE,
864
- current_version_id uuid,
865
- -- Private by default in the DATABASE, not only in the code, so a row
866
- -- inserted by a migration or a fixture is not accidentally open.
867
- access_submit text NOT NULL DEFAULT 'authenticated'
868
- CHECK (access_submit IN ('authenticated', 'public')),
869
- -- NULL: no allowlist in force. Empty array: nothing allowed. Different
870
- -- things, which is why this is nullable rather than defaulting to '{}'.
871
- allowed_origins text[],
872
- created_at timestamptz NOT NULL DEFAULT now()
873
- )`;
874
- await sql`
875
- ALTER TABLE forms
876
- ADD COLUMN IF NOT EXISTS access_submit text NOT NULL DEFAULT 'authenticated'`;
877
- await sql`ALTER TABLE forms ADD COLUMN IF NOT EXISTS allowed_origins text[]`;
878
- await sql`
879
- CREATE TABLE IF NOT EXISTS form_versions (
880
- id uuid PRIMARY KEY,
881
- form_id uuid NOT NULL REFERENCES forms(id),
882
- version integer NOT NULL,
883
- schema jsonb NOT NULL,
884
- schema_hash text NOT NULL,
885
- created_at timestamptz NOT NULL DEFAULT now(),
886
- UNIQUE (form_id, version),
887
- UNIQUE (form_id, schema_hash)
888
- )`;
889
- await sql`
890
- CREATE TABLE IF NOT EXISTS submissions (
891
- id uuid PRIMARY KEY,
892
- form_id uuid NOT NULL REFERENCES forms(id),
893
- form_version_id uuid NOT NULL REFERENCES form_versions(id) ON DELETE RESTRICT,
894
- data jsonb NOT NULL,
895
- submitted_at timestamptz NOT NULL
896
- )`;
897
- await sql`
898
- CREATE TABLE IF NOT EXISTS webhooks (
899
- id uuid PRIMARY KEY,
900
- form_id uuid NOT NULL REFERENCES forms(id) ON DELETE CASCADE,
901
- url text NOT NULL,
902
- secret text NOT NULL
903
- )`;
904
- await sql`
905
- ALTER TABLE webhooks
906
- ADD COLUMN IF NOT EXISTS consecutive_failures integer NOT NULL DEFAULT 0,
907
- -- When the breaker opened, null while the destination is answering. On
908
- -- the row rather than in the worker's memory: memory does not survive a
909
- -- restart, and a self-hoster with no operations team needs a failing
910
- -- webhook to be visible on a screen rather than in a log nobody tails.
911
- ADD COLUMN IF NOT EXISTS opened_at timestamptz`;
912
- await sql`
913
- CREATE TABLE IF NOT EXISTS deliveries (
914
- id uuid PRIMARY KEY,
915
- webhook_id uuid NOT NULL REFERENCES webhooks(id) ON DELETE CASCADE,
916
- -- RESTRICT, not CASCADE: a delivery is the record that something was
917
- -- sent about this submission, and deleting the submission must not
918
- -- quietly erase it.
919
- submission_id uuid NOT NULL REFERENCES submissions(id) ON DELETE RESTRICT,
920
- event_id uuid NOT NULL,
921
- body text NOT NULL,
922
- attempt integer NOT NULL DEFAULT 0,
923
- next_attempt_at timestamptz NOT NULL,
924
- state text NOT NULL DEFAULT 'pending'
925
- CHECK (state IN ('pending', 'delivered', 'dead')),
926
- last_error text
927
- )`;
928
- await sql`
929
- CREATE INDEX IF NOT EXISTS deliveries_due
930
- ON deliveries (next_attempt_at) WHERE state = 'pending'`;
931
- await sql`
932
- CREATE TABLE IF NOT EXISTS files (
933
- id uuid PRIMARY KEY,
934
- form_id uuid NOT NULL REFERENCES forms(id) ON DELETE RESTRICT,
935
- -- What the reader called it. Display only: the storage key is minted by
936
- -- the server, because a key built from a submitted filename is a path
937
- -- traversal waiting for somebody to try it.
938
- name text NOT NULL,
939
- size integer NOT NULL,
940
- content_type text NOT NULL,
941
- storage_key text NOT NULL,
942
- state text NOT NULL DEFAULT 'offered'
943
- CHECK (state IN ('offered', 'stored', 'claimed')),
944
- created_at timestamptz NOT NULL,
945
- -- RESTRICT for the same reason a delivery uses it: the row is the record
946
- -- that something was attached, and deleting the submission must not
947
- -- quietly erase what it carried.
948
- submission_id uuid REFERENCES submissions(id) ON DELETE RESTRICT,
949
- -- One file, one submission. Enforced here rather than in application
950
- -- code, because two concurrent submissions claiming the same file is
951
- -- exactly the race application code loses — and the loser would be a
952
- -- submission referencing bytes another submission owns.
953
- CONSTRAINT files_one_submission UNIQUE (id, submission_id)
954
- )`;
955
- await sql`
956
- CREATE INDEX IF NOT EXISTS files_abandoned
957
- ON files (created_at) WHERE state <> 'claimed'`;
958
- await sql`
959
- CREATE TABLE IF NOT EXISTS users (
960
- id uuid PRIMARY KEY,
961
- email text NOT NULL UNIQUE,
962
- password_hash text NOT NULL,
963
- role text NOT NULL,
964
- created_at timestamptz NOT NULL
965
- )`;
966
- await sql`
967
- CREATE TABLE IF NOT EXISTS api_keys (
968
- id uuid PRIMARY KEY,
969
- name text NOT NULL,
970
- prefix text NOT NULL,
971
- secret_hash text NOT NULL,
972
- role text NOT NULL,
973
- created_at timestamptz NOT NULL,
974
- revoked_at timestamptz
975
- )`;
976
- await sql`CREATE INDEX IF NOT EXISTS api_keys_prefix ON api_keys (prefix)`;
977
- await sql`
978
- CREATE TABLE IF NOT EXISTS spent_challenges (
979
- -- The primary key IS the mechanism. Two requests arriving with one
980
- -- solution both pass every stateless check — the signature is ours
981
- -- and the hash is right — and only the database can decide which of
982
- -- them spends it. A read-then-write would race exactly where somebody
983
- -- is looking.
984
- challenge text PRIMARY KEY,
985
- expires_at timestamptz NOT NULL
986
- )`;
987
- await sql`CREATE INDEX IF NOT EXISTS spent_challenges_expiry ON spent_challenges (expires_at)`;
988
- await sql`
989
- CREATE TABLE IF NOT EXISTS audit_log (
990
- id uuid PRIMARY KEY,
991
- at timestamptz NOT NULL,
992
- action text NOT NULL,
993
- -- Null for an anonymous public submission, which is an answer rather
994
- -- than a gap: the form was open and nobody was signed in.
995
- actor_kind text CHECK (actor_kind IN ('user', 'apiKey')),
996
- actor_id text,
997
- subject text,
998
- request_id text,
999
- -- Identifiers and counts. NEVER a submission's answers: an audit log is
1000
- -- read by more people and kept longer than the data it describes, so
1001
- -- data inside it is a second copy of the thing being protected.
1002
- detail jsonb
1003
- )`;
1004
- await sql`CREATE INDEX IF NOT EXISTS audit_log_at ON audit_log (at DESC)`;
1005
- await sql`CREATE INDEX IF NOT EXISTS audit_log_subject ON audit_log (subject, at DESC)`;
1006
- await sql`
1007
- CREATE TABLE IF NOT EXISTS drafts (
1008
- id text NOT NULL,
1009
- form_id uuid NOT NULL REFERENCES forms(id),
1010
- form_version_id uuid NOT NULL REFERENCES form_versions(id),
1011
- data jsonb NOT NULL,
1012
- updated_at timestamptz NOT NULL,
1013
- PRIMARY KEY (form_id, id)
1014
- )`;
1015
- await sql`
1016
- CREATE OR REPLACE FUNCTION refuse_version_update() RETURNS trigger AS $$
1017
- BEGIN
1018
- RAISE EXCEPTION 'form_versions rows are immutable; publish a new version instead';
1019
- END
1020
- $$ LANGUAGE plpgsql`;
1021
- await sql`DROP TRIGGER IF EXISTS form_versions_immutable ON form_versions`;
1022
- await sql`
1023
- CREATE TRIGGER form_versions_immutable
1024
- BEFORE UPDATE ON form_versions
1025
- FOR EACH ROW EXECUTE FUNCTION refuse_version_update()`;
1026
- await sql`
1027
- CREATE OR REPLACE FUNCTION refuse_audit_change() RETURNS trigger AS $$
1028
- BEGIN
1029
- RAISE EXCEPTION 'audit_log is append-only';
1030
- END
1031
- $$ LANGUAGE plpgsql`;
1032
- await sql`DROP TRIGGER IF EXISTS audit_log_append_only ON audit_log`;
1033
- await sql`
1034
- CREATE TRIGGER audit_log_append_only
1035
- BEFORE UPDATE OR DELETE ON audit_log
1036
- FOR EACH ROW EXECUTE FUNCTION refuse_audit_change()`;
1037
- }
1038
- //#endregion
1039
- //#region src/postgres-storage.ts
1040
- /** The Storage port over Postgres — the mirror of server-core's in-memory one. */
1041
- function createPostgresStorage(sql) {
1042
- const db = drizzle(sql);
1043
- return {
1044
- async getFormByPath(path) {
1045
- const row = (await db.select().from(forms).where(eq(forms.path, path)).limit(1))[0];
1046
- if (row === void 0) return void 0;
1047
- return {
1048
- id: row.id,
1049
- path: row.path,
1050
- currentVersionId: row.currentVersionId,
1051
- accessSubmit: row.accessSubmit === "public" ? "public" : "authenticated",
1052
- allowedOrigins: row.allowedOrigins
1053
- };
1054
- },
1055
- async listForms() {
1056
- return (await db.select().from(forms)).map((row) => ({
1057
- id: row.id,
1058
- path: row.path,
1059
- currentVersionId: row.currentVersionId,
1060
- accessSubmit: row.accessSubmit === "public" ? "public" : "authenticated",
1061
- allowedOrigins: row.allowedOrigins
1062
- }));
1063
- },
1064
- async createForm(record) {
1065
- await db.insert(forms).values({
1066
- id: record.id,
1067
- path: record.path,
1068
- currentVersionId: record.currentVersionId,
1069
- accessSubmit: record.accessSubmit,
1070
- allowedOrigins: record.allowedOrigins
1071
- });
1072
- },
1073
- async updateFormAccess(formId, access) {
1074
- await db.update(forms).set({
1075
- accessSubmit: access.accessSubmit,
1076
- allowedOrigins: access.allowedOrigins
1077
- }).where(eq(forms.id, formId));
1078
- },
1079
- async setCurrentVersion(formId, versionId) {
1080
- await db.update(forms).set({ currentVersionId: versionId }).where(eq(forms.id, formId));
1081
- },
1082
- async insertVersion(record) {
1083
- await db.insert(formVersions).values({
1084
- id: record.id,
1085
- formId: record.formId,
1086
- version: record.version,
1087
- schema: record.schema,
1088
- schemaHash: record.schemaHash
1089
- });
1090
- },
1091
- async publishVersion({ form, version, audit }) {
1092
- await db.transaction(async (tx) => {
1093
- if (form !== void 0) await tx.insert(forms).values({
1094
- id: form.id,
1095
- path: form.path,
1096
- currentVersionId: form.currentVersionId,
1097
- accessSubmit: form.accessSubmit,
1098
- allowedOrigins: form.allowedOrigins
1099
- });
1100
- await tx.insert(formVersions).values({
1101
- id: version.id,
1102
- formId: version.formId,
1103
- version: version.version,
1104
- schema: version.schema,
1105
- schemaHash: version.schemaHash
1106
- });
1107
- if ((await tx.update(forms).set({ currentVersionId: version.id }).where(eq(forms.id, version.formId)).returning({ id: forms.id })).length !== 1) throw new Error(`Publishing to form ${version.formId} matched no form row. Rolling back, so there is no version pointing at a form that is not there.`);
1108
- if (audit !== void 0) await tx.insert(auditLog).values(auditRow(audit));
1109
- });
1110
- },
1111
- async getVersionById(id) {
1112
- const row = (await db.select().from(formVersions).where(eq(formVersions.id, id)).limit(1))[0];
1113
- if (row === void 0) return void 0;
1114
- return {
1115
- id: row.id,
1116
- formId: row.formId,
1117
- version: row.version,
1118
- schema: row.schema,
1119
- schemaHash: row.schemaHash
1120
- };
1121
- },
1122
- async findVersionByHash(formId, schemaHash) {
1123
- const row = (await db.select().from(formVersions).where(eq(formVersions.formId, formId))).find((candidate) => candidate.schemaHash === schemaHash);
1124
- if (row === void 0) return void 0;
1125
- return {
1126
- id: row.id,
1127
- formId: row.formId,
1128
- version: row.version,
1129
- schema: row.schema,
1130
- schemaHash: row.schemaHash
1131
- };
1132
- },
1133
- async latestVersionNumber(formId) {
1134
- return (await db.select({ latest: max(formVersions.version) }).from(formVersions).where(eq(formVersions.formId, formId)))[0]?.latest ?? 0;
1135
- },
1136
- async insertSubmission(record, queued, claimFileIds, audit) {
1137
- await db.transaction(async (tx) => {
1138
- await tx.insert(submissions).values({
1139
- id: record.id,
1140
- formId: record.formId,
1141
- formVersionId: record.formVersionId,
1142
- data: record.data,
1143
- submittedAt: new Date(record.submittedAt)
1144
- });
1145
- if (queued !== void 0 && queued.length > 0) await tx.insert(deliveries).values(queued.map((delivery) => ({
1146
- id: delivery.id,
1147
- webhookId: delivery.webhookId,
1148
- submissionId: delivery.submissionId,
1149
- eventId: delivery.eventId,
1150
- body: delivery.body,
1151
- attempt: delivery.attempt,
1152
- nextAttemptAt: new Date(delivery.nextAttemptAt),
1153
- state: delivery.state,
1154
- lastError: delivery.lastError
1155
- })));
1156
- if (claimFileIds !== void 0 && claimFileIds.length > 0) {
1157
- if ((await tx.update(files).set({
1158
- state: "claimed",
1159
- submissionId: record.id
1160
- }).where(and(inArray(files.id, [...claimFileIds]), eq(files.state, "stored"))).returning({ id: files.id })).length !== claimFileIds.length) throw new Error("A file this submission names was claimed by another one first. Rolling back, so the submission does not reference bytes it does not own.");
1161
- }
1162
- if (audit !== void 0) await tx.insert(auditLog).values(auditRow(audit));
1163
- });
1164
- },
1165
- async spendChallenge(challenge, expiresAtIso) {
1166
- return (await db.insert(spentChallenges).values({
1167
- challenge,
1168
- expiresAt: new Date(expiresAtIso)
1169
- }).onConflictDoNothing().returning({ challenge: spentChallenges.challenge })).length === 1;
1170
- },
1171
- async forgetExpiredChallenges(beforeIso) {
1172
- return (await db.delete(spentChallenges).where(lt(spentChallenges.expiresAt, new Date(beforeIso))).returning({ challenge: spentChallenges.challenge })).length;
1173
- },
1174
- async recordAudit(entry) {
1175
- await db.insert(auditLog).values(auditRow(entry));
1176
- },
1177
- async listAudit(limit) {
1178
- return (await db.select().from(auditLog).orderBy(desc(auditLog.at)).limit(limit)).map((row) => ({
1179
- id: row.id,
1180
- at: row.at.toISOString(),
1181
- action: row.action,
1182
- ...row.actorKind === null ? {} : { actorKind: row.actorKind },
1183
- ...row.actorId === null ? {} : { actorId: row.actorId },
1184
- ...row.subject === null ? {} : { subject: row.subject },
1185
- ...row.requestId === null ? {} : { requestId: row.requestId },
1186
- ...row.detail === null ? {} : { detail: row.detail }
1187
- }));
1188
- },
1189
- async insertFile(record) {
1190
- await db.insert(files).values({
1191
- ...record,
1192
- createdAt: new Date(record.createdAt)
1193
- });
1194
- },
1195
- async getFile(id) {
1196
- const [row] = await db.select().from(files).where(eq(files.id, id)).limit(1);
1197
- return row === void 0 ? void 0 : toFileRecord(row);
1198
- },
1199
- async updateFile(record) {
1200
- await db.update(files).set({
1201
- state: record.state,
1202
- submissionId: record.submissionId
1203
- }).where(eq(files.id, record.id));
1204
- },
1205
- async abandonedFiles(beforeIso) {
1206
- return (await db.select().from(files).where(and(ne(files.state, "claimed"), lt(files.createdAt, new Date(beforeIso))))).map(toFileRecord);
1207
- },
1208
- async deleteFiles(ids) {
1209
- if (ids.length === 0) return;
1210
- await db.delete(files).where(inArray(files.id, [...ids]));
1211
- },
1212
- async webhooksForForm(formId) {
1213
- return (await db.select().from(webhooks).where(eq(webhooks.formId, formId))).map(toWebhookRecord);
1214
- },
1215
- async insertWebhook(record) {
1216
- await db.insert(webhooks).values({
1217
- id: record.id,
1218
- formId: record.formId,
1219
- url: record.url,
1220
- secret: record.secret,
1221
- consecutiveFailures: record.consecutiveFailures,
1222
- openedAt: record.openedAt === null ? null : new Date(record.openedAt)
1223
- });
1224
- },
1225
- async updateWebhook(record) {
1226
- await db.update(webhooks).set({
1227
- consecutiveFailures: record.consecutiveFailures,
1228
- openedAt: record.openedAt === null ? null : new Date(record.openedAt)
1229
- }).where(eq(webhooks.id, record.id));
1230
- },
1231
- async listWebhooks() {
1232
- return (await db.select().from(webhooks)).map(toWebhookRecord);
1233
- },
1234
- async deadDeliveries(limit) {
1235
- return (await db.select().from(deliveries).where(eq(deliveries.state, "dead")).orderBy(desc(deliveries.nextAttemptAt)).limit(limit)).map(toDeliveryRecord);
1236
- },
1237
- async getDelivery(id) {
1238
- const [row] = await db.select().from(deliveries).where(eq(deliveries.id, id)).limit(1);
1239
- return row === void 0 ? void 0 : toDeliveryRecord(row);
1240
- },
1241
- async claimDueDeliveries(nowIso, limit) {
1242
- return (await db.select().from(deliveries).where(and(eq(deliveries.state, "pending"), lte(deliveries.nextAttemptAt, new Date(nowIso)))).orderBy(deliveries.nextAttemptAt).limit(limit)).map(toDeliveryRecord);
1243
- },
1244
- async updateDelivery(record) {
1245
- await db.update(deliveries).set({
1246
- attempt: record.attempt,
1247
- nextAttemptAt: new Date(record.nextAttemptAt),
1248
- state: record.state,
1249
- lastError: record.lastError
1250
- }).where(eq(deliveries.id, record.id));
1251
- },
1252
- async listSubmissions() {
1253
- return (await db.select().from(submissions)).map(toSubmissionRecord);
1254
- },
1255
- async listSubmissionsByForm(formId) {
1256
- return (await db.select().from(submissions).where(eq(submissions.formId, formId)).orderBy(desc(submissions.submittedAt), desc(submissions.id))).map(toSubmissionRecord);
1257
- },
1258
- async listVersionsByForm(formId) {
1259
- return (await db.select().from(formVersions).where(eq(formVersions.formId, formId)).orderBy(desc(formVersions.version))).map((row) => ({
1260
- id: row.id,
1261
- formId: row.formId,
1262
- version: row.version,
1263
- schema: row.schema,
1264
- schemaHash: row.schemaHash
1265
- }));
1266
- },
1267
- async upsertDraft(record) {
1268
- await db.insert(drafts).values({
1269
- id: record.id,
1270
- formId: record.formId,
1271
- formVersionId: record.formVersionId,
1272
- data: record.data,
1273
- updatedAt: new Date(record.updatedAt)
1274
- }).onConflictDoUpdate({
1275
- target: [drafts.formId, drafts.id],
1276
- set: {
1277
- formVersionId: record.formVersionId,
1278
- data: record.data,
1279
- updatedAt: new Date(record.updatedAt)
1280
- }
1281
- });
1282
- },
1283
- async getDraft(formId, draftId) {
1284
- const row = (await db.select().from(drafts).where(and(eq(drafts.formId, formId), eq(drafts.id, draftId))).limit(1))[0];
1285
- if (row === void 0) return void 0;
1286
- return {
1287
- id: row.id,
1288
- formId: row.formId,
1289
- formVersionId: row.formVersionId,
1290
- data: row.data,
1291
- updatedAt: row.updatedAt.toISOString()
1292
- };
1293
- },
1294
- async insertUser(record) {
1295
- await db.insert(users).values({
1296
- id: record.id,
1297
- email: record.email,
1298
- passwordHash: record.passwordHash,
1299
- role: record.role,
1300
- createdAt: new Date(record.createdAt)
1301
- });
1302
- },
1303
- async getUserByEmail(email) {
1304
- const row = (await db.select().from(users).where(eq(users.email, email)).limit(1))[0];
1305
- if (row === void 0) return void 0;
1306
- return {
1307
- id: row.id,
1308
- email: row.email,
1309
- passwordHash: row.passwordHash,
1310
- role: row.role,
1311
- createdAt: row.createdAt.toISOString()
1312
- };
1313
- },
1314
- async insertApiKey(record) {
1315
- await db.insert(apiKeys).values({
1316
- id: record.id,
1317
- name: record.name,
1318
- prefix: record.prefix,
1319
- secretHash: record.secretHash,
1320
- role: record.role,
1321
- createdAt: new Date(record.createdAt),
1322
- revokedAt: record.revokedAt === null ? null : new Date(record.revokedAt)
1323
- });
1324
- },
1325
- async listApiKeys() {
1326
- return (await db.select().from(apiKeys)).map(toApiKeyRecord);
1327
- },
1328
- async findApiKeysByPrefix(prefix) {
1329
- return (await db.select().from(apiKeys).where(eq(apiKeys.prefix, prefix))).map(toApiKeyRecord);
1330
- },
1331
- async revokeApiKey(id, atIso) {
1332
- await db.update(apiKeys).set({ revokedAt: new Date(atIso) }).where(eq(apiKeys.id, id));
1333
- }
1334
- };
1335
- }
1336
- function toApiKeyRecord(row) {
1337
- return {
1338
- id: row.id,
1339
- name: row.name,
1340
- prefix: row.prefix,
1341
- secretHash: row.secretHash,
1342
- role: row.role,
1343
- createdAt: row.createdAt.toISOString(),
1344
- revokedAt: row.revokedAt === null ? null : row.revokedAt.toISOString()
1345
- };
1346
- }
1347
- function toSubmissionRecord(row) {
1348
- return {
1349
- id: row.id,
1350
- formId: row.formId,
1351
- formVersionId: row.formVersionId,
1352
- data: row.data,
1353
- submittedAt: row.submittedAt.toISOString()
1354
- };
1355
- }
1356
- /** A delivery row as the port describes it. */
1357
- function toDeliveryRecord(row) {
1358
- return {
1359
- id: row.id,
1360
- webhookId: row.webhookId,
1361
- submissionId: row.submissionId,
1362
- eventId: row.eventId,
1363
- body: row.body,
1364
- attempt: row.attempt,
1365
- nextAttemptAt: row.nextAttemptAt.toISOString(),
1366
- state: row.state,
1367
- lastError: row.lastError
1368
- };
1369
- }
1370
- /** A files row as the port describes it: timestamps as ISO strings. */
1371
- function toFileRecord(row) {
1372
- return {
1373
- id: row.id,
1374
- formId: row.formId,
1375
- name: row.name,
1376
- size: row.size,
1377
- contentType: row.contentType,
1378
- storageKey: row.storageKey,
1379
- state: row.state,
1380
- createdAt: row.createdAt.toISOString(),
1381
- submissionId: row.submissionId
1382
- };
1383
- }
1384
- /** A webhook row as the port describes it, health included. */
1385
- function toWebhookRecord(row) {
1386
- return {
1387
- id: row.id,
1388
- formId: row.formId,
1389
- url: row.url,
1390
- secret: row.secret,
1391
- consecutiveFailures: row.consecutiveFailures,
1392
- openedAt: row.openedAt === null ? null : row.openedAt.toISOString()
1393
- };
1394
- }
1395
- /**
1396
- * An entry as the table holds it.
1397
- *
1398
- * Absent and null are the same thing here, and the conversion is in one place
1399
- * so two call sites cannot disagree about which they write.
1400
- */
1401
- function auditRow(entry) {
1402
- return {
1403
- id: entry.id,
1404
- at: new Date(entry.at),
1405
- action: entry.action,
1406
- actorKind: entry.actorKind ?? null,
1407
- actorId: entry.actorId ?? null,
1408
- subject: entry.subject ?? null,
1409
- requestId: entry.requestId ?? null,
1410
- detail: entry.detail ?? null
1411
- };
1412
- }
1413
- //#endregion
1414
- //#region src/file-store.ts
1415
- /**
1416
- * Bytes under one directory, one file per key.
1417
- *
1418
- * **Keys are checked, not trusted.** A key is minted by the server from a form
1419
- * id and a file id, so it cannot contain a traversal — but this resolves it
1420
- * and refuses anything landing outside the root regardless. A path check that
1421
- * only runs when the input is untrusted is a path check that stops running the
1422
- * day somebody adds a caller.
1423
- */
1424
- function createLocalFileStore(root) {
1425
- const base = resolve(root);
1426
- const pathFor = (key) => {
1427
- if (key.includes("\\")) throw new Error(`Refusing a storage key that could resolve outside the store: ${key}`);
1428
- const full = resolve(join(base, key));
1429
- if (full !== base && !full.startsWith(base + sep)) throw new Error(`Refusing a storage key that resolves outside the store: ${key}`);
1430
- return full;
1431
- };
1432
- return {
1433
- async put(key, bytes) {
1434
- const full = pathFor(key);
1435
- await mkdir(dirname(full), { recursive: true });
1436
- await writeFile(full, bytes);
1437
- },
1438
- async open(key) {
1439
- const full = pathFor(key);
1440
- try {
1441
- await stat(full);
1442
- } catch {
1443
- return;
1444
- }
1445
- return createReadStream(full);
1446
- },
1447
- async remove(key) {
1448
- await rm(pathFor(key), { force: true });
1449
- },
1450
- async sizeOf(key) {
1451
- try {
1452
- return (await stat(pathFor(key))).size;
1453
- } catch {
1454
- return;
1455
- }
1456
- }
1457
- };
1458
- }
1459
- /**
1460
- * The checksum a receiver can compare against.
1461
- *
1462
- * Returned when the bytes are stored and never asked of the client: a checksum
1463
- * the uploader computed is a checksum the uploader can lie about, so this one
1464
- * is only useful for telling a *later* reader that the bytes have not changed.
1465
- */
1466
- function sha256(bytes) {
1467
- return createHash("sha256").update(bytes).digest("hex");
1468
- }
1469
- //#endregion
1470
- //#region src/file-collector.ts
1471
- function startFileCollector(storage, store, options = {}) {
1472
- const interval = options.intervalMs ?? 9e5;
1473
- const afterHours = options.afterHours ?? 24;
1474
- let stopped = false;
1475
- let running = false;
1476
- const tick = async () => {
1477
- if (running || stopped) return;
1478
- running = true;
1479
- try {
1480
- const abandoned = await collectAbandonedFiles(storage, /* @__PURE__ */ new Date(), afterHours);
1481
- if (abandoned.length === 0) return;
1482
- const removed = [];
1483
- for (const file of abandoned) {
1484
- await store.remove(file.storageKey);
1485
- removed.push(file.id);
1486
- }
1487
- await storage.deleteFiles(removed);
1488
- } catch (error) {
1489
- console.error("files: a collection pass failed", error);
1490
- } finally {
1491
- running = false;
1492
- }
1493
- };
1494
- const timer = setInterval(() => void tick(), interval);
1495
- timer.unref?.();
1496
- return { stop: () => {
1497
- stopped = true;
1498
- clearInterval(timer);
1499
- } };
1500
- }
1501
- //#endregion
1502
- export { bootstrapSchema as a, createPostgresStorage as i, createLocalFileStore as n, createApp as o, sha256 as r, SCHEMA_HASH_HEADER as s, startFileCollector as t };
1503
-
1504
- //# sourceMappingURL=file-collector-LmfD8Y_d.mjs.map