Skip to content

Commit cd46b0f

Browse files
committed
fix(auto-approval): preserve Windows volume boundaries
Distinguish UNC prefixes from rooted paths with extra separators. Normalize workspace roots with Windows path rules before conversion so parent segments cannot consume drive or share names. Add matcher and approval regressions for both separator spellings. Part of Zoo-Code-Org#1948.
1 parent d191fea commit cd46b0f

4 files changed

Lines changed: 130 additions & 6 deletions

File tree

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,86 @@ describe("matching on Windows", () => {
3030
const matches = (filePath: string, patterns: string[], cwd: string | undefined = WINDOWS_CWD) =>
3131
matcher().isFileMatchedByPatterns({ filePath, cwd, patterns, homeDir: WINDOWS_HOME })
3232

33+
it.each([
34+
"///server/share/docs/**",
35+
"////server/share/docs/**",
36+
"\\\\\\server\\share\\docs\\**",
37+
"\\\\\\\\server\\share\\docs\\**",
38+
])("treats extra leading separators in a grant as drive-rooted, not UNC: %s", (pattern) => {
39+
expect(matches("//server/share/docs/private.md", [pattern], "C:/repo")).toBe(false)
40+
expect(matches("C:/server/share/docs/private.md", [pattern], "C:/repo")).toBe(true)
41+
expect(matches("//server/share/docs/private.md", ["//server/share/docs/**"], "C:/repo")).toBe(true)
42+
})
43+
44+
it.each([
45+
"///server/share/docs/private.md",
46+
"////server/share/docs/private.md",
47+
"\\\\\\server\\share\\docs\\private.md",
48+
"\\\\\\\\server\\share\\docs\\private.md",
49+
])("treats extra leading separators in a candidate as drive-rooted, not UNC: %s", (filePath) => {
50+
expect(matches(filePath, ["//server/share/docs/**"], "C:/repo")).toBe(false)
51+
expect(matches(filePath, ["C:/server/share/docs/**"], "C:/repo")).toBe(true)
52+
expect(matches(filePath, ["///server/share/docs/**"], "C:/repo")).toBe(true)
53+
})
54+
55+
it.each(["//server//share", "//server///share", "\\\\server\\\\share", "\\\\server\\\\\\share"])(
56+
"normalizes repeated separators between a UNC server and share: %s",
57+
(root) => {
58+
expect(matches("//server/share/docs/private.md", [`${root}/docs/**`], "C:/repo")).toBe(true)
59+
expect(matches(`${root}/docs/private.md`, ["//server/share/docs/**"], "C:/repo")).toBe(true)
60+
expect(matches("//server/share/docs/private.md", ["docs/**"], root)).toBe(true)
61+
expect(matches("docs/private.md", ["//server/share/docs/**"], root)).toBe(true)
62+
expect(matches("docs/private.md", ["C:/server/share/docs/**"], root)).toBe(false)
63+
expect(matches("C:/server/share/docs/private.md", [`${root}/docs/**`], "C:/repo")).toBe(false)
64+
},
65+
)
66+
67+
it.each(["//server", "\\\\server"])("treats an incomplete UNC name as drive-rooted: %s", (filePath) => {
68+
expect(matches(filePath, ["C:/server"], "C:/repo")).toBe(true)
69+
expect(matches(filePath, ["D:/server"], "C:/repo")).toBe(false)
70+
expect(matches("C:/server", [filePath], "C:/repo")).toBe(true)
71+
expect(matches("//server/share/docs/private.md", [filePath], "C:/repo")).toBe(false)
72+
})
73+
74+
it.each([
75+
{
76+
cwd: "//server/share/../project",
77+
canonicalCwd: "//server/share/project",
78+
otherVolume: "//server/project",
79+
},
80+
{
81+
cwd: "\\\\server\\share\\..\\project",
82+
canonicalCwd: "//server/share/project",
83+
otherVolume: "//server/project",
84+
},
85+
{ cwd: "C:/../D/project", canonicalCwd: "C:/D/project", otherVolume: "D:/project" },
86+
{ cwd: "C:\\..\\D\\project", canonicalCwd: "C:/D/project", otherVolume: "D:/project" },
87+
])(
88+
"clamps parent traversal at the volume when normalizing equivalent workspace roots: $cwd",
89+
({ cwd, canonicalCwd, otherVolume }) => {
90+
const input = {
91+
cwd,
92+
canonicalCwd,
93+
filePath: `${canonicalCwd}/docs/private.md`,
94+
}
95+
expect(matcher().isFileMatchedByPatterns({ ...input, patterns: [`${otherVolume}/docs/**`] })).toBe(false)
96+
expect(matcher().isFileMatchedByPatterns({ ...input, patterns: ["docs/**"] })).toBe(true)
97+
expect(matcher().isFileMatchedByPatterns({ ...input, patterns: [`${canonicalCwd}/docs/**`] })).toBe(true)
98+
expect(
99+
matcher().isFileMatchedByPatterns({
100+
...input,
101+
patterns: ["docs/**", `!${canonicalCwd}/docs/private.md`],
102+
}),
103+
).toBe(false)
104+
expect(
105+
matcher().isFileMatchedByPatterns({
106+
...input,
107+
patterns: [`!${canonicalCwd}/docs/private.md`, "docs/**"],
108+
}),
109+
).toBe(true)
110+
},
111+
)
112+
33113
it.each([undefined, "/ws", "\\ws", "//server/share/ws", "\\\\server\\share\\ws"])(
34114
"preserves cross-volume wildcard exclusions and their order with workspace %s",
35115
(cwd) => {

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ describe("Windows rooted approval rules", () => {
4343
afterEach(() => vi.restoreAllMocks())
4444

4545
describe.each(approvalRoutes)("$name", ({ tool, setting }) => {
46+
it.each(["///server/share/docs/**", "\\\\\\server\\share\\docs\\**"])(
47+
"keeps the rooted pattern %s separate from an explicit UNC grant",
48+
async (pattern) => {
49+
const cwd = "C:/repo"
50+
const uncFile = "//server/share/docs/private.md"
51+
expect(await approval(tool, uncFile, cwd, { [setting]: [pattern] })).toEqual({ decision: "ask" })
52+
expect(await approval(tool, "C:/server/share/docs/private.md", cwd, { [setting]: [pattern] })).toEqual({
53+
decision: "approve",
54+
})
55+
expect(await approval(tool, uncFile, cwd, { [setting]: ["//server/share/docs/**"] })).toEqual({
56+
decision: "approve",
57+
})
58+
expect(
59+
await approval(tool, uncFile, cwd, { [setting]: ["//server/share/docs/**", `!${pattern}`] }),
60+
).toEqual({ decision: "approve" })
61+
},
62+
)
63+
4664
it.each([undefined, "/ws", "//server/share/ws"])(
4765
"applies ordered global exclusions when cwd is %s",
4866
async (cwd) => {
@@ -82,6 +100,26 @@ describe("Windows rooted approval rules", () => {
82100
)
83101
})
84102

103+
it.each(["allowedReadFiles", "allowedWriteFiles"] as const)(
104+
"keeps UNC parent traversal within the share when %s grants reads",
105+
async (setting) => {
106+
const cwd = "//server/share/../project"
107+
const file = "//server/share/project/docs/private.md"
108+
vi.mocked(fs.realpath).mockResolvedValue("//server/share/project")
109+
expect(await approval("readFile", file, cwd, { [setting]: ["//server/project/docs/**"] })).toEqual({
110+
decision: "ask",
111+
})
112+
expect(await approval("readFile", file, cwd, { [setting]: ["//server/share/project/docs/**"] })).toEqual({
113+
decision: "approve",
114+
})
115+
expect(
116+
await approval("readFile", file, cwd, {
117+
[setting]: ["//server/share/project/docs/**", "!//server/share/project/docs/private.md"],
118+
}),
119+
).toEqual({ decision: "ask" })
120+
},
121+
)
122+
85123
it("applies rooted UNC rules to reads through the verified drive alias", async () => {
86124
const cwd = "//server/sh[a]re/project"
87125
vi.mocked(fs.realpath).mockResolvedValue("D:/storage/project")

‎src/core/auto-approval/filePatterns.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ignore from "ignore"
55

66
import { WorkspaceRootCanonicalizer } from "./WorkspaceRootCanonicalizer"
77
import { WorkspaceRootStrategy } from "./strategies/WorkspaceRootStrategy"
8+
import { WINDOWS_UNC_PREFIX } from "./strategies/WindowsWorkspaceRootStrategy"
89

910
/**
1011
* Matching of file paths against user-configured file patterns.
@@ -171,7 +172,7 @@ const DRIVE_PREFIX = /^([a-zA-Z]):(?=\/)/
171172
*/
172173
function volumeSegmentOf(absolutePath: string, isWindows: boolean): string {
173174
if (isWindows) {
174-
const unc = /^\/\/([^/]+)\/([^/]+)/.exec(absolutePath)
175+
const unc = WINDOWS_UNC_PREFIX.exec(absolutePath)
175176
if (unc) return `/UNC/${unc[1]}/${unc[2]}`
176177
}
177178
const drive = isWindows ? DRIVE_PREFIX.exec(absolutePath) : null

‎src/core/auto-approval/strategies/WindowsWorkspaceRootStrategy.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@ import path from "path"
22

33
import type { WorkspaceRootStrategy } from "./WorkspaceRootStrategy"
44

5+
/** Windows UNC roots have exactly two leading separators and nonempty server/share names. */
6+
export const WINDOWS_UNC_PREFIX = /^\/\/([^/]+)\/+([^/]+)/
7+
58
export class WindowsWorkspaceRootStrategy implements WorkspaceRootStrategy {
69
/** Drop drive colons rejected by the Windows matcher while keeping UNC paths in their own namespace. */
710
toMatcherAbsolutePath(absolutePath: string, workspaceVolume: string): string {
811
// Keep UNC paths independent of the current drive and distinct from drive letters.
9-
if (absolutePath.startsWith("//")) return `/UNC/${absolutePath.replace(/^\/+/, "")}`
12+
const unc = WINDOWS_UNC_PREFIX.exec(absolutePath)
13+
if (unc) return `/UNC/${unc[1]}/${unc[2]}${absolutePath.slice(unc[0].length)}`
1014

1115
const drive = /^([a-zA-Z]):(?=\/)/.exec(absolutePath)
1216
// Rooted paths belong to the workspace drive/share. Without a known volume,
1317
// keep them in a separate namespace so /UNC/... cannot impersonate a share.
1418
return drive
1519
? `/${drive[1]}${absolutePath.slice(drive[0].length)}`
16-
: `${workspaceVolume || "/ROOT"}${absolutePath}`
20+
: `${workspaceVolume || "/ROOT"}${absolutePath.replace(/^\/+/, "/")}`
1721
}
1822

1923
normalizeAbsoluteRoot(root: string | undefined): string | undefined {
2024
const normalized = (root ?? "").replace(/\\/g, "/")
2125
const drive = /^([a-zA-Z]):(?=\/)/.exec(normalized)
2226
if (!normalized.startsWith("/") && !drive) return undefined
2327

24-
// Match the allowlist representation: a Windows drive is a POSIX path segment.
25-
const absolutePath = this.toMatcherAbsolutePath(normalized, "")
26-
return path.posix.resolve(absolutePath).replace(/\/$/, "")
28+
// Resolve parents while drives and UNC shares are still filesystem roots.
29+
// POSIX resolution after conversion could consume the drive or share name.
30+
const absolutePath = path.win32.normalize(normalized).replace(/\\/g, "/")
31+
return this.toMatcherAbsolutePath(absolutePath, "").replace(/\/$/, "")
2732
}
2833

2934
comparisonKey(value: string): string {

0 commit comments

Comments
 (0)