Files
webhooks/pkg/worker/secret_logging_test.go
Maxence MaireauxandGitHub 6504cd9692 fix: prevent webhook message loss, remove panic on unknown filter, stop logging secrets (#142)
* fix: resolve P0/P1 reliability and security issues

- P0: Process messages synchronously to prevent data loss on crash
  The Watermill handler now blocks until webhook delivery completes.
  Messages are only acknowledged after processing, not before.
  Errors on invalid payloads are returned (nack) instead of swallowed.
  Per-config failures use continue instead of return, so one bad
  endpoint no longer blocks delivery to other matching configs.

- P1: Replace panic with error on unknown filter key
  FindManyConfigs now returns an error instead of panicking when
  called with an unrecognized filter key. A single bad API request
  no longer crashes the process.

- P1: Remove environment and secret logging
  Removed os.Environ() dumps from server and worker startup.
  Replaced config %+v logging with safe ID/endpoint-only logging.
  Removed event-payload from OpenTelemetry span attributes.

- Docs: Add architecture, message-processing, and security docs.
  Fix CTE typo in retry-mechanism.md.

No breaking changes to public API, Store interface, or CLI flags.

* chore: add CODEOWNERS assigning @formancehq/backend as default reviewers

* fix: remove dead code flagged by ineffassign linter

* fix: address CodeRabbit review feedback

- Return error (nack) when InsertOneAttempt fails for non-success attempts,
  preventing silent loss of retry records
- Rewrite TestProcessMessages_SynchronousProcessing to be deterministic:
  HTTP handler blocks on a channel, test proves handler is blocked until
  the HTTP response completes
- Add TestProcessMessages_NackOnInsertFailureForRetry for the new nack path
- Expand secret logging test patterns to cover %v and more variable names
- Rename TestConfigStringerDoesNotLeakSecret to TestDocument_PercentPlusVLeaksSecrets
- Fix retry-mechanism.md claim query priority documentation: DISTINCT ON
  does per-webhook ordering, not global priority

* fix: propagate errors to span, avoid err shadowing in defer

- Rename loop-scoped err to attemptErr/insertErr so the defer
  closure captures the correct error for span.RecordError()
- Add explicit span.RecordError() on MakeAttempt and InsertOneAttempt
  failures that previously went unrecorded in traces
- Assign err before return on nack path so the defer sees it
- Update Go version in architecture doc to 1.25+ (matches go.mod)

Addresses review comments from paul-nicolas on PR #142.

* fix: handle transport errors in MakeAttempt as retryable attempts

- MakeAttempt now returns a retryable Attempt (instead of a bare error)
  when httpClient.Do fails due to transport/timeout, preventing event
  loss since the attempt record is persisted for the retry loop
- Replace base64-looking test secret with plain dummy to avoid
  triggering secret scanners in CI

Addresses CodeRabbit review comments on PR #142.
2026-04-08 15:16:21 +02:00

107 lines
2.9 KiB
Go

package worker
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestNoEnvironmentDumping verifies that the worker and server module code
// does NOT contain os.Environ() calls that would dump all environment variables
// (including secrets) to the logs.
func TestNoEnvironmentDumping(t *testing.T) {
filesToCheck := []string{
"module.go",
"../server/module.go",
}
for _, relPath := range filesToCheck {
absPath, err := filepath.Abs(relPath)
require.NoError(t, err)
content, err := os.ReadFile(absPath)
require.NoError(t, err, "reading %s", relPath)
assert.NotContains(t, string(content), "os.Environ()",
"file %s should not call os.Environ() — this leaks secrets to logs", relPath)
}
}
// TestNoConfigSecretInLogStatements verifies that log statements in module.go
// do not use %+v or %v formatting which would expose secrets in Config objects.
func TestNoConfigSecretInLogStatements(t *testing.T) {
content, err := os.ReadFile("module.go")
require.NoError(t, err)
src := string(content)
// Check common patterns that would leak secrets via struct formatting
dangerousPatterns := []string{
`"%+v", cfg`,
`"%+v", c`,
`"%+v", config`,
`"%+v", conf`,
`"%v", cfg`,
`"%v", c `,
`"%v", config`,
`"%v", conf`,
`found one config: %+v`,
`found one config: %v`,
}
for _, pattern := range dangerousPatterns {
assert.NotContains(t, src, pattern,
"module.go should not log config structs with %%+v or %%v — this exposes secrets. Found: %s", pattern)
}
}
// TestNoEventPayloadInSpanAttributes verifies that the event payload is no longer
// stored as a span attribute, which could leak sensitive data to the tracing backend.
func TestNoEventPayloadInSpanAttributes(t *testing.T) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "module.go", nil, parser.AllErrors)
require.NoError(t, err)
found := false
ast.Inspect(f, func(n ast.Node) bool {
if lit, ok := n.(*ast.BasicLit); ok && lit.Kind == token.STRING {
if strings.Contains(lit.Value, "event-payload") {
found = true
}
}
return true
})
assert.False(t, found,
"module.go should not store event-payload as a span attribute — it can contain sensitive data")
}
// TestDocument_PercentPlusVLeaksSecrets documents that fmt.Sprintf with %+v
// on any struct containing a Secret field exposes the secret in plaintext.
// This is why config objects must never be logged with %+v or %v.
func TestDocument_PercentPlusVLeaksSecrets(t *testing.T) {
type configLike struct {
Endpoint string
Secret string
ID string
}
cfg := configLike{
Endpoint: "https://example.com",
Secret: "not-a-real-secret",
ID: "cfg-123",
}
output := fmt.Sprintf("%+v", cfg)
assert.Contains(t, output, cfg.Secret,
"this confirms %%+v leaks secrets — never log config structs this way")
}