Skip to content

Commit a2f4694

Browse files
cliffhallclaude
andcommitted
fix(filesystem): skip UNC server/share names; expect trailing-dot refusal on Windows
The device-name and trailing dot/space checks treated a UNC root's server and share as path components, refusing \\nas\CON\notes.txt and an FQDN server ending in a dot. They are network names, so the UNC root is now skipped (\\.\NUL is still judged). The existing path-validation tests that expect test.., ... and .... to be accepted now expect the refusal when they run on Windows, and keep their POSIX expectation elsewhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 44a3835 commit a2f4694

3 files changed

Lines changed: 52 additions & 10 deletions

File tree

‎src/filesystem/__tests__/path-validation.test.ts‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,16 @@ describe("Path Validation", () => {
479479
expect(
480480
isPathWithinAllowedDirectories("/home/user/project/..test", allowed),
481481
).toBe(true); // Not traversal
482-
expect(
483-
isPathWithinAllowedDirectories("/home/user/project/test..", allowed),
484-
).toBe(true); // Not traversal
482+
// On Windows a trailing dot is refused instead (#5444): Win32 strips it.
483+
if (path.sep === "\\") {
484+
expect(() =>
485+
isPathWithinAllowedDirectories("/home/user/project/test..", allowed),
486+
).toThrow(/ending in a dot or a space/);
487+
} else {
488+
expect(
489+
isPathWithinAllowedDirectories("/home/user/project/test..", allowed),
490+
).toBe(true); // Not traversal
491+
}
485492
expect(
486493
isPathWithinAllowedDirectories("/home/user/project/te..st", allowed),
487494
).toBe(true); // Not traversal
@@ -716,12 +723,18 @@ describe("Path Validation", () => {
716723
expect(
717724
isPathWithinAllowedDirectories("/home/user/project/..", allowed),
718725
).toBe(false); // This normalizes to parent
719-
expect(
720-
isPathWithinAllowedDirectories("/home/user/project/...", allowed),
721-
).toBe(true); // Three dots is a valid filename
722-
expect(
723-
isPathWithinAllowedDirectories("/home/user/project/....", allowed),
724-
).toBe(true); // Four dots is a valid filename
726+
// Three and four dots are valid filenames on POSIX; on Windows they end
727+
// in a dot, which Win32 strips, so they are refused (#5444).
728+
for (const name of ["...", "...."]) {
729+
const p = `/home/user/project/${name}`;
730+
if (path.sep === "\\") {
731+
expect(() => isPathWithinAllowedDirectories(p, allowed)).toThrow(
732+
/ending in a dot or a space/,
733+
);
734+
} else {
735+
expect(isPathWithinAllowedDirectories(p, allowed)).toBe(true);
736+
}
737+
}
725738
});
726739

727740
it("handles UNC paths on Windows", () => {

‎src/filesystem/__tests__/windows-reserved-names.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ describe("namesWindowsDevice on win32", () => {
7777
"C:\\work\\.nul",
7878
"\\\\?\\C:\\work\\file.txt",
7979
"\\\\server\\share\\file.txt",
80+
// A UNC root's server and share are network names, not path components.
81+
"\\\\nas\\CON\\notes.txt",
82+
"\\\\aux\\share\\notes.txt",
83+
"\\\\?\\UNC\\nas\\NUL\\notes.txt",
8084
])("accepts %s", (p) => {
8185
expect(namesWindowsDevice(p)).toBe(false);
8286
});
@@ -92,6 +96,7 @@ describe("hasTrailingDotOrSpace on win32", () => {
9296
"C:\\work\\...",
9397
"\\\\?\\C:\\work\\file.txt.",
9498
"\\\\server\\share\\file.txt ",
99+
"\\\\?\\UNC\\server\\share\\file.txt.",
95100
])("refuses %s", (p) => {
96101
expect(hasTrailingDotOrSpace(p)).toBe(true);
97102
});
@@ -109,6 +114,9 @@ describe("hasTrailingDotOrSpace on win32", () => {
109114
"C:\\work\\..\\file.txt",
110115
"\\\\.\\C:\\work\\file.txt",
111116
"\\\\?\\C:\\work\\file.txt",
117+
// An FQDN server name may end in a dot; it is not a path component.
118+
"\\\\fileserver.example.com.\\share\\notes.txt",
119+
"\\\\?\\UNC\\fileserver.example.com.\\share\\notes.txt",
112120
])("accepts %s", (p) => {
113121
expect(hasTrailingDotOrSpace(p)).toBe(false);
114122
});
@@ -150,6 +158,20 @@ describe("isPathWithinAllowedDirectories on win32 refuses device names and trail
150158
).toThrow(TRAILING);
151159
});
152160

161+
it("accepts a file on a UNC share whose server or share looks reserved", () => {
162+
expect(
163+
isPathWithinAllowedDirectories("\\\\nas\\CON\\notes.txt", [
164+
"\\\\nas\\CON",
165+
]),
166+
).toBe(true);
167+
expect(
168+
isPathWithinAllowedDirectories(
169+
"\\\\fileserver.example.com.\\share\\notes.txt",
170+
["\\\\fileserver.example.com.\\share"],
171+
),
172+
).toBe(true);
173+
});
174+
153175
it("names the requested path in the error", () => {
154176
expect(() =>
155177
isPathWithinAllowedDirectories("C:\\work\\aux.log", ["C:\\work"]),

‎src/filesystem/path-validation.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,19 @@ export function namesAlternateDataStream(normalizedPath: string): boolean {
3636
// created through `\\?\` that cannot be opened the ordinary way afterwards.
3737
// The `.` and `..` components are not "trailing dot" names (resolve has
3838
// already folded them away, and `\\.\` keeps a lone `.` as its prefix), and
39-
// the drive (`C:`) is neither, so only real names are judged.
39+
// the drive (`C:`) is neither, so only real names are judged. A UNC root's
40+
// server and share (`\\server\share`, `\\?\UNC\server\share`) are network
41+
// names, not path components Win32 maps or strips (an FQDN server may end in
42+
// a dot), so they are skipped too; `\\.\NUL` is a device target and is not.
43+
const WINDOWS_UNC_ROOT =
44+
/^(?:\\\\[?.]\\UNC\\|\\\\(?![?.]\\))[^\\/]+[\\/][^\\/]+/i;
45+
4046
const WINDOWS_DEVICE_NAME =
4147
/^(?:CON|PRN|AUX|NUL|COM[1-9¹²³]|LPT[1-9¹²³]) *(?:\..*)?$/i;
4248

4349
function windowsNameComponents(normalizedPath: string): string[] {
4450
return normalizedPath
51+
.replace(WINDOWS_UNC_ROOT, "")
4552
.split(/[\\/]/)
4653
.filter((part) => part !== "" && part !== "." && part !== "..");
4754
}

0 commit comments

Comments
 (0)