Skip to content

🔒 fix(sink/postgres): redact ConnectError on export path (SEC-01-FUP3/FUP4) - #184

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

konih merged 1 commit into
mainfrom
fix/sec-01-fup3-export-redact

Conversation

@konih

@konih konih commented Aug 3, 2026 •

Copy link
Copy Markdown
Collaborator

What

Closes the last two SEC-01 redaction gaps in internal/sink/postgres, both found by an independent reviewer after SEC-01 / SEC-01-FUP merged.

SEC-01-FUP3 (P2) — redact *pgconn.ConnectError on the runtime Export path

SEC-01-FUP redacted *pgconn.ConnectError at construct time (pool.Ping, ensureTable), but the runtime Export path still wrapped a lazy pool-reconnect *pgconn.ConnectError unredacted via %w, re-embedding the tenant user / database / host (plus the netguard resolver echo) into controller status/events.

Every pgx-returned error on the Export path is now routed through the existing redactedConnectError helper (reused, not re-implemented):

File Site Sentinel
backend.go b.pool.Begin ErrBeginTxFailed
backend.go tx.Commit ErrCommitTxFailed
backend.go delete-all tx.Exec ErrDeleteAllFailed
backend.go delete-stale tx.Exec ErrDeleteStaleFailed
bulk_export.go row-upsert tx.Exec ErrUpsertFailed
bulk_export.go staging tx.Exec ErrBulkUpsertCreateStagingFailed
bulk_export.go tx.CopyFrom ErrBulkUpsertCopyFailed
bulk_export.go merge tx.Exec ErrBulkUpsertMergeFailed

redactedConnectError matches *pgconn.ConnectError via errors.As and returns a static message; non-ConnectError faults (PgError, context deadlines) pass through unchanged for diagnosis, and the sentinel errors.Is chain is preserved so callers/controller still see a non-nil failure. The error is redacted, never swallowed.

Excluded (already covered): connection.go pool.Ping and backend.go ensureTable are redacted by SEC-01-FUP; decode / buildUpsertRows errors carry no host.

SEC-01-FUP4 (P3) — collapse the vestigial branch

redactedParseError's *pgconn.ParseConfigError type check discriminated nothing post-FUP1 — both the errors.As branch and the fallthrough returned the identical static message. Collapsed to a single unconditional fail-closed return. Behavior is identical.

TDD (RED first)

New backend_export_redact_test.go drives a real *pgconn.ConnectError (netguard dial against 127.0.0.1:1, rejected pre-egress — hermetic, no Docker):

  • Begin site tested end-to-end through b.Export.
  • tx-level sites (row-upsert, staging, copy, merge, delete-stale, delete-all) tested by injecting the real ConnectError at the fake-tx seam — the only realistic way, since once a tx is open those calls run on an established connection.
  • Commit (ErrCommitTxFailed) is wrapped with the same helper but has no dedicated hermetic test: a *pgconn.ConnectError cannot surface on Commit of an already-open tx, and there is no seam to inject one. The wrap is defense-in-depth and passes non-ConnectError through unchanged.

Each test asserts BOTH errors.Is(err, <sentinel>) AND that the error string contains no user= / tenantuser / tenantdb / 127.0.0.1. RED confirmed pre-fix at every site (e.g. begin tx: failed to connect to + "user=tenantuser database=tenantdb", and delete all: failed to connect to ...); GREEN post-fix. A fixture guard fails loudly if pgx ever stops leaking the host so the tests can't pass vacuously.

Gates (all green)

go build ./..., go vet ./..., task lint (0 issues), task verify, task test (postgres 85.7%), task coverage 85.8% ≥ 85% floor, task scrub ok.

Scope: additive, internal/sink/postgres only. Does not touch internal/sink/git or internal/controller.

Copilot AI review requested due to automatic review settings August 3, 2026 16:24

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

Closes remaining SEC-01 redaction gaps in internal/sink/postgres by ensuring runtime Export-path errors cannot leak Postgres DSN host/user/db details via *pgconn.ConnectError, and simplifies parse-error redaction to a single fail-closed path.

Changes:

  • Route Export-path Begin, Commit, and Exec/CopyFrom failures through redactedConnectError while preserving existing sentinel error wrapping.
  • Collapse redactedParseError to always return a static redacted message (removing a vestigial typed-branch).
  • Add focused tests verifying both errors.Is(..., <sentinel>) behavior and absence of DSN tokens in error strings across Export-path sites.

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 Simplifies DSN parse-error redaction and continues to provide ConnectError redaction helper.
internal/sink/postgres/bulk_export.go Redacts *pgconn.ConnectError for row/bulk upsert execution paths by wrapping with redactedConnectError.
internal/sink/postgres/backend.go Redacts *pgconn.ConnectError on runtime Export path (Begin, Commit, delete paths).
internal/sink/postgres/backend_export_redact_test.go Adds regression tests ensuring ConnectError redaction and sentinel preservation on Export-related code paths.

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

Comment on lines +51 to +53
// single unconditional return (SEC-01-FUP4). The typed error remains available
// programmatically to callers via the original err; only the returned string
// is redacted.
…/FUP4)

Wrap every pgx-returned error on the runtime Export path (pool.Begin,
tx.Commit, both delete Exec calls, row-upsert Exec, bulk staging Exec,
CopyFrom, and merge Exec) through the existing redactedConnectError
helper so a lazy pool-reconnect *pgconn.ConnectError can no longer
re-embed the tenant user, database, or target host into controller
status/events. Non-ConnectError faults (PgError, context deadlines)
pass through unchanged for diagnosis, and the sentinel errors.Is chain
is preserved so callers still see a non-nil failure.

Also collapse the vestigial *pgconn.ParseConfigError type check in
redactedParseError: post-FUP1 both branches returned the identical
static message, so it discriminated nothing (SEC-01-FUP4).
@konih
konih force-pushed the fix/sec-01-fup3-export-redact branch from c8cb851 to c9654e9 Compare August 3, 2026 16:28
Copilot AI review requested due to automatic review settings August 3, 2026 16:28

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Suppressed comments (1)

internal/sink/postgres/pool.go:54

  • The comment on redactedParseError says the typed error remains available programmatically via the original err, but the function ignores its input and callers (e.g. newGuardedPool) return only the redacted error, so the original parse error is not available to downstream callers. This is misleading; either adjust the comment to reflect that the original error is intentionally dropped, or change the implementation to preserve it safely.
func redactedParseError(_ error) error {
	// Fail closed for EVERY parse fault: never fold any free-form pgx text (which
	// may echo the DSN or host) back into the returned string. Post-FUP1 the
	// *pgconn.ParseConfigError type check discriminated nothing — both it and the
	// fallthrough returned this identical static message — so it is collapsed to a
	// single unconditional return (SEC-01-FUP4). The typed error remains available
	// programmatically to callers via the original err; only the returned string
	// is redacted.
	return errors.New("parse postgres DSN: invalid connection 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

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/sink/postgres/backend.go 75.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@konih
konih merged commit dce8b98 into main Aug 3, 2026
30 of 31 checks passed
@konih
konih deleted the fix/sec-01-fup3-export-redact branch August 3, 2026 16:40
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