From 3e06162fa007746ab697f96133ebf5c80fa344ea Mon Sep 17 00:00:00 2001 From: Lucas Rayan Date: Mon, 7 Sep 2026 08:18:51 -0300 Subject: [PATCH] fix(test): TestBccNeverAppearsInTheHeaders falhava por acaso Procurava a sequencia "bcc" em qualquer sitio do bloco de cabecalhos, e o Message-ID e um UUID aleatorio. Quando calha conter essa sequencia -- o CI apanhou <1bcc5e82-9f6a-4e92-a1e5-65dc96b20208@mail.example.test> -- o teste falha num commit que nao lhe tocou. Sao algumas vezes em mil execucoes. Passa a procurar o campo Bcc no inicio de uma linha de cabecalho, que e o que lhe interessa, mantendo a verificacao de que o endereco em si nao aparece em lado nenhum. Confirmado por mutacao: com um cabecalho Bcc injectado, falha. Co-Authored-By: Claude Opus 5 --- .../application/usecase/webmail_compose_test.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/services/protocols/internal/application/usecase/webmail_compose_test.go b/services/protocols/internal/application/usecase/webmail_compose_test.go index 0da4f61..6a3bd6f 100644 --- a/services/protocols/internal/application/usecase/webmail_compose_test.go +++ b/services/protocols/internal/application/usecase/webmail_compose_test.go @@ -102,9 +102,20 @@ func TestBccNeverAppearsInTheHeaders(t *testing.T) { payload := string(buildMimeMessage(input, recipients, "mail.example.test")) headers, _, _ := strings.Cut(payload, "\r\n\r\n") - if strings.Contains(strings.ToLower(headers), "bcc") || - strings.Contains(headers, "secret@example.test") { - t.Errorf("bcc leaked into the headers:\n%s", headers) + + // The field, not the letters. Searching the whole block for "bcc" matched + // the random Message-ID whenever its UUID happened to contain that run of + // characters - <1bcc5e82-...> is a real example - so this failed a few + // times in a thousand on changes that had nothing to do with it. + for _, line := range strings.Split(headers, "\r\n") { + name, _, found := strings.Cut(line, ":") + if found && strings.EqualFold(strings.TrimSpace(name), "bcc") { + t.Errorf("a Bcc header was emitted:\n%s", headers) + } + } + // The address itself must not appear anywhere, under any field. + if strings.Contains(headers, "secret@example.test") { + t.Errorf("the blind copy address leaked into the headers:\n%s", headers) } }