Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

3.6 KB
Raw Blame
import { describe, expect, test } from "vitest";
import { SimpleBox } from "../lib/simplebox.js";

describe("SimpleBox network and secrets", { timeout: 180_000 }, () => {
  test("rejects legacy string network", () => {
    expect(
      () =>
        new SimpleBox({ image: "alpine:latest", network: "enabled" } as any),
    ).toThrow("SimpleBoxOptions.network must be an object");
  });

  test("rejects legacy top-level allowNet", () => {
    expect(
      () =>
        new SimpleBox({
          image: "alpine:latest",
          allowNet: ["example.com"],
        } as any),
    ).toThrow("SimpleBoxOptions.allowNet was removed");
  });

  test("accepts legacy flat network fields as outbound", () => {
    const box = new SimpleBox({
      image: "alpine:latest",
      network: { mode: "disabled" },
    } as any);

    expect((box as any)._boxOpts.network).toEqual({ mode: "disabled" });
  });

  test("rejects mixing legacy flat fields with outbound", () => {
    expect(
      () =>
        new SimpleBox({
          image: "alpine:latest",
          network: { mode: "disabled", outbound: { mode: "enabled" } },
        } as any),
    ).toThrow("cannot mix outbound with the deprecated mode/allowNet");
  });

  test("disabled network removes eth0", async () => {
    const box = new SimpleBox({
      image: "alpine:latest",
      network: { outbound: { mode: "disabled" } },
      autoRemove: true,
    });

    try {
      const result = await box.exec("sh", [
        "-c",
        "test ! -e /sys/class/net/eth0",
      ]);
      expect(result.exitCode).toBe(0);
    } finally {
      await box.stop();
    }
  });

  test("allowNet permits listed host access", async () => {
    const box = new SimpleBox({
      image: "alpine:latest",
      network: {
        outbound: {
          mode: "enabled",
          allowNet: ["example.com"],
        },
      },
      autoRemove: true,
    });

    try {
      const result = await box.exec("wget", [
        "-q",
        "-T",
        "10",
        "-O-",
        "http://example.com",
      ]);
      expect(result.exitCode).toBe(0);
      expect(result.stdout.toLowerCase()).toContain("example domain");
    } finally {
      await box.stop();
    }
  });

  test("secrets are injected into the configured header", async () => {
    const box = new SimpleBox({
      image: "python:slim",
      network: {
        outbound: {
          mode: "enabled",
          allowNet: ["httpbingo.org"],
        },
      },
      secrets: [
        {
          name: "testkey",
          value: "super-secret-value",
          hosts: ["httpbingo.org"],
          headers: { Authorization: "Bearer {value}" },
        },
      ],
      autoRemove: true,
    });

    try {
      const result = await box.exec("python3", [
        "-c",
        [
          "import json, os, urllib.request",
          "req = urllib.request.Request(",
          "  'https://httpbingo.org/headers',",
          "  headers={'Authorization': 'Bearer dummy', 'X-Probe': os.environ['BOXLITE_SECRET_TESTKEY']},",
          ")",
          "headers = json.load(urllib.request.urlopen(req, timeout=20))['headers']",
          "print('probe=' + headers['X-Probe'][0])",
          "print('authorization=' + headers['Authorization'][0])",
        ].join("\n"),
      ]);

      expect(result.exitCode).toBe(0);
      // This echo host returns the injected header, so the value is expected there.
      expect(result.stdout).toContain(
        "authorization=Bearer super-secret-value",
      );
      expect(result.stdout).toContain("probe=<BOXLITE_SECRET:testkey>");
    } finally {
      await box.stop();
    }
  });
});