Public: anyone can read this; the crew can change it.
Loopback is not a trusted proxy unless FORGE_TRUSTED_PROXIES names it #14984
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14950-loopback-is-not-a-trusted-proxy-unless-f → main 3 commits fork
21 files
changed +69
−41
docs/OPERATING.md +1 −1
| @@ -88,7 +88,7 @@ from `FORGE_CLIENT_IP_HEADER` when the edge sets one (`cf-connecting-ip` | ||
| 88 | 88 | behind a Cloudflare tunnel, where the ingress rewrites `X-Forwarded-For`; |
| 89 | 89 | read only from a listed proxy). Once set, only the listed ranges are proxies: |
| 90 | 90 | this machine is not one unless listed, since every app runs beside the forge. |
| 91 | Unset, only this machine may say so, and the named header is never read. Everything a forge | |
| 91 | Unset, no one may say so, this machine included: the socket peer is the client, and neither `X-Forwarded-For` nor the named header is read (a local proxy in development names `127.0.0.0/8,::1`). Everything a forge | |
| 92 | 92 | instance _is_ lives under the data directory — the SQLite index, the bare |
| 93 | 93 | repos, and the SSH host key, in plain layout. Tar it up and it moves. |
| 94 | 94 |
src/app.ts +2 −2
| @@ -144,8 +144,8 @@ export interface ForgeConfig { | ||
| 144 | 144 | mailer?: Mailer; |
| 145 | 145 | /** |
| 146 | 146 | * Proxies whose forwarded headers say who the client is |
| 147 | * (\`FORGE_TRUSTED_PROXIES\`, CIDR ranges). Empty or absent, only this | |
| 148 | * machine is trusted to say so (\`http/peer.ts\`). | |
| 147 | * (\`FORGE_TRUSTED_PROXIES\`, CIDR ranges). Empty or absent, no one is | |
| 148 | * trusted to say so, loopback included (\`http/peer.ts\`). | |
| 149 | 149 | */ |
| 150 | 150 | trustedProxies?: readonly string[]; |
| 151 | 151 | /** A header a trusted edge sets to the client's address (\`FORGE_CLIENT_IP_HEADER\`). */ |
src/http/peer.ts +6 −8
| @@ -6,8 +6,9 @@ | ||
| 6 | 6 | * The socket's peer is the answer unless that peer is a proxy the operator |
| 7 | 7 | * trusts. With `FORGE_TRUSTED_PROXIES` set, those ranges are the proxies and |
| 8 | 8 | * nothing else is — not even this machine, where every app and crew run is |
| 9 | * a process that could call the forge and claim to be anyone. Unset, this | |
| 10 | * machine alone may say (a local proxy in development). From a trusted | |
| 9 | * a process that could call the forge and claim to be anyone. Unset, no | |
| 10 | * one may say: the socket's peer is the client, loopback included (a local | |
| 11 | * proxy in development names `127.0.0.0/8,::1`). From a trusted | |
| 11 | 12 | * proxy, `X-Forwarded-For` is walked from the right, past each trusted hop, |
| 12 | 13 | * to the first address no trusted proxy vouches for; the left end is |
| 13 | 14 | * whatever the client wrote and is never read as such. A named header the |
| @@ -38,11 +39,9 @@ export function resolveClient( | ||
| 38 | 39 | trust: ProxyTrust, |
| 39 | 40 | ): string { |
| 40 | 41 | if (peer === undefined) return "unknown"; |
| 41 | const listed = trust.proxies.length > 0; | |
| 42 | const trusted = (address: string): boolean => | |
| 43 | listed ? inCidrs(address, trust.proxies) : LOOPBACK.has(address); | |
| 42 | const trusted = (address: string): boolean => inCidrs(address, trust.proxies); | |
| 44 | 43 | if (!trusted(peer)) return peer; |
| 45 | if (trust.header !== undefined && listed) { | |
| 44 | if (trust.header !== undefined) { | |
| 46 | 45 | const named = headers.get(trust.header)?.trim() ?? ""; |
| 47 | 46 | if (isAddress(named)) return named; |
| 48 | 47 | } |
| @@ -86,8 +85,7 @@ export const notePeer = ( | ||
| 86 | 85 | ): void => { |
| 87 | 86 | clients.set(request, resolveClient(address, request.headers, trust)); |
| 88 | 87 | if (isInternalPeer(address, request.headers, trust)) internals.add(request); |
| 89 | else if (address !== undefined && (trust.proxies.length > 0 ? inCidrs(address, trust.proxies) : LOOPBACK.has(address))) | |
| 90 | edged.add(request); | |
| 88 | else if (address !== undefined && inCidrs(address, trust.proxies)) edged.add(request); | |
| 91 | 89 | }; |
| 92 | 90 | |
| 93 | 91 | /** The request came through a proxy the operator trusts, so the headers an edge adds (bot score, country) are its, not the client's. */ |
test/anonymous-deep.test.ts +2 −1
| @@ -15,6 +15,7 @@ import { crawlerShare, createPerfRecord } from "../src/perf/record.ts"; | ||
| 15 | 15 | import { crawlReport } from "../src/perf/crawlers.ts"; |
| 16 | 16 | import { clearTreeCache, treeCacheStats } from "../src/web/pages/tree_cache.ts"; |
| 17 | 17 | import { keptPageStats } from "../src/web/pages/kept_page.ts"; |
| 18 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 18 | 19 | |
| 19 | 20 | let dataDir: string; |
| 20 | 21 | let app: App; |
| @@ -28,7 +29,7 @@ const get = (path: string, headers: Record<string, string> = {}) => fetch(`${ser | ||
| 28 | 29 | |
| 29 | 30 | beforeAll(async () => { |
| 30 | 31 | dataDir = await mkdtemp(join(tmpdir(), "forge-anon-deep-")); |
| 31 | app = createApp({ dataDir, port: 0 }); | |
| 32 | app = createApp({ dataDir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 32 | 33 | servers = await serve(app); |
| 33 | 34 | const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", isAdmin: true }); |
| 34 | 35 | if (!u.ok) throw new Error(u.error); |
test/apps-feedback.test.ts +2 −1
| @@ -25,6 +25,7 @@ import { createUser, type User } from "../src/core/users.ts"; | ||
| 25 | 25 | import { serve, type Servers } from "../src/http/server.ts"; |
| 26 | 26 | import { taskPrompt, type TaskInput } from "../src/crew/brief.ts"; |
| 27 | 27 | import { quietLogs } from "./helpers/globals.ts"; |
| 28 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 28 | 29 | |
| 29 | 30 | quietLogs(); |
| 30 | 31 | |
| @@ -39,7 +40,7 @@ describe("the forge button takes notes from anyone", () => { | ||
| 39 | 40 | |
| 40 | 41 | beforeAll(async () => { |
| 41 | 42 | dataDir = await mkdtemp(join(tmpdir(), "forge-app-feedback-")); |
| 42 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 43 | app = createApp({ dataDir, port: 0, host: "forge.test", trustedProxies: LOOPBACK_PROXIES }); | |
| 43 | 44 | servers = await serve(app); |
| 44 | 45 | runtime = new AppsRuntime(app, { |
| 45 | 46 | domain: DOMAIN, |
test/apps-proxy-client-ip.test.ts +2 −4
| @@ -94,10 +94,8 @@ test("with no client headers, the app hears the socket's address", async () => { | ||
| 94 | 94 | onlyForwardedFor(await seen(), LOOPBACK); |
| 95 | 95 | }); |
| 96 | 96 | |
| 97 | test("a list in x-forwarded-for reaches the app as the one address the forge resolved", async () => { | |
| 98 | // From loopback (the only trusted proxy here) the list is walked from the right: the | |
| 99 | // left end is whatever the client wrote and never reaches the app. | |
| 100 | onlyForwardedFor(await seen({ "x-forwarded-for": "203.0.113.9, 198.51.100.7" }), "198.51.100.7"); | |
| 97 | test("a forged x-forwarded-for from loopback never reaches the app: with no proxy listed, the client is the socket", async () => { | |
| 98 | onlyForwardedFor(await seen({ "x-forwarded-for": "203.0.113.9, 198.51.100.7" }), LOOPBACK); | |
| 101 | 99 | }); |
| 102 | 100 | |
| 103 | 101 | test("client-written x-real-ip, forwarded, cf-connecting-ip, true-client-ip and the edge header are dropped", async () => { |
test/client-address.test.ts +15 −10
| @@ -10,6 +10,7 @@ import { | ||
| 10 | 10 | clientAddress, |
| 11 | 11 | notePeer, |
| 12 | 12 | resolveClient, |
| 13 | wasViaEdge, | |
| 13 | 14 | type ProxyTrust, |
| 14 | 15 | } from "../src/http/peer.ts"; |
| 15 | 16 | import { serve, type Servers } from "../src/http/server.ts"; |
| @@ -134,17 +135,21 @@ describe("the client behind the proxy", () => { | ||
| 134 | 135 | ).toBe("198.51.100.8"); |
| 135 | 136 | }); |
| 136 | 137 | |
| 137 | test("this machine counts as a proxy, as before, but its hop is read from the right", () => { | |
| 138 | test("unset, this machine is not a proxy: a forged X-Forwarded-For from loopback is ignored", () => { | |
| 138 | 139 | const none = trust(""); |
| 139 | expect( | |
| 140 | resolveClient( | |
| 141 | "127.0.0.1", | |
| 142 | headers({ "x-forwarded-for": "1.1.1.1, 198.51.100.3" }), | |
| 143 | none, | |
| 144 | ), | |
| 145 | ).toBe("198.51.100.3"); | |
| 140 | const forged = headers({ "x-forwarded-for": "1.1.1.1, 198.51.100.3", "cf-connecting-ip": "2.2.2.2" }); | |
| 141 | expect(resolveClient("127.0.0.1", forged, none)).toBe("127.0.0.1"); | |
| 142 | expect(resolveClient("::1", forged, none)).toBe("::1"); | |
| 143 | expect(resolveClient("::ffff:127.0.0.1", forged, none)).toBe("::ffff:127.0.0.1"); | |
| 146 | 144 | expect(resolveClient("127.0.0.1", headers({}), none)).toBe("127.0.0.1"); |
| 147 | 145 | }); |
| 146 | ||
| 147 | test("unset, a request from loopback is not via the edge", () => { | |
| 148 | const r = new Request("http://x/", { headers: { "x-forwarded-for": "198.51.100.3" } }); | |
| 149 | notePeer(r, "127.0.0.1"); | |
| 150 | expect(clientAddress(r)).toBe("127.0.0.1"); | |
| 151 | expect(wasViaEdge(r)).toBe(false); | |
| 152 | }); | |
| 148 | 153 | }); |
| 149 | 154 | |
| 150 | 155 | describe("this machine is not a proxy once the proxies are named", () => { |
| @@ -169,9 +174,9 @@ describe("this machine is not a proxy once the proxies are named", () => { | ||
| 169 | 174 | expect(resolveClient("127.0.0.1", h, { proxies: parsed.value })).toBe("198.51.100.30"); |
| 170 | 175 | }); |
| 171 | 176 | |
| 172 | test("with no proxies named, the named header is never read, even from this machine", () => { | |
| 177 | test("with no proxies named, neither the named header nor X-Forwarded-For is read, even from this machine", () => { | |
| 173 | 178 | const h = headers({ "cf-connecting-ip": "2.2.2.2", "x-forwarded-for": "198.51.100.31" }); |
| 174 | expect(resolveClient("127.0.0.1", h, { proxies: [], header: "cf-connecting-ip" })).toBe("198.51.100.31"); | |
| 179 | expect(resolveClient("127.0.0.1", h, { proxies: [], header: "cf-connecting-ip" })).toBe("127.0.0.1"); | |
| 175 | 180 | }); |
| 176 | 181 | }); |
| 177 | 182 |
test/crawl-budget.test.ts +2 −1
| @@ -11,6 +11,7 @@ import { createUser, type User } from "../src/core/users.ts"; | ||
| 11 | 11 | import { AGENT_BUDGET, BEHIND_DIVISOR, STRANGER_BUDGET, CRAWL_BUDGET, DEEP_SEGMENTS, SHARED_BUDGET, agentOf, crawlAgentCount, isCrawler, pinCrawlLag, setCrawlClock, takeCrawl } from "../src/http/crawl.ts"; |
| 12 | 12 | import { serve, type Servers } from "../src/http/server.ts"; |
| 13 | 13 | import { crawlerShare, createPerfRecord } from "../src/perf/record.ts"; |
| 14 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 14 | 15 | |
| 15 | 16 | let dataDir: string; |
| 16 | 17 | let app: App; |
| @@ -33,7 +34,7 @@ const get = (path: string, headers: Record<string, string> = {}) => fetch(`${ser | ||
| 33 | 34 | |
| 34 | 35 | beforeAll(async () => { |
| 35 | 36 | dataDir = await mkdtemp(join(tmpdir(), "forge-crawl-")); |
| 36 | app = createApp({ dataDir, port: 0 }); | |
| 37 | app = createApp({ dataDir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 37 | 38 | servers = await serve(app); |
| 38 | 39 | const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", isAdmin: true }); |
| 39 | 40 | if (!u.ok) throw new Error(u.error); |
test/helpers/apps-forge.ts +2 −1
| @@ -18,6 +18,7 @@ import { listRuns, type Run } from "../../src/core/runs.ts"; | ||
| 18 | 18 | import { createUser, type User } from "../../src/core/users.ts"; |
| 19 | 19 | import { serve, type Servers } from "../../src/http/server.ts"; |
| 20 | 20 | import "./roomy-disk.ts"; |
| 21 | import { LOOPBACK_PROXIES } from "./loopback-proxy.ts"; | |
| 21 | 22 | |
| 22 | 23 | export const APPS_DOMAIN = "apps.test"; |
| 23 | 24 | |
| @@ -49,7 +50,7 @@ export async function startAppsForge( | ||
| 49 | 50 | ): Promise<AppsForge> { |
| 50 | 51 | const dataDir = await mkdtemp(join(tmpdir(), "forge-apps-")); |
| 51 | 52 | const work = await mkdtemp(join(tmpdir(), "forge-apps-work-")); |
| 52 | const app = createApp({ dataDir, port: 0 }); | |
| 53 | const app = createApp({ dataDir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 53 | 54 | setSsh(app.db, true, "test"); |
| 54 | 55 | const servers = await serve(app); |
| 55 | 56 | // Every app the runtime boots is handed this key; the tests sign as |
test/helpers/loopback-proxy.ts added +7
| @@ -0,0 +1,7 @@ | ||
| 1 | /** | |
| 2 | * The test client is this machine, and a test that plays several outside | |
| 3 | * clients sends `X-Forwarded-For` from loopback. Loopback is a proxy only | |
| 4 | * when named (`src/http/peer.ts`), so such a test names it, as an operator | |
| 5 | * running a local proxy would: `createApp({ …, trustedProxies: LOOPBACK_PROXIES })`. | |
| 6 | */ | |
| 7 | export const LOOPBACK_PROXIES: readonly string[] = ["127.0.0.0/8", "::1"]; |
test/muse-journey.test.ts +2 −1
| @@ -19,6 +19,7 @@ import { createUser } from "../src/core/users.ts"; | ||
| 19 | 19 | import { OFFER_REL } from "../src/apps/proxy.ts"; |
| 20 | 20 | import { serve, type Servers } from "../src/http/server.ts"; |
| 21 | 21 | import { quietLogs } from "./helpers/globals.ts"; |
| 22 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 22 | 23 | |
| 23 | 24 | quietLogs(); |
| 24 | 25 | |
| @@ -51,7 +52,7 @@ describe("the Muse test: a stranger agent needs no teaching", () => { | ||
| 51 | 52 | }), |
| 52 | 53 | }); |
| 53 | 54 | dataDir = await mkdtemp(join(tmpdir(), "forge-muse-")); |
| 54 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 55 | app = createApp({ dataDir, port: 0, host: "forge.test" , trustedProxies: LOOPBACK_PROXIES }); | |
| 55 | 56 | servers = await serve(app); |
| 56 | 57 | runtime = new AppsRuntime(app, { |
| 57 | 58 | domain: DOMAIN, |
test/muse-report-journey.test.ts +2 −1
| @@ -15,6 +15,7 @@ import { createToken } from "../src/core/tokens.ts"; | ||
| 15 | 15 | import { createUser } from "../src/core/users.ts"; |
| 16 | 16 | import { serve, type Servers } from "../src/http/server.ts"; |
| 17 | 17 | import { quietLogs } from "./helpers/globals.ts"; |
| 18 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 18 | 19 | |
| 19 | 20 | describe("an anonymous client finds the report door from the page alone", () => { |
| 20 | 21 | quietLogs(); |
| @@ -28,7 +29,7 @@ describe("an anonymous client finds the report door from the page alone", () => | ||
| 28 | 29 | |
| 29 | 30 | beforeAll(async () => { |
| 30 | 31 | dataDir = await mkdtemp(join(tmpdir(), "forge-muse-journey-")); |
| 31 | app = createApp({ dataDir, port: 0 }); | |
| 32 | app = createApp({ dataDir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 32 | 33 | servers = await serve(app); |
| 33 | 34 | base = servers.url; |
| 34 | 35 | const a = await createUser(app.db, { |
test/note-pictures.test.ts +2 −1
| @@ -23,6 +23,7 @@ import { createSession } from "../src/core/sessions.ts"; | ||
| 23 | 23 | import { createUser, getUserByName } from "../src/core/users.ts"; |
| 24 | 24 | import { serve, type Servers } from "../src/http/server.ts"; |
| 25 | 25 | import { quietLogs } from "./helpers/globals.ts"; |
| 26 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 26 | 27 | |
| 27 | 28 | quietLogs(); |
| 28 | 29 | |
| @@ -36,7 +37,7 @@ describe("a note may carry a picture of the page", () => { | ||
| 36 | 37 | |
| 37 | 38 | beforeAll(async () => { |
| 38 | 39 | dataDir = await mkdtemp(join(tmpdir(), "forge-app-feedback-")); |
| 39 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 40 | app = createApp({ dataDir, port: 0, host: "forge.test" , trustedProxies: LOOPBACK_PROXIES }); | |
| 40 | 41 | servers = await serve(app); |
| 41 | 42 | runtime = new AppsRuntime(app, { |
| 42 | 43 | domain: DOMAIN, |
test/offer-limits.test.ts +2 −1
| @@ -14,6 +14,7 @@ import { fileOffer, OFFER_REQUEST_MAX, OFFERS_WAITING_MAX } from "../src/actions | ||
| 14 | 14 | import { getRepo, type Repo } from "../src/core/repos.ts"; |
| 15 | 15 | import { createUser, type User } from "../src/core/users.ts"; |
| 16 | 16 | import { serve, type Servers } from "../src/http/server.ts"; |
| 17 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 17 | 18 | |
| 18 | 19 | const patchFor = (n: number) => `diff --git a/f${n}.txt b/f${n}.txt\nnew file mode 100644\n--- /dev/null\n+++ b/f${n}.txt\n@@ -0,0 +1 @@\n+${n}\n`; |
| 19 | 20 | |
| @@ -42,7 +43,7 @@ describe("offer limits", () => { | ||
| 42 | 43 | |
| 43 | 44 | beforeAll(async () => { |
| 44 | 45 | dataDir = await mkdtemp(join(tmpdir(), "forge-offer-limits-")); |
| 45 | app = createApp({ dataDir, port: 0 }); | |
| 46 | app = createApp({ dataDir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 46 | 47 | servers = await serve(app); |
| 47 | 48 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); |
| 48 | 49 | if (!a.ok) throw new Error(a.error); |
test/offer-mcp.test.ts +2 −1
| @@ -10,6 +10,7 @@ import { OFFER_RECEIVED, OFFER_WORDS } from "../src/actions/offer.ts"; | ||
| 10 | 10 | import { getRepo } from "../src/core/repos.ts"; |
| 11 | 11 | import { createUser } from "../src/core/users.ts"; |
| 12 | 12 | import { serve, type Servers } from "../src/http/server.ts"; |
| 13 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 13 | 14 | |
| 14 | 15 | const PATCH = `diff --git a/hello.txt b/hello.txt |
| 15 | 16 | --- a/hello.txt |
| @@ -39,7 +40,7 @@ describe("offer over MCP", () => { | ||
| 39 | 40 | |
| 40 | 41 | beforeAll(async () => { |
| 41 | 42 | dataDir = await mkdtemp(join(tmpdir(), "forge-offer-mcp-")); |
| 42 | app = createApp({ dataDir, port: 0 }); | |
| 43 | app = createApp({ dataDir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 43 | 44 | servers = await serve(app); |
| 44 | 45 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); |
| 45 | 46 | if (!a.ok) throw new Error(a.error); |
test/offer-receipt.test.ts +2 −1
| @@ -13,6 +13,7 @@ import { submitReview } from "../src/core/reviews.ts"; | ||
| 13 | 13 | import { getRepo } from "../src/core/repos.ts"; |
| 14 | 14 | import { createUser, type User } from "../src/core/users.ts"; |
| 15 | 15 | import { serve, type Servers } from "../src/http/server.ts"; |
| 16 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 16 | 17 | |
| 17 | 18 | const PATCH = `diff --git a/hello.txt b/hello.txt |
| 18 | 19 | --- a/hello.txt |
| @@ -47,7 +48,7 @@ describe("offer receipt", () => { | ||
| 47 | 48 | |
| 48 | 49 | beforeAll(async () => { |
| 49 | 50 | dataDir = await mkdtemp(join(tmpdir(), "forge-offer-receipt-")); |
| 50 | app = createApp({ dataDir, port: 0 }); | |
| 51 | app = createApp({ dataDir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 51 | 52 | servers = await serve(app); |
| 52 | 53 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); |
| 53 | 54 | if (!a.ok) throw new Error(a.error); |
test/offer-stranger.test.ts +3 −2
| @@ -18,6 +18,7 @@ import { runGit } from "../src/git/env.ts"; | ||
| 18 | 18 | import { repoDir } from "../src/git/repos.ts"; |
| 19 | 19 | import { guestWaits } from "../src/origins/guest.ts"; |
| 20 | 20 | import { serve, type Servers } from "../src/http/server.ts"; |
| 21 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 21 | 22 | |
| 22 | 23 | const PATCH = `diff --git a/hello.txt b/hello.txt |
| 23 | 24 | --- a/hello.txt |
| @@ -55,7 +56,7 @@ describe("offers from outside", () => { | ||
| 55 | 56 | |
| 56 | 57 | beforeAll(async () => { |
| 57 | 58 | dataDir = await mkdtemp(join(tmpdir(), "forge-offer-")); |
| 58 | app = createApp({ dataDir, port: 0 }); | |
| 59 | app = createApp({ dataDir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 59 | 60 | servers = await serve(app); |
| 60 | 61 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); |
| 61 | 62 | if (!a.ok) throw new Error(a.error); |
| @@ -239,7 +240,7 @@ describe("offers from outside", () => { | ||
| 239 | 240 | test("one source is limited to three a minute (rate_limited)", async () => { |
| 240 | 241 | // A forge of its own: the forge-wide minute is spent by the tests above. |
| 241 | 242 | const dir = await mkdtemp(join(tmpdir(), "forge-offer-mcp-limit-")); |
| 242 | const own = createApp({ dataDir: dir, port: 0 }); | |
| 243 | const own = createApp({ dataDir: dir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 243 | 244 | const srv = await serve(own); |
| 244 | 245 | try { |
| 245 | 246 | const u = await createUser(own.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); |
test/reports.test.ts +2 −1
| @@ -22,6 +22,7 @@ import { createToken } from "../src/core/tokens.ts"; | ||
| 22 | 22 | import { createUser, type User } from "../src/core/users.ts"; |
| 23 | 23 | import type { Repo } from "../src/core/repos.ts"; |
| 24 | 24 | import { serve, type Servers } from "../src/http/server.ts"; |
| 25 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 25 | 26 | |
| 26 | 27 | describe("reports from outside", () => { |
| 27 | 28 | let dataDir: string; |
| @@ -39,7 +40,7 @@ describe("reports from outside", () => { | ||
| 39 | 40 | |
| 40 | 41 | beforeAll(async () => { |
| 41 | 42 | dataDir = await mkdtemp(join(tmpdir(), "forge-reports-")); |
| 42 | app = createApp({ dataDir, port: 0 }); | |
| 43 | app = createApp({ dataDir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 43 | 44 | servers = await serve(app); |
| 44 | 45 | base = servers.url; |
| 45 | 46 | const a = await createUser(app.db, { origin: "seed", trusted: true, |
test/scrape-evidence.test.ts +7 −1
| @@ -10,6 +10,7 @@ import { tmpdir } from "node:os"; | ||
| 10 | 10 | import { join } from "node:path"; |
| 11 | 11 | import { createApp, type App } from "../src/app.ts"; |
| 12 | 12 | import { wasViaEdge, notePeer } from "../src/http/peer.ts"; |
| 13 | import { parseCidrs } from "../src/kernel/cidr.ts"; | |
| 13 | 14 | import { createSession } from "../src/core/sessions.ts"; |
| 14 | 15 | import { createUser, type User } from "../src/core/users.ts"; |
| 15 | 16 | import { serve, type Servers } from "../src/http/server.ts"; |
| @@ -207,7 +208,12 @@ test("a bot score and country are believed only from a trusted edge", () => { | ||
| 207 | 208 | expect(wasViaEdge(viaProxy) || wasViaEdge(direct)).toBe(false); // loopback with no client named is the forge itself; a stranger is no edge |
| 208 | 209 | const named = new Request("http://forge.test/", { headers: { "cf-connecting-ip": "203.0.113.5" } }); |
| 209 | 210 | notePeer(named, "127.0.0.1", { proxies: [] }); |
| 210 | expect(wasViaEdge(named)).toBe(true); | |
| 211 | expect(wasViaEdge(named)).toBe(false); // unlisted, this machine is no edge: any process beside the forge could write the score | |
| 212 | const listedLoopback = parseCidrs("127.0.0.0/8"); | |
| 213 | if (!listedLoopback.ok) throw new Error("cidr"); | |
| 214 | const viaListed = new Request("http://forge.test/", { headers: { "cf-connecting-ip": "203.0.113.5" } }); | |
| 215 | notePeer(viaListed, "127.0.0.1", { proxies: listedLoopback.value }); | |
| 216 | expect(wasViaEdge(viaListed)).toBe(true); | |
| 211 | 217 | }); |
| 212 | 218 | |
| 213 | 219 | test("an agent account, even an admin's, cannot start a capture or import networks", async () => { |
test/strangers.test.ts +2 −1
| @@ -33,6 +33,7 @@ import { forgeSearch } from "../src/tui/forge.ts"; | ||
| 33 | 33 | import { Session } from "../src/tui/session.ts"; |
| 34 | 34 | import { seedAdmin } from "./helpers/admin.ts"; |
| 35 | 35 | import { gitClientEnv } from "./helpers/git-client.ts"; |
| 36 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 36 | 37 | |
| 37 | 38 | describe("Fresh: one piece of work per key per while", () => { |
| 38 | 39 | test("kept for ttl, made again after, by the clock it is given", () => { |
| @@ -84,7 +85,7 @@ describe("strangers at every door", () => { | ||
| 84 | 85 | |
| 85 | 86 | beforeAll(async () => { |
| 86 | 87 | dataDir = await mkdtemp(join(tmpdir(), "forge-strangers-")); |
| 87 | app = createApp({ dataDir, port: 0 }); | |
| 88 | app = createApp({ dataDir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 88 | 89 | // The windows stand still: a slow request on a loaded machine never |
| 89 | 90 | // slides one, so a count is the same count however long the test takes. |
| 90 | 91 | setStrangerClock(app, () => 1_000_000); |
test/stripe-hook.test.ts +2 −1
| @@ -10,6 +10,7 @@ import { serve, type Servers } from "../src/http/server.ts"; | ||
| 10 | 10 | import { getStored } from "../src/billing/store.ts"; |
| 11 | 11 | import { refusedCount, setStripeHookCaps } from "../src/http/stripe_hook.ts"; |
| 12 | 12 | import { setStripeFetch } from "../src/billing/sync.ts"; |
| 13 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 13 | 14 | |
| 14 | 15 | const secret = "whsec_hook_test_secret"; |
| 15 | 16 | let dir: string; |
| @@ -19,7 +20,7 @@ let base: string; | ||
| 19 | 20 | |
| 20 | 21 | beforeAll(async () => { |
| 21 | 22 | dir = await mkdtemp(join(tmpdir(), "stripe-hook-")); |
| 22 | app = createApp({ dataDir: dir, port: 0 }); | |
| 23 | app = createApp({ dataDir: dir, port: 0 , trustedProxies: LOOPBACK_PROXIES }); | |
| 23 | 24 | servers = await serve(app); |
| 24 | 25 | base = `http://127.0.0.1:${servers.port}`; |
| 25 | 26 | }); |
new above
offline