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.
TypeScript browser-shellcontrol-planefile-managerhomelabmobile-firstnextjsptyreactself-hostedserver-managementshadcn-uisysadmintailwindcssterminaltypescriptvpsweb-desktopweb-terminal
fix(ci): close gateway typing and audit file races
Abdurrahman Fakhrul974e5e37eea5
diff --git a/lib/host/audit.ts b/lib/host/audit.tsindex c86b1008..aa17c68e 100644--- a/lib/host/audit.ts+++ b/lib/host/audit.ts@@ -150,6 +150,18 @@ async function readBoundedTail(file: string): Promise<string> {}}+async function copyHandle(source: Awaited<ReturnType<typeof fs.open>>, dest: Awaited<ReturnType<typeof fs.open>>, bytes: number): Promise<void> {+ const buffer = Buffer.allocUnsafe(64 * 1024);+ let position = 0;+ while (position < bytes) {+ const length = Math.min(buffer.length, bytes - position);+ const { bytesRead } = await source.read(buffer, 0, length, position);+ if (!bytesRead) break;+ await dest.write(buffer, 0, bytesRead, null);+ position += bytesRead;+ }+}+async function writeLine(line: string): Promise<void> {// A test run must never append to the OWNER's forensic trail. This is not// hypothetical: on 2026-08-10 two `mcp.denied` lines from lib/mcp/dispatch.test.ts@@ -158,22 +170,39 @@ async function writeLine(line: string): Promise<void> {// path, so this only silences the callers that forgot to.if (process.env.VITEST && !process.env.OS_AUDIT_LOG) return;const file = auditPath();+ let handle: Awaited<ReturnType<typeof fs.open>> | undefined;try {await fs.mkdir(path.dirname(file), { recursive: true, mode: 0o700 });- const stat = await fs.lstat(file).catch((error: NodeJS.ErrnoException) => {- if (error.code === "ENOENT") return null;- throw error;- });- if (stat && (!stat.isFile() || stat.isSymbolicLink())) throw new Error("audit log must be a regular file");- if (stat && stat.size + Buffer.byteLength(line) > AUDIT_MAX_BYTES) {+ handle = await fs.open(+ /* turbopackIgnore: true */ file,+ constants.O_RDWR | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW,+ 0o600,+ );+ const stat = await handle.stat();+ if (!stat.isFile()) throw new Error("audit log must be a regular file");++ if (stat.size + Buffer.byteLength(line) > AUDIT_MAX_BYTES) {const rotated = file + ".1";- await fs.rm(rotated, { force: true });- await fs.rename(file, rotated);+ let rotatedHandle: Awaited<ReturnType<typeof fs.open>> | undefined;+ try {+ rotatedHandle = await fs.open(+ /* turbopackIgnore: true */ rotated,+ constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC | constants.O_NOFOLLOW,+ 0o600,+ );+ await copyHandle(handle, rotatedHandle, stat.size);+ await rotatedHandle.sync();+ } finally {+ await rotatedHandle?.close().catch(() => undefined);+ }+ await handle.truncate(0);}- const handle = await fs.open(file, constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW, 0o600);- try { await handle.writeFile(line); } finally { await handle.close(); }++ await handle.writeFile(line);} catch (e) {console.error("[audit] write failed:", e instanceof Error ? e.message : e);+ } finally {+ await handle?.close().catch(() => undefined);}}diff --git a/scripts/mso-gateway-state.test.ts b/scripts/mso-gateway-state.test.tsindex e6db8785..93d79c58 100644--- a/scripts/mso-gateway-state.test.ts+++ b/scripts/mso-gateway-state.test.ts@@ -102,7 +102,7 @@ describe("gateway provider-neutral state model", () => {describe("managed tunnel local-runtime provenance", () => {it("refuses a health-shaped loopback listener with no owned or service process proof", () => {const f = fixture();- const env = { ...f.baseEnv, NODE_ENV: "production" };+ const env: NodeJS.ProcessEnv = { ...f.baseEnv, NODE_ENV: "production" };const out = spawnSync(GATEWAY, ["start"], { encoding: "utf8", env });expect(out.status).not.toBe(0);expect(out.stderr).toContain("not proven to belong to this checkout's mso.service");diff --git a/scripts/mso-gateway-test-fixture.ts b/scripts/mso-gateway-test-fixture.tsindex 40fa22f2..52e5b22c 100644--- a/scripts/mso-gateway-test-fixture.ts+++ b/scripts/mso-gateway-test-fixture.ts@@ -43,6 +43,7 @@ export function fixture() {const dir = fs.mkdtempSync(path.join(os.tmpdir(), "mso-gateway-"));onTestFinished(() => cleanupGatewayFixture(dir));const bin = path.join(dir, "bin"), state = path.join(dir, "state"), envFile = path.join(dir, ".env.local");+ const startFile = path.join(dir, "gateway-fake-starts");fs.mkdirSync(bin, { mode: 0o700 });fs.writeFileSync(envFile, "OS_SESSION_SECRET=fixture-only-not-a-real-secret\n", { mode: 0o600 });@@ -59,7 +60,10 @@ export function fixture() {`#!/usr/bin/env nodeif (process.argv.includes('--version')) { console.log('cloudflared version fixture'); process.exit(0); }if (process.env.OS_SESSION_SECRET || process.env.OS_LOGIN_PASSWORD) require('fs').writeFileSync(process.env.HOME + '/cloudflared-secret-env', 'leaked');-if (process.argv.includes('--url')) console.error('INF https://mso-gateway-fixture.trycloudflare.com');+if (process.argv.includes('--url')) {+ require('fs').appendFileSync(process.env.HOME + '/gateway-fake-starts', 'start\n');+ console.error('INF https://mso-gateway-fixture.trycloudflare.com');+}process.on('SIGTERM', () => process.exit(0));setInterval(() => {}, 1000);`,@@ -69,7 +73,7 @@ setInterval(() => {}, 1000);const procTcp = path.join(dir, "proc-tcp"), procTcp6 = path.join(dir, "proc-tcp6");fs.writeFileSync(procTcp, " sl local_address rem_address st\n");fs.writeFileSync(procTcp6, " sl local_address rem_address st\n");- const baseEnv = {+ const baseEnv: NodeJS.ProcessEnv = {...process.env,HOME: dir,MSO_GATEWAY_ROOT: ROOT,@@ -82,7 +86,7 @@ setInterval(() => {}, 1000);MSO_GATEWAY_PROC_NET_TCP: procTcp,MSO_GATEWAY_PROC_NET_TCP6: procTcp6,};- return { dir, bin, state, envFile, curl, cloudflared, baseEnv };+ return { dir, bin, state, envFile, curl, cloudflared, startFile, baseEnv };}export function runGateway(args: string[], env: NodeJS.ProcessEnv) {