Skip to content

🔒 fix(sink/postgres): harden DSN/host redaction (SEC-01-FUP1/FUP2) - #182

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

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

Conversation

@konih

@konih konih commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

Two operator-approved follow-ups to SEC-01 (PR #174), which redacted the DSN/host from pgx ParseConfig failures via redactedParseError. Both touch internal/sink/postgres/pool.go, so they land in one lane.

FUP1 (P3) — delete the dead backtick-strip fallback

redactedParseError had a fallback that, for any non-*pgconn.ParseConfigError, echoed the pgx reason (including the raw host) after the quoted DSN — a latent fail-open. Verified 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). A future pgx bump could reintroduce a non-PCE leaking path, so the function now fails closed with the static message unconditionally.

FUP2 (P3) — redact post-parse dial errors

pgxpool.NewWithConfig connects lazily (verified: returns a live pool + nil even with pool_min_conns=1), so a dial fault does not surface via newGuardedPool's return — it surfaces on first use: pool.Ping (connection.go) and the ensureTable Exec (backend.go). pgx wraps it in *pgconn.ConnectError, whose text embeds the tenant user, database, and — through 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.

Note on scope: the task located the leak at NewWithConfig; empirically it is lazy, so the fix redacts at the two sites where the ConnectError actually reaches a caller. Same intent, corrected site.

TDD (RED → GREEN)

Extended internal/sink/postgres/pool_test.go; all assertions confirmed RED before the fix:

  • FUP1: synthesizes a non-PCE error whose text echoes a backtick-quoted DSN + raw host; asserts the static message (RED: old code returned the stripped reason secret-db.internal).
  • FUP2: drives a real *pgconn.ConnectError through the netguard dial path (127.0.0.1:1, rejected as non-routable before any egress — hermetic, instant) and asserts no user/database/host tokens leak. Covered at the helper level and end-to-end through TestConnection and NewBackend; plus a pass-through test for non-connect errors.

Gate matrix (all green)

  • go build ./... — pass
  • go vet ./... — pass
  • task lint — 0 issues, arch-lint OK
  • task verify — pass
  • task test / task coverage — all packages ok, internal/ coverage 85.7% (floor 85%)

Follow-up (out of scope)

Runtime-query Exec leaks (bulk_export.go, backend.go upsert path) are a separate, larger surface — noted for a later lane.

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.
Copilot AI review requested due to automatic review settings August 3, 2026 15:57

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

This PR follows up on SEC-01 by hardening PostgreSQL error redaction so DSN/host/user/db details cannot leak via pgx error strings, covering both DSN parse failures and post-parse dial/connect failures.

Changes:

  • Removes the previously “fail-open” fallback path in redactedParseError, making it always fail closed with a static message.
  • Introduces redactedConnectError to redact *pgconn.ConnectError (lazy-connect/dial failures) and wires it into the two surfacing call sites.
  • Extends pool_test.go with unit and end-to-end tests to ensure sensitive tokens do not appear in returned error strings.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
internal/sink/postgres/pool.go Removes leaky parse-error fallback; adds connect-error redaction helper.
internal/sink/postgres/pool_test.go Adds regression tests for fail-closed parse redaction and dial/connect redaction, plus end-to-end coverage.
internal/sink/postgres/connection.go Applies connect-error redaction to pool.Ping failure path.
internal/sink/postgres/backend.go Applies connect-error redaction to ensureTable failure path during backend construction.

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

Comment on lines +34 to +36
// colon). We therefore return a STATIC message and never fold any of its
// free-form text back in — the typed error's ConnString/Unwrap remain available
// programmatically but never hit the returned string.
@sonarqubecloud

sonarqubecloud Bot commented Aug 3, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@konih
konih merged commit f151ddd into main Aug 3, 2026
31 checks passed
@konih
konih deleted the fix/sec-01-fup-postgres-redact branch August 3, 2026 16:10
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