Skip to content

Commit 755e65a

Browse files
committed
fix(auto-approval): match symlinked workspace roots
Canonicalize verified task-local workspace root spellings through platform-specific strategies, preserve ordered exclusions and lexical-parent rules, and log resolution failures without widening read or write permissions.
1 parent 1d37994 commit 755e65a

9 files changed

Lines changed: 473 additions & 7 deletions
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { PosixWorkspaceRootStrategy } from "./strategies/PosixWorkspaceRootStrategy"
2+
import { WindowsWorkspaceRootStrategy } from "./strategies/WindowsWorkspaceRootStrategy"
3+
import type { WorkspaceRootStrategy } from "./strategies/WorkspaceRootStrategy"
4+
5+
/** Translate only a verified workspace-root spelling, never a file symlink. */
6+
export class WorkspaceRootCanonicalizer {
7+
private constructor(
8+
private readonly root: string | undefined,
9+
private readonly canonicalRoot: string | undefined,
10+
private readonly strategy: WorkspaceRootStrategy,
11+
) {}
12+
13+
static forStrategy(
14+
cwd: string | undefined,
15+
canonicalCwd: string | undefined,
16+
strategy: WorkspaceRootStrategy,
17+
): WorkspaceRootCanonicalizer {
18+
const root = strategy.normalizeAbsoluteRoot(cwd)
19+
const canonicalRoot = strategy.normalizeAbsoluteRoot(canonicalCwd)
20+
return new WorkspaceRootCanonicalizer(root, canonicalRoot, strategy)
21+
}
22+
23+
static forPlatform(
24+
cwd: string | undefined,
25+
canonicalCwd: string | undefined,
26+
isWindows: boolean,
27+
): WorkspaceRootCanonicalizer {
28+
const strategy = isWindows ? new WindowsWorkspaceRootStrategy() : new PosixWorkspaceRootStrategy()
29+
return WorkspaceRootCanonicalizer.forStrategy(cwd, canonicalCwd, strategy)
30+
}
31+
32+
canonicalize(value: string): string {
33+
if (this.root === undefined || this.canonicalRoot === undefined) return value
34+
35+
return this.replaceWorkspaceRootPrefix(value, this.root, this.canonicalRoot)
36+
}
37+
38+
private replaceWorkspaceRootPrefix(value: string, root: string, canonicalRoot: string): string {
39+
const comparableValue = this.strategy.comparisonKey(value)
40+
const comparableRoot = this.strategy.comparisonKey(root)
41+
return comparableValue === comparableRoot || comparableValue.startsWith(`${comparableRoot}/`)
42+
? `${canonicalRoot}${value.slice(root.length)}`
43+
: value
44+
}
45+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import { PosixWorkspaceRootStrategy } from "../strategies/PosixWorkspaceRootStrategy"
2+
import { WindowsWorkspaceRootStrategy } from "../strategies/WindowsWorkspaceRootStrategy"
3+
import type { WorkspaceRootStrategy } from "../strategies/WorkspaceRootStrategy"
4+
import { WorkspaceRootCanonicalizer } from "../WorkspaceRootCanonicalizer"
5+
6+
it("normalizes roots once in the factory before any path comparisons", () => {
7+
const normalizeAbsoluteRoot = vi
8+
.fn<WorkspaceRootStrategy["normalizeAbsoluteRoot"]>()
9+
.mockReturnValueOnce("/aliases/project")
10+
.mockReturnValueOnce("/storage/project")
11+
const comparisonKey = vi.fn((value: string) => value)
12+
const strategy: WorkspaceRootStrategy = { normalizeAbsoluteRoot, comparisonKey }
13+
14+
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy("/aliases/project/", "/storage/project/", strategy)
15+
16+
expect(normalizeAbsoluteRoot).toHaveBeenCalledTimes(2)
17+
expect(normalizeAbsoluteRoot).toHaveBeenNthCalledWith(1, "/aliases/project/")
18+
expect(normalizeAbsoluteRoot).toHaveBeenNthCalledWith(2, "/storage/project/")
19+
expect(comparisonKey).not.toHaveBeenCalled()
20+
21+
expect(canonicalizer.canonicalize("/aliases/project/docs/file.md")).toBe("/storage/project/docs/file.md")
22+
expect(canonicalizer.canonicalize("/aliases/project/notes.md")).toBe("/storage/project/notes.md")
23+
expect(normalizeAbsoluteRoot).toHaveBeenCalledTimes(2)
24+
})
25+
26+
describe.each([
27+
{
28+
platform: "POSIX",
29+
isWindows: false,
30+
cwd: "/aliases/project",
31+
canonicalCwd: "/storage/project",
32+
root: "/aliases/project",
33+
canonicalRoot: "/storage/project",
34+
},
35+
{
36+
platform: "Windows",
37+
isWindows: true,
38+
cwd: "C:\\Aliases\\Project",
39+
canonicalCwd: "D:\\Storage\\Project",
40+
root: "/C/Aliases/Project",
41+
canonicalRoot: "/D/Storage/Project",
42+
},
43+
])("workspace-root canonicalization on $platform", ({ isWindows, cwd, canonicalCwd, root, canonicalRoot }) => {
44+
const canonicalizer = WorkspaceRootCanonicalizer.forPlatform(cwd, canonicalCwd, isWindows)
45+
46+
it("translates an exact root and preserves descendant spelling and globs", () => {
47+
expect(canonicalizer.canonicalize(root)).toBe(canonicalRoot)
48+
expect(canonicalizer.canonicalize(`${root}/Docs/NOTES.md`)).toBe(`${canonicalRoot}/Docs/NOTES.md`)
49+
expect(canonicalizer.canonicalize(`${root}/docs/**`)).toBe(`${canonicalRoot}/docs/**`)
50+
})
51+
52+
it("does not translate similarly prefixed siblings or lexical parents", () => {
53+
for (const value of [`${root}-other/docs/file.md`, `${root}2/file.md`, "/elsewhere/docs/file.md"]) {
54+
expect(canonicalizer.canonicalize(value)).toBe(value)
55+
}
56+
})
57+
58+
it("leaves canonical-root paths and relative values unchanged", () => {
59+
for (const value of [canonicalRoot, `${canonicalRoot}/docs/file.md`, "docs/file.md", "../shared/file.md"]) {
60+
expect(canonicalizer.canonicalize(value)).toBe(value)
61+
}
62+
})
63+
64+
it.each([undefined, "", "relative/project", "C:relative"])(
65+
"does not infer equivalence from an unknown or relative root: %s",
66+
(invalidRoot) => {
67+
const value = `${root}/docs/file.md`
68+
expect(
69+
WorkspaceRootCanonicalizer.forPlatform(invalidRoot, canonicalCwd, isWindows).canonicalize(value),
70+
).toBe(value)
71+
expect(WorkspaceRootCanonicalizer.forPlatform(cwd, invalidRoot, isWindows).canonicalize(value)).toBe(value)
72+
},
73+
)
74+
})
75+
76+
describe("POSIX workspace-root strategy", () => {
77+
it("compares case-sensitively and keeps backslashes as filename characters", () => {
78+
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
79+
"/aliases/My\\Project",
80+
"/storage/My\\Project",
81+
new PosixWorkspaceRootStrategy(),
82+
)
83+
expect(canonicalizer.canonicalize("/aliases/My\\Project/docs/file.md")).toBe(
84+
"/storage/My\\Project/docs/file.md",
85+
)
86+
expect(canonicalizer.canonicalize("/aliases/my\\project/docs/file.md")).toBe(
87+
"/aliases/my\\project/docs/file.md",
88+
)
89+
expect(canonicalizer.canonicalize("/aliases/My/Project/docs/file.md")).toBe("/aliases/My/Project/docs/file.md")
90+
})
91+
92+
it("normalizes root dot segments and trailing separators", () => {
93+
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
94+
"/aliases/temporary/../project/",
95+
"/storage/./project/",
96+
new PosixWorkspaceRootStrategy(),
97+
)
98+
expect(canonicalizer.canonicalize("/aliases/project/docs/file.md")).toBe("/storage/project/docs/file.md")
99+
})
100+
101+
it("does not interpret Windows drive paths as absolute roots", () => {
102+
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
103+
"C:/aliases/project",
104+
"D:/storage/project",
105+
new PosixWorkspaceRootStrategy(),
106+
)
107+
expect(canonicalizer.canonicalize("/C/aliases/project/docs/file.md")).toBe("/C/aliases/project/docs/file.md")
108+
})
109+
})
110+
111+
describe("Windows workspace-root strategy", () => {
112+
it("compares case-insensitively across drives without changing descendant case", () => {
113+
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
114+
"C:\\Aliases\\Project",
115+
"D:\\Storage\\Project",
116+
new WindowsWorkspaceRootStrategy(),
117+
)
118+
expect(canonicalizer.canonicalize("/c/ALIASES/project/Docs/NOTES.md")).toBe("/D/Storage/Project/Docs/NOTES.md")
119+
expect(canonicalizer.canonicalize("/E/Aliases/Project/Docs/NOTES.md")).toBe("/E/Aliases/Project/Docs/NOTES.md")
120+
})
121+
122+
it("normalizes mixed root separators, dot segments, and trailing separators", () => {
123+
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
124+
"C:\\Aliases/temporary/../Project\\",
125+
"D:/Storage/./Project/",
126+
new WindowsWorkspaceRootStrategy(),
127+
)
128+
expect(canonicalizer.canonicalize("/C/Aliases/Project/docs/file.md")).toBe("/D/Storage/Project/docs/file.md")
129+
})
130+
131+
it("supports drive roots and drive-less absolute roots in matcher notation", () => {
132+
const strategy = new WindowsWorkspaceRootStrategy()
133+
expect(WorkspaceRootCanonicalizer.forStrategy("C:", "D:/", strategy).canonicalize("/C/docs/file.md")).toBe(
134+
"/D/docs/file.md",
135+
)
136+
expect(
137+
WorkspaceRootCanonicalizer.forStrategy("\\Aliases\\Project", "/Storage/Project", strategy).canonicalize(
138+
"/Aliases/Project/docs/file.md",
139+
),
140+
).toBe("/Storage/Project/docs/file.md")
141+
})
142+
143+
it("preserves the existing matcher normalization of network roots", () => {
144+
const canonicalizer = WorkspaceRootCanonicalizer.forStrategy(
145+
"\\\\server\\share\\project",
146+
"\\\\server\\storage\\project",
147+
new WindowsWorkspaceRootStrategy(),
148+
)
149+
expect(canonicalizer.canonicalize("/SERVER/SHARE/project/docs/file.md")).toBe(
150+
"/server/storage/project/docs/file.md",
151+
)
152+
})
153+
})
154+
155+
it.each([false, true])("preserves filesystem-root equivalence (Windows=%s)", (isWindows) => {
156+
expect(WorkspaceRootCanonicalizer.forPlatform("/", "/storage", isWindows).canonicalize("/docs/file.md")).toBe(
157+
"/storage/docs/file.md",
158+
)
159+
expect(
160+
WorkspaceRootCanonicalizer.forPlatform("/aliases", "/", isWindows).canonicalize("/aliases/docs/file.md"),
161+
).toBe("/docs/file.md")
162+
})

‎src/core/auto-approval/__tests__/windowsPaths.spec.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,34 @@ describe("matching on Windows", () => {
9999
expect(matches("C:/path/to/repo/etc/passwd", ["passwd"])).toBe(true)
100100
expect(matches("C:/etc/passwd", ["passwd"])).toBe(false)
101101
})
102+
103+
it("matches equivalent workspace roots across drives with Windows case and ordered exclusions", () => {
104+
const rootMatch = (patterns: string[]) =>
105+
matcher().isFileMatchedByPatterns({
106+
filePath: "d:\\storage\\project\\DOCS\\NOTES.md",
107+
cwd: "C:\\Aliases\\Project",
108+
canonicalCwd: "D:\\Storage\\Project",
109+
patterns,
110+
})
111+
expect(rootMatch(["docs/**"])).toBe(true)
112+
expect(rootMatch(["c:/aliases/PROJECT/docs/**"])).toBe(true)
113+
expect(rootMatch(["d:/STORAGE/project/docs/**"])).toBe(true)
114+
expect(rootMatch(["docs/**", "!c:/aliases/project/docs/notes.md"])).toBe(false)
115+
expect(rootMatch(["docs/**", "!D:/Storage/Project/docs/notes.md"])).toBe(false)
116+
})
117+
118+
it("keeps Windows siblings and lexical-parent outside patterns separate from canonical roots", () => {
119+
const rootMatch = (filePath: string, patterns: string[]) =>
120+
matcher().isFileMatchedByPatterns({
121+
filePath,
122+
cwd: "C:\\Aliases\\Project",
123+
canonicalCwd: "D:\\Storage\\Project",
124+
patterns,
125+
})
126+
expect(rootMatch("C:/Aliases/shared/file.md", ["../shared/file.md"])).toBe(true)
127+
expect(rootMatch("D:/Storage/shared/file.md", ["../shared/file.md"])).toBe(false)
128+
expect(rootMatch("D:/Storage/shared/file.md", ["D:/Storage/shared/file.md"])).toBe(true)
129+
expect(rootMatch("D:/Storage/Project-other/docs/file.md", ["docs/**"])).toBe(false)
130+
expect(rootMatch("D:/Storage/Project-other/docs/file.md", ["C:/Aliases/Project-other/docs/**"])).toBe(false)
131+
})
102132
})

0 commit comments

Comments
 (0)