Public: anyone can read this; the crew can change it.

Full suite red: test/ask-to-reach.test.ts #12697

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/12649-full-suite-red-test-ask-to-reach-test-ts → main 6 commits fork 1 thread · 1 unresolved

  • Merged. torque/12649-full-suite-red-test-ask-to-reach-test-ts was merged into main as 0db8ab9.

Review

Sign in to review this change.
5 files changed +74 −11
src/apps/egress.ts +7 −1
@@ -423,6 +423,12 @@ export function isPublicAddress(address: string): boolean {
423423 return false;
424424 }
425425
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
426432 /**
427433 * What an app's code looks for in a refusal's status line and body (the
428434 * template's `src/egress.ts` reads the same words).
outdated

This constant splits REFUSED_PREFIX from its doc comment (lines above). Place it before that comment.

@@ -570,7 +576,7 @@ const decoder = new TextDecoder();
570576 * ASCII and short, or the plain status stands.
571577 */
572578 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;
574580 }
575581
576582 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";
1616 import { isIP } from "node:net";
1717 import { err, ok, type Result } from "./result.ts";
1818 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";
2020
2121 const REFUSED = [
2222 "0.0.0.0/8",
@@ -149,17 +149,27 @@ export async function outboundFetch(
149149 const name = target.hostname.replace(/^\[|\]$/g, "");
150150 const tls = { ...(options.ca ? { ca: options.ca } : {}) };
151151 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 }
158168 // The gate's refusal comes back as the answer to CONNECT, marked: it is not the host's.
159169 if (answer.headers.get("x-forge-egress") !== "refused") return answer;
160170 const said = (await answer.text().catch(() => "")).trim();
161171 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);
163173 }
164174 const ip = vetted.value.addresses[0]!;
165175 const pinned = new URL(url);
test/egress-refused.test.ts +9 −1
@@ -13,7 +13,7 @@ import { createApp, type App } from "../src/app.ts";
1313 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1414 import { allowBlocked, blockedLines, blockedView, ignoreBlocked, withLiveOnly } from "../src/actions/egress_refused.ts";
1515 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";
1717 import { setNotifyUrl, startNotifier } from "../src/crew/notify.ts";
1818 import { previewEnv } from "../src/apps/data.ts";
1919 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
415415 expect(refusedStatus("403 Forbidden", said.replace("api.example.org", "api\r\nX-Evil: 1"))).toBe("403 Forbidden");
416416 expect(refusedStatus("403 Forbidden", "some other no")).toBe("403 Forbidden");
417417 });
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 });
418426 });
test/helpers/gate-fixture.ts +2 −1
@@ -9,6 +9,7 @@ import { tmpdir } from "node:os";
99 import { join } from "node:path";
1010 import { gunzipSync } from "node:zlib";
1111 import { gitClientEnv } from "./git-client.ts";
12 import { refusedStatus } from "../../src/apps/egress.ts";
1213
1314 /** 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). */
1415 export function selfSigned(
@@ -176,7 +177,7 @@ export function fakeGate(
176177 const decided = rule(target.slice(0, at), Number(target.slice(at + 1)));
177178 if ("status" in decided) {
178179 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`,
180181 );
181182 return;
182183 }
test/outbound-gate.test.ts +38
@@ -24,6 +24,7 @@ import {
2424 GateRefusedError,
2525 outboundFetch,
2626 RefusedError,
27 UNRESOLVED_SENTENCE,
2728 vetHost,
2829 } from "../src/kernel/outbound.ts";
2930 import {
@@ -384,3 +385,40 @@ describe("the gate's own judgement of an answer", () => {
384385 expect(hostAllowed(hosts, other, 443)).toBe(false);
385386 });
386387 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs