Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

5.8 KB
Raw Blame
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import YAML from "yaml";
import { atomicReplace, readConfig } from "./lib/gateway-named-config.mjs";

const ROOT = path.resolve(__dirname, "..");
const HELPER = path.join(__dirname, "lib/gateway-named-config.mjs");
const TUNNEL = "11111111-2222-4333-8444-555555555555";
const dirs: string[] = [];
afterEach(() => { for (const dir of dirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); });

function fixture() {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "mso-named-config-")); dirs.push(dir);
  const file = path.join(dir, "config.yml"), cred = path.join(dir, "credential.json");
  fs.writeFileSync(cred, "must-not-be-read", { mode: 0o600 });
  const cfg: Record<string, unknown> = { tunnel: TUNNEL, "credentials-file": cred, ingress: [
    { hostname: "mso.example.test", service: "http://127.0.0.1:4005" }, { service: "http_status:404" },
  ] };
  const write = () => fs.writeFileSync(file, YAML.stringify(cfg), { mode: 0o600 });
  write();
  return { dir, file, cred, cfg, write };
}
function run(file: string, action = "inspect", template = "{id}.mso.example.test", host = "9router.mso.example.test") {
  return spawnSync(process.execPath, [HELPER, ROOT, file, "https://mso.example.test", "http://127.0.0.1:4005", template, action, host, TUNNEL], { encoding: "utf8", timeout: 3000 });
}

describe("named gateway YAML policy", () => {
  it.each(["{id}.mso.example.test", "{id}-mso.example.test"])("supports exact managed app template %s", (template) => {
    const f = fixture(), host = template.replace("{id}", "9router");
    expect(run(f.file, "ensure", template, host).status).toBe(0);
    expect(run(f.file, "validate", template, host).status).toBe(0);
    expect(YAML.parse(fs.readFileSync(f.file, "utf8")).ingress[1]).toEqual({ hostname: host, service: "http://127.0.0.1:4005" });
  });

  it.each([
    { hostname: "*.mso.example.test", service: "http://127.0.0.1:4005" },
    { hostname: "mso.example.test", service: "http://127.0.0.1:4005" },
    { hostname: "9router.mso.example.test", service: "http://127.0.0.1:4005", path: "/admin" },
    { hostname: "9router.mso.example.test", service: "http://127.0.0.2:4005" },
    { hostname: "9router.mso.example.test", service: "http://127.0.0.1:4005", originRequest: { httpHostHeader: "mso.example.test" } },
  ])("rejects conflicting/unsafe app ingress %#", (rule) => {
    const f = fixture(); (f.cfg.ingress as unknown[]).splice(1, 0, rule); f.write();
    const before = fs.readFileSync(f.file, "utf8");
    expect(run(f.file, "ensure").status).toBe(1);
    expect(fs.readFileSync(f.file, "utf8")).toBe(before);
  });

  it("overrides a valid cockpit-wide Host header for the app while preserving other global settings", () => {
    const f = fixture(); f.cfg.originRequest = { httpHostHeader: "mso.example.test", connectTimeout: "20s" }; f.write();
    const result = run(f.file, "ensure");
    expect(result.status, result.stderr).toBe(0);
    const cfg = YAML.parse(fs.readFileSync(f.file, "utf8"));
    expect(cfg.originRequest).toEqual(f.cfg.originRequest);
    expect(cfg.ingress[1].originRequest.httpHostHeader).toBe("9router.mso.example.test");
  });

  it("rejects cockpit Host overrides and a different loopback listener in app-route mode", () => {
    const f = fixture(); f.cfg.originRequest = { httpHostHeader: "other.example.test" }; f.write();
    expect(run(f.file).status).toBe(1);
    delete f.cfg.originRequest;
    (f.cfg.ingress as { service: string }[])[0].service = "http://127.0.0.2:4005"; f.write();
    expect(run(f.file).status).toBe(1);
    expect(run(f.file, "validate").status).toBe(0);
  });

  it("rejects a symlink or FIFO config without blocking", () => {
    const f = fixture(), link = path.join(f.dir, "link.yml"), fifo = path.join(f.dir, "fifo.yml");
    fs.symlinkSync(f.file, link);
    expect(run(link).status).toBe(1);
    expect(spawnSync("mkfifo", [fifo]).status).toBe(0);
    const result = run(fifo);
    expect(result.error).toBeUndefined();
    expect(result.status).toBe(1);
  });

  it("rejects a different inode before rereading its contents", () => {
    const f = fixture(), snapshot = readConfig(ROOT, f.file);
    fs.renameSync(f.file, path.join(f.dir, "original.yml"));
    fs.writeFileSync(f.file, snapshot.text, { mode: 0o600 });
    const read = vi.spyOn(fs, "readFileSync");
    try {
      expect(() => atomicReplace(f.file, snapshot, snapshot.text)).toThrow(/changed concurrently/);
      expect(read).not.toHaveBeenCalled();
    } finally { read.mockRestore(); }
  });

  it("does not read a replacement symlink target during the final config comparison", () => {
    const f = fixture(), snapshot = readConfig(ROOT, f.file);
    const target = path.join(f.dir, "unrelated.yml"), retained = path.join(f.dir, "original.yml");
    const unrelated = "unrelated private fixture\n", next = snapshot.text + "# retained configuration\n";
    fs.writeFileSync(target, unrelated, { mode: 0o600 });
    const originalRead = fs.readFileSync;
    let swapped = false, followed = false, failure: unknown;
    const read = vi.spyOn(fs, "readFileSync").mockImplementation((...args) => {
      if (!swapped) {
        fs.renameSync(f.file, retained);
        fs.symlinkSync(target, f.file);
        swapped = true;
      }
      const value = originalRead(...args);
      if (value === unrelated) followed = true;
      return value;
    });
    try { atomicReplace(f.file, snapshot, next); }
    catch (error) { failure = error; }
    finally { read.mockRestore(); }
    expect(swapped).toBe(true);
    expect(followed).toBe(false);
    expect(failure).toBeUndefined();
    expect(fs.readFileSync(target, "utf8")).toBe(unrelated);
    expect(fs.readFileSync(f.file, "utf8")).toBe(next);
  });
});