diff --git a/lib/path.js b/lib/path.js index 63b037cddfb986..c4350558b4e2d0 100644 --- a/lib/path.js +++ b/lib/path.js @@ -402,11 +402,22 @@ const win32 = { device = `\\\\${firstPart}`; rootEnd = 4; const colonIndex = StringPrototypeIndexOf(path, ':'); - // Special case: handle \\?\COM1: or similar reserved device paths - const possibleDevice = StringPrototypeSlice(path, 4, colonIndex + 1); - if (isWindowsReservedName(possibleDevice, possibleDevice.length - 1)) { - device = `\\\\?\\${possibleDevice}`; - rootEnd = 4 + possibleDevice.length; + + if (colonIndex !== -1) { + // Handle \\?\COM1: or similar reserved device paths with colon + const possibleDevice = StringPrototypeSlice(path, 4, colonIndex + 1); + if (isWindowsReservedName(possibleDevice, possibleDevice.length - 1)) { + device = `\\\\?\\${possibleDevice}`; + rootEnd = 4 + possibleDevice.length; + } + } else { + // Handle \\.\CON or \\?\CON where the colon is missing + const possibleDevice = StringPrototypeSlice(path, 4); + const upper = StringPrototypeToUpperCase(possibleDevice); + if (ArrayPrototypeIncludes(WINDOWS_RESERVED_NAMES, upper)) { + device = `\\\\.\\${possibleDevice}`; + rootEnd = 4 + possibleDevice.length; + } } } else if (j === len) { // We matched a UNC root only diff --git a/test/parallel/test-path-win32-normalize-device-missing-colon.js b/test/parallel/test-path-win32-normalize-device-missing-colon.js new file mode 100644 index 00000000000000..5f171bbdaeec2f --- /dev/null +++ b/test/parallel/test-path-win32-normalize-device-missing-colon.js @@ -0,0 +1,27 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const path = require('path'); + +// هذا السطر يمنع تشغيل الاختبار على غير ويندوز لأنه خاص بويندوز فقط +if (!common.isWindows) + common.skip('this test is for win32 only'); + +// Test cases for reserved device names missing the trailing colon +// See: https://github.com/nodejs/node/pull/[YOUR_PR_NUMBER] (optional) + +// 1. Check \\.\CON (Missing colon) +assert.strictEqual(path.win32.normalize('\\\\.\\CON'), '\\\\.\\CON'); +assert.strictEqual(path.win32.normalize('\\\\.\\con'), '\\\\.\\con'); // Case insensitive + +// 2. Check \\?\CON (Missing colon) +assert.strictEqual(path.win32.normalize('\\\\?\\CON'), '\\\\?\\CON'); +assert.strictEqual(path.win32.normalize('\\\\?\\con'), '\\\\?\\con'); + +// 3. Check that regular files are NOT affected (Sanity check) +assert.strictEqual(path.win32.normalize('\\\\.\\PhysicalDrive0'), '\\\\.\\PhysicalDrive0'); + +// 4. Check join behavior (to ensure it acts as a root) +// If it's a root, joining '..' should not strip the device. +const joined = path.win32.join('\\\\.\\CON', '..'); +assert.strictEqual(joined, '\\\\.\\CON');