Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: address #503 round-2 review (chmod error handling + win32/strip …
…coverage)

- runtime/runtime-observability.ts: stop swallowing every chmod failure. Only
  ENOENT (dir removed by a concurrent process) is ignored; any other failure is
  surfaced rather than silently leaving a world-readable dir holding account
  ids/labels.
- runtime-observability.test.ts: add a win32 case asserting the snapshot persists
  WITHOUT calling chmod (POSIX-only branch), and reset the chmod mock per test.
- local-bridge.test.ts: extend the auth-enabled runtime-proxy path test to assert
  cookie + proxy-authorization are stripped too (parity with the no-key path).

typecheck + lint clean; full suite 4250 passed / 2 skipped.
  • Loading branch information
ndycode committed Jun 2, 2026
commit 3dcc500edf67d7b583437378974801aae538c8d4
14 changes: 11 additions & 3 deletions lib/runtime/runtime-observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,18 @@ async function writeSnapshot(snapshot: RuntimeObservabilitySnapshot): Promise<vo
await fs.mkdir(dir, { recursive: true, mode: 0o700 });
// mkdir's mode only applies to a freshly-created dir; an upgrade path with a
// pre-existing multi-auth dir keeps its old (possibly permissive) perms, so
// re-assert 0o700 on POSIX. Best-effort: a concurrent wrapper may race the
// chmod, and win32 ignores POSIX modes.
// re-assert 0o700 on POSIX. Only ENOENT is swallowed (the dir was removed by a
// concurrent process — the snapshot write below will recreate/fail as needed);
// any other chmod failure is surfaced rather than silently leaving a
// world-readable dir to hold account ids/labels.
if (process.platform !== "win32") {
await fs.chmod(dir, 0o700).catch(() => undefined);
try {
await fs.chmod(dir, 0o700);
} catch (error) {
if ((error as NodeJS.ErrnoException | undefined)?.code !== "ENOENT") {
throw error;
}
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let lastError: unknown = null;
for (let attempt = 0; attempt < 3; attempt += 1) {
Expand Down
8 changes: 6 additions & 2 deletions test/local-bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,19 @@ describe("local bridge", () => {
headers: {
authorization: "Bearer inbound-client-token",
"x-api-key": "inbound-secret-key",
cookie: "session=inbound-cookie-secret",
"proxy-authorization": "Basic inbound-proxy-cred",
},
});

const forwarded = calls.find((c) => c.url.endsWith("/v1/models"));
const headers = new Headers(forwarded?.init?.headers as HeadersInit);
// The runtime key is injected as Authorization, but the inbound x-api-key is
// still stripped — it must never cross the bridge, runtime key or not.
// The runtime key is injected as Authorization, but every inbound credential
// header is still stripped — none may cross the bridge, runtime key or not.
expect(headers.get("authorization")).toBe("Bearer runtime-secret-key");
expect(headers.get("x-api-key")).toBeNull();
expect(headers.get("cookie")).toBeNull();
expect(headers.get("proxy-authorization")).toBeNull();
});

it("refuses to start with a runtimeClientApiKey when auth is disabled", async () => {
Expand Down
23 changes: 23 additions & 0 deletions test/runtime-observability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe("runtime observability snapshot versioning", () => {
renameMock.mockReset();
unlinkMock.mockReset();
mkdirMock.mockReset();
chmodMock.mockReset();
if (originalVitestEnv === undefined) {
delete process.env.VITEST;
} else {
Expand Down Expand Up @@ -100,6 +101,28 @@ describe("runtime observability snapshot versioning", () => {
expect(unlinkMock).toHaveBeenCalled();
});

it("does not chmod the dir on win32 and still persists the snapshot", async () => {
// The 0o700 re-assert is POSIX-only (win32 perms are ACL-based, mode is a
// no-op). Persistence must still succeed without calling chmod.
process.env.VITEST = "";
const platformSpy = vi
.spyOn(process, "platform", "get")
.mockReturnValue("win32");
renameMock.mockResolvedValue(undefined);
try {
const mod = await import("../lib/runtime/runtime-observability.js");
mod.mutateRuntimeObservabilitySnapshot((snapshot) => {
snapshot.responsesRequests = 7;
});
await vi.waitFor(() => {
expect(renameMock).toHaveBeenCalled();
});
expect(chmodMock).not.toHaveBeenCalled();
} finally {
platformSpy.mockRestore();
}
});
Comment thread
ndycode marked this conversation as resolved.
Resolved

it("contains permanent snapshot write failures without leaving pending writes rejected", async () => {
process.env.VITEST = "";
renameMock.mockImplementation(async () => {
Expand Down