Public: anyone can read this; the crew can change it.
The check runs its tests without nested cells, so the gate can be green in a cell #1105
trevato/check-cells-off → main 1 commit
2 files
changed +136
−66
forge.json +1 −1
| @@ -14,7 +14,7 @@ | ||
| 14 | 14 | } |
| 15 | 15 | }, |
| 16 | 16 | "check": { |
| 17 | "run": "node --max-old-space-size=1024 ./node_modules/typescript/lib/tsc.js --noEmit && FORGE_CHECK_SHARDS=1 bun scripts/test-shards.ts --timeout 60000 --gate", | |
| 17 | "run": "node --max-old-space-size=1024 ./node_modules/typescript/lib/tsc.js --noEmit && FORGE_CELLS=off FORGE_CHECK_SHARDS=1 bun scripts/test-shards.ts --timeout 60000 --gate", | |
| 18 | 18 | "timeout": 7200, |
| 19 | 19 | "limits": { |
| 20 | 20 | "memory": 4096, |
test/contain.test.ts +135 −65
| @@ -53,7 +53,9 @@ describe("egress: what a cell may reach", () => { | ||
| 53 | 53 | expect(hostAllowed(["api.example.com"], "api.example.com", 443)).toBe(true); |
| 54 | 54 | expect(hostAllowed(["api.example.com"], "API.example.com.", 80)).toBe(true); |
| 55 | 55 | expect(hostAllowed(["api.example.com"], "example.com", 443)).toBe(false); |
| 56 | expect(hostAllowed(["api.example.com"], "evilapi.example.com", 443)).toBe(false); | |
| 56 | expect(hostAllowed(["api.example.com"], "evilapi.example.com", 443)).toBe( | |
| 57 | false, | |
| 58 | ); | |
| 57 | 59 | expect(hostAllowed(["*.example.com"], "a.b.example.com", 443)).toBe(true); |
| 58 | 60 | expect(hostAllowed(["*.example.com"], "example.com", 443)).toBe(false); |
| 59 | 61 | expect(hostAllowed(["*.example.com"], "badexample.com", 443)).toBe(false); |
| @@ -64,8 +66,12 @@ describe("egress: what a cell may reach", () => { | ||
| 64 | 66 | expect(hostAllowed(["api.example.com"], "api.example.com", 22)).toBe(false); |
| 65 | 67 | expect(hostAllowed(["api.example.com"], "api.example.com", 25)).toBe(false); |
| 66 | 68 | expect(hostAllowed(["*.example.com"], "a.example.com", 22)).toBe(false); |
| 67 | expect(hostAllowed(["db.example.com:5432"], "db.example.com", 5432)).toBe(true); | |
| 68 | expect(hostAllowed(["db.example.com:5432"], "db.example.com", 443)).toBe(false); | |
| 69 | expect(hostAllowed(["db.example.com:5432"], "db.example.com", 5432)).toBe( | |
| 70 | true, | |
| 71 | ); | |
| 72 | expect(hostAllowed(["db.example.com:5432"], "db.example.com", 443)).toBe( | |
| 73 | false, | |
| 74 | ); | |
| 69 | 75 | }); |
| 70 | 76 | |
| 71 | 77 | test("private, loopback, link-local and cluster addresses are never public", () => { |
| @@ -151,7 +157,9 @@ describe("egress: the proxy forwards what is allowed, byte for byte, and refuses | ||
| 151 | 157 | port: 0, |
| 152 | 158 | hostname: "127.0.0.1", |
| 153 | 159 | fetch: async (req) => |
| 154 | new Response(`${req.method} ${new URL(req.url).pathname} ${await req.text()}`), | |
| 160 | new Response( | |
| 161 | `${req.method} ${new URL(req.url).pathname} ${await req.text()}`, | |
| 162 | ), | |
| 155 | 163 | }); |
| 156 | 164 | socket = join(dir, "out.sock"); |
| 157 | 165 | egress = startEgress(socket, { |
| @@ -214,21 +222,33 @@ describe("egress: the proxy forwards what is allowed, byte for byte, and refuses | ||
| 214 | 222 | |
| 215 | 223 | test("a host nobody declared, a declared name on a private address, and the origin at another port: refused, named", async () => { |
| 216 | 224 | const port = upstream.port ?? 0; |
| 217 | const undeclared = await exchange(`CONNECT example.com:443 HTTP/1.1\r\n\r\n`); | |
| 225 | const undeclared = await exchange( | |
| 226 | `CONNECT example.com:443 HTTP/1.1\r\n\r\n`, | |
| 227 | ); | |
| 218 | 228 | expect(undeclared).toStartWith("HTTP/1.1 403"); |
| 219 | expect(undeclared).toContain('example.com:443 is not in this repository\'s egress (forge.json "egress")'); | |
| 220 | const privateName = await exchange(`GET http://localhost:${port}/ HTTP/1.1\r\nHost: localhost\r\n\r\n`); | |
| 229 | expect(undeclared).toContain( | |
| 230 | 'example.com:443 is not in this repository\'s egress (forge.json "egress")', | |
| 231 | ); | |
| 232 | const privateName = await exchange( | |
| 233 | `GET http://localhost:${port}/ HTTP/1.1\r\nHost: localhost\r\n\r\n`, | |
| 234 | ); | |
| 221 | 235 | expect(privateName).toStartWith("HTTP/1.1 403"); |
| 222 | 236 | expect(privateName).toContain("localhost resolves to a private address"); |
| 223 | const otherPort = await exchange(`CONNECT 127.0.0.1:${port + 1} HTTP/1.1\r\n\r\n`); | |
| 237 | const otherPort = await exchange( | |
| 238 | `CONNECT 127.0.0.1:${port + 1} HTTP/1.1\r\n\r\n`, | |
| 239 | ); | |
| 224 | 240 | expect(otherPort).toStartWith("HTTP/1.1 403"); |
| 225 | 241 | }); |
| 226 | 242 | |
| 227 | 243 | test("a declared host on a port it did not declare (ssh, mail): refused", async () => { |
| 228 | 244 | for (const port of [22, 25]) { |
| 229 | const reply = await exchange(`CONNECT localhost:${port} HTTP/1.1\r\n\r\n`); | |
| 245 | const reply = await exchange( | |
| 246 | `CONNECT localhost:${port} HTTP/1.1\r\n\r\n`, | |
| 247 | ); | |
| 230 | 248 | expect(reply).toStartWith("HTTP/1.1 403"); |
| 231 | expect(reply).toContain(`localhost:${port} is not in this repository's egress`); | |
| 249 | expect(reply).toContain( | |
| 250 | `localhost:${port} is not in this repository's egress`, | |
| 251 | ); | |
| 232 | 252 | } |
| 233 | 253 | }); |
| 234 | 254 | }); |
| @@ -285,7 +305,9 @@ describe("egress, reported: an undeclared public host goes through and is counte | ||
| 285 | 305 | for (const port of [25, 22, 6443]) { |
| 286 | 306 | const reply = await head(`CONNECT example.com:${port} HTTP/1.1\r\n\r\n`); |
| 287 | 307 | expect(reply).toStartWith("HTTP/1.1 403"); |
| 288 | expect(reply).toContain(`example.com:${port} is not in this repository's egress`); | |
| 308 | expect(reply).toContain( | |
| 309 | `example.com:${port} is not in this repository's egress`, | |
| 310 | ); | |
| 289 | 311 | } |
| 290 | 312 | expect(seen).toEqual([]); |
| 291 | 313 | }); |
| @@ -294,7 +316,9 @@ describe("egress, reported: an undeclared public host goes through and is counte | ||
| 294 | 316 | const reply = await head(`CONNECT example.com:443 HTTP/1.1\r\n\r\n`); |
| 295 | 317 | // Through: a tunnel, or the host not answering — never the refusal. |
| 296 | 318 | if (reply.includes("does not resolve")) { |
| 297 | console.log("SKIPPING: no DNS on this machine, so a public host cannot be resolved"); | |
| 319 | console.log( | |
| 320 | "SKIPPING: no DNS on this machine, so a public host cannot be resolved", | |
| 321 | ); | |
| 298 | 322 | return; |
| 299 | 323 | } |
| 300 | 324 | expect(reply).not.toStartWith("HTTP/1.1 403"); |
| @@ -323,11 +347,13 @@ describe("egress, reported: what an app reached is kept per repository and shown | ||
| 323 | 347 | expect(reachedHosts(dataDir, "01other")).toEqual([]); |
| 324 | 348 | const note = String(egressNote(dataDir, "01repo", [])); |
| 325 | 349 | expect(note).toContain("api.stripe.com:443"); |
| 326 | expect(note).toContain('"api.stripe.com"'); | |
| 350 | expect(note).toContain(""api.stripe.com""); | |
| 327 | 351 | expect(note).toContain("db.example.com:5432"); |
| 328 | 352 | const after = String(egressNote(dataDir, "01repo", ["api.stripe.com"])); |
| 329 | 353 | expect(after).not.toContain("api.stripe.com"); |
| 330 | expect(egressNote(dataDir, "01repo", ["api.stripe.com", "db.example.com:5432"])).toBeNull(); | |
| 354 | expect( | |
| 355 | egressNote(dataDir, "01repo", ["api.stripe.com", "db.example.com:5432"]), | |
| 356 | ).toBeNull(); | |
| 331 | 357 | }); |
| 332 | 358 | }); |
| 333 | 359 | |
| @@ -350,14 +376,31 @@ describe("cells in a pod see an allow-list of mounts, not the pod's mounts minus | ||
| 350 | 376 | |
| 351 | 377 | test("every mount but the root, /dev, /proc, /sys and the resolver's files is hidden", () => { |
| 352 | 378 | const hidden = hiddenMounts(mountinfo, "allow-list", ["/usr/local/bin"]); |
| 353 | for (const path of ["/data", "/etc/app-config", "/mnt/host", "/mnt/csi", "/nix/store", "/etc/plat-ca"]) | |
| 379 | for (const path of [ | |
| 380 | "/data", | |
| 381 | "/etc/app-config", | |
| 382 | "/mnt/host", | |
| 383 | "/mnt/csi", | |
| 384 | "/nix/store", | |
| 385 | "/etc/plat-ca", | |
| 386 | ]) | |
| 354 | 387 | expect(hidden).toContain(path); |
| 355 | for (const path of ["/", "/proc", "/dev", "/sys", "/etc/hosts", "/etc/resolv.conf", "/etc/hostname"]) | |
| 388 | for (const path of [ | |
| 389 | "/", | |
| 390 | "/proc", | |
| 391 | "/dev", | |
| 392 | "/sys", | |
| 393 | "/etc/hosts", | |
| 394 | "/etc/resolv.conf", | |
| 395 | "/etc/hostname", | |
| 396 | ]) | |
| 356 | 397 | expect(hidden).not.toContain(path); |
| 357 | 398 | }); |
| 358 | 399 | |
| 359 | 400 | test("a mount the runtime or a tool lives on stays, or no cell could start", () => { |
| 360 | const hidden = hiddenMounts(mountinfo, "allow-list", ["/nix/store/abc-bun/bin"]); | |
| 401 | const hidden = hiddenMounts(mountinfo, "allow-list", [ | |
| 402 | "/nix/store/abc-bun/bin", | |
| 403 | ]); | |
| 361 | 404 | expect(hidden).not.toContain("/nix/store"); |
| 362 | 405 | expect(hidden).toContain("/mnt/host"); |
| 363 | 406 | }); |
| @@ -370,7 +413,10 @@ describe("cells in a pod see an allow-list of mounts, not the pod's mounts minus | ||
| 370 | 413 | }); |
| 371 | 414 | |
| 372 | 415 | test("inside Kubernetes the allow-list is the default; FORGE_CELL_MOUNTS chooses either", () => { |
| 373 | const saved = { k: process.env["KUBERNETES_SERVICE_HOST"], m: process.env["FORGE_CELL_MOUNTS"] }; | |
| 416 | const saved = { | |
| 417 | k: process.env["KUBERNETES_SERVICE_HOST"], | |
| 418 | m: process.env["FORGE_CELL_MOUNTS"], | |
| 419 | }; | |
| 374 | 420 | try { |
| 375 | 421 | delete process.env["FORGE_CELL_MOUNTS"]; |
| 376 | 422 | process.env["KUBERNETES_SERVICE_HOST"] = "10.43.0.1"; |
| @@ -410,12 +456,24 @@ describe("cells hide every mount that may carry a secret", () => { | ||
| 410 | 456 | expect(hidden).toContain("/run/creds x"); |
| 411 | 457 | expect(hidden).toContain("/run/secrets"); |
| 412 | 458 | expect(hidden).toContain("/var/run/secrets"); |
| 413 | for (const kept of ["/", "/proc", "/dev", "/dev/shm", "/data", "/etc/hosts"]) | |
| 459 | for (const kept of [ | |
| 460 | "/", | |
| 461 | "/proc", | |
| 462 | "/dev", | |
| 463 | "/dev/shm", | |
| 464 | "/data", | |
| 465 | "/etc/hosts", | |
| 466 | ]) | |
| 414 | 467 | expect(hidden).not.toContain(kept); |
| 415 | 468 | }); |
| 416 | 469 | }); |
| 417 | 470 | |
| 418 | const noCells = missing("bwrap", "socat") || process.platform !== "linux"; | |
| 471 | // A host told not to hold cells (`FORGE_CELLS=off`: a check cell, where a | |
| 472 | // cell inside a cell is not a cell) is a host without cells for these. | |
| 473 | const noCells = | |
| 474 | process.env["FORGE_CELLS"] === "off" || | |
| 475 | missing("bwrap", "socat") || | |
| 476 | process.platform !== "linux"; | |
| 419 | 477 | |
| 420 | 478 | /** What the probe tries, from inside a deploy or a check. */ |
| 421 | 479 | function probeSource(targets: { |
| @@ -637,7 +695,11 @@ describe.skipIf(noCells)( | ||
| 637 | 695 | const forgeJson = JSON.stringify({ |
| 638 | 696 | // Declared, but it is this machine: still refused. |
| 639 | 697 | egress: ["localhost"], |
| 640 | app: { run: "bun probe.ts serve", health: "/", tty: "bun probe.ts check" }, | |
| 698 | app: { | |
| 699 | run: "bun probe.ts serve", | |
| 700 | health: "/", | |
| 701 | tty: "bun probe.ts check", | |
| 702 | }, | |
| 641 | 703 | check: { run: "bun probe.ts check", timeout: 120 }, |
| 642 | 704 | }); |
| 643 | 705 | const committed = await commitToBranch(app, mallory, repo, { |
| @@ -700,8 +762,9 @@ describe.skipIf(noCells)( | ||
| 700 | 762 | const shown = screen.split(/\r?\n/).find((l) => l.startsWith("PROBE ")); |
| 701 | 763 | expect(shown).toBeDefined(); |
| 702 | 764 | expectContained(JSON.parse(shown!.slice(6)) as Record<string, string>); |
| 703 | expect(screen.split(/\r?\n/).find((l) => l.startsWith("OWNAPP "))).toStartWith("OWNAPP reached"); | |
| 704 | ||
| 765 | expect( | |
| 766 | screen.split(/\r?\n/).find((l) => l.startsWith("OWNAPP ")), | |
| 767 | ).toStartWith("OWNAPP reached"); | |
| 705 | 768 | |
| 706 | 769 | const check = await settled( |
| 707 | 770 | "check", |
| @@ -824,51 +887,58 @@ describe.skipIf(noCells)("cells: limits stop a step, and say which", () => { | ||
| 824 | 887 | }, 30_000); |
| 825 | 888 | }); |
| 826 | 889 | |
| 827 | describe.skipIf(noCells)("cells, egress reported: a step reaches an undeclared public host and it is counted", () => { | |
| 828 | let dir: string; | |
| 829 | let listener: ReturnType<typeof Bun.serve>; | |
| 830 | beforeAll(async () => { | |
| 831 | dir = await mkdtemp(join(tmpdir(), "forge-cell-report-")); | |
| 832 | listener = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => new Response("inside") }); | |
| 833 | process.env["FORGE_CELLS_EGRESS"] = "report"; | |
| 834 | }); | |
| 835 | afterAll(async () => { | |
| 836 | delete process.env["FORGE_CELLS_EGRESS"]; | |
| 837 | listener.stop(true); | |
| 838 | await rm(dir, { recursive: true, force: true }); | |
| 839 | }); | |
| 890 | describe.skipIf(noCells)( | |
| 891 | "cells, egress reported: a step reaches an undeclared public host and it is counted", | |
| 892 | () => { | |
| 893 | let dir: string; | |
| 894 | let listener: ReturnType<typeof Bun.serve>; | |
| 895 | beforeAll(async () => { | |
| 896 | dir = await mkdtemp(join(tmpdir(), "forge-cell-report-")); | |
| 897 | listener = Bun.serve({ | |
| 898 | port: 0, | |
| 899 | hostname: "127.0.0.1", | |
| 900 | fetch: () => new Response("inside"), | |
| 901 | }); | |
| 902 | process.env["FORGE_CELLS_EGRESS"] = "report"; | |
| 903 | }); | |
| 904 | afterAll(async () => { | |
| 905 | delete process.env["FORGE_CELLS_EGRESS"]; | |
| 906 | listener.stop(true); | |
| 907 | await rm(dir, { recursive: true, force: true }); | |
| 908 | }); | |
| 840 | 909 | |
| 841 | test("the public host is let through and named; the private one is still refused", async () => { | |
| 842 | const seen: string[] = []; | |
| 843 | const lines: string[] = []; | |
| 844 | const result = await runStep( | |
| 845 | "check", | |
| 846 | `bun -e " | |
| 910 | test("the public host is let through and named; the private one is still refused", async () => { | |
| 911 | const seen: string[] = []; | |
| 912 | const lines: string[] = []; | |
| 913 | const result = await runStep( | |
| 914 | "check", | |
| 915 | `bun -e " | |
| 847 | 916 | const a = await fetch('https://example.com/', { signal: AbortSignal.timeout(10000) }).then((r) => r.status, () => 'refused'); |
| 848 | 917 | const b = await fetch('http://localhost:${listener.port}/', { proxy: process.env.HTTP_PROXY, signal: AbortSignal.timeout(5000) }).then((r) => r.status, () => 'refused'); |
| 849 | 918 | console.log('PUBLIC', a, 'PRIVATE', b); |
| 850 | 919 | "`, |
| 851 | { | |
| 852 | cwd: dir, | |
| 853 | env: childEnv({}), | |
| 854 | log: (line) => lines.push(line), | |
| 855 | signal: new AbortController().signal, | |
| 856 | timeoutMs: 60_000, | |
| 857 | cell: { | |
| 858 | dir, | |
| 859 | writable: [], | |
| 860 | home: join(dir, ".home"), | |
| 861 | hide: [], | |
| 862 | egress: [], | |
| 863 | reach: [], | |
| 864 | limits: { memoryMiB: 1024, cpus: 2, diskMiB: 1024 }, | |
| 865 | onUndeclared: (host, port) => seen.push(`${host}:${port}`), | |
| 920 | { | |
| 921 | cwd: dir, | |
| 922 | env: childEnv({}), | |
| 923 | log: (line) => lines.push(line), | |
| 924 | signal: new AbortController().signal, | |
| 925 | timeoutMs: 60_000, | |
| 926 | cell: { | |
| 927 | dir, | |
| 928 | writable: [], | |
| 929 | home: join(dir, ".home"), | |
| 930 | hide: [], | |
| 931 | egress: [], | |
| 932 | reach: [], | |
| 933 | limits: { memoryMiB: 1024, cpus: 2, diskMiB: 1024 }, | |
| 934 | onUndeclared: (host, port) => seen.push(`${host}:${port}`), | |
| 935 | }, | |
| 866 | 936 | }, |
| 867 | }, | |
| 868 | ); | |
| 869 | expect(result.ok).toBe(true); | |
| 870 | const said = lines.find((l) => l.startsWith("PUBLIC ")); | |
| 871 | expect(said).toMatch(/^PUBLIC 200 PRIVATE (403|refused)$/); | |
| 872 | expect(seen).toEqual(["example.com:443"]); | |
| 873 | }, 60_000); | |
| 874 | }); | |
| 937 | ); | |
| 938 | expect(result.ok).toBe(true); | |
| 939 | const said = lines.find((l) => l.startsWith("PUBLIC ")); | |
| 940 | expect(said).toMatch(/^PUBLIC 200 PRIVATE (403|refused)$/); | |
| 941 | expect(seen).toEqual(["example.com:443"]); | |
| 942 | }, 60_000); | |
| 943 | }, | |
| 944 | ); |
new above
offline