atdatabases icon indicating copy to clipboard operation
atdatabases copied to clipboard

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

Open dantman opened this issue 6 months ago • 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)

dantman avatar Dec 19 '23 23:12 dantman