Skip to content

Redact input URL string to prevent potentially console logging passwords #3486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/pg-connection-string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ function parse(str, options = {}) {
}

try {
result = new URL(str, 'postgres://base')
} catch (e) {
// The URL is invalid so try again with a dummy host
result = new URL(str.replace('@/', '@___DUMMY___/'), 'postgres://base')
dummyHost = true
try {
result = new URL(str, 'postgres://base')
} catch (e) {
// The URL is invalid so try again with a dummy host
result = new URL(str.replace('@/', '@___DUMMY___/'), 'postgres://base')
dummyHost = true
}
} catch (err) {
// Remove the input from the error message to avoid leaking sensitive information
err.input && (err.input = '*****REDACTED*****')
}

// We'd like to use Object.fromEntries() here but Node.js 10 does not support it
Expand Down
20 changes: 20 additions & 0 deletions packages/pg-connection-string/test/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,26 @@ describe('parse', function () {
}).to.throw()
})

it('when throwing on invalid url does not print out the password in the error message', function () {
const host = 'localhost'
const port = 5432
const user = 'user'
const password = 'g#4624$@F$#v`'
const database = 'db'

const connectionString = `postgres://${user}:${password}@${host}:${port}/${database}`
expect(function () {
parse(connectionString)
}).to.throw()
try {
parse(connectionString)
} catch (err: unknown) {
expect(JSON.stringify(err)).to.not.include(password, 'Password should not be in the error message')
return
}
throw new Error('Expected an error to be thrown')
})

it('configuration parameter sslmode=verify-ca and sslrootcert with uselibpqcompat query param', function () {
const connectionString = 'pg:///?sslmode=verify-ca&uselibpqcompat=true&sslrootcert=' + __dirname + '/example.ca'
const subject = parse(connectionString)
Expand Down