Skip to content

🔒 fix(sink/postgres): redact DSN/host from pgx ParseConfig error (SEC-01) - #174

Merged
konih merged 1 commit into
mainfrom
fix/sec-01-postgres-dsn-redact
Aug 3, 2026
Merged

konih merged 1 commit into
mainfrom
fix/sec-01-postgres-dsn-redact

Conversation

@konih

@konih konih commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Audit finding SEC-01 (P3) — postgres credential/host leak via wrapped pgx ParseConfig error.

Fix: redactedParseError uses errors.As(*pgconn.ParseConfigError) and returns a static "parse postgres DSN: invalid connection string" — never folding in the free-form reason or the ConnString field. Closes a leak where the raw HOST survived on pgx's URL host:port-split path (IPv6 zone-id+port, or a typo'd extra colon).

TDD: table test with the two host-leak DSNs asserting host/user/dbname absent — red on pre-fix code, green after.

Independent review: APPROVE across two rounds. First round caught the host-leak P1 (fixed); fresh re-review confirmed closed — verified in pinned pgx v5.10.0 that every ParseConfig fault is a *pgconn.ParseConfigError, so the static branch always matches. Non-blocking P3 follow-ups logged: delete the now-dead backtick fallback (latent fail-open on a hypothetical future non-PCE error); re-verify the invariant on a pgx bump; consider redacting post-parse dial errors from NewWithConfig.

Disjoint (postgres/pool only).

Copilot AI review requested due to automatic review settings August 3, 2026 12:21
@konih
konih merged commit 5320132 into main Aug 3, 2026
23 checks passed
@konih
konih deleted the fix/sec-01-postgres-dsn-redact branch August 3, 2026 12:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Hardens Postgres sink pool creation by ensuring pgxpool.ParseConfig failures never surface DSN/host/user/dbname content in returned error strings (SEC-01), adding a regression test to prevent future leaks.

Changes:

  • Replaces fmt.Errorf("parse postgres DSN: %w", err) with a redacting error path to avoid pgx ParseConfigError DSN/host echo.
  • Introduces redactedParseError to return a sanitized parse error message.
  • Adds table-driven tests asserting parse errors do not contain sensitive DSN tokens.

Reviewed changes

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

File Description
internal/sink/postgres/pool.go Redacts pgx DSN parse errors instead of wrapping/returning pgx’s raw parse error text.
internal/sink/postgres/pool_test.go Adds regression tests ensuring parse errors never leak DSN components.

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

Comment on lines +44 to +59
func redactedParseError(err error) error {
var parseErr *pgconn.ParseConfigError
if errors.As(err, &parseErr) {
return errors.New("parse postgres DSN: invalid connection string")
}

msg := err.Error()
// A well-formed DSN (URL or keyword form) never contains a backtick, so
// LastIndex reliably lands on the DSN echo's closing delimiter.
if i := strings.LastIndex(msg, "`"); i >= 0 {
if reason := strings.TrimPrefix(msg[i+1:], ": "); reason != "" {
return fmt.Errorf("parse postgres DSN: %s", reason)
}
}
return errors.New("parse postgres DSN: invalid connection string")
}
Comment on lines +50 to +56
msg := err.Error()
// A well-formed DSN (URL or keyword form) never contains a backtick, so
// LastIndex reliably lands on the DSN echo's closing delimiter.
if i := strings.LastIndex(msg, "`"); i >= 0 {
if reason := strings.TrimPrefix(msg[i+1:], ": "); reason != "" {
return fmt.Errorf("parse postgres DSN: %s", reason)
}
Comment on lines +21 to +25
const (
user = "tenantuser"
pass = "hunter2SECRET" // fixture value, not a real credential
db = "tenantdb"
)
@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/sink/postgres/pool.go 50.00% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@sonarqubecloud

sonarqubecloud Bot commented Aug 3, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
36.4% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

konih added a commit that referenced this pull request Aug 3, 2026
SEC-01 (PR #174) redacted the DSN/host from pgx ParseConfig failures. Two
operator-approved follow-ups close the remaining latent gaps.

FUP1 — delete the dead backtick-strip fallback in redactedParseError. It was
a latent fail-open: for any non-*pgconn.ParseConfigError it echoed the pgx
reason (including the raw host) after the quoted DSN. That branch is
unreachable in pinned pgx v5.10.0 — pgxpool.ParseConfig routes every fault
through *pgconn.ParseConfigError (pgx.ParseConfig for connection-string
faults; pgconn.NewParseConfigError for every pool-param fault) — but a future
pgx bump could reintroduce a non-PCE leaking path. redactedParseError now
fails closed with the static message unconditionally.

FUP2 — redact post-parse dial errors. pgxpool.NewWithConfig connects lazily
(verified: it returns a live pool + nil even with pool_min_conns=1), so the
dial fault surfaces via pool.Ping (connection.go) and the ensureTable Exec
(backend.go), NOT via newGuardedPool's return. pgx wraps it in
*pgconn.ConnectError whose text embeds the tenant user, database, and — via
the wrapped perDialConnectError and the netguard resolver echo — the target
host. New redactedConnectError returns a static message for that type while
keeping the error non-nil (callers still see the failure); non-connect errors
pass through unchanged. Wired into both surfacing sites.

Runtime-query Exec leaks (bulk_export.go, backend.go upsert path) are a
separate, larger surface and left as a follow-up.

Tests: extended pool_test.go (RED before fix) — FUP1 synthesizes a non-PCE
backtick-echoing error and asserts the static message; FUP2 drives a real
*pgconn.ConnectError through the netguard path (127.0.0.1:1, blocked before
egress) and asserts no user/database/host tokens leak, via the helper and
end-to-end through TestConnection and NewBackend.
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.

2 participants