@fabrika/app 0.9.0-dev.10 → 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/skill/references/tests.md +12 -6
- 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/defaults.ts +5 -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 +190 -60
- package/src/testing/sample-file.ts +4 -1
- package/types/src/contract/declaration.d.ts +3 -1
- package/types/src/contract/defaults.d.ts +4 -2
- 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/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
|