Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

fix(torrent): reject superscript Windows device names (#176)

Rudy Mizrahi Celeklida179d667de7
diff --git a/e2e/torrentPaths.spec.ts b/e2e/torrentPaths.spec.ts
index cb20e67..1248f06 100644
--- a/e2e/torrentPaths.spec.ts
+++ b/e2e/torrentPaths.spec.ts
@@ -75,10 +75,18 @@ test.describe('torrent paths', () => {
refused(['Name/x', 'Name/x/y'])
refused(['Name/::'])
refused(['Name/CON.txt'], 'win32')
+ // Windows reserves COM¹/COM²/COM³ and LPT¹/LPT²/LPT³ the same way as COM1 (Win32 file names).
+ refused(['Name/COM\u00b9.txt'], 'win32')
+ refused(['Name/LPT\u00b2.txt'], 'win32')
+ refused(['COM\u00b3/file.txt'], 'win32')
refused(['Name/a:b/file'], 'win32')
refused([])
// The same names are fine where nothing reserves them, as are names that only start with dots.
expect(safeTorrentPaths([{ path: 'Name/CON.txt', length: 1 }], 'darwin')).toHaveLength(1)
+ expect(safeTorrentPaths([{ path: 'Name/COM\u00b9.txt', length: 1 }], 'darwin')).toHaveLength(1)
+ // COM10 and an ordinary name that only starts with COM are not device names.
+ expect(safeTorrentPaths([{ path: 'Name/COM10.txt', length: 1 }], 'win32')).toHaveLength(1)
+ expect(safeTorrentPaths([{ path: 'Name/COMPANY.txt', length: 1 }], 'win32')).toHaveLength(1)
expect(safeTorrentPaths([{ path: '..notes.txt', length: 1 }], 'darwin')).toEqual([
{ path: '..notes.txt', length: 1 }
])
diff --git a/src/main/download/torrent/paths.ts b/src/main/download/torrent/paths.ts
index 2f47fbc..c30d2a4 100644
--- a/src/main/download/torrent/paths.ts
+++ b/src/main/download/torrent/paths.ts
@@ -5,8 +5,9 @@ import { posix, win32 } from 'node:path'
// eslint-disable-next-line no-control-regex -- control characters are exactly what it strips
export const STRIPPED_FROM_NAMES = /[<>:"/\\|?*\u0000-\u001F]/g
// Unlike filename-reserved-regex's own check, this also catches the name with an extension:
-// Windows reserves CON.txt as well as CON.
-const WINDOWS_RESERVED = /^(con|prn|aux|nul|com\d|lpt\d)(\..*)?$/i
+// Windows reserves CON.txt as well as CON, and the superscript device names COM¹/²/³ and LPT¹/²/³
+// the same way as COM1 (see the Win32 "Naming a File" reserved names).
+const WINDOWS_RESERVED = /^(con|prn|aux|nul|com[0-9¹²³]|lpt[0-9¹²³])(\..*)?$/i
// eslint-disable-next-line no-control-regex -- Windows can't save control characters either
const WINDOWS_INVALID = /[<>:"|?*\u0000-\u001F]|[. ]$/
const MAX_NAME_BYTES = 255