Skip to content

Commit c47b4e5

Browse files
committed
fix(path): reject Windows reserved basenames in sanitizeUntrustedFileName
Reuse the device-path reserved-name set so untrusted names like CON, NUL.txt, and COM1 fall back instead of staging as Windows devices. Signed-off-by: Sebastien Tardif <[email protected]>
1 parent 4e85b67 commit c47b4e5

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Security and Correctness
6+
7+
- **Security — `sanitizeUntrustedFileName()`:** Reject Windows reserved device basenames (`CON`, `PRN`, `AUX`, `NUL`, `COM1`–`COM9`, `LPT1`–`LPT9`, and variants with extensions or trailing spaces/dots) and return the caller fallback, matching the reserved-name rules already used by device-path read guards. Prevents staging/output helpers from accepting untrusted names that open devices on Windows.
8+
39
## 0.5.0 - 2026-07-26
410

511
### Highlights

‎src/device-path.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ function normalizeWindowsDeviceBaseName(filePath: string): string {
104104
return (withoutTrailingIgnoredChars.split(".")[0] ?? withoutTrailingIgnoredChars).toUpperCase();
105105
}
106106

107+
/** True when the basename is a Windows reserved device name (CON, NUL, COM1, …). */
108+
export function isWindowsReservedDeviceBaseName(fileName: string): boolean {
109+
return WINDOWS_RESERVED_DEVICE_NAMES.has(normalizeWindowsDeviceBaseName(fileName));
110+
}
111+
107112
function matchWindowsDeviceReadPath(filePath: string): UnsafeDeviceReadPathMatch | undefined {
108113
const normalized = filePath.replace(/\//g, "\\");
109114
if (/^\\\\\.\\/.test(normalized) || /^\\\\\?\\GLOBALROOT\\Device\\/i.test(normalized)) {

‎src/filename.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "node:path";
2+
import { isWindowsReservedDeviceBaseName } from "./device-path.js";
23

34
const WINDOWS_INVALID_FILE_NAME_CHARACTERS = new Set('<>:"/\\|?*');
45

@@ -25,6 +26,12 @@ export function sanitizeUntrustedFileName(fileName: string, fallbackName: string
2526
if (!base || base === "." || base === "..") {
2627
return fallbackName;
2728
}
29+
// Windows treats these basenames as devices even with an extension
30+
// (NUL.txt opens the NUL device). Refuse them so staging/output helpers
31+
// never create reserved paths from untrusted names.
32+
if (isWindowsReservedDeviceBaseName(base)) {
33+
return fallbackName;
34+
}
2835
if (base.length > 200) {
2936
base = base.slice(0, 200);
3037
}

‎test/filename.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ describe("sanitizeUntrustedFileName", () => {
1818
sanitizeUntrustedFileName('re<po>r:t"|?*\u0085\u009f.pdf', "fallback.bin"),
1919
).toBe("report.pdf");
2020
});
21+
22+
it("rejects Windows reserved device basenames", () => {
23+
expect(sanitizeUntrustedFileName("CON", "fallback.bin")).toBe("fallback.bin");
24+
expect(sanitizeUntrustedFileName("nul.txt", "fallback.bin")).toBe("fallback.bin");
25+
expect(sanitizeUntrustedFileName("COM1", "fallback.bin")).toBe("fallback.bin");
26+
expect(sanitizeUntrustedFileName("LPT9.doc", "fallback.bin")).toBe("fallback.bin");
27+
expect(sanitizeUntrustedFileName("aux ", "fallback.bin")).toBe("fallback.bin");
28+
expect(sanitizeUntrustedFileName("PRN.backup", "fallback.bin")).toBe("fallback.bin");
29+
// Not reserved: longer names that only contain reserved tokens as a prefix/suffix.
30+
expect(sanitizeUntrustedFileName("console.txt", "fallback.bin")).toBe("console.txt");
31+
expect(sanitizeUntrustedFileName("null.pdf", "fallback.bin")).toBe("null.pdf");
32+
});
2133
});

0 commit comments

Comments
 (0)