|
| 1 | +package email |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "log/slog" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + sestypes "github.com/aws/aws-sdk-go-v2/service/sesv2/types" |
| 14 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 15 | +) |
| 16 | + |
| 17 | +// TestMaskEmail_BasicShapes pins the masking algorithm — must match the |
| 18 | +// api maskEmail behaviour exactly. |
| 19 | +func TestMaskEmail_BasicShapes(t *testing.T) { |
| 20 | + cases := []struct { |
| 21 | + in, want string |
| 22 | + }{ |
| 23 | + {"alice@example.com", "a***@example.com"}, |
| 24 | + {"a@example.com", "a@example.com"}, // 1-char local preserved |
| 25 | + {"bb20-t7-1779218881@instanode-test.dev", "b***@instanode-test.dev"}, |
| 26 | + {"mastermanas805@gmail.com", "m***@gmail.com"}, |
| 27 | + {"@onlydomain.com", "@onlydomain.com"}, // empty local — return unchanged (defensive) |
| 28 | + {"no-at-sign", "no-at-sign"}, |
| 29 | + {"", ""}, |
| 30 | + } |
| 31 | + for _, c := range cases { |
| 32 | + if got := maskEmail(c.in); got != c.want { |
| 33 | + t.Errorf("maskEmail(%q) = %q; want %q", c.in, got, c.want) |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// captureSlog redirects the default slog logger into the returned buffer |
| 39 | +// for the duration of fn. Returns the captured text. Used by the provider |
| 40 | +// regression tests so they don't have to plumb a logger through. |
| 41 | +func captureSlog(t *testing.T, fn func()) string { |
| 42 | + t.Helper() |
| 43 | + var buf bytes.Buffer |
| 44 | + h := slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}) |
| 45 | + orig := slog.Default() |
| 46 | + slog.SetDefault(slog.New(h)) |
| 47 | + defer slog.SetDefault(orig) |
| 48 | + fn() |
| 49 | + return buf.String() |
| 50 | +} |
| 51 | + |
| 52 | +// TestWorkerEmailProviders_NoRawRecipientInLogs is the T22 P1-1 / MR-P1-46 |
| 53 | +// regression test. |
| 54 | +// |
| 55 | +// BUG (pre-fix): worker brevo_provider.go and ses_provider.go logged |
| 56 | +// `"recipient", evt.Recipient` raw at INFO/WARN/ERROR on every send. The |
| 57 | +// L1 PII-masking fix in api `4078ca3` masked api-side logs but did NOT |
| 58 | +// touch the worker providers — verified live in prod |
| 59 | +// (instant-worker pod commit_id=7169493 emitted full recipient strings |
| 60 | +// into NR Logs on every send). |
| 61 | +// |
| 62 | +// FIX: maskEmail applied at every `"recipient"` slog site in both |
| 63 | +// providers. This test stamps a unique recipient address, drives each |
| 64 | +// provider through a real send path that produces a `"recipient"` slog |
| 65 | +// field, and asserts the raw local part NEVER appears in the captured |
| 66 | +// log output — only the masked form. Registry-style: covers BOTH worker |
| 67 | +// providers in one table, so a future third provider that ships without |
| 68 | +// the maskEmail wrap fails this test. |
| 69 | +func TestWorkerEmailProviders_NoRawRecipientInLogs(t *testing.T) { |
| 70 | + const ( |
| 71 | + rawRecipient = "regressioncanary12345@instanode-test.dev" |
| 72 | + rawLocal = "regressioncanary12345" |
| 73 | + wantMasked = "r***@instanode-test.dev" |
| 74 | + kind = "subscription.upgraded" |
| 75 | + ) |
| 76 | + |
| 77 | + t.Run("brevo_success_2xx_sent", func(t *testing.T) { |
| 78 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 79 | + _, _ = io.ReadAll(r.Body) |
| 80 | + w.WriteHeader(http.StatusCreated) |
| 81 | + _, _ = w.Write([]byte(`{"messageId":"x"}`)) |
| 82 | + })) |
| 83 | + defer srv.Close() |
| 84 | + p, err := NewBrevoProvider(BrevoConfig{APIKey: "k", TemplateIDs: map[string]int{kind: 42}}) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("NewBrevoProvider: %v", err) |
| 87 | + } |
| 88 | + p.url = srv.URL |
| 89 | + out := captureSlog(t, func() { |
| 90 | + _ = p.SendEvent(context.Background(), EventEmail{Kind: kind, Recipient: rawRecipient}) |
| 91 | + }) |
| 92 | + assertNoRawRecipient(t, "brevo:2xx", out, rawLocal, wantMasked) |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("brevo_permanent_4xx_logs_recipient", func(t *testing.T) { |
| 96 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 97 | + _, _ = io.ReadAll(r.Body) |
| 98 | + w.WriteHeader(http.StatusBadRequest) |
| 99 | + _, _ = w.Write([]byte(`{"code":"bad_request"}`)) |
| 100 | + })) |
| 101 | + defer srv.Close() |
| 102 | + p, err := NewBrevoProvider(BrevoConfig{APIKey: "k", TemplateIDs: map[string]int{kind: 42}}) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("NewBrevoProvider: %v", err) |
| 105 | + } |
| 106 | + p.url = srv.URL |
| 107 | + out := captureSlog(t, func() { |
| 108 | + _ = p.SendEvent(context.Background(), EventEmail{Kind: kind, Recipient: rawRecipient}) |
| 109 | + }) |
| 110 | + assertNoRawRecipient(t, "brevo:4xx", out, rawLocal, wantMasked) |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("brevo_auth_wall_401", func(t *testing.T) { |
| 114 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 115 | + _, _ = io.ReadAll(r.Body) |
| 116 | + w.WriteHeader(http.StatusUnauthorized) |
| 117 | + _, _ = w.Write([]byte(`{"code":"unauthorized"}`)) |
| 118 | + })) |
| 119 | + defer srv.Close() |
| 120 | + p, err := NewBrevoProvider(BrevoConfig{APIKey: "k", TemplateIDs: map[string]int{kind: 42}}) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("NewBrevoProvider: %v", err) |
| 123 | + } |
| 124 | + p.url = srv.URL |
| 125 | + out := captureSlog(t, func() { |
| 126 | + _ = p.SendEvent(context.Background(), EventEmail{Kind: kind, Recipient: rawRecipient}) |
| 127 | + }) |
| 128 | + assertNoRawRecipient(t, "brevo:auth_wall", out, rawLocal, wantMasked) |
| 129 | + }) |
| 130 | + |
| 131 | + t.Run("ses_success_sent", func(t *testing.T) { |
| 132 | + fake := &fakeSESClient{} |
| 133 | + p := &SESProvider{ |
| 134 | + client: fake, |
| 135 | + fromEmail: "noreply@example.com", |
| 136 | + templates: map[string]string{kind: "tmpl-1"}, |
| 137 | + } |
| 138 | + out := captureSlog(t, func() { |
| 139 | + _ = p.SendEvent(context.Background(), EventEmail{Kind: kind, Recipient: rawRecipient}) |
| 140 | + }) |
| 141 | + assertNoRawRecipient(t, "ses:success", out, rawLocal, wantMasked) |
| 142 | + }) |
| 143 | + |
| 144 | + t.Run("ses_permanent_rejected", func(t *testing.T) { |
| 145 | + fake := &fakeSESClient{err: &sestypes.MessageRejected{Message: aws.String("rejected")}} |
| 146 | + p := &SESProvider{ |
| 147 | + client: fake, |
| 148 | + fromEmail: "noreply@example.com", |
| 149 | + templates: map[string]string{kind: "tmpl-1"}, |
| 150 | + } |
| 151 | + out := captureSlog(t, func() { |
| 152 | + _ = p.SendEvent(context.Background(), EventEmail{Kind: kind, Recipient: rawRecipient}) |
| 153 | + }) |
| 154 | + assertNoRawRecipient(t, "ses:rejected", out, rawLocal, wantMasked) |
| 155 | + }) |
| 156 | + |
| 157 | + t.Run("ses_transient_throttling", func(t *testing.T) { |
| 158 | + fake := &fakeSESClient{err: &sestypes.TooManyRequestsException{Message: aws.String("rate")}} |
| 159 | + p := &SESProvider{ |
| 160 | + client: fake, |
| 161 | + fromEmail: "noreply@example.com", |
| 162 | + templates: map[string]string{kind: "tmpl-1"}, |
| 163 | + } |
| 164 | + out := captureSlog(t, func() { |
| 165 | + _ = p.SendEvent(context.Background(), EventEmail{Kind: kind, Recipient: rawRecipient}) |
| 166 | + }) |
| 167 | + assertNoRawRecipient(t, "ses:transient", out, rawLocal, wantMasked) |
| 168 | + }) |
| 169 | +} |
| 170 | + |
| 171 | +func assertNoRawRecipient(t *testing.T, label, out, rawLocal, wantMasked string) { |
| 172 | + t.Helper() |
| 173 | + if out == "" { |
| 174 | + t.Fatalf("%s: provider emitted no log output — cannot verify masking; update the harness so the recipient slog field still fires", label) |
| 175 | + } |
| 176 | + if strings.Contains(out, rawLocal) { |
| 177 | + t.Errorf("%s: provider log contains raw recipient local-part %q — PII leak.\nFull output:\n%s", label, rawLocal, out) |
| 178 | + } |
| 179 | + if !strings.Contains(out, wantMasked) { |
| 180 | + t.Errorf("%s: provider log does not contain masked form %q — masking was not applied.\nFull output:\n%s", label, wantMasked, out) |
| 181 | + } |
| 182 | +} |
0 commit comments