Skip to content

new URL() incorrectly removes localhost when working with UNC paths. #56261

@Jason3S

Description

@Jason3S

Version

18, 20, 22, 23.4.0

Platform

Microsoft Windows NT 10.0.22631.0 x64

Subsystem

No response

What steps will reproduce the bug?

new URL('file://localhost/c$/Users/')

or

url.pathToFileURL('\\\\localhost\\c$\\Users\\')

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

100%

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

For localhost to be preserved as the host name.

Note: the current behavior breaks localhost UNC paths.

What do you see instead?

URL {
  href: 'file:///c$/Users/',
  origin: 'null',
  protocol: 'file:',
  username: '',
  password: '',
  host: '',
  hostname: '',
  port: '',
  pathname: '/c$/Users/',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

or

Node REPL:

> fs.readdirSync(new URL('file://localhost/c$/Users'))
Uncaught TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute
    at Object.readdirSync (node:fs:1569:10) {
  code: 'ERR_INVALID_FILE_URL_PATH'
}

Example with url.pathToFileURL

> url.pathToFileURL('\\\\localhost\\c$\\Users\\')
URL {
  href: 'file:///c$/Users/',
  origin: 'null',
  protocol: 'file:',
  username: '',
  password: '',
  host: '',
  hostname: '',
  port: '',
  pathname: '/c$/Users/',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

Additional information

This behavior creates a broken URL that cannot be used to access files.

I do realize that this is a special case. It came up because I was testing out UNC paths and every windows machine has UNC \\localhost\c$\. So, it an easy one to pick.

Note: Using the machine name in place of localhost does work.

Related to #34736

Metadata

Metadata

Assignees

No one assigned

    Labels

    urlIssues and PRs related to the legacy built-in url module.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions