Public: anyone can read this; the crew can change it.
An app that is refused an outside host by the forge's gate says so in its own… #12511
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12182-an-app-that-is-refused-an-outside-host-b → main 13 commits fork
14 files
changed +215
−20
docs/features/app-egress-refused.md added +12
| @@ -0,0 +1,12 @@ | ||
| 1 | # An app refused an outside host says so in the forge's words (#12182) | |
| 2 | ||
| 3 | The forge's gate refuses every outside host that a repository's `forge.json` `egress` does not declare. The refusal says whose no it was, so an app never blames the service it was calling. | |
| 4 | ||
| 5 | - **The words.** The status line and body are `403 refused by this forge: <host>:<port> is not in this app's forge.json egress`, with `X-Forge-Egress: refused` (`REFUSED_PREFIX`, `src/apps/egress.ts`). The template's `src/egress.ts` reads the same words. | |
| 6 | - **The log.** The first refusal of a host and port in an hour is one line in the forge's log, naming the app (`cellHooks`, `src/apps/egress_refused.ts`). The line is printable ASCII, at most 200 bytes (`logSafe`), because the host is the app's own choice. The memory behind "once an hour" holds at most 2,000 keys (`SAID_MAX`); the oldest goes first. | |
| 7 | - **Needs you.** An admin of the repository sees one line per refused host: "game tried to reach huggingface.co and was refused: Allow it (3 tries, last just now)", linking to the app's settings (`needsYouLine`, one wording for `blockedLines`). The page's needs-you stack and the terminal's both read `blockedLines` through `src/crew/notify.ts`; Allow on the settings page lands the host live and in `forge.json`. | |
| 8 | - **The crew.** The brief tells an agent to add a host its change calls to `forge.json` `egress` in the same change. | |
| 9 | - **The check.** A new app's `forge.json` carries `check.run: bun scripts/egress-check.ts && bun test`, which fails when a literal host in the code is not declared. | |
| 10 | - **The template copy.** `templates/app-template.json` carries `scripts/egress-check.ts`, `src/egress.ts` and their tests. It is hand-edited until the template repository gets the same files: `bun scripts/take-template.ts` replaces it from trevato/app-template, so the same change must land there first, or the next take reverts it. | |
| 11 | ||
| 12 | Tests: `test/egress-refused.test.ts`, `test/app-template.test.ts`. |
src/actions/egress_refused.ts +2 −1
| @@ -19,6 +19,7 @@ import { | ||
| 19 | 19 | allowedHosts, |
| 20 | 20 | blockedHosts, |
| 21 | 21 | entryFor, |
| 22 | needsYouLine, | |
| 22 | 23 | refusalLine, |
| 23 | 24 | ignoreRefused, |
| 24 | 25 | type Refusal, |
| @@ -212,7 +213,7 @@ export function blockedLines( | ||
| 212 | 213 | if (!canAdmin(app.db, repo, user) || getAppByRepo(app.db, repo.id) === undefined) continue; |
| 213 | 214 | for (const r of blockedHosts(app.config.dataDir, repo.id)) |
| 214 | 215 | out.push({ |
| 215 | line: `${repo.name}: ${refusalLine(r, now)}`, | |
| 216 | line: needsYouLine(repo.name, r, now), | |
| 216 | 217 | items: [`egress:${repo.id}:${r.host}:${r.port}`], |
| 217 | 218 | link: `/${repo.ownerName}/${repo.name}/settings/app#blocked`, |
| 218 | 219 | }); |
src/apps/egress.ts +17 −1
| @@ -423,6 +423,12 @@ export function isPublicAddress(address: string): boolean { | ||
| 423 | 423 | return false; |
| 424 | 424 | } |
| 425 | 425 | |
| 426 | /** | |
| 427 | * What an app's code looks for in a refusal's status line and body (the | |
| 428 | * template's `src/egress.ts` reads the same words). | |
| 429 | */ | |
| 430 | export const REFUSED_PREFIX = "refused by this forge:"; | |
| 431 | ||
| 426 | 432 | export type Verdict = |
| 427 | 433 | | { ok: true; address: string; undeclared: boolean } |
| 428 | 434 | | { ok: false; why: string; policy?: { private: boolean } }; |
| @@ -456,7 +462,7 @@ export async function decide( | ||
| 456 | 462 | if (!declared && (policy.report === undefined || payment || !WEB_PORTS.includes(port))) |
| 457 | 463 | return { |
| 458 | 464 | ok: false, |
| 459 | why: `${name}:${port} is not in this repository's egress (forge.json "egress")`, | |
| 465 | why: `${REFUSED_PREFIX} ${name}:${port} is not in this app's forge.json egress`, | |
| 460 | 466 | policy: { private: false }, |
| 461 | 467 | }; |
| 462 | 468 | // Inside a cell, the cell's proxy resolves and checks the name itself. |
| @@ -558,8 +564,18 @@ function flush( | ||
| 558 | 564 | const encoder = new TextEncoder(); |
| 559 | 565 | const decoder = new TextDecoder(); |
| 560 | 566 | |
| 567 | /** | |
| 568 | * The status line of a refusal. The gate's own words ride it too, so a | |
| 569 | * client that reads only the status knows whose no it was; only printable | |
| 570 | * ASCII and short, or the plain status stands. | |
| 571 | */ | |
| 572 | export function refusedStatus(status: string, why: string): string { | |
| 573 | return why.startsWith(REFUSED_PREFIX) && /^[\x20-\x7e]{1,200}$/.test(why) ? `${status.slice(0, 3)} ${why}` : status; | |
| 574 | } | |
| 575 | ||
| 561 | 576 | function refuse(client: Socket<Conn>, status: string, why: string): void { |
| 562 | 577 | client.data.done = true; |
| 578 | status = refusedStatus(status, why); | |
| 563 | 579 | client.end( |
| 564 | 580 | encoder.encode( |
| 565 | 581 | `HTTP/1.1 ${status}\r\nContent-Type: text/plain\r\nX-Forge-Egress: refused\r\nConnection: close\r\nContent-Length: ${encoder.encode(why).length + 1}\r\n\r\n${why}\n`, |
src/apps/egress_refused.ts +30
| @@ -204,6 +204,25 @@ export function allowNow(dataDir: string, repoId: string, entry: string): void { | ||
| 204 | 204 | cache.delete(file); |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | const said = new Map<string, number>(); | |
| 208 | const SAID_MS = 60 * 60_000; | |
| 209 | /** The most keys remembered: an app picks its own hosts, so the map may never grow with them. */ | |
| 210 | export const SAID_MAX = 2_000; | |
| 211 | /** True at most once an hour per key; the map never holds more than SAID_MAX keys. */ | |
| 212 | export function saidNow(key: string, now: number): boolean { | |
| 213 | const last = said.get(key); | |
| 214 | if (last !== undefined && now - last < SAID_MS) return false; | |
| 215 | if (said.size >= SAID_MAX) for (const [k, at] of said) if (now - at >= SAID_MS) said.delete(k); | |
| 216 | // Insertion order is age order: a key re-said is moved to the end. | |
| 217 | said.delete(key); | |
| 218 | while (said.size >= SAID_MAX) said.delete(said.keys().next().value as string); | |
| 219 | said.set(key, now); | |
| 220 | return true; | |
| 221 | } | |
| 222 | ||
| 223 | /** Printable ASCII only, and short: a host an app chose never carries a newline into a log. */ | |
| 224 | export const logSafe = (text: string): string => text.replace(/[^\x20-\x7e]/g, "?").slice(0, 200); | |
| 225 | ||
| 207 | 226 | /** |
| 208 | 227 | * What a cell of `env` is given to ask and tell: production alone. A preview |
| 209 | 228 | * runs a pull's code (a fork's too), so it neither inherits the owner's yes |
| @@ -214,6 +233,10 @@ export function cellHooks( | ||
| 214 | 233 | repoId: string, |
| 215 | 234 | env: string, |
| 216 | 235 | first: (host: string, port: number) => void, |
| 236 | /** The parent's log: one line per host an hour, naming the app. */ | |
| 237 | say?: (line: string) => void, | |
| 238 | /** What the line calls the app: its slug where the caller knows it. */ | |
| 239 | name: string = repoId, | |
| 217 | 240 | ): { |
| 218 | 241 | allowed?: () => readonly string[]; |
| 219 | 242 | onRefused?: (host: string, port: number, isPrivate: boolean) => void; |
| @@ -223,6 +246,8 @@ export function cellHooks( | ||
| 223 | 246 | allowed: () => allowedHosts(dataDir, repoId), |
| 224 | 247 | onRefused: (host, port, isPrivate) => { |
| 225 | 248 | if (noteRefused(dataDir, repoId, host, port, isPrivate)) first(host, port); |
| 249 | if (say !== undefined && saidNow(`${repoId} ${host}:${port}`, Date.now())) | |
| 250 | say(logSafe(`[egress] ${name} was refused ${entryFor(host, port)}: not in its forge.json egress`)); | |
| 226 | 251 | }, |
| 227 | 252 | }; |
| 228 | 253 | } |
| @@ -233,6 +258,11 @@ export function cellHooks( | ||
| 233 | 258 | export const entryFor = (host: string, port: number): string => |
| 234 | 259 | port === 80 || port === 443 ? host : `${host}:${port}`; |
| 235 | 260 | |
| 261 | /** The needs-you line: one wording for every door that shows the stack. */ | |
| 262 | export function needsYouLine(appName: string, r: Refusal, now = Date.now()): string { | |
| 263 | return `${appName} tried to reach ${entryFor(r.host, r.port)} and was refused: Allow it (${r.count} ${r.count === 1 ? "try" : "tries"}, last ${sinceWords(r.last, now)})`; | |
| 264 | } | |
| 265 | ||
| 236 | 266 | /** "Blocked: huggingface.co (14 tries, last 2 min ago)" */ |
| 237 | 267 | export function refusalLine(r: Refusal, now = Date.now()): string { |
| 238 | 268 | const name = r.port === 80 || r.port === 443 ? r.host : `${r.host}:${r.port}`; |
src/apps/runtime.ts +2
| @@ -1732,6 +1732,8 @@ export class AppsRuntime { | ||
| 1732 | 1732 | // Said once per host: the first refusal is an event, the rest a count. |
| 1733 | 1733 | ...cellHooks(dataDir, record.repoId, env, (host, port) => |
| 1734 | 1734 | this.app.bus.append({ kind: "app.egress.refused", repoId: record.repoId, payload: { host, port } }), |
| 1735 | (line) => this.app.log.warn(line), | |
| 1736 | record.slug, | |
| 1735 | 1737 | ), |
| 1736 | 1738 | // Asked at every start: an admin's grant reaches the next cell. |
| 1737 | 1739 | granted: grantsHere(this.app, () => grantedFor(this.app.db, record.repoId)), |
src/crew/brief.ts +1
| @@ -1032,6 +1032,7 @@ export function ownAppLines(own: OwnApp | undefined): string[] { | ||
| 1032 | 1032 | ? "; no pull has a preview yet." |
| 1033 | 1033 | : `, and the previews of the open pulls: ${previews.join(", ")}.`) + |
| 1034 | 1034 | " Use them for checks that need the running app (end-to-end, screenshots, the public launch check).", |
| 1035 | "- When your change makes the app call an outside host (a fetch to a literal or configured host), add that host to `forge.json` `egress` in the same change. The forge's gate refuses every other with `403`, `X-Forge-Egress: refused` and \"refused by this forge: <host> is not in this app's forge.json egress\": that is the forge's no, not the service's, and the app must say so in those words. The app's check fails when a literal host is not declared.", | |
| 1035 | 1036 | ...(own.blocked === undefined |
| 1036 | 1037 | ? [] |
| 1037 | 1038 | : [ |
templates/app-template.json +6 −2
Diff not shown — 106,186 bytes of diff (limit 100,000). +6 −2
View filetest/app-template.test.ts +13
| @@ -197,3 +197,16 @@ describe("a template copy over the caps is not used", () => { | ||
| 197 | 197 | expect(got.some((f) => f.path === "big.bin")).toBe(false); |
| 198 | 198 | }); |
| 199 | 199 | }); |
| 200 | ||
| 201 | describe("the template teaches egress (#12182)", () => { | |
| 202 | const files = bakedTemplate().files; | |
| 203 | const get = (p: string) => text(files.find((f) => f.path === p)?.content); | |
| 204 | test("forge.json declares egress and the check runs the declaration scan", () => { | |
| 205 | const manifest = JSON.parse(get("forge.json")); | |
| 206 | expect(manifest.egress).toEqual([]); | |
| 207 | expect(manifest.check.run).toContain("scripts/egress-check.ts"); | |
| 208 | }); | |
| 209 | test("its helper and scan ship with their own tests", () => { | |
| 210 | for (const p of ["src/egress.ts", "src/egress.test.ts", "scripts/egress-check.ts", "scripts/egress-check.test.ts"]) expect(get(p).length).toBeGreaterThan(100); | |
| 211 | }); | |
| 212 | }); |
test/apps-new.test.ts +5 −4
| @@ -491,13 +491,14 @@ describe("a new app's own words promise only the check the forge runs (issue #19 | ||
| 491 | 491 | (sentence) => /\bcheck/i.test(sentence) && /\bpull/i.test(sentence), |
| 492 | 492 | ); |
| 493 | 493 | |
| 494 | test("forge.json declares no separate check, and the prose does not claim one", () => { | |
| 494 | test("the only check forge.json declares is the egress scan, and prose claims a check only when one is declared", () => { | |
| 495 | 495 | const checked = parseCheck(content("forge.json"), content("package.json")); |
| 496 | 496 | expect(checked.ok).toBe(true); |
| 497 | 497 | const declaresCheck = checked.ok && checked.value !== undefined; |
| 498 | expect(declaresCheck).toBe(false); | |
| 499 | expect(promisesACheck(content("README.md"))).toBe(declaresCheck); | |
| 500 | expect(promisesACheck(content("AGENTS.md"))).toBe(declaresCheck); | |
| 498 | expect(declaresCheck).toBe(true); | |
| 499 | expect(JSON.parse(content("forge.json")).check.run).toContain("scripts/egress-check.ts"); | |
| 500 | // A check is declared, so the prose may talk of one; it may never promise one that is not. | |
| 501 | for (const path of ["README.md", "AGENTS.md"]) expect(!promisesACheck(content(path)) || declaresCheck).toBe(true); | |
| 501 | 502 | }); |
| 502 | 503 | |
| 503 | 504 | test("the README and AGENTS.md agree on what app.test actually does", () => { |
test/contain.test.ts +3 −3
| @@ -236,7 +236,7 @@ bySocket("egress: the proxy forwards what is allowed, byte for byte, and refuses | ||
| 236 | 236 | ); |
| 237 | 237 | expect(undeclared).toStartWith("HTTP/1.1 403"); |
| 238 | 238 | expect(undeclared).toContain( |
| 239 | 'example.com:443 is not in this repository\'s egress (forge.json "egress")', | |
| 239 | "example.com:443 is not in this app's forge.json egress", | |
| 240 | 240 | ); |
| 241 | 241 | const privateName = await exchange( |
| 242 | 242 | `GET http://localhost:${port}/ HTTP/1.1\r\nHost: localhost\r\n\r\n`, |
| @@ -256,7 +256,7 @@ bySocket("egress: the proxy forwards what is allowed, byte for byte, and refuses | ||
| 256 | 256 | ); |
| 257 | 257 | expect(reply).toStartWith("HTTP/1.1 403"); |
| 258 | 258 | expect(reply).toContain( |
| 259 | `localhost:${port} is not in this repository's egress`, | |
| 259 | `localhost:${port} is not in this app's forge.json egress`, | |
| 260 | 260 | ); |
| 261 | 261 | } |
| 262 | 262 | }); |
| @@ -315,7 +315,7 @@ bySocket("egress, reported: an undeclared public host goes through and is counte | ||
| 315 | 315 | const reply = await head(`CONNECT example.com:${port} HTTP/1.1\r\n\r\n`); |
| 316 | 316 | expect(reply).toStartWith("HTTP/1.1 403"); |
| 317 | 317 | expect(reply).toContain( |
| 318 | `example.com:${port} is not in this repository's egress`, | |
| 318 | `example.com:${port} is not in this app's forge.json egress`, | |
| 319 | 319 | ); |
| 320 | 320 | } |
| 321 | 321 | expect(seen).toEqual([]); |
test/crew-app-hosts.test.ts +3 −2
| @@ -145,8 +145,9 @@ describe("a crew run's own app hosts", () => { | ||
| 145 | 145 | expect( |
| 146 | 146 | ownAppLines({ access: "public", production: "https://x.example", previews: [] })[0], |
| 147 | 147 | ).toContain("no pull has a preview yet"); |
| 148 | expect(ownAppLines({ access: "public", production: "https://x.example", previews: [] })).toHaveLength(1); | |
| 149 | const [, door] = ownAppLines({ access: "private", production: "https://x.example", previews: [] }); | |
| 148 | expect(ownAppLines({ access: "public", production: "https://x.example", previews: [] })).toHaveLength(2); | |
| 149 | expect(ownAppLines({ access: "public", production: "https://x.example", previews: [] })[1]).toContain("add that host to"); | |
| 150 | const [, , door] = ownAppLines({ access: "private", production: "https://x.example", previews: [] }); | |
| 150 | 151 | expect(door).toContain("This app is private"); |
| 151 | 152 | expect(door).toContain("404 by design"); |
| 152 | 153 | expect(door).toContain("Bearer $FORGE_TOKEN"); |
test/egress-refused.test.ts +118 −4
| @@ -12,8 +12,9 @@ import { join } from "node:path"; | ||
| 12 | 12 | import { createApp, type App } from "../src/app.ts"; |
| 13 | 13 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 14 | 14 | import { allowBlocked, blockedLines, blockedView, ignoreBlocked, withLiveOnly } from "../src/actions/egress_refused.ts"; |
| 15 | import { allowNow, allowedHosts, blockedHosts, cellHooks, cleanHost, flushRefused, noteRefused, refusalLine, ROWS_MAX } from "../src/apps/egress_refused.ts"; | |
| 16 | import { startEgress } from "../src/apps/egress.ts"; | |
| 15 | import { allowNow, allowedHosts, blockedHosts, cellHooks, cleanHost, flushRefused, logSafe, noteRefused, refusalLine, ROWS_MAX, SAID_MAX, saidNow } from "../src/apps/egress_refused.ts"; | |
| 16 | import { refusedStatus, startEgress } from "../src/apps/egress.ts"; | |
| 17 | import { setNotifyUrl, startNotifier } from "../src/crew/notify.ts"; | |
| 17 | 18 | import { previewEnv } from "../src/apps/data.ts"; |
| 18 | 19 | import { blockedCommands, blockedScreen } from "../src/tui/screens/blocked.ts"; |
| 19 | 20 | import type { Ctx } from "../src/tui/app/app.ts"; |
| @@ -144,14 +145,14 @@ describe("refused hosts", () => { | ||
| 144 | 145 | |
| 145 | 146 | test("it reaches the owner's notifications, and the crew's brief", () => { |
| 146 | 147 | const lines = blockedLines(app, cy); |
| 147 | expect(lines.map((l) => l.line)).toContain("game: Blocked: huggingface.co (3 tries, last just now)"); | |
| 148 | expect(lines.map((l) => l.line)).toContain("game tried to reach huggingface.co and was refused: Allow it (3 tries, last just now)"); | |
| 148 | 149 | expect(lines[0]!.items[0]).toBe(`egress:${repo.id}:huggingface.co:443`); |
| 149 | 150 | // Counting more tries does not make it a new item. |
| 150 | 151 | refuse("huggingface.co"); |
| 151 | 152 | expect(blockedLines(app, cy)[0]!.items[0]).toBe(lines[0]!.items[0]); |
| 152 | 153 | const own = { access: "public" as const, production: "https://x", previews: [], blocked: ["huggingface.co (4 tries, last just now)"] }; |
| 153 | 154 | expect(ownAppLines(own).join("\n")).toContain("huggingface.co"); |
| 154 | expect(ownAppLines({ access: "public", production: "https://x", previews: [] }).join("\n")).not.toContain("refused"); | |
| 155 | expect(ownAppLines({ access: "public", production: "https://x", previews: [] }).join("\n")).not.toContain("were refused these hosts"); | |
| 155 | 156 | }); |
| 156 | 157 | |
| 157 | 158 | test("a host that is not a hostname or an address is dropped, and long names too", () => { |
| @@ -302,3 +303,116 @@ describe("refused hosts", () => { | ||
| 302 | 303 | expect(blockedHosts(dataDir, repo.id).map((r) => r.host)).not.toContain("term.example.org"); |
| 303 | 304 | }); |
| 304 | 305 | }); |
| 306 | ||
| 307 | describe("an app told in the forge's own words (#12182)", () => { | |
| 308 | test("the refusal's status line and body say whose no it was, and the hook logs once an hour", async () => { | |
| 309 | const lines: string[] = []; | |
| 310 | const hooks = cellHooks(dataDir, "walk1", "production", () => {}, (l) => lines.push(l)); | |
| 311 | for (let i = 0; i < 3; i++) hooks.onRefused?.("beta-bridge.simplefin.org", 443, false); | |
| 312 | expect(lines).toEqual(["[egress] walk1 was refused beta-bridge.simplefin.org: not in its forge.json egress"]); | |
| 313 | const policy = { hosts: [] as string[], origins: [], allowed: () => [] as string[] }; | |
| 314 | const probe = Bun.listen({ hostname: "127.0.0.1", port: 0, socket: { data() {} } }); | |
| 315 | const port = probe.port; | |
| 316 | probe.stop(true); | |
| 317 | const proxy = startEgress({ tcp: port }, policy); | |
| 318 | let reply = ""; | |
| 319 | await new Promise<void>((resolve) => { | |
| 320 | void Bun.connect({ | |
| 321 | hostname: "127.0.0.1", | |
| 322 | port, | |
| 323 | socket: { | |
| 324 | open: (s) => void s.write("CONNECT beta-bridge.simplefin.org:443 HTTP/1.1\r\n\r\n"), | |
| 325 | data: (s, d) => { | |
| 326 | reply += new TextDecoder().decode(d); | |
| 327 | s.end(); | |
| 328 | }, | |
| 329 | close: () => resolve(), | |
| 330 | error: () => resolve(), | |
| 331 | }, | |
| 332 | }); | |
| 333 | }); | |
| 334 | proxy.close(); | |
| 335 | const say = "refused by this forge: beta-bridge.simplefin.org:443 is not in this app's forge.json egress"; | |
| 336 | expect(reply).toStartWith(`HTTP/1.1 403 ${say}\r\n`); | |
| 337 | expect(reply).toContain("X-Forge-Egress: refused"); | |
| 338 | expect(reply).toContain(`\r\n\r\n${say}\n`); | |
| 339 | }); | |
| 340 | ||
| 341 | test("the said map never passes its cap, however many hosts an app tries", () => { | |
| 342 | const now = Date.now(); | |
| 343 | for (let i = 0; i < 3_000; i++) expect(saidNow(`flood h${i}.example.org:443`, now)).toBe(true); | |
| 344 | // Every key was new and within the hour, so the oldest were dropped, not the newest. | |
| 345 | expect(saidNow("flood h2999.example.org:443", now)).toBe(false); | |
| 346 | expect(saidNow("flood h0.example.org:443", now)).toBe(true); | |
| 347 | let held = 0; | |
| 348 | for (let i = 0; i < 3_000; i++) if (!saidNow(`flood h${i}.example.org:443`, now)) held++; | |
| 349 | expect(held).toBeLessThanOrEqual(SAID_MAX); | |
| 350 | }); | |
| 351 | ||
| 352 | test("a host with a newline or control bytes cannot start a log line of its own", () => { | |
| 353 | const lines: string[] = []; | |
| 354 | const hooks = cellHooks(dataDir, "inj1", "production", () => {}, (l) => lines.push(l)); | |
| 355 | hooks.onRefused?.("evil.example\n[egress] forged\u001b[31m", 443, false); | |
| 356 | expect(lines).toHaveLength(1); | |
| 357 | expect(lines[0]).not.toMatch(/[^\x20-\x7e]/); | |
| 358 | expect(logSafe("a\r\nb")).toBe("a??b"); | |
| 359 | expect(logSafe("x".repeat(500))).toHaveLength(200); | |
| 360 | }); | |
| 361 | ||
| 362 | test("the brief tells the crew to declare a host it adds", () => { | |
| 363 | expect(ownAppLines({ production: "https://x", previews: [], access: "public" }).join("\n")).toContain("add that host to `forge.json` `egress`"); | |
| 364 | }); | |
| 365 | }); | |
| 366 | ||
| 367 | describe("the needs-you line at every door, and the log and status line it rides beside (#12182)", () => { | |
| 368 | test("the notifier the terminal sets up pushes the same line to a repository admin, and nothing to a guest or member", async () => { | |
| 369 | const make = async (username: string): Promise<User> => { | |
| 370 | const made = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase" }); | |
| 371 | if (!made.ok) throw new Error(made.error); | |
| 372 | return made.value; | |
| 373 | }; | |
| 374 | const guest = await make("dee"); | |
| 375 | const member = await make("eve"); | |
| 376 | promote(app.db, member); | |
| 377 | for (const [u, topic] of [[cy, "cy"], [guest, "dee"], [member, "eve"]] as const) | |
| 378 | expect(setNotifyUrl(app.db, u.id, `https://ntfy.example.test/${topic}-topic`).ok).toBe(true); | |
| 379 | const sent: { url: string; body: string }[] = []; | |
| 380 | const notifier = startNotifier(app, { | |
| 381 | forgeUrl: "https://forge.example.test", | |
| 382 | send: async (url, body) => (sent.push({ url, body }), true), | |
| 383 | }); | |
| 384 | await notifier.settled(); | |
| 385 | refuse("door.example.org"); | |
| 386 | refuse("door.example.org"); | |
| 387 | app.bus.append({ kind: "repo.settings_updated", repoId: repo.id, payload: {} }); | |
| 388 | await notifier.settled(); | |
| 389 | await notifier.close(); | |
| 390 | const line = "game tried to reach door.example.org and was refused: Allow it (2 tries, last just now)"; | |
| 391 | expect(blockedLines(app, cy).map((l) => l.line)).toContain(line); | |
| 392 | const toCy = sent.filter((s) => s.url.includes("/cy-topic")).map((s) => s.body.split("\n")[0]); | |
| 393 | expect(toCy).toContain(line); | |
| 394 | expect(sent.filter((s) => !s.url.includes("/cy-topic") && s.body.includes("door.example.org"))).toEqual([]); | |
| 395 | expect(blockedLines(app, guest)).toEqual([]); | |
| 396 | expect(blockedLines(app, member)).toEqual([]); | |
| 397 | }); | |
| 398 | ||
| 399 | test("the slug is named before the line is capped: one line out, never past 200 bytes", () => { | |
| 400 | const lines: string[] = []; | |
| 401 | const slug = "s".repeat(150); | |
| 402 | const hooks = cellHooks(dataDir, "slug1", "production", () => {}, (l) => lines.push(l), slug); | |
| 403 | hooks.onRefused?.(`${"h".repeat(120)}.example.org`, 443, false); | |
| 404 | expect(lines).toHaveLength(1); | |
| 405 | expect(lines[0]).toStartWith(`[egress] ${slug} was refused hhh`); | |
| 406 | expect(lines[0]).not.toContain("slug1"); | |
| 407 | expect(lines[0]!.length).toBeLessThanOrEqual(200); | |
| 408 | expect(lines[0]).not.toContain("\n"); | |
| 409 | }); | |
| 410 | ||
| 411 | test("the status line carries the sentence only in printable ASCII, else the plain status", () => { | |
| 412 | const said = "refused by this forge: api.example.org:443 is not in this app's forge.json egress"; | |
| 413 | expect(refusedStatus("403 Forbidden", said)).toBe(`403 ${said}`); | |
| 414 | expect(refusedStatus("403 Forbidden", said.replace("api.example.org", "api.ex\u00e4mple.org"))).toBe("403 Forbidden"); | |
| 415 | expect(refusedStatus("403 Forbidden", said.replace("api.example.org", "api\r\nX-Evil: 1"))).toBe("403 Forbidden"); | |
| 416 | expect(refusedStatus("403 Forbidden", "some other no")).toBe("403 Forbidden"); | |
| 417 | }); | |
| 418 | }); |
test/egress-upstream.test.ts +2 −2
| @@ -94,7 +94,7 @@ test("a host the inner policy allows but the outer cell refuses is refused, and | ||
| 94 | 94 | // The inner policy still refuses on its own, before the outer is asked. |
| 95 | 95 | const own = await through(innerPort, "CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n"); |
| 96 | 96 | expect(own).toStartWith("HTTP/1.1 403"); |
| 97 | expect(own).toContain("not in this repository's egress"); | |
| 97 | expect(own).toContain("not in this app's forge.json egress"); | |
| 98 | 98 | } finally { |
| 99 | 99 | inner.close(); |
| 100 | 100 | outer.close(); |
| @@ -126,7 +126,7 @@ test("a payment host passes a child's inner proxy only when the app declares it, | ||
| 126 | 126 | expect(await through(innerPort, connect)).toContain("not reachable from this forge's cell"); |
| 127 | 127 | const refused = await through(innerPort + 1, connect); |
| 128 | 128 | expect(refused).toStartWith("HTTP/1.1 403"); |
| 129 | expect(refused).toContain("not in this repository's egress"); | |
| 129 | expect(refused).toContain("not in this app's forge.json egress"); | |
| 130 | 130 | expect(seen).toEqual([]); |
| 131 | 131 | // Report mode still lets another undeclared public host reach the outer proxy. |
| 132 | 132 | if (report) expect(await through(innerPort + 1, "CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n")).toContain("not reachable from this forge's cell"); |
test/outbound-gate.test.ts +1 −1
| @@ -65,7 +65,7 @@ const allowing = () => | ||
| 65 | 65 | ? { status: 403, why: "nowhere.example.test does not resolve" } |
| 66 | 66 | : { |
| 67 | 67 | status: 403, |
| 68 | why: `${host}:${port} is not in this repository's egress (forge.json "egress")`, | |
| 68 | why: `refused by this forge: ${host}:${port} is not in this app's forge.json egress`, | |
| 69 | 69 | }, |
| 70 | 70 | ); |
| 71 | 71 |
new above
offline