0

I have next url:

postgres://someuser:pas#%w#@rd-some-db.cgosdsd8op.us-east-1.rds.amazonaws.com:5432

I am parsing it by next way:

const url = require('url');
const { hostname: host, port, auth, path } = url.parse(bdUrl);
    
const [user, password] = auth.split(':');

It fails with next error:

  const [user, password] = auth.split(':');
                                ^
TypeError: Cannot read property 'split' of null

If I am removing special symbols (% #) from password, everything works fine.

Ted
  • 1,682
  • 3
  • 25
  • 52
  • 2
    `postgres://someuser:pas#%w#@rd-some-db.cgosdsd8op.us-east-1.rds.amazonaws.com:5432` is not a valid URL - you need to `encodeURIComponent("pas#%w#@rd")` – mplungjan Apr 22 '21 at 09:15
  • Does this answer your question? [Encode URL in JavaScript?](https://stackoverflow.com/questions/332872/encode-url-in-javascript) – Pete Apr 22 '21 at 09:18
  • nope, url is hardcoded and it works perfect without special chars. Also I can extract password separately – Ted Apr 22 '21 at 09:19

1 Answers1

0

The problem is likely to be the # in he password. If you look at the definition of a url (see https://en.wikipedia.org/wiki/URL for example)

URI = scheme:[//authority]path[?query][#fragment]
authority = [userinfo@]host[:port]

If you look at how the url parser interprets that url

with some code like:

const url = require('url');
const parsed = url.parse('postgres://someuser:pas#%w#@rd-some-db.cgosdsd8op.us-east-1.rds.amazonaws.com:5432');
console.log(parsed);

you will get some output like:

Url {
  protocol: 'postgres:',
  slashes: true,
  auth: null,
  host: 'someuser',
  port: null,
  hostname: 'someuser',
  hash: '#%w#@rd-some-db.cgosdsd8op.us-east-1.rds.amazonaws.com:5432',
  search: null,
  query: null,
  pathname: '/:pas',
  path: '/:pas',
  href: 'postgres://someuser/:pas#%w#@rd-some-db.cgosdsd8op.us-east-1.rds.amazonaws.com:5432' }

The error you get is because the auth element returns null

if you urlencode the username and password so the url becomes

'postgres://someuser%3Apas%23%25w%23@some-db.cgosdsd8op.us-east-1.rds.amazonaws.com:5432'

you can see the output is correctly parsed

Url {
  protocol: 'postgres:',
  slashes: true,
  auth: 'someuser:pas#%w#',
  host: 'some-db.cgosdsd8op.us-east-1.rds.amazonaws.com:5432',
  port: '5432',
  hostname: 'some-db.cgosdsd8op.us-east-1.rds.amazonaws.com',
  hash: null,
  search: null,
  query: null,
  pathname: null,
  path: null,
  href: 'postgres://someuser:pas%23%25w%23@some-db.cgosdsd8op.us-east-1.rds.amazonaws.com:5432' }
Ian Kenney
  • 6,376
  • 1
  • 25
  • 44
  • But I have whole url on input, I can't encode username and password before – Ted Apr 22 '21 at 10:44
  • you may be able to extract the username and pasword using a regex for example `url.match(/\/\/([^@]+)@/)` – Ian Kenney Apr 22 '21 at 11:10
  • also this may help you https://stackoverflow.com/questions/26145778/how-to-fetch-username-and-password-from-url-using-basic-authentication-in-node-j – Ian Kenney Apr 22 '21 at 11:12