mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.27k stars 2.52k forks source link

the connection string is parsed wrongly #2561

Open its-dibo opened 1 year ago

its-dibo commented 1 year ago

the method createConnection accepts a connection URL as a string or an object of type ConnectionConfig if a string is passed, it uses require('url').parse to parse the string and returns a connection object.

this approach assumes that the property pathname that is returned by url.parse has a string value, but in fact, it returns null when providing a port which is an integer

example let's parse this connection URL string mysql://username:password@host:1234

const urlParse = require('url').parse;

let url = "mysql://username:password@host:123",
 parsed = urlParse(url, true);

console.log(parsed)

output:

 {
  protocol: 'mysql:',
  slashes: true,
  auth: 'username:password',
  host: 'host:123',
  port: '123',
  hostname: 'host',
  hash: null,
  search: null,
  query: [Object: null prototype] {},
  pathname: null, // <----------------------------------
  path: null,
  href: 'mysql://username:password@host:123'
}

note that pathname here is null

dougwilson commented 10 months ago

Good catch. We can fix the module to properly handle null, as it should be the same as just no database like otherwise. Sorry for that bug, it is an easy fix.