Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assigning a hostname with port 80 to URL.host will not override the existing port #20465

Closed
giltayar opened this issue May 2, 2018 · 17 comments
Labels
confirmed-bug Issues with confirmed bugs. whatwg-url Issues and PRs related to the WHATWG URL implementation.

Comments

@giltayar
Copy link
Contributor

giltayar commented May 2, 2018

  • Version: 8.8.1, but also occurs in 10.0.0
  • Platform: MacOS
  • Subsystem: url

Overriding the host property with a host that has port 80, will not override the port.
Example:

const {URL} = require('url')

const u = new URL('http://localhost:3000/foo')
u.host = 'some-domain:80'
console.log(u.href) // expected http://some-domain:80/foo, actual: http://some-domain:3000/foo 

// note that any other port besides 80 works correctly:

const u2 = new URL('http://localhost:3000/foo')
u2.host = 'some-domain:801'
console.log(u2.href) // http://some-domain:801/foo

The same bug applies to https with port 443.

@vsemozhetbyt vsemozhetbyt added the whatwg-url Issues and PRs related to the WHATWG URL implementation. label May 2, 2018
@targos targos added the confirmed-bug Issues with confirmed bugs. label May 2, 2018
@targos
Copy link
Member

targos commented May 2, 2018

/cc @nodejs/url

@targos
Copy link
Member

targos commented May 2, 2018

I can see where the problem is in node_url.cc but I don't understand enough how the state machine is implemented to fix it.
The port is ignored because scheme is http and port is 80, but it doesn't check what the current port is.

Refs:

url->port = NormalizePort(url->scheme, static_cast<int>(port));

node/src/node_url.cc

Lines 682 to 687 in 9c8b479

inline int NormalizePort(const std::string& scheme, int p) {
#define XX(name, port) if (scheme == name && p == port) return -1;
SPECIALS(XX);
#undef XX
return p;
}

@AyushG3112
Copy link
Contributor

Should the port be overridden unconditionally?

@targos
Copy link
Member

targos commented May 2, 2018

Not exactly. In this case, port should be deleted (because the default port for http is 80)

@AyushG3112
Copy link
Contributor

I've been going through the code and will give fixing this a shot in a couple hours if nobody else picks it up first. I believe a test for this would be needed too, right?

@domenic
Copy link
Contributor

domenic commented May 2, 2018

This is per spec I believe, so I don't know why this is tagged confirmed-bug. https://url.spec.whatwg.org/#port-state step 2.1.3.

@targos
Copy link
Member

targos commented May 2, 2018

@domenic What happens here is that the port remains at its original value (3000) instead of being set to null.

@domenic
Copy link
Contributor

domenic commented May 2, 2018

Got it! Yeah, adding a test to https://github.com/w3c/web-platform-tests/blob/master/url/setters_tests.json#L608 would be great to catch this kind of thing in all implementations.

@eduardbme
Copy link
Contributor

eduardbme commented May 2, 2018

checked on master

$ ~/Documents/opensource/node/node -v
v11.0.0-pre
$ ~/Documents/opensource/node/node test.js
http://some-domain:3000/foo
http://some-domain:3000/foo

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented May 2, 2018

@eduardbcom There is a typo in OP, the last line should be console.log(u2.href).

@eduardbme
Copy link
Contributor

my bad, overlooked

@AyushG3112
Copy link
Contributor

AyushG3112 commented May 2, 2018

The fix is simple but causing a regression. Trying to figure it out.

@giltayar
Copy link
Contributor Author

giltayar commented May 2, 2018

@vsemozhetbyt - the typo is in my bug description, and I fixed it (the typo, not the bug...).

@AyushG3112
Copy link
Contributor

AyushG3112 added a commit to AyushG3112/node that referenced this issue May 2, 2018
Call `onParsePortComplete` on null port from `onParseHostComplete` to
set url’s port to null, if port is url’s scheme’s default port,
and to port otherwise, as specified in the spec.

Also, trim all trailing `:` from `host` in the setter to fix
regressions caused by the change.

Refs: https://url.spec.whatwg.org/#port-state
Fixes: nodejs#20465
@eduardbme
Copy link
Contributor

eduardbme commented May 2, 2018

I think in that way we spread URL parse logic between two different parts (js and c++), and that's not good. Much better approach (to my mind) is to make all parse stuff within c++ part, where main logic is placed.

So I propose to change

if (url.port > -1)

to

if (url.port > -1 && !IsSpecial(url.scheme.c_str(), url.port))

And add second isSpecial version:

inline bool IsSpecial(const std::string& scheme, const int& p) {
  #define XX(name, port) if (scheme == name && p == port) return true;

  SPECIALS(XX);

  #undef XX

  return false;
}

No need to change current tests, [03:37|% 100|+ 2240|- 0]: Done.
But of course new tests to cover that are required.

@AyushG3112
Copy link
Contributor

AyushG3112 commented May 2, 2018

@eduardbcom The problem with that(I think) will be that the port will be overwritten and not deleted. I tried to do that earlier.

@eduardbme
Copy link
Contributor

@AyushG3112 Please, provide problem example. Thx.

AyushG3112 added a commit to AyushG3112/node that referenced this issue May 15, 2018
Introduce `URL_FLAGS_IS_DEFAULT_SCHEME_PORT` flag which is retured
when the parser detects that the port passed is the default port
for that scheme.

Fixes: nodejs#20465
MylesBorins pushed a commit that referenced this issue May 22, 2018
Introduce `URL_FLAGS_IS_DEFAULT_SCHEME_PORT` flag which is retured
when the parser detects that the port passed is the default port
for that scheme.

PR-URL: #20479
Fixes: #20465
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
6 participants