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

Closed opened this
Closed without merging

trevato/check-cells-off → main 1 commit

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +136 −66
forge.json +1 −1
@@ -14,7 +14,7 @@
1414 }
1515 },
1616 "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",
1818 "timeout": 7200,
1919 "limits": {
2020 "memory": 4096,
test/contain.test.ts +135 −65
@@ -53,7 +53,9 @@ describe("egress: what a cell may reach", () => {
5353 expect(hostAllowed(["api.example.com"], "api.example.com", 443)).toBe(true);
5454 expect(hostAllowed(["api.example.com"], "API.example.com.", 80)).toBe(true);
5555 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 );
5759 expect(hostAllowed(["*.example.com"], "a.b.example.com", 443)).toBe(true);
5860 expect(hostAllowed(["*.example.com"], "example.com", 443)).toBe(false);
5961 expect(hostAllowed(["*.example.com"], "badexample.com", 443)).toBe(false);
@@ -64,8 +66,12 @@ describe("egress: what a cell may reach", () => {
6466 expect(hostAllowed(["api.example.com"], "api.example.com", 22)).toBe(false);
6567 expect(hostAllowed(["api.example.com"], "api.example.com", 25)).toBe(false);
6668 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 );
6975 });
7076
7177 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
151157 port: 0,
152158 hostname: "127.0.0.1",
153159 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 ),
155163 });
156164 socket = join(dir, "out.sock");
157165 egress = startEgress(socket, {
@@ -214,21 +222,33 @@ describe("egress: the proxy forwards what is allowed, byte for byte, and refuses
214222
215223 test("a host nobody declared, a declared name on a private address, and the origin at another port: refused, named", async () => {
216224 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 );
218228 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 );
221235 expect(privateName).toStartWith("HTTP/1.1 403");
222236 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 );
224240 expect(otherPort).toStartWith("HTTP/1.1 403");
225241 });
226242
227243 test("a declared host on a port it did not declare (ssh, mail): refused", async () => {
228244 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 );
230248 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 );
232252 }
233253 });
234254 });
@@ -285,7 +305,9 @@ describe("egress, reported: an undeclared public host goes through and is counte
285305 for (const port of [25, 22, 6443]) {
286306 const reply = await head(`CONNECT example.com:${port} HTTP/1.1\r\n\r\n`);
287307 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 );
289311 }
290312 expect(seen).toEqual([]);
291313 });
@@ -294,7 +316,9 @@ describe("egress, reported: an undeclared public host goes through and is counte
294316 const reply = await head(`CONNECT example.com:443 HTTP/1.1\r\n\r\n`);
295317 // Through: a tunnel, or the host not answering — never the refusal.
296318 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 );
298322 return;
299323 }
300324 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
323347 expect(reachedHosts(dataDir, "01other")).toEqual([]);
324348 const note = String(egressNote(dataDir, "01repo", []));
325349 expect(note).toContain("api.stripe.com:443");
326 expect(note).toContain('"api.stripe.com"');
350 expect(note).toContain(""api.stripe.com"");
327351 expect(note).toContain("db.example.com:5432");
328352 const after = String(egressNote(dataDir, "01repo", ["api.stripe.com"]));
329353 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();
331357 });
332358 });
333359
@@ -350,14 +376,31 @@ describe("cells in a pod see an allow-list of mounts, not the pod's mounts minus
350376
351377 test("every mount but the root, /dev, /proc, /sys and the resolver's files is hidden", () => {
352378 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 ])
354387 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 ])
356397 expect(hidden).not.toContain(path);
357398 });
358399
359400 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 ]);
361404 expect(hidden).not.toContain("/nix/store");
362405 expect(hidden).toContain("/mnt/host");
363406 });
@@ -370,7 +413,10 @@ describe("cells in a pod see an allow-list of mounts, not the pod's mounts minus
370413 });
371414
372415 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 };
374420 try {
375421 delete process.env["FORGE_CELL_MOUNTS"];
376422 process.env["KUBERNETES_SERVICE_HOST"] = "10.43.0.1";
@@ -410,12 +456,24 @@ describe("cells hide every mount that may carry a secret", () => {
410456 expect(hidden).toContain("/run/creds x");
411457 expect(hidden).toContain("/run/secrets");
412458 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 ])
414467 expect(hidden).not.toContain(kept);
415468 });
416469 });
417470
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";
419477
420478 /** What the probe tries, from inside a deploy or a check. */
421479 function probeSource(targets: {
@@ -637,7 +695,11 @@ describe.skipIf(noCells)(
637695 const forgeJson = JSON.stringify({
638696 // Declared, but it is this machine: still refused.
639697 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 },
641703 check: { run: "bun probe.ts check", timeout: 120 },
642704 });
643705 const committed = await commitToBranch(app, mallory, repo, {
@@ -700,8 +762,9 @@ describe.skipIf(noCells)(
700762 const shown = screen.split(/\r?\n/).find((l) => l.startsWith("PROBE "));
701763 expect(shown).toBeDefined();
702764 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");
705768
706769 const check = await settled(
707770 "check",
@@ -824,51 +887,58 @@ describe.skipIf(noCells)("cells: limits stop a step, and say which", () => {
824887 }, 30_000);
825888 });
826889
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 });
840909
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 "
847916 const a = await fetch('https://example.com/', { signal: AbortSignal.timeout(10000) }).then((r) => r.status, () => 'refused');
848917 const b = await fetch('http://localhost:${listener.port}/', { proxy: process.env.HTTP_PROXY, signal: AbortSignal.timeout(5000) }).then((r) => r.status, () => 'refused');
849918 console.log('PUBLIC', a, 'PRIVATE', b);
850919 "`,
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 },
866936 },
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 );

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs