Public: anyone can read this; the crew can change it.
Full suite red: test/ask-to-reach.test.ts #12697
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/12649-full-suite-red-test-ask-to-reach-test-ts → main 6 commits fork 1 thread · 1 unresolved
5 files
changed +74
−11
src/apps/egress.ts +7 −1
| @@ -423,6 +423,12 @@ export function isPublicAddress(address: string): boolean { | ||
| 423 | 423 | return false; |
| 424 | 424 | } |
| 425 | 425 | |
| 426 | /** | |
| 427 | * How the gate says a name did not resolve; it rides the status line too, so a client | |
| 428 | * that reads only the status can tell it from a refusal. | |
| 429 | */ | |
| 430 | export const UNRESOLVED_WORDS = "does not resolve"; | |
| 431 | ||
| 426 | 432 | /** |
| 427 | 433 | * What an app's code looks for in a refusal's status line and body (the |
| 428 | 434 | * template's `src/egress.ts` reads the same words). |
| @@ -570,7 +576,7 @@ const decoder = new TextDecoder(); | ||
| 570 | 576 | * ASCII and short, or the plain status stands. |
| 571 | 577 | */ |
| 572 | 578 | 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; | |
| 579 | return (why.startsWith(REFUSED_PREFIX) || why.endsWith(UNRESOLVED_WORDS)) && /^[\x20-\x7e]{1,200}$/.test(why) ? `${status.slice(0, 3)} ${why}` : status; | |
| 574 | 580 | } |
| 575 | 581 | |
| 576 | 582 | function refuse(client: Socket<Conn>, status: string, why: string): void { |
src/kernel/outbound.ts +18 −8
| @@ -16,7 +16,7 @@ import { lookup } from "node:dns/promises"; | ||
| 16 | 16 | import { isIP } from "node:net"; |
| 17 | 17 | import { err, ok, type Result } from "./result.ts"; |
| 18 | 18 | import { addressBytes, inCidrs, parseCidr, type Cidr } from "./cidr.ts"; |
| 19 | import { cellProxy } from "../apps/egress.ts"; | |
| 19 | import { cellProxy, UNRESOLVED_WORDS } from "../apps/egress.ts"; | |
| 20 | 20 | |
| 21 | 21 | const REFUSED = [ |
| 22 | 22 | "0.0.0.0/8", |
| @@ -149,17 +149,27 @@ export async function outboundFetch( | ||
| 149 | 149 | const name = target.hostname.replace(/^\[|\]$/g, ""); |
| 150 | 150 | const tls = { ...(options.ca ? { ca: options.ca } : {}) }; |
| 151 | 151 | if (vetted.value.via === "gate") { |
| 152 | const answer = await fetch(target, { | |
| 153 | ...init, | |
| 154 | redirect: "manual", | |
| 155 | proxy: vetted.value.proxy, | |
| 156 | ...(target.protocol === "https:" ? { tls } : {}), | |
| 157 | } satisfies BunFetchRequestInit); | |
| 152 | let answer: Response; | |
| 153 | try { | |
| 154 | answer = await fetch(target, { | |
| 155 | ...init, | |
| 156 | redirect: "manual", | |
| 157 | proxy: vetted.value.proxy, | |
| 158 | ...(target.protocol === "https:" ? { tls } : {}), | |
| 159 | } satisfies BunFetchRequestInit); | |
| 160 | } catch (e) { | |
| 161 | // Newer Bun throws when the gate answers CONNECT with a refusal, | |
| 162 | // where older Bun returned that answer. | |
| 163 | const said = e instanceof Error ? e.message : ""; | |
| 164 | if ((e as { code?: string }).code === "ERR_PROXY_TUNNEL" && (e as { status?: number }).status === 403) | |
| 165 | throw said.includes(UNRESOLVED_WORDS) ? new Error(UNRESOLVED_SENTENCE) : new GateRefusedError(name); | |
| 166 | throw e; | |
| 167 | } | |
| 158 | 168 | // The gate's refusal comes back as the answer to CONNECT, marked: it is not the host's. |
| 159 | 169 | if (answer.headers.get("x-forge-egress") !== "refused") return answer; |
| 160 | 170 | const said = (await answer.text().catch(() => "")).trim(); |
| 161 | 171 | if (answer.status !== 403) throw new Error(said); |
| 162 | throw said.includes("does not resolve") ? new Error(UNRESOLVED_SENTENCE) : new GateRefusedError(name); | |
| 172 | throw said.includes(UNRESOLVED_WORDS) ? new Error(UNRESOLVED_SENTENCE) : new GateRefusedError(name); | |
| 163 | 173 | } |
| 164 | 174 | const ip = vetted.value.addresses[0]!; |
| 165 | 175 | const pinned = new URL(url); |
test/egress-refused.test.ts +9 −1
| @@ -13,7 +13,7 @@ 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 | 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"; | |
| 16 | import { refusedStatus, startEgress, UNRESOLVED_WORDS } from "../src/apps/egress.ts"; | |
| 17 | 17 | import { setNotifyUrl, startNotifier } from "../src/crew/notify.ts"; |
| 18 | 18 | import { previewEnv } from "../src/apps/data.ts"; |
| 19 | 19 | import { blockedCommands, blockedScreen } from "../src/tui/screens/blocked.ts"; |
| @@ -415,4 +415,12 @@ describe("the needs-you line at every door, and the log and status line it rides | ||
| 415 | 415 | expect(refusedStatus("403 Forbidden", said.replace("api.example.org", "api\r\nX-Evil: 1"))).toBe("403 Forbidden"); |
| 416 | 416 | expect(refusedStatus("403 Forbidden", "some other no")).toBe("403 Forbidden"); |
| 417 | 417 | }); |
| 418 | ||
| 419 | test("a name that does not resolve rides the status line too; a non-ASCII or over-long sentence does not", () => { | |
| 420 | const said = `nowhere.example.test ${UNRESOLVED_WORDS}`; | |
| 421 | expect(refusedStatus("403 Forbidden", said)).toBe(`403 ${said}`); | |
| 422 | expect(refusedStatus("403 Forbidden", `n\u00f6where.example.test ${UNRESOLVED_WORDS}`)).toBe("403 Forbidden"); | |
| 423 | expect(refusedStatus("403 Forbidden", `${"a".repeat(201)} ${UNRESOLVED_WORDS}`)).toBe("403 Forbidden"); | |
| 424 | expect(refusedStatus("403 Forbidden", `${UNRESOLVED_WORDS} and more`)).toBe("403 Forbidden"); | |
| 425 | }); | |
| 418 | 426 | }); |
test/helpers/gate-fixture.ts +2 −1
| @@ -9,6 +9,7 @@ import { tmpdir } from "node:os"; | ||
| 9 | 9 | import { join } from "node:path"; |
| 10 | 10 | import { gunzipSync } from "node:zlib"; |
| 11 | 11 | import { gitClientEnv } from "./git-client.ts"; |
| 12 | import { refusedStatus } from "../../src/apps/egress.ts"; | |
| 12 | 13 | |
| 13 | 14 | /** A throwaway self-signed certificate for `name`, made with openssl so no key is committed (RSA: LibreSSL's EC keys are ones Bun cannot read). */ |
| 14 | 15 | export function selfSigned( |
| @@ -176,7 +177,7 @@ export function fakeGate( | ||
| 176 | 177 | const decided = rule(target.slice(0, at), Number(target.slice(at + 1))); |
| 177 | 178 | if ("status" in decided) { |
| 178 | 179 | socket.end( |
| 179 | `HTTP/1.1 ${decided.status} Refused\r\nX-Forge-Egress: refused\r\nContent-Length: ${decided.why.length + 1}\r\n\r\n${decided.why}\n`, | |
| 180 | `HTTP/1.1 ${refusedStatus(`${decided.status} Refused`, decided.why)}\r\nX-Forge-Egress: refused\r\nContent-Length: ${decided.why.length + 1}\r\n\r\n${decided.why}\n`, | |
| 180 | 181 | ); |
| 181 | 182 | return; |
| 182 | 183 | } |
test/outbound-gate.test.ts +38
| @@ -24,6 +24,7 @@ import { | ||
| 24 | 24 | GateRefusedError, |
| 25 | 25 | outboundFetch, |
| 26 | 26 | RefusedError, |
| 27 | UNRESOLVED_SENTENCE, | |
| 27 | 28 | vetHost, |
| 28 | 29 | } from "../src/kernel/outbound.ts"; |
| 29 | 30 | import { |
| @@ -384,3 +385,40 @@ describe("the gate's own judgement of an answer", () => { | ||
| 384 | 385 | expect(hostAllowed(hosts, other, 443)).toBe(false); |
| 385 | 386 | }); |
| 386 | 387 | }); |
| 388 | ||
| 389 | /** | |
| 390 | * A gate that answers every CONNECT with this literal status line and marked body, | |
| 391 | * not the fixture's. | |
| 392 | */ | |
| 393 | const literalGate = (statusLine: string, body: string) => { | |
| 394 | const server = Bun.listen<undefined>({ | |
| 395 | hostname: "127.0.0.1", | |
| 396 | port: 0, | |
| 397 | socket: { | |
| 398 | data(socket) { | |
| 399 | socket.end(`HTTP/1.1 ${statusLine}\r\nX-Forge-Egress: refused\r\nContent-Length: ${body.length}\r\n\r\n${body}`); | |
| 400 | }, | |
| 401 | open() {}, | |
| 402 | close() {}, | |
| 403 | }, | |
| 404 | }); | |
| 405 | return { proxy: `http://127.0.0.1:${server.port}`, stop: () => server.stop(true) }; | |
| 406 | }; | |
| 407 | ||
| 408 | describe("outboundFetch reads the gate's refusal, however Bun hands it over", () => { | |
| 409 | test("a 403 to CONNECT is a GateRefusedError; one that does not resolve is the unresolved sentence", async () => { | |
| 410 | const refused = "refused by this forge: x.example.test:443 is not in this app's forge.json egress"; | |
| 411 | const a = literalGate(`403 ${refused}`, refused); | |
| 412 | const b = literalGate("403 x.example.test does not resolve", "x.example.test does not resolve"); | |
| 413 | try { | |
| 414 | const one = await outboundFetch("https://x.example.test/", {}, { proxy: a.proxy }).catch((e) => e); | |
| 415 | expect(one).toBeInstanceOf(GateRefusedError); | |
| 416 | const two = await outboundFetch("https://x.example.test/", {}, { proxy: b.proxy }).catch((e) => e); | |
| 417 | expect(two).not.toBeInstanceOf(GateRefusedError); | |
| 418 | expect((two as Error).message).toBe(UNRESOLVED_SENTENCE); | |
| 419 | } finally { | |
| 420 | a.stop(); | |
| 421 | b.stop(); | |
| 422 | } | |
| 423 | }); | |
| 424 | }); |
new above
offline
This constant splits REFUSED_PREFIX from its doc comment (lines above). Place it before that comment.