@fabrika/app 0.9.0-dev.11 → 0.9.0-dev.12
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 +10 -10
- package/skill/SKILL.md +2 -2
- package/skill/references/app-setup.md +8 -8
- package/skill/references/endpoint.md +12 -4
- package/skill/references/files.md +11 -1
- package/skill/references/table-and-migration.md +22 -0
- package/src/contract/artifact-schema.ts +27 -5
- package/src/contract/build.ts +53 -32
- package/src/contract/declaration.ts +4 -2
- package/src/contract/define.ts +7 -1
- package/src/contract/group.ts +12 -10
- package/src/contract/index.ts +2 -0
- package/src/dev/launcher.ts +14 -15
- package/src/dev/output.ts +8 -11
- package/src/files/kinds.ts +1 -1
- package/src/files/sniff.ts +18 -1
- package/src/files/treatment.ts +12 -10
- package/src/implement/client-address.ts +5 -71
- package/src/implement/dispatch.ts +6 -4
- package/src/implement/egress.ts +30 -12
- package/src/implement/public.ts +3 -3
- package/src/jobs/report.ts +32 -5
- package/src/jobs/scrub.ts +1 -1
- package/src/testing/endpoint-access.ts +2 -1
- package/src/testing/sample-file.ts +4 -1
- package/types/src/contract/declaration.d.ts +3 -1
- package/types/src/contract/define.d.ts +6 -1
- package/types/src/contract/group.d.ts +5 -3
- package/types/src/contract/index.d.ts +1 -0
- package/types/src/dev/output.d.ts +7 -6
- package/types/src/files/treatment.d.ts +4 -3
- package/types/src/implement/client-address.d.ts +0 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/app",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.12",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -105,18 +105,18 @@
|
|
|
105
105
|
}
|
|
106
106
|
},
|
|
107
107
|
"scripts": {
|
|
108
|
-
"typecheck": "
|
|
108
|
+
"typecheck": "tsrs --build tsconfig.test.json",
|
|
109
109
|
"test": "bun test --path-ignore-patterns='src/react/**' && bun test --isolate src/react"
|
|
110
110
|
},
|
|
111
111
|
"dependencies": {
|
|
112
|
-
"@fabrika/auth": "0.9.0-dev.
|
|
113
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
114
|
-
"@fabrika/db": "0.9.0-dev.
|
|
115
|
-
"@fabrika/email": "0.9.0-dev.
|
|
116
|
-
"@fabrika/jwks": "0.9.0-dev.
|
|
117
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
118
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
119
|
-
"@fabrika/proxy-core": "0.9.0-dev.
|
|
112
|
+
"@fabrika/auth": "0.9.0-dev.12",
|
|
113
|
+
"@fabrika/auth-core": "0.9.0-dev.12",
|
|
114
|
+
"@fabrika/db": "0.9.0-dev.12",
|
|
115
|
+
"@fabrika/email": "0.9.0-dev.12",
|
|
116
|
+
"@fabrika/jwks": "0.9.0-dev.12",
|
|
117
|
+
"@fabrika/platform": "0.9.0-dev.12",
|
|
118
|
+
"@fabrika/platform-node": "0.9.0-dev.12",
|
|
119
|
+
"@fabrika/proxy-core": "0.9.0-dev.12",
|
|
120
120
|
"altcha-lib": "2.6.0",
|
|
121
121
|
"jose": "^5.9.0"
|
|
122
122
|
},
|
package/skill/SKILL.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: fabrika-app
|
|
3
|
-
version: 0.9.0-dev.
|
|
3
|
+
version: 0.9.0-dev.12
|
|
4
4
|
description: >
|
|
5
5
|
How to write and change a Fabrika application: src/vocabulary.ts, db/migrations/, src/db/tables.ts, the
|
|
6
6
|
slices' endpoints.ts with their handlers, fabrika.contract.ts, jobs, files, webhooks, share links, connectors
|
|
@@ -231,7 +231,7 @@ the access check. `references/spa.md` shows the stack.
|
|
|
231
231
|
| a share link | `issues: '<link mount>'` | `ctx.links.issue` |
|
|
232
232
|
| e-mail (a cron job only) | `mailer: 'installation'` | `ctx.mail.send` |
|
|
233
233
|
| a file from a public procedure | `file('<kind>')` input, `admission` with `bytesPerHour` | a reference the handler writes into a file column |
|
|
234
|
-
| an outside HTTPS API | `egress: ['https://api.example.com']` | `fetch` to
|
|
234
|
+
| an outside HTTPS API | `egress: ['https://api.example.com']` | `fetch` to those (`https://*.<domain>`: one label) |
|
|
235
235
|
| a signed link (unsubscribe) | `signs: ['<purpose>']` | `ctx.tokens.sign` |
|
|
236
236
|
| a webhook binding | `binds: '<webhook>'` | `ctx.webhooks('<webhook>').bind` and `.unbind` |
|
|
237
237
|
| a job run after the commit | `kicks: ['<job>']` | `ctx.kick('<job>')`, `ctx.kick('<job>', { at })` |
|
|
@@ -19,20 +19,20 @@ the SPA is added with it.
|
|
|
19
19
|
"test": "bun test --isolate"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@fabrika/app": "0.9.0-dev.
|
|
23
|
-
"@fabrika/db": "0.9.0-dev.
|
|
24
|
-
"@fabrika/provider-zerops": "0.9.0-dev.
|
|
25
|
-
"@fabrika/proxy-core": "0.9.0-dev.
|
|
26
|
-
"@fabrika/ui": "0.9.0-dev.
|
|
22
|
+
"@fabrika/app": "0.9.0-dev.12",
|
|
23
|
+
"@fabrika/db": "0.9.0-dev.12",
|
|
24
|
+
"@fabrika/provider-zerops": "0.9.0-dev.12",
|
|
25
|
+
"@fabrika/proxy-core": "0.9.0-dev.12",
|
|
26
|
+
"@fabrika/ui": "0.9.0-dev.12",
|
|
27
27
|
"kysely": "0.29.6",
|
|
28
28
|
"react": "^19.0.0",
|
|
29
29
|
"react-dom": "^19.0.0",
|
|
30
30
|
"zod": "^3.25.76"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@fabrika/check": "0.9.0-dev.
|
|
34
|
-
"@fabrika/cli": "0.9.0-dev.
|
|
35
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
33
|
+
"@fabrika/check": "0.9.0-dev.12",
|
|
34
|
+
"@fabrika/cli": "0.9.0-dev.12",
|
|
35
|
+
"@fabrika/platform": "0.9.0-dev.12",
|
|
36
36
|
"@happy-dom/global-registrator": "^20.11.2",
|
|
37
37
|
"@types/bun": "^1.4.3",
|
|
38
38
|
"@types/react": "^19.0.0",
|
|
@@ -654,10 +654,18 @@ export const app = defineApp(vocabulary, tables, {
|
|
|
654
654
|
## Outside calls: `egress`
|
|
655
655
|
|
|
656
656
|
A credentialed procedure, a webhook or a job that calls an outside HTTPS API declares its origins:
|
|
657
|
-
`egress: ['https://api.example.com']`, exact origins, no path
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
657
|
+
`egress: ['https://api.example.com']`, exact origins, no path, at most 16. `fetch` to any other origin is refused
|
|
658
|
+
while the site runs (`capability_refused: undeclared_egress`). A public, link or export procedure declares none. The
|
|
659
|
+
checker's `cross-app-fetch` accepts an absolute URL only under an origin some site declares. The approver sees every
|
|
660
|
+
origin. The guard stops mistakes; it is no sandbox.
|
|
661
|
+
|
|
662
|
+
A provider with one host per customer is one pattern: `egress: ['https://*.tenants.example.com']` reaches
|
|
663
|
+
`https://acme.tenants.example.com`, exactly one label in front of the domain, over `https` on the default port
|
|
664
|
+
(`https://*.tenants.example.com:8443` for another). It does not reach `a.b.tenants.example.com`,
|
|
665
|
+
`tenants.example.com` or `evil-tenants.example.com`. Write it in lower case and punycode, without a trailing dot; the
|
|
666
|
+
build names the spelling it wants. It refuses `http`, a second wildcard, and a wildcard on a public suffix (`*.com`,
|
|
667
|
+
`*.co.uk`). The checker accepts ``fetch(`https://${tenant}.tenants.example.com/v1`)`` under it. The approver reads a
|
|
668
|
+
pattern as every host under that domain, so prefer exact origins when there are a few.
|
|
661
669
|
|
|
662
670
|
On Cloudflare a procedure waits for its outside call inside the object's transaction, so the whole environment waits
|
|
663
671
|
with it (`SKILL.md`). A mutation records a row and kicks a job, which declares the `egress` and calls outside
|
|
@@ -70,7 +70,7 @@ task_attachments: {
|
|
|
70
70
|
- A file column is `UUID REFERENCES fabrika_files (id)` with an index on it; the checker's `file-column-migration`
|
|
71
71
|
verifies both for every column the policy names in `files`. `fabrika_files` is the framework's table: never write
|
|
72
72
|
it.
|
|
73
|
-
- A file kind (`c.files({ types, maxBytes, scan? })`) has no owner, no actions and no endpoint. The table's actions
|
|
73
|
+
- A file kind (`c.files({ types, maxBytes, scan?, inline? })`) has no owner, no actions and no endpoint. The table's actions
|
|
74
74
|
are the file's authority, and the approver reads "`tasks.write` writes `task_attachments.file` (attachments)".
|
|
75
75
|
- A file reaches what its row reaches, now: a moved task moves its files. Deleting the row (here with its task, by the
|
|
76
76
|
cascade) or clearing a nullable column removes the reference; the framework's sweep deletes a file no column
|
|
@@ -159,6 +159,16 @@ export const attachments = app.group({ callers: ['human'] }, (r) => ({
|
|
|
159
159
|
- A public image (a catalog picture in a page or an e-mail) is a public `GET` query with `http: { cache: 'immutable' }`
|
|
160
160
|
whose input names the file as well as its row (`{ product, file }`), so a new image changes the URL. The handler
|
|
161
161
|
refuses a file id that is not the row's.
|
|
162
|
+
- A download is an attachment (`Content-Disposition: attachment`); only `image/png`, `image/jpeg`, `image/gif` and
|
|
163
|
+
`image/webp` are shown inline. A file a machine reads at its URL (an RSS or Atom feed, a sitemap, an e-shop import)
|
|
164
|
+
is of a kind declared `inline: true`, e.g. `c.files({ types: ['application/rss+xml'], maxBytes: 1048576, inline: true })`,
|
|
165
|
+
served by a public `GET` query (`http: { path: '/feed.xml' }`). An inline kind admits only those images and
|
|
166
|
+
`application/xml`, `text/xml`, `application/rss+xml`, `application/atom+xml`; the build refuses SVG, HTML and every
|
|
167
|
+
other type there. Only `ctx.files.put` fills an inline kind, with a document the handler renders from checked data
|
|
168
|
+
(a feed built from rows, its text escaped), never bytes the caller sent or a file it opened: XML in the XHTML or SVG
|
|
169
|
+
namespace still renders as a page. The build refuses a procedure input of `file('<inline kind>')`; a user upload goes
|
|
170
|
+
into a kind without `inline`. An XML
|
|
171
|
+
file is UTF-8 whose bytes start with `<` (after a byte order mark and white space), or the upload is 415.
|
|
162
172
|
|
|
163
173
|
The SPA calls them through the typed client, never a hand-written `fetch`. `api.attachments.download.url(input)` is
|
|
164
174
|
the URL of a `GET` file procedure for a same-origin page (`<img src>`, `<a download>`), and
|
|
@@ -328,3 +328,25 @@ export const tables = defineTables(vocabulary, columns)({
|
|
|
328
328
|
- IAM already audits sign-ins (its auth log, refusals included) and every grant and revocation (its admin events
|
|
329
329
|
`iam.grant.create` and `iam.grant.revoke`): do not rebuild either in the application. An operator's access to the
|
|
330
330
|
database or to the provider's console is outside Fabrika.
|
|
331
|
+
|
|
332
|
+
## A view read as a table
|
|
333
|
+
|
|
334
|
+
`fabrika db codegen` reads every view a migration creates (not a materialized view: read one with `ctx.sql`): its
|
|
335
|
+
interface is `<Pascal>View`, every column is `| null`, and its column list entry says `view: true`. A view needs no
|
|
336
|
+
policy; without one only `ctx.sql` reads it. With one, `ctx.db` reads it like a child table, typed and scoped, and nothing writes it:
|
|
337
|
+
|
|
338
|
+
- `scope: { child: '<parent>' }` puts it under its parent's `Ref`, with the child's conventions: the view selects the
|
|
339
|
+
parent column (`<parent dimension>_id`) and every chain column of the parent. A view of one row per parent states
|
|
340
|
+
`key: '<parent column>'` (`{ child: 'deals', key: 'deal_id' }`), and a handler `on` the parent reads that row with
|
|
341
|
+
`ctx.db.target.<view>.get(ctx.target.value)`.
|
|
342
|
+
- `scope: { reference: '<why>' }` is a view every endpoint reads, through `ctx.db.reference.<view>`.
|
|
343
|
+
- It takes `actions: { read, appRead?, appCount? }` and column `read` actions only. `insert`, `update`, `delete`,
|
|
344
|
+
`version`, `createdBy`, `queue`, `files`, a column `write` and `unscoped: { <view>: 'write' }` are refused by tsc and
|
|
345
|
+
the checker; its handles' writes are `Unavailable`, and the data layer refuses them.
|
|
346
|
+
- No foreign key ties a view's rows to its parent: its SQL is what puts a row in a scope, and the approver reviews it.
|
|
347
|
+
Select the parent and chain columns from the parent row itself, or from a row whose foreign keys tie it there. A row
|
|
348
|
+
whose parent column is null is read by no `Ref`, but a `Within` reaches it through the chain columns it carries.
|
|
349
|
+
- A column of a type the data layer does not read (an array, `numeric` on SQLite) is left out, and every codegen run
|
|
350
|
+
names it. On SQLite an expression has a type only through `CAST(… AS INTEGER | REAL | TEXT)`; on Postgres cast a
|
|
351
|
+
count you want as a number (`count(*)::int4`).
|
|
352
|
+
- `fabrika db test-scaffold` writes no policy isolation test for a view: its parent's cases cover the rows it reads.
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
displayNameProblem,
|
|
13
13
|
type EndpointRow,
|
|
14
14
|
FILE_RESPONSE_KINDS,
|
|
15
|
+
fileKindsIn,
|
|
15
16
|
isOwnWorkloadName,
|
|
16
17
|
JOB_MAILERS,
|
|
17
18
|
LOCALES,
|
|
@@ -20,6 +21,7 @@ import {
|
|
|
20
21
|
type MessageKind,
|
|
21
22
|
type NumberShape,
|
|
22
23
|
type OutputShape,
|
|
24
|
+
parseEgressEntry,
|
|
23
25
|
type RecordShape,
|
|
24
26
|
type SchemaShape,
|
|
25
27
|
type StringShape,
|
|
@@ -27,7 +29,7 @@ import {
|
|
|
27
29
|
terminalIdentifyProblem,
|
|
28
30
|
type TupleShape,
|
|
29
31
|
} from '@fabrika/auth-core'
|
|
30
|
-
import {
|
|
32
|
+
import { inlineFileTypes, WORKER_BLOB_SOURCE } from '@fabrika/proxy-core/headers'
|
|
31
33
|
import { z } from 'zod/v4'
|
|
32
34
|
import {
|
|
33
35
|
ALTCHA_LIMITS,
|
|
@@ -172,7 +174,7 @@ const rowBase = {
|
|
|
172
174
|
admission: z.strictObject({ contentTypes: z.tuple([name], name), maxBytes: z.int().positive().nullable() }).nullable(),
|
|
173
175
|
input: schemaShapeSchema.nullable(),
|
|
174
176
|
output: outputShapeSchema.nullable(),
|
|
175
|
-
response: z.
|
|
177
|
+
response: z.enum(['file', 'inline-file']).optional(),
|
|
176
178
|
}
|
|
177
179
|
|
|
178
180
|
const endpointRow: z.ZodType<EndpointRow> = z.union([
|
|
@@ -228,7 +230,7 @@ const endpointRow: z.ZodType<EndpointRow> = z.union([
|
|
|
228
230
|
}),
|
|
229
231
|
]).refine(
|
|
230
232
|
// Only a procedure answers a stored file, and exactly when its output is one.
|
|
231
|
-
(row) => (row.response
|
|
233
|
+
(row) => (row.response !== undefined) === (row.output?.type === 'file') && (row.response === undefined || FILE_RESPONSE_KINDS.includes(row.kind)),
|
|
232
234
|
'only a procedure whose output is a file answers a stored file',
|
|
233
235
|
).refine(
|
|
234
236
|
// Only an RPC procedure, a public procedure and a form open to another origin, and only the anonymous two to any (ADR-0022).
|
|
@@ -328,6 +330,7 @@ const tablePolicy: z.ZodType<TablePolicyArtifact> = z.strictObject({
|
|
|
328
330
|
createdBy: nullableName,
|
|
329
331
|
columns: record(z.strictObject({ read: action.optional(), write: action.optional() })),
|
|
330
332
|
files: z.record(name, name).optional(),
|
|
333
|
+
view: z.literal(true).optional(),
|
|
331
334
|
})
|
|
332
335
|
|
|
333
336
|
export const contractArtifactSchema: z.ZodType<ContractArtifact> = z.strictObject({
|
|
@@ -363,7 +366,13 @@ export const contractArtifactSchema: z.ZodType<ContractArtifact> = z.strictObjec
|
|
|
363
366
|
link: z.union([z.boolean(), z.array(name).nonempty()]),
|
|
364
367
|
scan: z.literal(true).optional(),
|
|
365
368
|
})),
|
|
366
|
-
fileKinds: z.record(
|
|
369
|
+
fileKinds: z.record(
|
|
370
|
+
fileKindName,
|
|
371
|
+
z.strictObject({ types: names, maxBytes: z.int().positive(), scan: z.literal(true).optional(), inline: z.literal(true).optional() }).refine(
|
|
372
|
+
(kind) => kind.inline === undefined || kind.types.every((type) => inlineFileTypes('inline-file').includes(type)),
|
|
373
|
+
'an inline kind admits only inert images and XML documents',
|
|
374
|
+
),
|
|
375
|
+
).optional(),
|
|
367
376
|
jobs: record(z.strictObject({
|
|
368
377
|
schedule: name,
|
|
369
378
|
actions,
|
|
@@ -417,7 +426,7 @@ export const contractArtifactSchema: z.ZodType<ContractArtifact> = z.strictObjec
|
|
|
417
426
|
kicks: z.array(name).nonempty().optional(),
|
|
418
427
|
binds: name.optional(),
|
|
419
428
|
signs: z.array(name).nonempty().optional(),
|
|
420
|
-
egress: z.array(z.string().refine((origin) =>
|
|
429
|
+
egress: z.array(z.string().refine((origin) => parseEgressEntry(origin).ok)).nonempty().optional(),
|
|
421
430
|
})).superRefine((declared, context) => {
|
|
422
431
|
for (const [site, reach] of Object.entries(declared)) {
|
|
423
432
|
if (reach.actions !== undefined && !isOwnWorkloadName('public', site)) {
|
|
@@ -448,6 +457,19 @@ export const contractArtifactSchema: z.ZodType<ContractArtifact> = z.strictObjec
|
|
|
448
457
|
tokens: record(z.strictObject({ claims: schemaShapeSchema, maxDays: z.int().positive().max(TOKEN_MAX_DAYS) })).optional(),
|
|
449
458
|
}).superRefine((artifact, context) => {
|
|
450
459
|
for (const row of artifact.endpoints) {
|
|
460
|
+
// A row answering a file of an inline kind shows it, every other row that answers a file downloads it.
|
|
461
|
+
if (row.output?.type === 'file' && (row.response === 'inline-file') !== (artifact.fileKinds?.[row.output.kind]?.inline === true)) {
|
|
462
|
+
context.addIssue({ code: 'custom', path: ['endpoints'], message: `'${row.id}' is inline-file exactly when its file kind is inline` })
|
|
463
|
+
}
|
|
464
|
+
// Only the application writes a file of an inline kind: an uploaded document could render as a page.
|
|
465
|
+
const inline = row.input === null ? undefined : fileKindsIn(row.input).find((kind) => artifact.fileKinds?.[kind]?.inline === true)
|
|
466
|
+
if (inline !== undefined) {
|
|
467
|
+
context.addIssue({
|
|
468
|
+
code: 'custom',
|
|
469
|
+
path: ['endpoints', artifact.endpoints.indexOf(row)],
|
|
470
|
+
message: `'${row.id}' takes a file of the inline kind '${inline}', which only the application writes`,
|
|
471
|
+
})
|
|
472
|
+
}
|
|
451
473
|
if (row.kind === 'public' && row.source.kind !== 'challenge' && artifact.declared?.[row.id]?.actions === undefined) {
|
|
452
474
|
context.addIssue({ code: 'custom', path: ['declared', row.id], message: 'a public procedure declares its workload actions' })
|
|
453
475
|
}
|
package/src/contract/build.ts
CHANGED
|
@@ -29,6 +29,7 @@ import {
|
|
|
29
29
|
type EndpointSource,
|
|
30
30
|
type ExportArtifact,
|
|
31
31
|
type FileKindArtifact,
|
|
32
|
+
fileKindsIn,
|
|
32
33
|
type FileShape,
|
|
33
34
|
FORM_URLENCODED_TYPE,
|
|
34
35
|
type ImportArtifact,
|
|
@@ -49,6 +50,7 @@ import {
|
|
|
49
50
|
type MessageKind,
|
|
50
51
|
type MountArtifact,
|
|
51
52
|
type OutputShape,
|
|
53
|
+
parseEgressEntry,
|
|
52
54
|
planEndpoint,
|
|
53
55
|
type ProcedureArtifact,
|
|
54
56
|
type ProcedureChallenge,
|
|
@@ -62,7 +64,7 @@ import {
|
|
|
62
64
|
type TokenPurposeArtifact,
|
|
63
65
|
type WebhookScheme,
|
|
64
66
|
} from '@fabrika/auth-core'
|
|
65
|
-
import { isPolicyOrigin, MAX_CSP_ORIGINS, WORKER_BLOB_SOURCE } from '@fabrika/proxy-core/headers'
|
|
67
|
+
import { inlineFileTypes, isPolicyOrigin, MAX_CSP_ORIGINS, WORKER_BLOB_SOURCE } from '@fabrika/proxy-core/headers'
|
|
66
68
|
import { z } from 'zod/v4'
|
|
67
69
|
import { CONTRACT_APP_ID_PATTERN as APP_ID } from './app-id'
|
|
68
70
|
import { contractArtifactSchema, RESERVED_FILE_KIND_PREFIX } from './artifact-schema'
|
|
@@ -147,7 +149,7 @@ const FRAMEWORK_TABLE_PREFIX = 'fabrika_'
|
|
|
147
149
|
/** The most origins one site calls: a site that reaches more is an integration hub, which a connector import serves. */
|
|
148
150
|
const MAX_EGRESS_ORIGINS = 16
|
|
149
151
|
const DECLARED_ALLOWED =
|
|
150
|
-
"sql: 'read' | 'write', unscoped: { <table>: 'read' | 'write' }, connect: ['<import>'], issues: '<link mount>', mailer: 'installation' | 'own', kicks: ['<cron job>'], binds: '<webhook>', signs: ['<token purpose>'], egress: ['https://<host>']"
|
|
152
|
+
"sql: 'read' | 'write', unscoped: { <table>: 'read' | 'write' }, connect: ['<import>'], issues: '<link mount>', mailer: 'installation' | 'own', kicks: ['<cron job>'], binds: '<webhook>', signs: ['<token purpose>'], egress: ['https://<host>' | 'https://*.<domain>']"
|
|
151
153
|
const CSP_FIELDS = ['connectSrc', 'imgSrc', 'frameAncestors', 'scriptSrc', 'frameSrc', 'styleSrc', 'fontSrc']
|
|
152
154
|
/** An environment variable an application reads; a challenge names its secret by one. */
|
|
153
155
|
const APPLICATION_VARIABLE = /^[A-Z][A-Z0-9_]*$/
|
|
@@ -223,6 +225,15 @@ class ArtifactBuilder {
|
|
|
223
225
|
if (row.kind === 'webhook' && row.input !== null && containsFile(row.input)) {
|
|
224
226
|
this.#problem(`mount '${row.id}'`, "takes a file in its input, which only a procedure's top-level field does (file('<kind>'))")
|
|
225
227
|
}
|
|
228
|
+
// Only the application writes a file of an inline kind: an uploaded document could render as a page.
|
|
229
|
+
for (const kind of new Set(row.input === null ? [] : fileKindsIn(row.input))) {
|
|
230
|
+
if (Object.hasOwn(this.#fileKinds, kind) && this.#fileKinds[kind]?.inline === true) {
|
|
231
|
+
this.#problem(
|
|
232
|
+
`procedure '${row.id}'`,
|
|
233
|
+
`takes a file of the inline kind '${kind}' in its input, which only the application writes: an uploaded document could render as a page (allowed: a kind without inline for an upload; ctx.files.put of a document the application renders)`,
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
226
237
|
}
|
|
227
238
|
const jobs = this.#jobs()
|
|
228
239
|
const exports = this.#exports()
|
|
@@ -1456,7 +1467,15 @@ class ArtifactBuilder {
|
|
|
1456
1467
|
if (row.output !== null && isSchemaOutput(row.output)) {
|
|
1457
1468
|
this.#tokenFields(`${where} output`, row.output, false)
|
|
1458
1469
|
}
|
|
1459
|
-
|
|
1470
|
+
// A file of an inline kind is shown (`inline-file`), any other downloaded; the proxy reads which from the row.
|
|
1471
|
+
this.#endpoints.push(
|
|
1472
|
+
row.output?.type === 'file'
|
|
1473
|
+
? {
|
|
1474
|
+
...row,
|
|
1475
|
+
response: Object.hasOwn(this.#fileKinds, row.output.kind) && this.#fileKinds[row.output.kind]?.inline === true ? 'inline-file' : 'file',
|
|
1476
|
+
}
|
|
1477
|
+
: row,
|
|
1478
|
+
)
|
|
1460
1479
|
}
|
|
1461
1480
|
|
|
1462
1481
|
/**
|
|
@@ -1679,8 +1698,8 @@ class ArtifactBuilder {
|
|
|
1679
1698
|
|
|
1680
1699
|
// ── Sections ────────────────────────────────────────────────────────────────────────────────
|
|
1681
1700
|
|
|
1682
|
-
/** The media types and the
|
|
1683
|
-
#fileTypes(where: string, declared: Pick<FileKindDeclaration, 'types' | 'maxBytes' | 'scan'>): void {
|
|
1701
|
+
/** The media types, the scan and the inline serving of a file kind: exact types, a scan that can vouch for each file. */
|
|
1702
|
+
#fileTypes(where: string, declared: Pick<FileKindDeclaration, 'types' | 'maxBytes' | 'scan' | 'inline'>): void {
|
|
1684
1703
|
this.#unique(where, 'types', declared.types)
|
|
1685
1704
|
for (const type of declared.types) {
|
|
1686
1705
|
if (!MIME_TYPE.test(type)) {
|
|
@@ -1693,6 +1712,18 @@ class ArtifactBuilder {
|
|
|
1693
1712
|
if (declared.scan !== undefined && typeof declared.scan !== 'boolean') {
|
|
1694
1713
|
this.#problem(where, 'scan is true or false')
|
|
1695
1714
|
}
|
|
1715
|
+
if (declared.inline !== undefined && typeof declared.inline !== 'boolean') {
|
|
1716
|
+
this.#problem(where, 'inline is true or false')
|
|
1717
|
+
}
|
|
1718
|
+
const shown = inlineFileTypes('inline-file')
|
|
1719
|
+
for (const type of declared.inline === true ? declared.types.filter((candidate) => !shown.includes(candidate)) : []) {
|
|
1720
|
+
this.#problem(
|
|
1721
|
+
where,
|
|
1722
|
+
`is served inline and admits '${type}', which a browser could run or render as a page (allowed: ${
|
|
1723
|
+
shown.join(', ')
|
|
1724
|
+
}; or leave out inline, so each file is an attachment)`,
|
|
1725
|
+
)
|
|
1726
|
+
}
|
|
1696
1727
|
for (const type of declared.scan === true ? declared.types.filter((candidate) => FILE_LIMITS.unscannedTypes.includes(candidate)) : []) {
|
|
1697
1728
|
this.#problem(
|
|
1698
1729
|
where,
|
|
@@ -1707,7 +1738,7 @@ class ArtifactBuilder {
|
|
|
1707
1738
|
}
|
|
1708
1739
|
}
|
|
1709
1740
|
|
|
1710
|
-
/** The file kinds (`c.files({ types, maxBytes, scan? })`), before the mounts that name them. */
|
|
1741
|
+
/** The file kinds (`c.files({ types, maxBytes, scan?, inline? })`), before the mounts that name them. */
|
|
1711
1742
|
#readFileKinds(): Record<string, FileKindArtifact> {
|
|
1712
1743
|
const kinds: Record<string, FileKindArtifact> = {}
|
|
1713
1744
|
for (const [name, declared] of Object.entries(this.#contract.files)) {
|
|
@@ -1735,7 +1766,12 @@ class ArtifactBuilder {
|
|
|
1735
1766
|
)
|
|
1736
1767
|
}
|
|
1737
1768
|
this.#fileTypes(where, declared)
|
|
1738
|
-
kinds[name] = {
|
|
1769
|
+
kinds[name] = {
|
|
1770
|
+
types: [...declared.types],
|
|
1771
|
+
maxBytes: declared.maxBytes,
|
|
1772
|
+
...(declared.scan === true ? { scan: true } : {}),
|
|
1773
|
+
...(declared.inline === true ? { inline: true } : {}),
|
|
1774
|
+
}
|
|
1739
1775
|
}
|
|
1740
1776
|
return kinds
|
|
1741
1777
|
}
|
|
@@ -2163,8 +2199,10 @@ class ArtifactBuilder {
|
|
|
2163
2199
|
if (readOnly && mode === 'write') {
|
|
2164
2200
|
this.#problem(where, `declares unscoped '${table}': 'write', and it runs read-only (allowed: 'read')`)
|
|
2165
2201
|
}
|
|
2166
|
-
const
|
|
2167
|
-
if (mode === 'write' &&
|
|
2202
|
+
const policy = Object.hasOwn(this.#tables, table) ? this.#tables[table] : undefined
|
|
2203
|
+
if (mode === 'write' && policy?.view === true) {
|
|
2204
|
+
this.#problem(where, `declares unscoped '${table}': 'write', and no site writes a view: its rows come from the tables it reads (allowed: 'read')`)
|
|
2205
|
+
} else if (mode === 'write' && policy?.scope.mode === 'reference') {
|
|
2168
2206
|
this.#problem(
|
|
2169
2207
|
where,
|
|
2170
2208
|
`declares unscoped '${table}': 'write', and no site writes a reference table: its rows come from a migration (allowed: 'read')`,
|
|
@@ -2309,8 +2347,8 @@ class ArtifactBuilder {
|
|
|
2309
2347
|
|
|
2310
2348
|
/**
|
|
2311
2349
|
* The HTTPS origins a site's code calls: a credentialed procedure of an RPC mount, a webhook or a job, never a public,
|
|
2312
|
-
* export or link procedure. Each is an exact origin
|
|
2313
|
-
* a problem.
|
|
2350
|
+
* export or link procedure. Each is an exact origin or a one-label pattern (`parseEgressEntry`), once; `[]` when it
|
|
2351
|
+
* declares none or after a problem.
|
|
2314
2352
|
*/
|
|
2315
2353
|
#egress(where: string, value: unknown, place: DeclaringSite): string[] {
|
|
2316
2354
|
if (value === undefined) {
|
|
@@ -2336,8 +2374,9 @@ class ArtifactBuilder {
|
|
|
2336
2374
|
this.#problem(where, `${origins.length} egress origins exceed the limit of ${MAX_EGRESS_ORIGINS}`)
|
|
2337
2375
|
}
|
|
2338
2376
|
for (const origin of origins) {
|
|
2339
|
-
|
|
2340
|
-
|
|
2377
|
+
const parsed = parseEgressEntry(origin)
|
|
2378
|
+
if (!parsed.ok) {
|
|
2379
|
+
this.#problem(where, `egress '${origin}' ${parsed.problem} (allowed: 'https://api.example.com' or 'https://*.example.com')`)
|
|
2341
2380
|
}
|
|
2342
2381
|
}
|
|
2343
2382
|
this.#unique(where, 'egress', origins)
|
|
@@ -2638,25 +2677,7 @@ function fileField(shape: SchemaShape): { readonly kind: string; readonly count:
|
|
|
2638
2677
|
|
|
2639
2678
|
/** Whether a schema holds a file anywhere. */
|
|
2640
2679
|
export function containsFile(shape: SchemaShape): boolean {
|
|
2641
|
-
|
|
2642
|
-
case 'file':
|
|
2643
|
-
return true
|
|
2644
|
-
case 'array':
|
|
2645
|
-
return containsFile(shape.items)
|
|
2646
|
-
case 'tuple':
|
|
2647
|
-
return shape.items.some(containsFile) || (shape.rest !== null && containsFile(shape.rest))
|
|
2648
|
-
case 'record':
|
|
2649
|
-
return containsFile(shape.value)
|
|
2650
|
-
case 'object':
|
|
2651
|
-
return Object.values(shape.fields).some(containsFile)
|
|
2652
|
-
case 'optional':
|
|
2653
|
-
case 'nullable':
|
|
2654
|
-
return containsFile(shape.inner)
|
|
2655
|
-
case 'union':
|
|
2656
|
-
return shape.options.some(containsFile)
|
|
2657
|
-
default:
|
|
2658
|
-
return false
|
|
2659
|
-
}
|
|
2680
|
+
return fileKindsIn(shape).length > 0
|
|
2660
2681
|
}
|
|
2661
2682
|
|
|
2662
2683
|
/** The file fields of an input, by name: the parts a multipart body carries beside its JSON. */
|
|
@@ -56,7 +56,7 @@ export const BUILDER_FIELDS: Readonly<Record<Builder, BuilderFields>> = {
|
|
|
56
56
|
link: { options: ['spa', 'dimension', 'grants', 'issuedBy', 'maxDays', 'procedures'], owned: ['kind', 'prefix'] },
|
|
57
57
|
spa: { options: ['dir', 'access', 'browserConfig'], owned: ['kind', 'prefix'] },
|
|
58
58
|
webhook: { options: ['scheme', 'boundTo', 'actions', 'input', 'limits'], owned: ['kind', 'path'] },
|
|
59
|
-
fileKind: { options: ['types', 'maxBytes', 'scan'], owned: ['kind'] },
|
|
59
|
+
fileKind: { options: ['types', 'maxBytes', 'scan', 'inline'], owned: ['kind'] },
|
|
60
60
|
cron: { options: ['actions', 'limits', 'retries'], owned: ['kind', 'schedule'] },
|
|
61
61
|
export: { options: ['version', 'consumers', 'audience', 'procedures'], owned: ['kind'] },
|
|
62
62
|
attachment: { options: ['format', 'filename', 'row', 'maxBytes', 'maxRows'], owned: [] },
|
|
@@ -283,7 +283,7 @@ export interface WebhookMountDeclaration<S> extends OptionsRecord {
|
|
|
283
283
|
}
|
|
284
284
|
|
|
285
285
|
/**
|
|
286
|
-
* A file kind (`c.files({ types, maxBytes, scan? })`, no `owner`): a class of stored files a table's file column holds.
|
|
286
|
+
* A file kind (`c.files({ types, maxBytes, scan?, inline? })`, no `owner`): a class of stored files a table's file column holds.
|
|
287
287
|
* It has no owner, no actions and no endpoint.
|
|
288
288
|
*/
|
|
289
289
|
export interface FileKindDeclaration extends OptionsRecord {
|
|
@@ -292,6 +292,8 @@ export interface FileKindDeclaration extends OptionsRecord {
|
|
|
292
292
|
readonly maxBytes: number
|
|
293
293
|
/** Every file waits `quarantined` until the scan gives it a clean verdict; a byte consumer answers 409 until then. */
|
|
294
294
|
readonly scan?: boolean
|
|
295
|
+
/** Its files are shown, not downloaded: only inert images and the XML documents a machine reads. */
|
|
296
|
+
readonly inline?: boolean
|
|
295
297
|
}
|
|
296
298
|
|
|
297
299
|
export interface CronDeclaration extends OptionsRecord {
|
package/src/contract/define.ts
CHANGED
|
@@ -295,7 +295,7 @@ export interface WebhookMount<B extends string, Acts extends string, I extends S
|
|
|
295
295
|
readonly input: I
|
|
296
296
|
}
|
|
297
297
|
|
|
298
|
-
/** A file kind `c.files({ types, maxBytes, scan? })` declares: what a file column of that kind holds. */
|
|
298
|
+
/** A file kind `c.files({ types, maxBytes, scan?, inline? })` declares: what a file column of that kind holds. */
|
|
299
299
|
export interface FileKind extends FileKindDeclaration {}
|
|
300
300
|
|
|
301
301
|
export interface CronJob<Acts extends string> extends CronDeclaration {
|
|
@@ -427,6 +427,11 @@ export interface Kit<A extends string, D extends string, CD extends string> {
|
|
|
427
427
|
readonly maxBytes: number
|
|
428
428
|
/** Each file is scanned for malware and served only after a clean verdict; until then it answers 409. */
|
|
429
429
|
readonly scan?: boolean
|
|
430
|
+
/**
|
|
431
|
+
* Each file is shown, not downloaded (`Content-Disposition: inline`): a feed or a sitemap a machine reads. Every
|
|
432
|
+
* type is an inert image or `application/xml`, `text/xml`, `application/rss+xml`, `application/atom+xml`.
|
|
433
|
+
*/
|
|
434
|
+
readonly inline?: boolean
|
|
430
435
|
}
|
|
431
436
|
& Owned<'kind'>,
|
|
432
437
|
): FileKind
|
|
@@ -528,6 +533,7 @@ export function surfaceKit<A extends string, D extends string, CD extends string
|
|
|
528
533
|
types: kind.types,
|
|
529
534
|
maxBytes: kind.maxBytes,
|
|
530
535
|
scan: kind.scan,
|
|
536
|
+
inline: kind.inline,
|
|
531
537
|
unknownOptions: unknownOptions(kind, 'fileKind'),
|
|
532
538
|
}),
|
|
533
539
|
cron: (schedule, options) => ({
|
package/src/contract/group.ts
CHANGED
|
@@ -111,8 +111,8 @@ export type SignsField<Sg, Purposes extends string> =
|
|
|
111
111
|
& ([Sg] extends [never] ? unknown : { readonly signs: Sg & NoInfer<KnownPurposes<Sg, Purposes>> })
|
|
112
112
|
|
|
113
113
|
/**
|
|
114
|
-
* The HTTPS origins a site's code calls (`fetch`): each an exact origin, no path
|
|
115
|
-
* `fetch` refuses any other while the site runs; a public procedure declares none.
|
|
114
|
+
* The HTTPS origins a site's code calls (`fetch`): each an exact origin or `https://*.<domain>`, one label, no path. A
|
|
115
|
+
* guard on the global `fetch` refuses any other while the site runs; a public procedure declares none.
|
|
116
116
|
*/
|
|
117
117
|
export interface EgressField {
|
|
118
118
|
readonly egress?: readonly `https://${string}`[]
|
|
@@ -291,16 +291,18 @@ export interface Endpoint<T extends ProcedureType, I extends InputSchema | null,
|
|
|
291
291
|
/**
|
|
292
292
|
* An endpoint before `.handle`: a group refuses it. `Tk` are the kit's token purposes: a signed token field of the input
|
|
293
293
|
* reaches the handler as the claims of its purpose.
|
|
294
|
+
* The parameters are declared invariant, stricter than their structure: measuring that variance through `ExactReturn`
|
|
295
|
+
* exceeds TypeScript 7's instantiation limit (TS2589), and the compiler does not check an `in out` annotation.
|
|
294
296
|
*/
|
|
295
297
|
export interface EndpointBuilder<
|
|
296
|
-
T extends ProcedureType,
|
|
297
|
-
I extends InputSchema | null,
|
|
298
|
-
O extends Schema,
|
|
299
|
-
Acc,
|
|
300
|
-
D,
|
|
301
|
-
Ctx,
|
|
302
|
-
X = unknown,
|
|
303
|
-
Tk = Record<never, never>,
|
|
298
|
+
in out T extends ProcedureType,
|
|
299
|
+
in out I extends InputSchema | null,
|
|
300
|
+
in out O extends Schema,
|
|
301
|
+
in out Acc,
|
|
302
|
+
in out D,
|
|
303
|
+
in out Ctx,
|
|
304
|
+
in out X = unknown,
|
|
305
|
+
in out Tk = Record<never, never>,
|
|
304
306
|
> extends PendingEndpoint {
|
|
305
307
|
/** The endpoint's handler, typed by its access form and declaration; it may return only the output's fields. */
|
|
306
308
|
handle<const F extends KitHandler<WithClaims<HandlerInput<I, Acc>, Tk>, Ctx, HandlerReturn<F, O>, X>>(
|
package/src/contract/index.ts
CHANGED
|
@@ -14,6 +14,8 @@ export {
|
|
|
14
14
|
export { buildArtifact } from './build'
|
|
15
15
|
export { deniedCaseRuledOutBy } from './capability-cases'
|
|
16
16
|
export { compileContract, describeContract } from './compile'
|
|
17
|
+
// The type compileContract answers, so an application's declaration can name it (TS2883 otherwise).
|
|
18
|
+
export type { ContractArtifact } from '@fabrika/auth-core'
|
|
17
19
|
export {
|
|
18
20
|
type ConnectSnapshot,
|
|
19
21
|
describeSnapshot,
|
package/src/dev/launcher.ts
CHANGED
|
@@ -19,6 +19,7 @@ import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'
|
|
|
19
19
|
import { dirname, join } from 'node:path'
|
|
20
20
|
import { fileURLToPath } from 'node:url'
|
|
21
21
|
import { z } from 'zod/v4'
|
|
22
|
+
import { environmentScrubber, type Scrubber } from '../jobs/scrub.js'
|
|
22
23
|
import { makeProcessUndumpable } from '../undumpable.js'
|
|
23
24
|
import { bootstrapReason, migrationsChanged, webSourceChanged } from './changes.js'
|
|
24
25
|
import { OutputRing, type OutputSink, type OutputStream, processOutput, readLines } from './output.js'
|
|
@@ -152,20 +153,19 @@ const SUPERSEDED = 'a newer bootstrap run replaced this container; it stops foll
|
|
|
152
153
|
|
|
153
154
|
const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms))
|
|
154
155
|
|
|
155
|
-
/**
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
add(url)
|
|
163
|
-
if (url !== undefined && URL.canParse(url)) add(decodeURIComponent(new URL(url).password))
|
|
164
|
-
}
|
|
156
|
+
/**
|
|
157
|
+
* The scrubber of every line the launcher logs or reports: Operations' rule over the launcher's environment, with the
|
|
158
|
+
* runtime credential it derives, so the provisioning and `source` credentials count as the framework's secrets.
|
|
159
|
+
*/
|
|
160
|
+
function outputScrubber(environment: Readonly<Record<string, string>>, runtimeUrl: string | null): Scrubber {
|
|
161
|
+
const framework: Record<string, string> = {}
|
|
162
|
+
const application: Record<string, string> = {}
|
|
165
163
|
for (const [name, value] of Object.entries(environment)) {
|
|
166
|
-
if (
|
|
164
|
+
if (name.startsWith('FABRIKA_')) framework[name] = value
|
|
165
|
+
else application[name] = value
|
|
167
166
|
}
|
|
168
|
-
|
|
167
|
+
if (runtimeUrl !== null) framework[RUNTIME_DATABASE_URL] = runtimeUrl
|
|
168
|
+
return environmentScrubber({ framework, application })
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
const withoutFramework = (environment: Readonly<Record<string, string>>): Record<string, string> =>
|
|
@@ -215,7 +215,7 @@ class Launcher {
|
|
|
215
215
|
if (runtimeUrl !== null) application[RUNTIME_DATABASE_URL] = runtimeUrl
|
|
216
216
|
this.applicationEnvironment = application
|
|
217
217
|
this.commandEnvironment = withoutFramework(options.environment)
|
|
218
|
-
this.ring = new OutputRing(RING_LINES,
|
|
218
|
+
this.ring = new OutputRing(RING_LINES, outputScrubber(options.environment, runtimeUrl), options.output ?? processOutput)
|
|
219
219
|
}
|
|
220
220
|
|
|
221
221
|
async start(): Promise<void> {
|
|
@@ -584,8 +584,7 @@ class Launcher {
|
|
|
584
584
|
const child = Bun.spawn([...argv], { cwd: this.root, env, stdin: 'ignore', stdout: 'pipe', stderr: 'pipe' })
|
|
585
585
|
const lines: string[] = []
|
|
586
586
|
const collect = (stream: OutputStream) => (line: string) => {
|
|
587
|
-
lines.push(line)
|
|
588
|
-
this.ring.push(classify(stream, line), line)
|
|
587
|
+
lines.push(this.ring.push(classify(stream, line), line))
|
|
589
588
|
}
|
|
590
589
|
const timer = setTimeout(() => child.kill('SIGKILL'), timeoutMs)
|
|
591
590
|
await Promise.all([readLines(child.stdout, collect('stdout')), readLines(child.stderr, collect('stderr')), child.exited])
|
package/src/dev/output.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// The launcher's view of output: every line of the application and of the commands it runs is forwarded to its own
|
|
2
2
|
// stream, as Zerops' runtime log reads it, and the newest lines are kept to report. Secret values never reach either.
|
|
3
3
|
|
|
4
|
+
import type { Scrubber } from '../jobs/scrub.js'
|
|
4
5
|
import { DEV_REPORT_MAX_LINE_CHARACTERS, type DevLogLine } from './wire.js'
|
|
5
6
|
|
|
6
7
|
export type OutputStream = DevLogLine['stream']
|
|
@@ -17,22 +18,24 @@ export const processOutput: OutputSink = {
|
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
|
-
* The newest `capacity` lines, numbered from 0 since the launcher started,
|
|
21
|
-
* mark starts the lines a report carries: those of the newest sync or reset, and the output after it.
|
|
21
|
+
* The newest `capacity` lines, numbered from 0 since the launcher started, each scrubbed of secrets once, as it arrives.
|
|
22
|
+
* A mark starts the lines a report carries: those of the newest sync or reset, and the output after it.
|
|
22
23
|
*/
|
|
23
24
|
export class OutputRing {
|
|
24
25
|
private readonly lines: DevLogLine[] = []
|
|
25
26
|
private next = 0
|
|
26
27
|
private marked = 0
|
|
27
28
|
|
|
28
|
-
constructor(private readonly capacity: number, private readonly
|
|
29
|
+
constructor(private readonly capacity: number, private readonly scrubber: Scrubber, private readonly sink: OutputSink) {}
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
/** Log and keep `text` scrubbed, and return the scrubbed line: a caller keeps that copy and never the raw one. */
|
|
32
|
+
push(stream: OutputStream, text: string): string {
|
|
33
|
+
const line = this.scrubber.scrub(text)
|
|
32
34
|
this.sink.write(stream, line)
|
|
33
35
|
this.lines.push({ n: this.next, stream, text: line.slice(0, DEV_REPORT_MAX_LINE_CHARACTERS) })
|
|
34
36
|
this.next += 1
|
|
35
37
|
if (this.lines.length > this.capacity) this.lines.shift()
|
|
38
|
+
return line
|
|
36
39
|
}
|
|
37
40
|
|
|
38
41
|
/** Report from the next line on: the lines before it belong to an older head. */
|
|
@@ -45,12 +48,6 @@ export class OutputRing {
|
|
|
45
48
|
const since = this.lines.filter((line) => line.n >= this.marked)
|
|
46
49
|
return since.slice(Math.max(0, since.length - count))
|
|
47
50
|
}
|
|
48
|
-
|
|
49
|
-
private redact(text: string): string {
|
|
50
|
-
let line = text
|
|
51
|
-
for (const secret of this.secrets) line = line.replaceAll(secret, '[redacted]')
|
|
52
|
-
return line
|
|
53
|
-
}
|
|
54
51
|
}
|
|
55
52
|
|
|
56
53
|
/** Feed `stream` to `onLine` one line at a time; a last line without a newline is fed when the stream ends. */
|
package/src/files/kinds.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// The runtime of file kinds (`c.files({ types, maxBytes, scan? })`): a stored file is a value in a table column, and
|
|
1
|
+
// The runtime of file kinds (`c.files({ types, maxBytes, scan?, inline? })`): a stored file is a value in a table column, and
|
|
2
2
|
// the table's policy is its authority. This module stages a file (an incoming part or a `ctx.files.put`): its pending
|
|
3
3
|
// row first, on a connection of its own, then its object, sniffed and bounded by the kind. It serves and opens a file
|
|
4
4
|
// only through a reference the work holds and may read, and only a live one, by its scanned version. The sweep
|
package/src/files/sniff.ts
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// a file is stored only when its bytes are that type. A collection may admit only the types listed here, because
|
|
3
3
|
// the framework cannot verify any other.
|
|
4
4
|
|
|
5
|
+
import { INLINE_DOCUMENT_TYPES } from '@fabrika/proxy-core/headers'
|
|
6
|
+
|
|
5
7
|
/** How many leading bytes an upload is judged by. */
|
|
6
8
|
export const SNIFF_BYTES = 512
|
|
7
9
|
|
|
@@ -45,7 +47,10 @@ const BINARY_SIGNATURES: readonly Signature[] = [
|
|
|
45
47
|
/** Types judged as text: UTF-8 without NUL bytes, and no binary signature. SVG is text, and always an attachment. */
|
|
46
48
|
const TEXT_TYPES: readonly string[] = ['text/plain', 'text/csv', 'text/markdown', 'application/json', 'image/svg+xml']
|
|
47
49
|
|
|
48
|
-
|
|
50
|
+
/** XML documents (a feed, a sitemap), the ones an inline kind shows: text whose first character, after a BOM and white space, is `<`. */
|
|
51
|
+
const XML_TYPES: readonly string[] = INLINE_DOCUMENT_TYPES
|
|
52
|
+
|
|
53
|
+
export const SNIFFABLE_TYPES: readonly string[] = [...BINARY_SIGNATURES.flatMap((signature) => signature.types), ...TEXT_TYPES, ...XML_TYPES]
|
|
49
54
|
|
|
50
55
|
function isText(head: Uint8Array): boolean {
|
|
51
56
|
if (head.includes(0) || BINARY_SIGNATURES.some((signature) => signature.matches(head))) {
|
|
@@ -66,5 +71,17 @@ export function bytesAreOfType(type: string, head: Uint8Array): boolean {
|
|
|
66
71
|
if (signature !== undefined) {
|
|
67
72
|
return signature.matches(head)
|
|
68
73
|
}
|
|
74
|
+
if (XML_TYPES.includes(type)) {
|
|
75
|
+
return isText(head) && startsWithMarkup(head)
|
|
76
|
+
}
|
|
69
77
|
return TEXT_TYPES.includes(type) && isText(head)
|
|
70
78
|
}
|
|
79
|
+
|
|
80
|
+
const UTF8_BOM = [0xef, 0xbb, 0xbf]
|
|
81
|
+
const XML_WHITE_SPACE: readonly number[] = [0x20, 0x09, 0x0a, 0x0d]
|
|
82
|
+
|
|
83
|
+
function startsWithMarkup(head: Uint8Array): boolean {
|
|
84
|
+
let offset = bytesAt(head, 0, UTF8_BOM) ? UTF8_BOM.length : 0
|
|
85
|
+
while (offset < head.byteLength && XML_WHITE_SPACE.includes(head[offset] ?? -1)) offset++
|
|
86
|
+
return head[offset] === 0x3c
|
|
87
|
+
}
|
package/src/files/treatment.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// The file treatment (ADR-0022 rule 2): the header policy a download carries instead of the application's. Only
|
|
2
|
-
// the inert images are shown inline; every other type, SVG and HTML
|
|
3
|
-
// download contains can run as this origin. The proxy applies the same
|
|
2
|
+
// the inert images, and on a row of an inline kind the XML documents, are shown inline; every other type, SVG and HTML
|
|
3
|
+
// included, is an attachment, and nothing a download contains can run as this origin. The proxy applies the same
|
|
4
|
+
// definition, from `@fabrika/proxy-core/headers`.
|
|
4
5
|
|
|
5
|
-
import { applyFileTreatment, fileDisposition, withResponseHeaders } from '@fabrika/proxy-core/headers'
|
|
6
|
+
import { applyFileTreatment, FILE_TREATMENT, fileDisposition, INLINE_FILE_TYPES, withResponseHeaders } from '@fabrika/proxy-core/headers'
|
|
6
7
|
|
|
7
8
|
/** A file the download endpoint answers. `ContractRuntime` turns it into the response, with the file treatment. */
|
|
8
9
|
export class FileDownload {
|
|
@@ -26,23 +27,24 @@ function filenameParameter(name: string): string {
|
|
|
26
27
|
return `; filename*=UTF-8''${encoded}`
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
/** `inline` is the row's `inlineFileTypes`. */
|
|
31
|
+
export function contentDisposition(contentType: string, name: string | null, inline: readonly string[] = INLINE_FILE_TYPES): string {
|
|
32
|
+
return `${fileDisposition(contentType, inline)}${name === null ? '' : filenameParameter(name)}`
|
|
31
33
|
}
|
|
32
34
|
|
|
33
|
-
export function fileResponse(file: FileDownload): Response {
|
|
35
|
+
export function fileResponse(file: FileDownload, inline: readonly string[]): Response {
|
|
34
36
|
const headers = new Headers({
|
|
35
37
|
'Content-Type': file.contentType,
|
|
36
38
|
'Content-Length': String(file.size),
|
|
37
|
-
'Content-Disposition': contentDisposition(file.contentType, file.name),
|
|
39
|
+
'Content-Disposition': contentDisposition(file.contentType, file.name, inline),
|
|
38
40
|
// A Range request gets the whole file: ignoring Range is always a valid answer, and no partial path exists to get wrong.
|
|
39
41
|
'Accept-Ranges': 'none',
|
|
40
42
|
})
|
|
41
|
-
applyFileTreatment(headers)
|
|
43
|
+
applyFileTreatment(headers, FILE_TREATMENT, inline)
|
|
42
44
|
return new Response(file.body, { status: 200, headers })
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
/** An error of a download endpoint, with the file treatment in place of the header policy. */
|
|
46
|
-
export function withFileTreatment(response: Response): Response {
|
|
47
|
-
return withResponseHeaders(response, applyFileTreatment)
|
|
48
|
+
export function withFileTreatment(response: Response, inline: readonly string[]): Response {
|
|
49
|
+
return withResponseHeaders(response, (headers) => applyFileTreatment(headers, FILE_TREATMENT, inline))
|
|
48
50
|
}
|
|
@@ -2,78 +2,11 @@
|
|
|
2
2
|
// `Admissions`), so every instance shares them. Admission is not authorization: it only bounds how much one client
|
|
3
3
|
// calls a public procedure.
|
|
4
4
|
|
|
5
|
-
import { CLIENT_ADDRESS_HEADER } from '@fabrika/auth-core'
|
|
5
|
+
import { CLIENT_ADDRESS_HEADER, clientAddressKey } from '@fabrika/auth-core'
|
|
6
6
|
import { createHash } from 'node:crypto'
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const IPV4 = /^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$/
|
|
12
|
-
const HEXTET = /^[0-9a-f]{1,4}$/
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The eight groups of an IPv6 address, or `null`. An embedded IPv4 tail (`::ffff:192.0.2.1`) counts as two groups,
|
|
16
|
-
* and a zone id (`%eth0`) is dropped.
|
|
17
|
-
*/
|
|
18
|
-
function ipv6Groups(text: string): number[] | null {
|
|
19
|
-
const address = text.split('%')[0] ?? ''
|
|
20
|
-
const halves = address.split('::')
|
|
21
|
-
if (halves.length > 2) {
|
|
22
|
-
return null
|
|
23
|
-
}
|
|
24
|
-
const groupsOf = (half: string): number[] | null => {
|
|
25
|
-
if (half === '') {
|
|
26
|
-
return []
|
|
27
|
-
}
|
|
28
|
-
const groups: number[] = []
|
|
29
|
-
const parts = half.split(':')
|
|
30
|
-
for (const [index, part] of parts.entries()) {
|
|
31
|
-
if (index === parts.length - 1 && IPV4.test(part)) {
|
|
32
|
-
const [a = 0, b = 0, c = 0, d = 0] = part.split('.').map(Number)
|
|
33
|
-
groups.push(a * 256 + b, c * 256 + d)
|
|
34
|
-
} else if (HEXTET.test(part)) {
|
|
35
|
-
groups.push(Number.parseInt(part, 16))
|
|
36
|
-
} else {
|
|
37
|
-
return null
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return groups
|
|
41
|
-
}
|
|
42
|
-
const head = groupsOf(halves[0] ?? '')
|
|
43
|
-
const tail = halves.length === 2 ? groupsOf(halves[1] ?? '') : []
|
|
44
|
-
if (head === null || tail === null) {
|
|
45
|
-
return null
|
|
46
|
-
}
|
|
47
|
-
if (halves.length === 1) {
|
|
48
|
-
return head.length === 8 ? head : null
|
|
49
|
-
}
|
|
50
|
-
const missing = 8 - head.length - tail.length
|
|
51
|
-
return missing < 1 ? null : [...head, ...Array.from({ length: missing }, () => 0), ...tail]
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* The client a per-client limit counts: an IPv4 address, or an IPv6 address's /64 (one subscriber's prefix, of which
|
|
56
|
-
* it may hold every address). Anything else, the header's absence included, is one shared `unknown` client.
|
|
57
|
-
*/
|
|
58
|
-
export function clientKey(header: string | null): string {
|
|
59
|
-
const value = (header ?? '').split(',')[0]?.trim().toLowerCase() ?? ''
|
|
60
|
-
if (value === '' || value.length > MAX_ADDRESS_LENGTH) {
|
|
61
|
-
return 'unknown'
|
|
62
|
-
}
|
|
63
|
-
if (IPV4.test(value)) {
|
|
64
|
-
return value
|
|
65
|
-
}
|
|
66
|
-
const groups = ipv6Groups(value.startsWith('[') && value.endsWith(']') ? value.slice(1, -1) : value)
|
|
67
|
-
if (groups === null) {
|
|
68
|
-
return 'unknown'
|
|
69
|
-
}
|
|
70
|
-
const mapped = groups.slice(0, 5).every((group) => group === 0) && groups[5] === 0xffff
|
|
71
|
-
if (mapped) {
|
|
72
|
-
const [high = 0, low = 0] = groups.slice(6)
|
|
73
|
-
return [high >> 8, high & 0xff, low >> 8, low & 0xff].join('.')
|
|
74
|
-
}
|
|
75
|
-
return `${groups.slice(0, 4).map((group) => group.toString(16)).join(':')}::/64`
|
|
76
|
-
}
|
|
8
|
+
/** The one client every request without a readable address shares. */
|
|
9
|
+
const UNKNOWN_CLIENT = 'unknown'
|
|
77
10
|
|
|
78
11
|
/**
|
|
79
12
|
* The bucket of the client the proxy saw. Only `X-Fabrika-Client-Ip` is read: the proxy deletes it and sets it from
|
|
@@ -81,5 +14,6 @@ export function clientKey(header: string | null): string {
|
|
|
81
14
|
* client can write (`X-Forwarded-For`, `CF-Connecting-IP`) is never read. The address is kept as its digest.
|
|
82
15
|
*/
|
|
83
16
|
export function clientBucket(request: Request): string {
|
|
84
|
-
|
|
17
|
+
const key = clientAddressKey(request.headers.get(CLIENT_ADDRESS_HEADER)) ?? UNKNOWN_CLIENT
|
|
18
|
+
return `client:${createHash('sha256').update(key).digest('hex')}`
|
|
85
19
|
}
|
|
@@ -29,6 +29,7 @@ import {
|
|
|
29
29
|
PROXY_TOKEN_HEADER,
|
|
30
30
|
rawRequestTarget,
|
|
31
31
|
type RedirectShape,
|
|
32
|
+
sameOriginTarget,
|
|
32
33
|
satisfies,
|
|
33
34
|
type SchemaShape,
|
|
34
35
|
type ScopeBinding,
|
|
@@ -41,7 +42,7 @@ import { type CapabilityAuthority, type ContainerGrant, MAX_WITHIN_VALUES } from
|
|
|
41
42
|
import type { DataAccess, FileBinding, FrameworkTables } from '@fabrika/db/framework'
|
|
42
43
|
import type { DataKind } from '@fabrika/db/tables'
|
|
43
44
|
import type { TokenVerifier } from '@fabrika/jwks'
|
|
44
|
-
import { applyAnswerCache, withResponseHeaders } from '@fabrika/proxy-core/headers'
|
|
45
|
+
import { applyAnswerCache, INLINE_FILE_TYPES, inlineFileTypes, withResponseHeaders } from '@fabrika/proxy-core/headers'
|
|
45
46
|
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
46
47
|
import { createHash, timingSafeEqual } from 'node:crypto'
|
|
47
48
|
import { z } from 'zod/v4'
|
|
@@ -412,10 +413,11 @@ export class ContractRuntime {
|
|
|
412
413
|
async fetch(request: Request, exec?: RequestExecutionContext): Promise<Response> {
|
|
413
414
|
const plan = planEndpoint(this.#parts.table, request.method, rawRequestTarget(request.url).pathname)
|
|
414
415
|
const answer = await this.#answer(request, plan, exec)
|
|
416
|
+
const inline = plan.outcome === 'endpoint' ? inlineFileTypes(plan.endpoint.response) : INLINE_FILE_TYPES
|
|
415
417
|
const answered = answer instanceof FileDownload
|
|
416
|
-
? fileResponse(answer)
|
|
418
|
+
? fileResponse(answer, inline)
|
|
417
419
|
: plan.outcome === 'endpoint' && answersFile(plan.endpoint)
|
|
418
|
-
? withFileTreatment(answer)
|
|
420
|
+
? withFileTreatment(answer, inline)
|
|
419
421
|
: withHeaderPolicy(answer, this.#parts.policy)
|
|
420
422
|
if (plan.outcome !== 'endpoint' || plan.endpoint.cache === undefined) {
|
|
421
423
|
return answered
|
|
@@ -1894,7 +1896,7 @@ function readQuery(
|
|
|
1894
1896
|
*/
|
|
1895
1897
|
function redirectResponse(row: EndpointRow, shape: RedirectShape, output: unknown, request: Request, carried: ReadonlySet<string>): Response {
|
|
1896
1898
|
const to = isRecord(output) && typeof output['to'] === 'string' ? output['to'] : ''
|
|
1897
|
-
const own =
|
|
1899
|
+
const own = sameOriginTarget(new URL(request.url).origin, to) !== null
|
|
1898
1900
|
let listed = false
|
|
1899
1901
|
if (!own) {
|
|
1900
1902
|
try {
|
package/src/implement/egress.ts
CHANGED
|
@@ -1,23 +1,41 @@
|
|
|
1
|
-
// `egress`: while a site's handler runs, the global `fetch` reaches only the HTTPS origins the site declares
|
|
2
|
-
//
|
|
3
|
-
// boot, or opens its own sockets, escapes it.
|
|
4
|
-
// run outside the site's scope, under their own
|
|
1
|
+
// `egress`: while a site's handler runs, the global `fetch` reaches only the HTTPS origins the site declares, exactly or
|
|
2
|
+
// by a one-label pattern (`egressAdmits` of `@fabrika/auth-core`). It stops a mistake and makes a bypass conspicuous; it
|
|
3
|
+
// is not a sandbox (ADR-0012 §8): code that kept the `fetch` it found before boot, or opens its own sockets, escapes it.
|
|
4
|
+
// The framework's own facilities (mail, connectors, IAM, the object store) run outside the site's scope, under their own
|
|
5
|
+
// declarations.
|
|
5
6
|
|
|
7
|
+
import { egressAdmits, type EgressEntry, parseEgressEntry } from '@fabrika/auth-core'
|
|
6
8
|
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
7
9
|
import { CapabilityRefusedError } from './capability'
|
|
8
10
|
|
|
9
11
|
interface EgressScope {
|
|
10
12
|
/** The site, as a refusal names it. */
|
|
11
13
|
readonly site: string
|
|
12
|
-
readonly
|
|
14
|
+
readonly entries: readonly EgressEntry[]
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
const scope = new AsyncLocalStorage<EgressScope>()
|
|
16
18
|
const guards = new WeakSet<object>()
|
|
19
|
+
const parsed = new WeakMap<readonly string[], readonly EgressEntry[]>()
|
|
17
20
|
|
|
18
21
|
/** Runs a site's handler: every `fetch` it starts reaches only `egress`. */
|
|
19
22
|
export function runAsSite<T>(site: string, egress: readonly string[], work: () => Promise<T>): Promise<T> {
|
|
20
|
-
return scope.run({ site,
|
|
23
|
+
return scope.run({ site, entries: entriesOf(egress) }, work)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The approved declaration's entries, parsed once per list. The build and the artifact parser refused every other
|
|
28
|
+
* value, so one that does not parse here admits nothing.
|
|
29
|
+
*/
|
|
30
|
+
function entriesOf(egress: readonly string[]): readonly EgressEntry[] {
|
|
31
|
+
const known = parsed.get(egress)
|
|
32
|
+
if (known !== undefined) return known
|
|
33
|
+
const entries = egress.flatMap((value) => {
|
|
34
|
+
const entry = parseEgressEntry(value)
|
|
35
|
+
return entry.ok ? [entry.entry] : []
|
|
36
|
+
})
|
|
37
|
+
parsed.set(egress, entries)
|
|
38
|
+
return entries
|
|
21
39
|
}
|
|
22
40
|
|
|
23
41
|
/**
|
|
@@ -56,16 +74,16 @@ export function installEgressGuard(): void {
|
|
|
56
74
|
if (running === undefined) {
|
|
57
75
|
return current(input, init)
|
|
58
76
|
}
|
|
59
|
-
const
|
|
60
|
-
if (
|
|
77
|
+
const url = urlOf(input)
|
|
78
|
+
if (url !== null && egressAdmits(running.entries, url)) {
|
|
61
79
|
return current(input, init)
|
|
62
80
|
}
|
|
63
81
|
return Promise.reject(
|
|
64
82
|
new CapabilityRefusedError(
|
|
65
83
|
'undeclared_egress',
|
|
66
|
-
|
|
84
|
+
url === null
|
|
67
85
|
? `${running.site} called fetch with a URL that is not absolute`
|
|
68
|
-
: `${running.site} calls ${origin}, which it does not declare (egress: ['${origin}'])`,
|
|
86
|
+
: `${running.site} calls ${url.origin}, which it does not declare (egress: ['${url.origin}'])`,
|
|
69
87
|
),
|
|
70
88
|
)
|
|
71
89
|
},
|
|
@@ -75,9 +93,9 @@ export function installEgressGuard(): void {
|
|
|
75
93
|
globalThis.fetch = guarded
|
|
76
94
|
}
|
|
77
95
|
|
|
78
|
-
function
|
|
96
|
+
function urlOf(input: string | URL | Request): URL | null {
|
|
79
97
|
try {
|
|
80
|
-
return new URL(typeof input === 'string' ? input : input instanceof URL ? input.href : input.url)
|
|
98
|
+
return new URL(typeof input === 'string' ? input : input instanceof URL ? input.href : input.url)
|
|
81
99
|
} catch {
|
|
82
100
|
return null
|
|
83
101
|
}
|
package/src/implement/public.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// public procedure's challenge, and its time limit. The buckets live in the application's database
|
|
3
3
|
// (`@fabrika/db`'s `Admissions`, keyed by the endpoint id), so every instance shares them. Admission is not authorization: it bounds how much one client, one caller and everyone together send.
|
|
4
4
|
|
|
5
|
-
import { type AdmissionRate, CLIENT_ADDRESS_HEADER, type ContractArtifact, type ProcedureArtifact } from '@fabrika/auth-core'
|
|
5
|
+
import { type AdmissionRate, CLIENT_ADDRESS_HEADER, clientAddressKey, type ContractArtifact, type ProcedureArtifact } from '@fabrika/auth-core'
|
|
6
6
|
import { createHash } from 'node:crypto'
|
|
7
7
|
import { ALTCHA_LIMITS } from '../contract/defaults'
|
|
8
8
|
import { REQUEST_BUDGET_MS } from './bounded'
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
type ChallengeWork,
|
|
15
15
|
turnstileVerifier,
|
|
16
16
|
} from './challenge'
|
|
17
|
-
import { clientBucket
|
|
17
|
+
import { clientBucket } from './client-address'
|
|
18
18
|
import type { RequestClient } from './types'
|
|
19
19
|
|
|
20
20
|
/** The bucket every call of an endpoint takes from; a client's is `client:<digest>`, a caller's `caller:<digest>`. */
|
|
@@ -128,7 +128,7 @@ export function requestClient(request: Request, vouched: boolean): RequestClient
|
|
|
128
128
|
const address = vouched ? request.headers.get(CLIENT_ADDRESS_HEADER)?.trim() ?? '' : ''
|
|
129
129
|
const agent = request.headers.get('User-Agent')
|
|
130
130
|
return Object.freeze({
|
|
131
|
-
ip: address === '' ||
|
|
131
|
+
ip: address === '' || clientAddressKey(address) === null ? null : address,
|
|
132
132
|
userAgent: agent === null || agent === '' ? null : agent.slice(0, MAX_USER_AGENT_LENGTH),
|
|
133
133
|
})
|
|
134
134
|
}
|
package/src/jobs/report.ts
CHANGED
|
@@ -32,6 +32,8 @@ export interface OperationsTarget {
|
|
|
32
32
|
const PUBLIC_KEY = /^[0-9a-f]{32}$/
|
|
33
33
|
const PROJECT_ID = /^[1-9][0-9]{0,18}$/
|
|
34
34
|
const DELIVERY_TIMEOUT_MS = 5_000
|
|
35
|
+
const RETRY_DELAY_MIN_MS = 200
|
|
36
|
+
const RETRY_DELAY_JITTER_MS = 300
|
|
35
37
|
|
|
36
38
|
/** The envelope endpoint and key of a DSN `https://<key>@<host>/<project>`, or `null` when it is not one. */
|
|
37
39
|
export function operationsTarget(dsn: string): OperationsTarget | null {
|
|
@@ -135,7 +137,16 @@ export function crashEnvelope(kind: CrashKind, error: unknown, options: Envelope
|
|
|
135
137
|
return eventEnvelope({ tags: { crash: kind }, values: exceptionChain(error, options.scrubber) }, options, now)
|
|
136
138
|
}
|
|
137
139
|
|
|
138
|
-
|
|
140
|
+
/**
|
|
141
|
+
* One delivery attempt. `retry` is a network failure, a timeout or a 5xx: the retry resends the same body, so the same
|
|
142
|
+
* `event_id`, which the ingest ignores when it already stored it.
|
|
143
|
+
*/
|
|
144
|
+
type Attempt = { readonly kind: 'delivered' } | { readonly kind: 'refused'; readonly status: number } | {
|
|
145
|
+
readonly kind: 'retry'
|
|
146
|
+
readonly status: number | null
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function attempt(options: OperationsReporterOptions, body: string): Promise<Attempt> {
|
|
139
150
|
const send = options.fetch ?? ((request: Request) => fetch(request))
|
|
140
151
|
const timer = deadline(DELIVERY_TIMEOUT_MS)
|
|
141
152
|
const request = new Request(options.target.envelopeUrl, {
|
|
@@ -150,17 +161,33 @@ async function deliver(options: OperationsReporterOptions, body: string): Promis
|
|
|
150
161
|
try {
|
|
151
162
|
const response = await send(request)
|
|
152
163
|
await response.body?.cancel()
|
|
153
|
-
if (
|
|
154
|
-
|
|
164
|
+
if (response.ok) {
|
|
165
|
+
return { kind: 'delivered' }
|
|
155
166
|
}
|
|
167
|
+
return response.status >= 500 ? { kind: 'retry', status: response.status } : { kind: 'refused', status: response.status }
|
|
156
168
|
} catch {
|
|
157
|
-
// The error may quote the ingest URL with its key.
|
|
158
|
-
|
|
169
|
+
// The error may quote the ingest URL with its key, so it is not kept.
|
|
170
|
+
return { kind: 'retry', status: null }
|
|
159
171
|
} finally {
|
|
160
172
|
timer.clear()
|
|
161
173
|
}
|
|
162
174
|
}
|
|
163
175
|
|
|
176
|
+
/** One retry after a short jittered pause, so a transient ingress fault (a reset, a timeout, a 5xx) loses no report. */
|
|
177
|
+
async function deliver(options: OperationsReporterOptions, body: string): Promise<void> {
|
|
178
|
+
let outcome = await attempt(options, body)
|
|
179
|
+
if (outcome.kind === 'retry') {
|
|
180
|
+
await new Promise<void>((resolve) => setTimeout(resolve, RETRY_DELAY_MIN_MS + Math.random() * RETRY_DELAY_JITTER_MS))
|
|
181
|
+
outcome = await attempt(options, body)
|
|
182
|
+
}
|
|
183
|
+
if (outcome.kind === 'delivered') {
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
console.error(
|
|
187
|
+
outcome.status === null ? 'fabrika: a report to Operations could not be delivered' : `fabrika: a report to Operations answered ${outcome.status}`,
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
|
|
164
191
|
export function operationsReporter(options: OperationsReporterOptions): OperationsReporter {
|
|
165
192
|
return {
|
|
166
193
|
report: (report) => deliver(options, reportEnvelope(report, options, new Date())),
|
package/src/jobs/scrub.ts
CHANGED
|
@@ -41,7 +41,7 @@ const PUBLIC_FRAMEWORK_VARIABLES: ReadonlySet<string> = new Set([
|
|
|
41
41
|
* An application variable whose name says it holds a secret. The runtime does not know which names the application
|
|
42
42
|
* declared as secrets, so the name decides (backlog 176).
|
|
43
43
|
*/
|
|
44
|
-
const SECRET_NAME = /secret|token|
|
|
44
|
+
const SECRET_NAME = /secret|token|pass|private|credential|key|auth|dsn|salt|connection|cookie|session|signature|cert|database_?ur[il]|db_?ur[il]/i
|
|
45
45
|
|
|
46
46
|
const JWT = /\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*/g
|
|
47
47
|
const AUTHORIZATION_SCHEME = /\b(Bearer|Basic|Token)\s+[A-Za-z0-9._~+/=-]{8,}/gi
|
|
@@ -415,7 +415,8 @@ export function endpointAccessKit<DB, P>(
|
|
|
415
415
|
challengeFetch: () => Promise.resolve(Response.json({ success: false, 'error-codes': ['invalid-input-response'] })),
|
|
416
416
|
})
|
|
417
417
|
const policies = tablePolicies(tables)
|
|
418
|
-
|
|
418
|
+
// A view's rows come from the tables it reads, which the kit seeds instead.
|
|
419
|
+
const tableNames = Object.keys(policies).filter((name) => policies[name]?.view !== true).sort()
|
|
419
420
|
|
|
420
421
|
const ownerOf = (dimension: string): string | undefined =>
|
|
421
422
|
tableNames.find((name) => {
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// A small file the testing kits write: the leading bytes `files/sniff.ts` accepts for its type, then a text marker.
|
|
2
2
|
|
|
3
|
+
import { INLINE_DOCUMENT_TYPES } from '@fabrika/proxy-core/headers'
|
|
4
|
+
|
|
3
5
|
const randomHex = (): string => crypto.randomUUID().replaceAll('-', '').slice(0, 8)
|
|
4
6
|
|
|
5
7
|
const ascii = (text: string): number[] => [...text].map((char) => char.charCodeAt(0))
|
|
6
8
|
const ZIP_HEAD = [0x50, 0x4b, 0x03, 0x04]
|
|
7
9
|
|
|
8
|
-
/** The leading bytes `files/sniff.ts` accepts for each binary type it verifies;
|
|
10
|
+
/** The leading bytes `files/sniff.ts` accepts for each binary or XML type it verifies; another text type needs none. */
|
|
9
11
|
const SAMPLE_HEADS: Readonly<Record<string, readonly number[]>> = {
|
|
10
12
|
'image/png': [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a],
|
|
11
13
|
'image/jpeg': [0xff, 0xd8, 0xff, 0xe0],
|
|
@@ -17,6 +19,7 @@ const SAMPLE_HEADS: Readonly<Record<string, readonly number[]>> = {
|
|
|
17
19
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ZIP_HEAD,
|
|
18
20
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ZIP_HEAD,
|
|
19
21
|
'application/vnd.openxmlformats-officedocument.presentationml.presentation': ZIP_HEAD,
|
|
22
|
+
...Object.fromEntries(INLINE_DOCUMENT_TYPES.map((type) => [type, ascii('<?xml version="1.0"?>\n')])),
|
|
20
23
|
}
|
|
21
24
|
const TEXT_TYPES: ReadonlySet<string> = new Set(['text/plain', 'text/csv', 'text/markdown', 'application/json', 'image/svg+xml'])
|
|
22
25
|
|
|
@@ -258,7 +258,7 @@ export interface WebhookMountDeclaration<S> extends OptionsRecord {
|
|
|
258
258
|
readonly declared?: unknown;
|
|
259
259
|
}
|
|
260
260
|
/**
|
|
261
|
-
* A file kind (`c.files({ types, maxBytes, scan? })`, no `owner`): a class of stored files a table's file column holds.
|
|
261
|
+
* A file kind (`c.files({ types, maxBytes, scan?, inline? })`, no `owner`): a class of stored files a table's file column holds.
|
|
262
262
|
* It has no owner, no actions and no endpoint.
|
|
263
263
|
*/
|
|
264
264
|
export interface FileKindDeclaration extends OptionsRecord {
|
|
@@ -267,6 +267,8 @@ export interface FileKindDeclaration extends OptionsRecord {
|
|
|
267
267
|
readonly maxBytes: number;
|
|
268
268
|
/** Every file waits `quarantined` until the scan gives it a clean verdict; a byte consumer answers 409 until then. */
|
|
269
269
|
readonly scan?: boolean;
|
|
270
|
+
/** Its files are shown, not downloaded: only inert images and the XML documents a machine reads. */
|
|
271
|
+
readonly inline?: boolean;
|
|
270
272
|
}
|
|
271
273
|
export interface CronDeclaration extends OptionsRecord {
|
|
272
274
|
readonly kind: 'cron';
|
|
@@ -224,7 +224,7 @@ export interface WebhookMount<B extends string, Acts extends string, I extends S
|
|
|
224
224
|
readonly actions: readonly Acts[];
|
|
225
225
|
readonly input: I;
|
|
226
226
|
}
|
|
227
|
-
/** A file kind `c.files({ types, maxBytes, scan? })` declares: what a file column of that kind holds. */
|
|
227
|
+
/** A file kind `c.files({ types, maxBytes, scan?, inline? })` declares: what a file column of that kind holds. */
|
|
228
228
|
export interface FileKind extends FileKindDeclaration {
|
|
229
229
|
}
|
|
230
230
|
export interface CronJob<Acts extends string> extends CronDeclaration {
|
|
@@ -331,6 +331,11 @@ export interface Kit<A extends string, D extends string, CD extends string> {
|
|
|
331
331
|
readonly maxBytes: number;
|
|
332
332
|
/** Each file is scanned for malware and served only after a clean verdict; until then it answers 409. */
|
|
333
333
|
readonly scan?: boolean;
|
|
334
|
+
/**
|
|
335
|
+
* Each file is shown, not downloaded (`Content-Disposition: inline`): a feed or a sitemap a machine reads. Every
|
|
336
|
+
* type is an inert image or `application/xml`, `text/xml`, `application/rss+xml`, `application/atom+xml`.
|
|
337
|
+
*/
|
|
338
|
+
readonly inline?: boolean;
|
|
334
339
|
} & Owned<'kind'>): FileKind;
|
|
335
340
|
/** A job of the whole application, holding `actions`; it runs on its schedule (UTC, five fields) and when kicked. */
|
|
336
341
|
cron<const Acts extends readonly [A, ...A[]]>(schedule: string, options: {
|
|
@@ -70,8 +70,8 @@ export type SignsField<Sg, Purposes extends string> = {
|
|
|
70
70
|
readonly signs: Sg & NoInfer<KnownPurposes<Sg, Purposes>>;
|
|
71
71
|
});
|
|
72
72
|
/**
|
|
73
|
-
* The HTTPS origins a site's code calls (`fetch`): each an exact origin, no path
|
|
74
|
-
* `fetch` refuses any other while the site runs; a public procedure declares none.
|
|
73
|
+
* The HTTPS origins a site's code calls (`fetch`): each an exact origin or `https://*.<domain>`, one label, no path. A
|
|
74
|
+
* guard on the global `fetch` refuses any other while the site runs; a public procedure declares none.
|
|
75
75
|
*/
|
|
76
76
|
export interface EgressField {
|
|
77
77
|
readonly egress?: readonly `https://${string}`[];
|
|
@@ -208,8 +208,10 @@ export interface Endpoint<T extends ProcedureType, I extends InputSchema | null,
|
|
|
208
208
|
/**
|
|
209
209
|
* An endpoint before `.handle`: a group refuses it. `Tk` are the kit's token purposes: a signed token field of the input
|
|
210
210
|
* reaches the handler as the claims of its purpose.
|
|
211
|
+
* The parameters are declared invariant, stricter than their structure: measuring that variance through `ExactReturn`
|
|
212
|
+
* exceeds TypeScript 7's instantiation limit (TS2589), and the compiler does not check an `in out` annotation.
|
|
211
213
|
*/
|
|
212
|
-
export interface EndpointBuilder<T extends ProcedureType, I extends InputSchema | null, O extends Schema, Acc, D, Ctx, X = unknown, Tk = Record<never, never>> extends PendingEndpoint {
|
|
214
|
+
export interface EndpointBuilder<in out T extends ProcedureType, in out I extends InputSchema | null, in out O extends Schema, in out Acc, in out D, in out Ctx, in out X = unknown, in out Tk = Record<never, never>> extends PendingEndpoint {
|
|
213
215
|
/** The endpoint's handler, typed by its access form and declaration; it may return only the output's fields. */
|
|
214
216
|
handle<const F extends KitHandler<WithClaims<HandlerInput<I, Acc>, Tk>, Ctx, HandlerReturn<F, O>, X>>(handler: F): ExactReturn<F, O, Endpoint<T, I, O, Acc, D>>;
|
|
215
217
|
}
|
|
@@ -3,6 +3,7 @@ export { type Attachment, attachment, type AttachmentBrand, type AttachmentMark,
|
|
|
3
3
|
export { buildArtifact } from './build';
|
|
4
4
|
export { deniedCaseRuledOutBy } from './capability-cases';
|
|
5
5
|
export { compileContract, describeContract } from './compile';
|
|
6
|
+
export type { ContractArtifact } from '@fabrika/auth-core';
|
|
6
7
|
export { type ConnectSnapshot, describeSnapshot, exportDescription, exportDescriptionSchema, incompatibleChanges, incompatibleMountChanges, parseExportDescription, type SnapshotProcedure, } from './connect';
|
|
7
8
|
export type * from './context';
|
|
8
9
|
export * from './declaration';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Scrubber } from '../jobs/scrub.js';
|
|
1
2
|
import { type DevLogLine } from './wire.js';
|
|
2
3
|
export type OutputStream = DevLogLine['stream'];
|
|
3
4
|
export interface OutputSink {
|
|
@@ -6,23 +7,23 @@ export interface OutputSink {
|
|
|
6
7
|
/** The launcher process's own stdout and stderr. */
|
|
7
8
|
export declare const processOutput: OutputSink;
|
|
8
9
|
/**
|
|
9
|
-
* The newest `capacity` lines, numbered from 0 since the launcher started,
|
|
10
|
-
* mark starts the lines a report carries: those of the newest sync or reset, and the output after it.
|
|
10
|
+
* The newest `capacity` lines, numbered from 0 since the launcher started, each scrubbed of secrets once, as it arrives.
|
|
11
|
+
* A mark starts the lines a report carries: those of the newest sync or reset, and the output after it.
|
|
11
12
|
*/
|
|
12
13
|
export declare class OutputRing {
|
|
13
14
|
private readonly capacity;
|
|
14
|
-
private readonly
|
|
15
|
+
private readonly scrubber;
|
|
15
16
|
private readonly sink;
|
|
16
17
|
private readonly lines;
|
|
17
18
|
private next;
|
|
18
19
|
private marked;
|
|
19
|
-
constructor(capacity: number,
|
|
20
|
-
|
|
20
|
+
constructor(capacity: number, scrubber: Scrubber, sink: OutputSink);
|
|
21
|
+
/** Log and keep `text` scrubbed, and return the scrubbed line: a caller keeps that copy and never the raw one. */
|
|
22
|
+
push(stream: OutputStream, text: string): string;
|
|
21
23
|
/** Report from the next line on: the lines before it belong to an older head. */
|
|
22
24
|
mark(): void;
|
|
23
25
|
/** The newest `count` lines since the mark, oldest first. */
|
|
24
26
|
tail(count: number): DevLogLine[];
|
|
25
|
-
private redact;
|
|
26
27
|
}
|
|
27
28
|
/** Feed `stream` to `onLine` one line at a time; a last line without a newline is fed when the stream ends. */
|
|
28
29
|
export declare function readLines(stream: ReadableStream<Uint8Array>, onLine: (line: string) => void): Promise<void>;
|
|
@@ -12,7 +12,8 @@ export declare class FileDownload {
|
|
|
12
12
|
readonly name: string | null;
|
|
13
13
|
});
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
export declare function
|
|
15
|
+
/** `inline` is the row's `inlineFileTypes`. */
|
|
16
|
+
export declare function contentDisposition(contentType: string, name: string | null, inline?: readonly string[]): string;
|
|
17
|
+
export declare function fileResponse(file: FileDownload, inline: readonly string[]): Response;
|
|
17
18
|
/** An error of a download endpoint, with the file treatment in place of the header policy. */
|
|
18
|
-
export declare function withFileTreatment(response: Response): Response;
|
|
19
|
+
export declare function withFileTreatment(response: Response, inline: readonly string[]): Response;
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The client a per-client limit counts: an IPv4 address, or an IPv6 address's /64 (one subscriber's prefix, of which
|
|
3
|
-
* it may hold every address). Anything else, the header's absence included, is one shared `unknown` client.
|
|
4
|
-
*/
|
|
5
|
-
export declare function clientKey(header: string | null): string;
|
|
6
1
|
/**
|
|
7
2
|
* The bucket of the client the proxy saw. Only `X-Fabrika-Client-Ip` is read: the proxy deletes it and sets it from
|
|
8
3
|
* the connection on every request, and the proxy secret proves the request came through it. A forwarding header a
|