Skip to content

fix: catch invalid url early #932

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 3 commits into from
Apr 13, 2025
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
43 changes: 29 additions & 14 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,39 @@ import { PG_CONNECTION, CRYPTO_KEY } from '../constants.js'
export default async (fastify: FastifyInstance) => {
// Adds a "pg" object to the request if it doesn't exist
fastify.addHook('onRequest', (request, _reply, done) => {
// Node converts headers to lowercase
const encryptedHeader = request.headers['x-connection-encrypted']?.toString()
if (encryptedHeader) {
try {
// Node converts headers to lowercase
const encryptedHeader = request.headers['x-connection-encrypted']?.toString()
if (encryptedHeader) {
try {
request.headers.pg = CryptoJS.AES.decrypt(encryptedHeader, CRYPTO_KEY)
.toString(CryptoJS.enc.Utf8)
.trim()
} catch (e: any) {
request.log.warn({
message: 'failed to parse encrypted connstring',
error: e.toString(),
})
throw new Error('failed to process upstream connection details')
}
} else {
request.headers.pg = PG_CONNECTION
}
Comment on lines +31 to +46
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note

To identify why and how this happen, would it be okay to console.error the x-connection-encrypted ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no, safer not to log any potentially sensitive information

if (!request.headers.pg) {
request.log.error({ message: 'failed to get connection string' })
throw new Error('failed to get upstream connection details')
}
// Ensure the resulting connection string is a valid URL
try {
request.headers.pg = CryptoJS.AES.decrypt(encryptedHeader, CRYPTO_KEY).toString(
CryptoJS.enc.Utf8
)
} catch (e: any) {
request.log.warn({
message: 'failed to parse encrypted connstring',
error: e.toString(),
})
new URL(request.headers.pg)
} catch (error) {
request.log.error({ message: 'pg connection string is invalid url' })
throw new Error('failed to process upstream connection details')
}
} else {
request.headers.pg = PG_CONNECTION
return done()
} catch (err) {
return done(err as Error)
}
done()
})

fastify.register(ColumnPrivilegesRoute, { prefix: '/column-privileges' })
Expand Down
54 changes: 54 additions & 0 deletions test/server/ssl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,57 @@ test('query with ssl with root cert', async () => {

DEFAULT_POOL_CONFIG.ssl = defaultSsl
})

test('query with invalid space empty encrypted connection string', async () => {
const res = await app.inject({
method: 'POST',
path: '/query',
headers: {
'x-connection-encrypted': CryptoJS.AES.encrypt(` `, CRYPTO_KEY).toString(),
},
payload: { query: 'select 1;' },
})
expect(res.statusCode).toBe(500)
expect(res.json()).toMatchInlineSnapshot(`
{
"error": "failed to get upstream connection details",
}
`)
})

test('query with invalid empty encrypted connection string', async () => {
const res = await app.inject({
method: 'POST',
path: '/query',
headers: {
'x-connection-encrypted': CryptoJS.AES.encrypt(``, CRYPTO_KEY).toString(),
},
payload: { query: 'select 1;' },
})
expect(res.statusCode).toBe(500)
expect(res.json()).toMatchInlineSnapshot(`
{
"error": "failed to get upstream connection details",
}
`)
})

test('query with missing host connection string encrypted connection string', async () => {
const res = await app.inject({
method: 'POST',
path: '/query',
headers: {
'x-connection-encrypted': CryptoJS.AES.encrypt(
`postgres://name:password@:5432/postgres?sslmode=prefer`,
CRYPTO_KEY
).toString(),
},
payload: { query: 'select 1;' },
})
expect(res.statusCode).toBe(500)
expect(res.json()).toMatchInlineSnapshot(`
{
"error": "failed to process upstream connection details",
}
`)
})