Skip to content
55 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { SANDBOX_BINDINGS, allows, harnessEnv, normalizeHost, sandboxNamespace, timeCapMessage } from "./egress.ts";
5
6const hosts = ["registry.npmjs.org", "*.example.com"];
7
8test("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
17test("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
23test("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
30test("the harness is told the run's rules, caps and branch, and nothing about the network", () => {
31 const guard = {
32 policy: { rules: { sudo: true }, deny: ["Bash(kubectl:*)"], budgetUsd: 5, minutes: { implement: 90 } },
33 minutes: 90,
34 };
35 const vars = harnessEnv(guard, { UPSTREAM_BRANCH: "trunk" });
36 assert.deepEqual(Object.keys(vars), ["GUARDRAILS"]);
37 assert.deepEqual(JSON.parse(vars.GUARDRAILS), {
38 rules: { sudo: true },
39 deny: ["Bash(kubectl:*)"],
40 budgetUsd: 5,
41 minutes: 90,
42 defaultBranch: "trunk",
43 });
44 assert.equal(timeCapMessage(1), "Stopped: it reached its time cap of 1 minute.");
45});
46
47test("each sandbox class reports to its own namespace", () => {
48 const env = { SANDBOX: "standard", SANDBOX_2CORE: "two", SANDBOX_4CORE: "four" } as unknown as object;
49 assert.equal(sandboxNamespace(env, "AttemptSandbox"), "standard");
50 assert.equal(sandboxNamespace(env, "Sandbox4Core"), "four");
51 assert.equal(sandboxNamespace(env, "Sandbox2Core"), "two");
52 assert.equal(sandboxNamespace(env, undefined), "standard");
53 assert.equal(sandboxNamespace({ SANDBOX: "standard" }, "Sandbox4Core"), "standard");
54 assert.deepEqual(Object.keys(SANDBOX_BINDINGS), ["AttemptSandbox", "Sandbox2Core", "Sandbox4Core"]);
55});