ForbesLindesay/atdatabases

When PostgreSQL DSN contains a password with ampersand createConnectionString "Uncaught Error: Expected "port" to be an integer" error

dantman opened this issue · 2 comments

When using createConnectionPool with a DSN string, if the database password contains an unquoted ampersand you will get an Uncaught Error: Expected "port" to be an integer error from parsePort.

This is a notable issue because URL does not consider quoting & to be necessary.

Reproduction:

const {default: createConnectionPool} = await import('@databases/pg')
const dsn = new URL("postgres://localhost:5432/postgres")
dsn.username = 'red';
dsn.password = 'a&b^c'
dsn.href; // => 'postgres://red:a&b%5Ec@localhost:5432/postgres'
createConnectionPool(dsn.href);

Results in the error:

Uncaught Error: Expected "port" to be an integer
    at parseInteger ({...}/node_modules/@databases/pg-connection-string/lib/Configuration.js:254:13)
    at ConfigurationBuilder.set ({...}/node_modules/@databases/pg-connection-string/lib/Configuration.js:87:11)
    at parsePort ({...}/node_modules/@databases/pg-connection-string/lib/parseConnectionURI.js:119:10)
    at Object.parseConnectionURI [as default] ({...}/node_modules/@databases/pg-connection-string/lib/parseConnectionURI.js:26:26)
    at parseConnectionString ({...}/node_modules/@databases/pg-connection-string/lib/index.js:33:40)
    at createConnectionPool ({...}/node_modules/@databases/pg/lib/index.js:33:71)

This may not be as big of a deal. It appears that URL also has cases where it doesn't percent encode the % sign and creates a connection string @databases/pg cannot understand, except in this case psql also does not understand it. So URL may not actually be a good way of making the database connection string.

If you can find a raw string that is understood by psql but not by @databases/pg I'd be happy to look into that? If new URL escapes/encodes characters in a way that psql cannot understand either, I'm probably not going to fix that.