diff --git a/src/permission/fs_permission.cc b/src/permission/fs_permission.cc index d47c4e17994..8b79850d156 100644 --- a/src/permission/fs_permission.cc +++ b/src/permission/fs_permission.cc @@ -49,15 +49,18 @@ bool is_tree_granted( const std::string_view& param) { std::string resolved_param = node::PathResolve(env, {param}); #ifdef _WIN32 - // is UNC file path - if (resolved_param.rfind("\\\\", 0) == 0) { - // return lookup with normalized param - size_t starting_pos = 4; // "\\?\" - if (resolved_param.rfind("\\\\?\\UNC\\") == 0) { - starting_pos += 4; // "UNC\" - } - auto normalized = param.substr(starting_pos); - return granted_tree->Lookup(normalized, true); + // Remove leading "\\?\" from UNC path + if (resolved_param.substr(0, 4) == "\\\\?\\") { + resolved_param.erase(0, 4); + } + + // Remove leading "UNC\" from UNC path + if (resolved_param.substr(0, 4) == "UNC\\") { + resolved_param.erase(0, 4); + } + // Remove leading "//" from UNC path + if (resolved_param.substr(0, 2) == "//") { + resolved_param.erase(0, 2); } #endif return granted_tree->Lookup(resolved_param, true); diff --git a/test/parallel/test-permission-fs-windows-path.js b/test/parallel/test-permission-fs-windows-path.js index b64cef12b47..552f8e1c216 100644 --- a/test/parallel/test-permission-fs-windows-path.js +++ b/test/parallel/test-permission-fs-windows-path.js @@ -38,3 +38,12 @@ if (!common.isWindows) { assert.strictEqual(stdout.toString(), 'true\n', stderr.toString()); assert.strictEqual(status, 0); } + +{ + const { stdout, status, stderr } = spawnSync(process.execPath, [ + '--experimental-permission', '--allow-fs-write', 'C:\\*', '-e', + "console.log(process.permission.has('fs.write', '\\\\\\\\A\\\\C:\\Users'))", + ]); + assert.strictEqual(stdout.toString(), 'false\n', stderr.toString()); + assert.strictEqual(status, 0); +}