nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
106.56k stars 29.05k forks source link

`pathToFileURL` function in url fails to handle `\\?\UNC\` Prefix properly #54261

Closed EarlyRiser42 closed 1 month ago

EarlyRiser42 commented 1 month ago

Version

22.5.1

Platform

Microsoft Windows NT 10.0.22631.0 x64

Subsystem

No response

What steps will reproduce the bug?

const { pathToFileURL } = require('url');

console.log(pathToFileURL('\\\\?\\UNC\\server\\share\\folder\\file.txt').href); 
// output: 'file:///UNC/server/share/folder/file.txt'

How often does it reproduce? Is there a required condition?

Anytime

What is the expected behavior? Why is that the expected behavior?

According to the Microsoft documentation on maximum file path limitations, the \?\UNC\ prefix are not used as part of the path itself. so it should be ignored when converting to a file URI. The current implementation does not adhere to this guideline, resulting in incorrect URL conversions. Its output should be file://server/share/folder/file.txt.

What do you see instead?

file:///UNC/server/share/folder/file.txt

Additional information

No response

RedYetiDev commented 1 month ago

Reproduction:

$ node
Welcome to Node.js v22.5.1.
Type ".help" for more information.
> require('url').pathToFileURL('\\\\?\\UNC\\server\\share\\folder\\file.txt', { windows: false }).href;
'file:///<path>/%5C%5C%3F%5CUNC%5Cserver%5Cshare%5Cfolder%5Cfile.txt'
> require('url').pathToFileURL('\\\\?\\UNC\\server\\share\\folder\\file.txt', { windows: true }).href;
'file:///UNC/server/share/folder/file.txt'