| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { SANDBOX_BINDINGS, allows, harnessEnv, normalizeHost, sandboxNamespace, timeCapMessage } from "./egress.ts"; |
| 5 | |
| 6 | const hosts = ["registry.npmjs.org", "*.example.com"]; |
| 7 | |
| 8 | test("a brokered secret's hosts match exactly, or by wildcard", () => { |
| 9 | assert.equal(allows(hosts, "registry.npmjs.org"), true); |
| 10 | assert.equal(allows(hosts, "evil.com"), false); |
| 11 | assert.equal(allows(hosts, "registry.npmjs.org.evil.com"), false); |
| 12 | assert.equal(allows(hosts, "npmjs.org"), false); |
| 13 | assert.equal(allows(hosts, ""), false); |
| 14 | assert.equal(allows([], "registry.npmjs.org"), false); |
| 15 | }); |
| 16 | |
| 17 | test("hosts are compared without case, port or trailing dot", () => { |
| 18 | assert.equal(allows(hosts, "Registry.NPMJS.org:443"), true); |
| 19 | assert.equal(allows(hosts, "registry.npmjs.org."), true); |
| 20 | assert.equal(normalizeHost("[::1]:8080"), "[::1]"); |
| 21 | }); |
| 22 | |
| 23 | test("a wildcard covers subdomains at any depth, not the domain itself", () => { |
| 24 | assert.equal(allows(hosts, "api.example.com"), true); |
| 25 | assert.equal(allows(hosts, "a.b.example.com"), true); |
| 26 | assert.equal(allows(hosts, "example.com"), false); |
| 27 | assert.equal(allows(hosts, "badexample.com"), false); |
| 28 | }); |
| 29 | |
| 30 | test("the harness is told the run's time cap and branch, and nothing about the network", () => { |
| 31 | const guard = { policy: { minutes: { checks: 90 } }, minutes: 90 }; |
| 32 | const vars = harnessEnv(guard, { UPSTREAM_BRANCH: "trunk" }); |
| 33 | assert.deepEqual(Object.keys(vars), ["GUARDRAILS"]); |
| 34 | assert.deepEqual(JSON.parse(vars.GUARDRAILS), { |
| 35 | minutes: 90, |
| 36 | defaultBranch: "trunk", |
| 37 | }); |
| 38 | assert.equal(timeCapMessage(1), "Stopped: it reached its time cap of 1 minute."); |
| 39 | }); |
| 40 | |
| 41 | test("each sandbox class reports to its own namespace", () => { |
| 42 | const env = { SANDBOX: "standard", SANDBOX_2CORE: "two", SANDBOX_4CORE: "four" } as unknown as object; |
| 43 | assert.equal(sandboxNamespace(env, "AttemptSandbox"), "standard"); |
| 44 | assert.equal(sandboxNamespace(env, "Sandbox4Core"), "four"); |
| 45 | assert.equal(sandboxNamespace(env, "Sandbox2Core"), "two"); |
| 46 | assert.equal(sandboxNamespace(env, undefined), "standard"); |
| 47 | assert.equal(sandboxNamespace({ SANDBOX: "standard" }, "Sandbox4Core"), "standard"); |
| 48 | assert.deepEqual(Object.keys(SANDBOX_BINDINGS), ["AttemptSandbox", "Sandbox2Core", "Sandbox4Core"]); |
| 49 | }); |