Skip to content

fix: respect PG_META_DB_PORT when encrypted connection header uses internal 'db' alias#1061

Open
nancysangani wants to merge 1 commit intosupabase:masterfrom
nancysangani:fix/respect-pg-meta-db-port-encrypted-header
Open

fix: respect PG_META_DB_PORT when encrypted connection header uses internal 'db' alias#1061
nancysangani wants to merge 1 commit intosupabase:masterfrom
nancysangani:fix/respect-pg-meta-db-port-encrypted-header

Conversation

@nancysangani
Copy link
Copy Markdown
Contributor

Fixes #1020

In self-hosted Supabase setups, the upstream proxy (Studio/Kong) sends an x-connection-encrypted header that decodes to a connection string with host=db, port=5432 — the default internal Docker service name. This header was used as-is, completely ignoring PG_META_DB_URL / PG_META_DB_PORT, causing either ENOTFOUND db or connections to the wrong port.

When the decrypted connection string uses hostname db, this fix substitutes the host and port from PG_CONNECTION (built from env vars). The multi-tenant case (encrypted header pointing to an arbitrary external DB) is unaffected.

@nancysangani nancysangani requested review from a team, avallete and soedirgo as code owners April 10, 2026 13:11
Copilot AI review requested due to automatic review settings April 10, 2026 13:11
@nancysangani
Copy link
Copy Markdown
Contributor Author

/cc @avallete

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes self-hosted setups where the upstream x-connection-encrypted header decrypts to an internal host=db connection string, causing postgres-meta to ignore the configured DB host/port and attempt to connect to the internal Docker alias/default port.

Changes:

  • Decrypt x-connection-encrypted to a UTF-8 connection string and, when hostname is db, substitute host/port from PG_CONNECTION.
  • Keep non-db (multi-tenant/external) encrypted connection strings unchanged.
  • Add a regression test for encrypted connection strings using the internal db hostname.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/server/routes/index.ts Decrypts and conditionally rewrites encrypted connection strings when the hostname is db.
test/server/ssl.ts Adds a test covering the encrypted db hostname scenario.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +41 to +46
const decryptedUrl = new URL(decrypted)
if (decryptedUrl.hostname === 'db') {
const configuredUrl = new URL(PG_CONNECTION)
decryptedUrl.hostname = configuredUrl.hostname
decryptedUrl.port = configuredUrl.port
}
Comment on lines +130 to +138
test('query with encrypted connection string using internal "db" host respects PG_META_DB_PORT', async () => {
const res = await app.inject({
method: 'POST',
path: '/query',
headers: {
'x-connection-encrypted': CryptoJS.AES.encrypt(
'postgresql://postgres:postgres@db:5432/postgres',
CRYPTO_KEY
).toString(),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Postgres-meta Hardcoded Connection Alias 'db' Causes Port Mismatch with External DB

2 participants