unshiftio / url-parse

Small footprint URL parser that works seamlessly across Node.js and browser environments.
http://unshift.io
MIT License
1.03k stars 104 forks source link

Parsing url with invalid port doesn't throw an error #195

Closed mchen391 closed 4 years ago

mchen391 commented 4 years ago

Version: 1.4.7 I noticed that

url = new Url("https://test.com:invalidport");
console.log(url)

resulted in

    {
      slashes: true,
      protocol: 'https:',
      hash: '',
      query: '',
      pathname: '',
      auth: '',
      host: 'test.com:invalidport',
      port: '',
      hostname: 'test.com:invalidport',
      password: '',
      username: '',
      origin: 'https://test.com:invalidport',
      href: 'https://test.com:invalidport'
    }

with no error and the invalid port was included in "hostname".

I would expect either an error is thrown or at least the port field being invalidport so that I can verify it only contains numbers.

lpinca commented 4 years ago

url-parse was designed to not throw errors like the legacy Node.js URL parser.

> url.parse('https://test.com:invalidport')
Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'test.com',
  port: null,
  hostname: 'test.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/:invalidport',
  path: '/:invalidport',
  href: 'https://test.com/:invalidport'
}

The result is different in this case but both are wrong as the URL to parse is not a valid URL.

mchen391 commented 4 years ago

Got it. I would switch to node.js url parser instead. Thanks.