GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — url: fix WHATWG URL host formatting error caused by port string by peakji · Pull Request #20493 · nodejs/node

Conversation

@peakji

@peakji peakji commented May 3, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

The current url.format implementation will return an invalid URL string without the host if there is a port and unicode: true.

This unexpected behavior is caused by domainToUnicode, which expects a hostname instead of a host string according to node_url.cc.

For example: url.domainToUnicode('xn--3kq375bonc.com') will be correctly converted into Chinese '搞事情.com', but url.domainToUnicode('xn--3kq375bonc.com:80') would produce an empty string.

Code to reproduce
'use strict';

const {format, URL} = require('url');

const myURL = new URL('http://examlpe.com:8080/path');

console.log(format(myURL, {unicode: false}));
// OK: 'http://examlpe.com:8080/path'

console.log(format(myURL, {unicode: true}));
// Error: 'http:///path' <-- the host is missing!
References
  1. RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax
  2. RFC 3987 - Internationalized Resource Identifiers (IRIs)
  3. https://url.spec.whatwg.org/#concept-host
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bricss and marvinhagemeister reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
peakji added 2 commits May 3, 2018 15:13
@peakji
url: fix WHATWG host formatting error
6135fca
@peakji
test: format WHATWG URL with port
450272c
@nodejs-github-bot nodejs-github-bot added dont-land-on-v4.x whatwg-url Issues and PRs related to the WHATWG URL implementation. labels May 3, 2018
@peakji

peakji commented May 3, 2018

Copy link
Copy Markdown
Contributor Author

This is a pretty serious bug and I'm wondering if these ambiguous names (host vs hostname) affected more stuffs in the code base?

ping @TimothyGu @jasnell @joyeecheung

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AyushG3112

AyushG3112 commented May 3, 2018

Copy link
Copy Markdown
Contributor

cc @nodejs/url

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

targos
targos approved these changes May 3, 2018
View reviewed changes
cjihrig
cjihrig approved these changes May 3, 2018
View reviewed changes
jasnell
jasnell approved these changes May 3, 2018
View reviewed changes
@targos

targos commented May 3, 2018

Copy link
Copy Markdown
Member

CI: https://ci.nodejs.org/job/node-test-pull-request/14649/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@peakji
url: fix the missing semicolon
75f3f68
apapirovski
apapirovski reviewed May 3, 2018
View reviewed changes

@apapirovski apapirovski left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic needs to be fixed slightly.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread lib/internal/url.js Outdated
ret += options.unicode ?
domainToUnicode(this.host) : this.host;
domainToUnicode(this.hostname) : this.hostname;
if (ctx.port !== '')

@apapirovski apapirovski May 3, 2018

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be ctx.port !== null?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@peakji peakji May 3, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WHATWG URL API uses '' as the empty value, not null.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@peakji peakji May 3, 2018

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> new url.URL("https://nodejs.org/en/")
URL {
  href: 'https://nodejs.org/en/',
  origin: 'https://nodejs.org',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'nodejs.org',
  hostname: 'nodejs.org',
  port: '',
  pathname: '/en/',
  search: '',
  searchParams: URLSearchParams {},
  hash: '' }

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@peakji peakji May 3, 2018

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my bad! This is the internal context...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
apapirovski
apapirovski reviewed May 3, 2018
View reviewed changes
Comment thread lib/internal/url.js Outdated
const ctx = this[context];
var ret = ctx.scheme;
if (ctx.host !== null) {
if (ctx.hostname !== null) {

@apapirovski apapirovski May 3, 2018

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be ctx.host !== null. The hostname change only applies below.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

peakji reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@peakji peakji May 3, 2018

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, URLContext uses null:

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

const myURL = new URL('https://nodejs.org/en/');

const kContext = Object.getOwnPropertySymbols(myURL)[0];

console.log(myURL[kContext]);
URLContext {
  flags: 400,
  scheme: 'https:',
  username: '',
  password: '',
  host: 'nodejs.org',
  port: null,
  path: [ 'en', '' ],
  query: null,
  fragment: null }

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@apapirovski
apapirovski requested a review from TimothyGu May 3, 2018 11:10
@apapirovski

apapirovski commented May 3, 2018

Copy link
Copy Markdown
Contributor

FWIW you might want to contribute your test case back to https://github.com/w3c/web-platform-tests/tree/master/url — clearly this is a case that's not covered by the official tests.

peakji reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@peakji
url: fix WHATWG formatting logic
3db8aac
apapirovski
apapirovski approved these changes May 3, 2018
View reviewed changes

@apapirovski apapirovski left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be good to go now.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@apapirovski

apapirovski commented May 3, 2018

Copy link
Copy Markdown
Contributor

CI: https://ci.nodejs.org/job/node-test-pull-request/14650/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski apapirovski added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label May 3, 2018
trivikr
trivikr approved these changes May 3, 2018
View reviewed changes
@domenic

domenic commented May 3, 2018

Copy link
Copy Markdown
Contributor

FWIW you might want to contribute your test case back to w3c/web-platform-tests:url@master — clearly this is a case that's not covered by the official tests.

Although I agree with this general sentiment, the url.format() API is nonstandard, so it doesn't make sense to test this in the official tests.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@joyeecheung

joyeecheung commented May 3, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Yeah, the unicode option of url.format is used to expose the internal "domain to Unicode" operation of WHATWG URL, in the context of that spec when the URL needs to be serialized the "domain to Unicode" operation is never run on the hostname, it's only used on the domain in URL rendering which is probably not even a thing in the context of Node.js EDIT: right it's probably a thing in say, REPLs or CLIs, or in places like electron or node-canvas.

Also as the name implies domainToUnicode runs on domains, and xn--3kq375bonc.com:80 is not a valid domain. The spec only talks about signaling validation errors and agents are encouraged to report them somewhere. In our parser we signal the error by returning an empty string.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

joyeecheung
joyeecheung approved these changes May 3, 2018
View reviewed changes
@joyeecheung

joyeecheung commented May 3, 2018

Copy link
Copy Markdown
Member

@jasnell @nodejs/url I don't know if it makes sense to throw in the non-internal part of our JS API when the non-standard methods like domainToUnicode or domainToASCII fail? At least if we throw then this bug would be more obvious.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell

jasnell commented May 3, 2018

Copy link
Copy Markdown
Member

Throwing is fine, I think.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

TimothyGu
TimothyGu approved these changes May 3, 2018
View reviewed changes
@trivikr

trivikr commented May 5, 2018

Copy link
Copy Markdown
Member

CI re-run for node-test-commit-arm-fanned: https://ci.nodejs.org/job/node-test-commit-arm-fanned/977/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

BridgeAR
BridgeAR approved these changes May 5, 2018
View reviewed changes
watilde
watilde approved these changes May 5, 2018
View reviewed changes
@addaleax

addaleax commented May 6, 2018

Copy link
Copy Markdown
Member

@joyeecheung I can’t really tell whether your suggestion is something for this PR or not – should we wait with landing this or is it good to go?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@peakji

peakji commented May 7, 2018

Copy link
Copy Markdown
Contributor Author

@trivikr Could you help me to re-run node-test-commit as well?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@joyeecheung

joyeecheung commented May 7, 2018

Copy link
Copy Markdown
Member

@addaleax This PR is good to go, the error handling can be changed in another PR

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@joyeecheung

joyeecheung commented May 7, 2018

Copy link
Copy Markdown
Member

CI: https://ci.nodejs.org/job/node-test-pull-request/14692/

peakji reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski

apapirovski commented May 7, 2018

Copy link
Copy Markdown
Contributor

Landed in 2a96ee2

Congrats on your first commit in Node.js core and becoming a Contributor! 🎉

peakji reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski apapirovski closed this May 7, 2018
apapirovski pushed a commit that referenced this pull request May 7, 2018
@peakji @apapirovski
url: fix WHATWG host formatting error
2a96ee2
The current url.format implementation will return an invalid URL string
without the host if there is a port and unicode: true.

This unexpected behavior is caused by domainToUnicode, which expects
a hostname instead of a host string according to node_url.cc.

Adds both a fix and a test for the issue.

PR-URL: #20493
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
MylesBorins pushed a commit that referenced this pull request May 8, 2018
@peakji @MylesBorins
url: fix WHATWG host formatting error
b06d90d
The current url.format implementation will return an invalid URL string
without the host if there is a port and unicode: true.

This unexpected behavior is caused by domainToUnicode, which expects
a hostname instead of a host string according to node_url.cc.

Adds both a fix and a test for the issue.

PR-URL: #20493
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
@MylesBorins MylesBorins mentioned this pull request May 8, 2018
v10.1.0 proposal #20606
Merged
MylesBorins pushed a commit that referenced this pull request May 8, 2018
@peakji @MylesBorins
url: fix WHATWG host formatting error
2a54965
The current url.format implementation will return an invalid URL string
without the host if there is a port and unicode: true.

This unexpected behavior is caused by domainToUnicode, which expects
a hostname instead of a host string according to node_url.cc.

Adds both a fix and a test for the issue.

PR-URL: #20493
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
MylesBorins pushed a commit that referenced this pull request May 9, 2018
@peakji @MylesBorins
url: fix WHATWG host formatting error
ff619d3
The current url.format implementation will return an invalid URL string
without the host if there is a port and unicode: true.

This unexpected behavior is caused by domainToUnicode, which expects
a hostname instead of a host string according to node_url.cc.

Adds both a fix and a test for the issue.

PR-URL: #20493
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
@apapirovski apapirovski mentioned this pull request May 22, 2018
Inconsistent documentation: hostname vs host #20892
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jasnell jasnell jasnell approved these changes
@watilde watilde watilde approved these changes
@cjihrig cjihrig cjihrig approved these changes
@joyeecheung joyeecheung joyeecheung approved these changes
@BridgeAR BridgeAR BridgeAR approved these changes
@trivikr trivikr trivikr approved these changes
+3 more reviewers
@apapirovski apapirovski apapirovski approved these changes
@TimothyGu TimothyGu TimothyGu approved these changes
@targos targos targos approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. whatwg-url Issues and PRs related to the WHATWG URL implementation.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

14 participants

@peakji @AyushG3112 @targos @apapirovski @domenic @joyeecheung @jasnell @trivikr @addaleax @TimothyGu @watilde @cjihrig @BridgeAR @nodejs-github-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.