fix: problems with illegal names under Windows #1250
Massimo Melina committed
Jun 26, 2026 at 15:49 UTC
18a7f7a81897b1cdefaa9c41c563b7b59426d3c3
1 file changed
+8
-1
src/util-files.ts
+8
-1
@@ -165,10 +165,17 @@ export async function createSafeWriteStream(path: string, options?: Parameters<t
165
}
166
167
export function isValidFileName(name: string, acceptUnreadable=false) {
168
- return name && name !== '.' && !(IS_WINDOWS ? /[/:"*?<>|\\]/ : /\//).test(name) && !hasDirTraversal(name)
168
+ return name && name !== '.' && !(IS_WINDOWS ? !isValidWindowsFileName(name) : /\//.test(name)) && !hasDirTraversal(name)
169
&& (acceptUnreadable || !/[\u0000-\u001F\u007F]/.test(name))
170
}
171
172
+export function isValidWindowsFileName(name: string) {
173
+ return !/[/:"*?<>|\\]/.test(name)
174
+ && !/[. ]$/.test(name)
175
+ // windows treats these legacy DOS device names as device paths even when an extension is present
176
+ && !/^(?:con|prn|aux|nul|conin\$|conout\$|com[1-9\u00b9\u00b2\u00b3]|lpt[1-9\u00b9\u00b2\u00b3])(?:\..*)?$/i.test(name)
177
+}
178
+
179
export function exists(path: string) {
180
return access(path).then(() => true, () => false)
181
}