Repository navigation
Base URL hostnames are overridden by certain paths when constructing a URL #38963
Description
Activity
It's the same behavior in the browser (Chrome 90) — it's probably actually correct functionality per the spec 🤔
I believe this is working as intended. According to the spec, the
URLconstructor take two parameters:urland optionalbase. Ifbaseis notundefinedandurlhas no base (a base is likehttps://abcor\\host), thenbasegets applied tourlto produce the final URL. To demonstrate:// `base` (second param) is used here because `url` (first param) has no base (it's just a relative path) new URL('/hello/world', 'http://def') // -> { href: 'http://def/hello/world' ... } // `base` is ignored here because `url` already has a base new URL('http://abc/hello/world', 'http://def') // -> { href: 'http://abc/hello/world' }
You might be getting confused because
\\-—which is a normal Windows UNC path like\\hostname—is also interpreted as a base, thus yourbaseparameter (http://asd) is getting ignored.cc @aduh95
Reacted by Jonny GreenAh that makes sense about the base interpretation, yeah - thanks for the clarification! :) If this is correct functionality per the spec for v16, would that mean it's incorrect for Node.js v14 in that case, since this does not get interpreted as a base?
Not sure what you mean about v14 — from v10.0.0 (when
URLwas introduced) to v16.3.0 (latest) the behavior is the same (I checked the latest of all major versions, 10, 11, 12, 13, 14, 15, 16 🙂).Sorry, I should've provided an example! On v14, I ran the same command but got a different result, i.e. the path
'\\\\-'was not interpreted as a base andhttp://asdwas not ignored?$ node Welcome to Node.js v14.15.4. Type ".help" for more information. > new URL('\\\\-', 'http://asd') URL { href: 'http://asd///-', origin: 'http://asd', protocol: 'http:', username: '', password: '', host: 'asd', hostname: 'asd', <-- Was expecting this to be '-' like in v16 port: '', pathname: '///-', search: '', searchParams: URLSearchParams {}, hash: '' }Ohhhh — I made a mistake. I didn't realize what you were saying :)
Yeah, something has definitely changed recently 😬 let me investigate.
Reacted by Jonny GreenCool, thanks - happy to provide more testing/debugging output if you need :)
I'm guessing that the previous functionality (that which you just demontrated) was a bug, and fixed with v15.6.0 by @RaisinTen in #36613.
Reacted by James M Snell, Jonny Green and Darshan SenThanks for the update, looking at that PR it looks like it's included in the v14.17.0 proposal? I've just run on this version and it works okay!
$ node Welcome to Node.js v14.17.0. Type ".help" for more information. > new URL('\\\\-', 'http://asd') URL { href: 'http://-/', origin: 'http://-', protocol: 'http:', username: '', password: '', host: '-', hostname: '-', port: '', pathname: '/', search: '', searchParams: URLSearchParams {}, hash: '' }I guess we can close this now?
Ah yes, #36613 was backported to v14 with v14.17.0.
I guess we can close this now?
Yes, feel free to. Thank you for the issue! I'm glad I could help out ;)
Will do and thanks for the help and clarifications - I learned something new today about Windows UNC paths! :)
Reacted by bl-ueYeah.
Now let me tell you something: you might be inclined to think that in
https://google.com,https://is the protocol. Actually, it's not.https:is. The actual URL is//google.com, which is simply a UNIX UNC path, because, you know, UNIX uses/for slashes and Windows uses\\for slashes, so//google.com==\\google.com:)Reacted by Jonny GreenI did not know that either, that's really interesting and things make a lot more sense now! 🚀 - thank you very much!
Reacted by bl-ueReacted by bl-ueFor future reference: this is a duplicate of #36559.
Reacted by Jonny Green
v16.2.0Darwin bisterix.local 20.5.0 Darwin Kernel Version 20.5.0: Sat May 8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64 x86_64urlWhat steps will reproduce the bug?
Running
new URL('\\\\-', 'http://asd')in Node.js v16.2.0 shell will override the hostname of the arbitrarily chosen base URL.How often does it reproduce? Is there a required condition?
Every time.
What is the expected behavior?
The URL is correctly constructed and the hostname is preserved:
What do you see instead?
The hostname of the base URL is overridden by the path:
Additional information
This does not occur in Node.js 14.15.4 (I haven't tested in other versions). Happy to help resolve this, although I would need some pointers for where to start :)