- tip gate: CreateTipPayment saved-card 2FA gate now has scaTokenizedSavedCard skip matching every other charge surface (booking, terminal, gift-card); isSCATokenizeResultShape escape added to tip SAVE gate - webhook: align UPDATE clears VAT fields before re-apply (matches sweep rescue); 503 unknown-event tracking with 24h timeout notification via square_webhook_events table - cash-tip: cashChargeBasePence no longer restores campaign or subtracts loyalty — overcharge and tip shortfall fixed; 2FA dead code remnants removed from gift-card buy flow; TwoFactorCodeInput help text deconfused; refund pre-fill unit mismatch fixed (pounds vs pence); SCA buyer names split from full_name; passwordless delete UI accepts empty password - lockout: successful current-password clears shared failed_attempts/locked_until (victim can recover from login lockout via password change); passwordless delete condition changed to require 2FA only in enforced env - erasure: stale-guest batch erasure persists Square card/customer targets to durable outbox before NULLing them (crash-safe); S3 deletion retry capped at 10 attempts with admin notification; S3_PROFILE_PICS_BUCKET startup check added - env parsing: IsExplicitDevOrMockEnv and Square HTTP client base-URL switch now normalize (ToLower+TrimSpace) for consistency - auth: change-password/delete-account get per-user rate limiters (10/min); consume param dead code suppressed with TODO - frontend: 2FA/SCA dead code removed from gift-card buy flow, TwoFactorCodeInput help text fixed, refund pre-fill unit mismatch fixed, buyer names populated from full_name Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai> Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)
233 lines
9.9 KiB
Go
233 lines
9.9 KiB
Go
//go:build test
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
|
|
"crussell/internal/s3"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// captureLog redirects the process-wide logger into a buffer for the duration
|
|
// of fn and returns what was logged. These startup checks log through the
|
|
// global logger, so the tests that use this helper must stay sequential (no
|
|
// t.Parallel).
|
|
func captureLog(t *testing.T, fn func()) string {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
orig := log.Writer()
|
|
log.SetOutput(&buf)
|
|
defer log.SetOutput(orig)
|
|
fn()
|
|
return buf.String()
|
|
}
|
|
|
|
// TestCheckSnapshotEncKey_NonFatalBranches pins the surviving non-fatal
|
|
// branches of the startup check for SNAPSHOT_ENC_KEY: a valid base64 32-byte
|
|
// key logs nothing, and a dev/mock SQUARE_ENVIRONMENT skips the check
|
|
// entirely (the warn+plaintext-fallback posture survives ONLY there). The
|
|
// fail-closed branches (missing / invalid-base64 / wrong-length key in a
|
|
// non-mock env) log.Fatalf and are covered by the subprocess test below.
|
|
func TestCheckSnapshotEncKey_NonFatalBranches(t *testing.T) {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "production")
|
|
|
|
t.Run("valid_key_logs_nothing", func(t *testing.T) {
|
|
t.Setenv("SNAPSHOT_ENC_KEY", base64.StdEncoding.EncodeToString([]byte("12345678901234567890123456789012"))) // 32 bytes
|
|
got := captureLog(t, checkSnapshotEncKey)
|
|
require.Empty(t, got, "a valid key must not log: %s", got)
|
|
})
|
|
|
|
t.Run("mock_env_skips_check", func(t *testing.T) {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "mock")
|
|
t.Setenv("SNAPSHOT_ENC_KEY", "")
|
|
got := captureLog(t, checkSnapshotEncKey)
|
|
require.Empty(t, got, "a dev/mock env must skip the check: %s", got)
|
|
})
|
|
}
|
|
|
|
// TestCheckSnapshotEncKey_FatalBranch_Exits covers the fail-closed branches:
|
|
// in a non-mock environment an unset / invalid-base64 / wrong-length
|
|
// SNAPSHOT_ENC_KEY must refuse to start (log.Fatalf → os.Exit) instead of
|
|
// logging a CRITICAL warning and storing square_request_snapshot rows (buyer
|
|
// PII: email + ccof card tokens) PLAINTEXT at rest. log.Fatalf cannot run
|
|
// in-process, so the test re-executes the test binary with a marker env var
|
|
// and asserts the subprocess exits non-zero with a FATAL message naming the
|
|
// key.
|
|
func TestCheckSnapshotEncKey_FatalBranch_Exits(t *testing.T) {
|
|
if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "production")
|
|
switch os.Getenv("SNAPSHOT_ENC_KEY_BRANCH") {
|
|
case "MISSING":
|
|
t.Setenv("SNAPSHOT_ENC_KEY", "")
|
|
case "INVALID_BASE64":
|
|
t.Setenv("SNAPSHOT_ENC_KEY", "!!!not-base64!!!")
|
|
case "WRONG_LENGTH":
|
|
// 16 bytes base64 → not 32 bytes → not AES-256. Built at runtime
|
|
// so no secret-shaped literal exists in source.
|
|
t.Setenv("SNAPSHOT_ENC_KEY", base64.StdEncoding.EncodeToString([]byte("1234567890123456")))
|
|
}
|
|
checkSnapshotEncKey()
|
|
return
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
branch string
|
|
}{
|
|
{name: "unset_key_exits", branch: "MISSING"},
|
|
{name: "invalid_base64_exits", branch: "INVALID_BASE64"},
|
|
{name: "wrong_length_exits", branch: "WRONG_LENGTH"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
cmd := exec.Command(os.Args[0], "-test.run=TestCheckSnapshotEncKey_FatalBranch_Exits")
|
|
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1", "SNAPSHOT_ENC_KEY_BRANCH="+tc.branch)
|
|
out, err := cmd.CombinedOutput()
|
|
require.Error(t, err, "expected the non-mock invalid-key branch to exit (log.Fatalf); output: %s", out)
|
|
require.Contains(t, string(out), "SNAPSHOT_ENC_KEY", "the fatal log must name SNAPSHOT_ENC_KEY: %s", out)
|
|
require.False(t, strings.Contains(string(out), "unexpected argument"), "the helper must not fail on argument parsing: %s", out)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCheckProxyRateLimitConfig_WarnsWithoutTrustedProxy pins the fail-loud
|
|
// warning: a non-mock deployment without TRUST_PROXY_HEADERS collapses every
|
|
// per-IP limiter onto the proxy's address, so startup must warn. A dev/mock env
|
|
// skips the check.
|
|
func TestCheckProxyRateLimitConfig_WarnsWithoutTrustedProxy(t *testing.T) {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "production")
|
|
got := captureLog(t, checkProxyRateLimitConfig)
|
|
require.Contains(t, got, "TRUST_PROXY_HEADERS", "a non-mock deployment without TRUST_PROXY_HEADERS must warn: %s", got)
|
|
|
|
t.Setenv("SQUARE_ENVIRONMENT", "mock")
|
|
got = captureLog(t, checkProxyRateLimitConfig)
|
|
require.Empty(t, got, "a dev/mock env must skip the check: %s", got)
|
|
}
|
|
|
|
// TestCheckWebhookSignatureKey_NonFatalBranches pins the non-fatal branches of
|
|
// the webhook signing-key startup check: both configured → silent; neither →
|
|
// CRITICAL. The fatal branches — key-less-with-URL and key-without-URL — are
|
|
// covered by the subprocess tests below (log.Fatalf exits the process).
|
|
func TestCheckWebhookSignatureKey_NonFatalBranches(t *testing.T) {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "production")
|
|
|
|
t.Run("both_configured_silent", func(t *testing.T) {
|
|
t.Setenv("SQUARE_WEBHOOK_SIGNATURE_KEY", "k")
|
|
t.Setenv("SQUARE_WEBHOOK_NOTIFICATION_URL", "https://example.com/webhooks")
|
|
got := captureLog(t, checkWebhookSignatureKey)
|
|
require.Empty(t, got, "both configured must be silent: %s", got)
|
|
})
|
|
|
|
t.Run("neither_configured_critical", func(t *testing.T) {
|
|
t.Setenv("SQUARE_WEBHOOK_SIGNATURE_KEY", "")
|
|
t.Setenv("SQUARE_WEBHOOK_NOTIFICATION_URL", "")
|
|
got := captureLog(t, checkWebhookSignatureKey)
|
|
require.Contains(t, got, "SQUARE_WEBHOOK_SIGNATURE_KEY is not set", "neither configured must log CRITICAL: %s", got)
|
|
})
|
|
|
|
t.Run("mock_env_skips_check", func(t *testing.T) {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "mock")
|
|
t.Setenv("SQUARE_WEBHOOK_SIGNATURE_KEY", "")
|
|
t.Setenv("SQUARE_WEBHOOK_NOTIFICATION_URL", "https://example.com/webhooks")
|
|
got := captureLog(t, checkWebhookSignatureKey)
|
|
require.Empty(t, got, "a dev/mock env must skip the check: %s", got)
|
|
})
|
|
}
|
|
|
|
// TestCheckWebhookSignatureKey_FatalBranch_Exits covers the fail-fast branches
|
|
// — a signing key is REQUIRED when a notification URL is configured, and a
|
|
// configured key with NO URL is equally fatal (the handler would verify HMACs
|
|
// against the public default URL, breaking every genuine event) — both call
|
|
// log.Fatalf (os.Exit). That cannot run in-process, so the test re-executes
|
|
// the test binary with a marker env var and asserts the subprocess exits
|
|
// non-zero with a FATAL message naming the misconfigured variable.
|
|
func TestCheckWebhookSignatureKey_FatalBranch_Exits(t *testing.T) {
|
|
if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "production")
|
|
switch os.Getenv("WEBHOOK_BRANCH") {
|
|
case "KEY_WITHOUT_URL":
|
|
t.Setenv("SQUARE_WEBHOOK_SIGNATURE_KEY", "k")
|
|
t.Setenv("SQUARE_WEBHOOK_NOTIFICATION_URL", "")
|
|
default: // URL_WITHOUT_KEY
|
|
t.Setenv("SQUARE_WEBHOOK_SIGNATURE_KEY", "")
|
|
t.Setenv("SQUARE_WEBHOOK_NOTIFICATION_URL", "https://example.com/webhooks")
|
|
}
|
|
checkWebhookSignatureKey()
|
|
return
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
branch string
|
|
wantIn string
|
|
}{
|
|
{name: "key_without_url_exits", branch: "KEY_WITHOUT_URL", wantIn: "SQUARE_WEBHOOK_NOTIFICATION_URL"},
|
|
{name: "url_without_key_exits", branch: "URL_WITHOUT_KEY", wantIn: "SQUARE_WEBHOOK_SIGNATURE_KEY"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
cmd := exec.Command(os.Args[0], "-test.run=TestCheckWebhookSignatureKey_FatalBranch_Exits")
|
|
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1", "WEBHOOK_BRANCH="+tc.branch)
|
|
out, err := cmd.CombinedOutput()
|
|
require.Error(t, err, "expected the branch to exit (log.Fatalf); output: %s", out)
|
|
require.Contains(t, string(out), tc.wantIn, "the fatal log must name the misconfigured variable: %s", out)
|
|
require.False(t, strings.Contains(string(out), "unexpected argument"), "the helper must not fail on argument parsing: %s", out)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCheckS3ProfilePicsBucket pins the S3_PROFILE_PICS_BUCKET startup check
|
|
// (FIX 3): a dev/mock env skips it, a configured bucket is silent, an unset
|
|
// bucket without R2_ENDPOINT warns, and an unset bucket WITH a configured
|
|
// R2_ENDPOINT (active S3 client, silently-skipped deletions) elevates to
|
|
// CRITICAL.
|
|
func TestCheckS3ProfilePicsBucket(t *testing.T) {
|
|
t.Run("mock_env_skips_check", func(t *testing.T) {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "mock")
|
|
t.Setenv("S3_PROFILE_PICS_BUCKET", "")
|
|
t.Setenv("R2_ENDPOINT", "")
|
|
got := captureLog(t, checkS3ProfilePicsBucket)
|
|
require.Empty(t, got, "a dev/mock env must skip the check: %s", got)
|
|
})
|
|
|
|
t.Run("bucket_configured_silent", func(t *testing.T) {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "production")
|
|
t.Setenv("S3_PROFILE_PICS_BUCKET", "crussell-profile-pics")
|
|
t.Setenv("R2_ENDPOINT", "https://r2.example.com")
|
|
got := captureLog(t, checkS3ProfilePicsBucket)
|
|
require.Empty(t, got, "a configured bucket must be silent: %s", got)
|
|
})
|
|
|
|
t.Run("unset_bucket_without_endpoint_warns", func(t *testing.T) {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "production")
|
|
t.Setenv("S3_PROFILE_PICS_BUCKET", "")
|
|
t.Setenv("R2_ENDPOINT", "")
|
|
got := captureLog(t, checkS3ProfilePicsBucket)
|
|
require.Contains(t, got, "WARNING", "an unset bucket without R2_ENDPOINT must warn: %s", got)
|
|
require.NotContains(t, got, "CRITICAL", "an unset bucket without R2_ENDPOINT must not be CRITICAL: %s", got)
|
|
})
|
|
|
|
t.Run("unset_bucket_with_endpoint_critical", func(t *testing.T) {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "production")
|
|
t.Setenv("S3_PROFILE_PICS_BUCKET", "")
|
|
t.Setenv("R2_ENDPOINT", "https://r2.example.com")
|
|
got := captureLog(t, checkS3ProfilePicsBucket)
|
|
require.Contains(t, got, "CRITICAL", "an unset bucket with an active R2_ENDPOINT must elevate to CRITICAL: %s", got)
|
|
})
|
|
|
|
t.Run("stub_client_warns_even_with_bucket", func(t *testing.T) {
|
|
t.Setenv("SQUARE_ENVIRONMENT", "production")
|
|
t.Setenv("S3_PROFILE_PICS_BUCKET", "crussell-profile-pics")
|
|
t.Setenv("R2_ENDPOINT", "https://r2.example.com")
|
|
orig := s3.ClientIsStub
|
|
s3.ClientIsStub = true
|
|
defer func() { s3.ClientIsStub = orig }()
|
|
got := captureLog(t, checkS3ProfilePicsBucket)
|
|
require.Contains(t, got, "CRITICAL", "a stub client must warn even when the bucket is set: %s", got)
|
|
})
|
|
}
|