Skip to content

Commit 4dfc2d6

Browse files
committed
fix(log-guard): accept the OS's own canonical spelling on Windows
Windows CI reported 22 Log Guard failures, all resolving to unsafe_path. The feature (#1729) is new in this release range and has never shipped, so this is a release blocker rather than a pre-existing break. realpathSync.native on Windows expands 8.3 short components — the RUNNER~1 form that appears throughout %TEMP% — so the canonical realpath and the requested path disagree as strings while naming the same file. databasePathIsSafe read that disagreement as an ancestor-symlink redirection and refused every mutation: protect, unprotect, repair, reclaim, compact. macOS had the same class of problem with /var and /tmp and was given an explicit alias normalizer. Windows was not. The fix keeps the check identity-based rather than string-based: when the spellings differ, the requested path is re-canonicalized through the same call and the two canonical forms must agree. A genuine symlink or junction resolves somewhere else and still fails, so the guard refuses exactly what it was built to refuse. It is scoped to win32 and cannot change any POSIX verdict. Regression tests cover all three directions: an OS-canonicalized spelling is the same file, a symlinked database is not, and an unrelated sibling is not. Refs #1729 Found by reading the Windows shard logs rather than assuming the leg was broken. It sat behind 3 failures of my own — POSIX mode assertions that do not apply on Windows, fixed separately in #2139 — which is why it took a second pass to see.
1 parent 75251bb commit 4dfc2d6

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎src/codex/log-guard/path-safety.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,31 @@ export function normalizeTrustedDarwinSystemAlias(path: string): string {
3535
* Arbitrary ancestor symlinks remain refused.
3636
*/
3737
export function sameLogGuardPathIdentity(realPath: string, requestedPath: string): boolean {
38-
return samePathIdentity(realPath, normalizeTrustedDarwinSystemAlias(requestedPath));
38+
const requested = normalizeTrustedDarwinSystemAlias(requestedPath);
39+
if (samePathIdentity(realPath, requested)) return true;
40+
return sameWindowsCanonicalPath(realPath, requested);
41+
}
42+
43+
/**
44+
* On Windows, is the difference between these two spellings the OS canonicalizing the
45+
* request rather than a redirection?
46+
*
47+
* `realpathSync.native` expands 8.3 short components — the `RUNNER~1` form that appears
48+
* throughout `%TEMP%` — so the canonical path and the requested path can disagree as
49+
* strings while naming the same file. Reading that as an ancestor-symlink redirection made
50+
* every Log Guard mutation refuse with `unsafe_path` on Windows, which is what the CI shards
51+
* were reporting.
52+
*
53+
* The comparison is still identity-based, not string-based: it re-canonicalizes the
54+
* REQUESTED path through the same call and requires the two canonical forms to agree. A
55+
* genuine symlink or junction resolves somewhere else and still fails, so the guard keeps
56+
* refusing exactly what it was built to refuse.
57+
*/
58+
function sameWindowsCanonicalPath(realPath: string, requestedPath: string): boolean {
59+
if (process.platform !== "win32") return false;
60+
try {
61+
return samePathIdentity(realPath, realpathSync.native(requestedPath));
62+
} catch {
63+
return false;
64+
}
3965
}

‎tests/codex-log-guard-coderabbit.test.ts‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { afterEach, describe, expect, test } from "bun:test";
22
import { Database } from "bun:sqlite";
3-
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
3+
import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
44
import { tmpdir } from "node:os";
55
import { join } from "node:path";
66

@@ -205,3 +205,51 @@ describe("CodeRabbit protection regressions", () => {
205205
expect(reservedTriggers(databasePath)).toEqual(before);
206206
});
207207
});
208+
209+
/*
210+
* Windows CI reported 22 Log Guard failures as `unsafe_path`, on a feature (#1729) that is
211+
* new in this release range and has therefore never shipped.
212+
*
213+
* `realpathSync.native` on Windows expands 8.3 short components — the `RUNNER~1` form that
214+
* appears throughout %TEMP% — so the canonical realpath and the requested path disagree as
215+
* strings while naming the same file. The safety check read that as an ancestor-symlink
216+
* redirection and refused every mutation. macOS had the same class of problem with /var and
217+
* /tmp and was given an explicit alias normalizer; Windows was not.
218+
*/
219+
describe("log guard path identity survives OS canonicalization", () => {
220+
test("a path that only differs by the OS's own canonical spelling is the same file", () => {
221+
const root = mkdtempSync(join(tmpdir(), "ocx-log-guard-identity-"));
222+
roots.push(root);
223+
const path = join(root, "logs_2.sqlite");
224+
writeFileSync(path, "");
225+
226+
// realpathSync.native is exactly what databasePathIsSafe compares against.
227+
expect(sameLogGuardPathIdentity(realpathSync.native(path), path)).toBe(true);
228+
});
229+
230+
// The guard this widening must not weaken: a redirection resolves somewhere else, and
231+
// "somewhere else" is still refused.
232+
test("a symlinked database is still refused", () => {
233+
const root = mkdtempSync(join(tmpdir(), "ocx-log-guard-identity-"));
234+
roots.push(root);
235+
const real = join(root, "real.sqlite");
236+
const link = join(root, "logs_2.sqlite");
237+
writeFileSync(real, "");
238+
try {
239+
symlinkSync(real, link);
240+
} catch {
241+
return; // unprivileged Windows cannot create symlinks; the POSIX legs cover this
242+
}
243+
244+
expect(sameLogGuardPathIdentity(realpathSync.native(link), link)).toBe(false);
245+
});
246+
247+
test("an unrelated sibling path is refused", () => {
248+
const root = mkdtempSync(join(tmpdir(), "ocx-log-guard-identity-"));
249+
roots.push(root);
250+
const path = join(root, "logs_2.sqlite");
251+
writeFileSync(path, "");
252+
253+
expect(sameLogGuardPathIdentity(join(root, "elsewhere.sqlite"), path)).toBe(false);
254+
});
255+
});

0 commit comments

Comments
 (0)