Repository navigation
🔒 fix(sink/postgres): redact DSN/host from pgx ParseConfig error (SEC-01) - #174
Merged
Merged
Conversation
…tials can't leak (SEC-01)
There was a problem hiding this comment.
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 pgxParseConfigErrorDSN/host echo. - Introduces
redactedParseErrorto 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 Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Audit finding SEC-01 (P3) — postgres credential/host leak via wrapped pgx
ParseConfigerror.Fix:
redactedParseErroruseserrors.As(*pgconn.ParseConfigError)and returns a static"parse postgres DSN: invalid connection string"— never folding in the free-form reason or theConnStringfield. 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 fromNewWithConfig.Disjoint (postgres/pool only).