From 932bc981b5b2f3ca3e68a07084d5c39213878292 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 24 Oct 2025 16:12:19 +0700 Subject: [PATCH] Filter Authentication-Results to keep only local ones --- pkg/analyzer/authentication_dkim_test.go | 2 ++ pkg/analyzer/parser.go | 28 +++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pkg/analyzer/authentication_dkim_test.go b/pkg/analyzer/authentication_dkim_test.go index 0d00031..323e421 100644 --- a/pkg/analyzer/authentication_dkim_test.go +++ b/pkg/analyzer/authentication_dkim_test.go @@ -270,6 +270,8 @@ func TestParseLegacyDKIM(t *testing.T) { } func TestParseLegacyDKIM_Integration(t *testing.T) { + hostname = "" + // Test that parseLegacyDKIM is properly integrated into AnalyzeAuthentication t.Run("Legacy DKIM is used when no Authentication-Results", func(t *testing.T) { analyzer := NewAuthenticationAnalyzer() diff --git a/pkg/analyzer/parser.go b/pkg/analyzer/parser.go index 13c012c..ca3cb46 100644 --- a/pkg/analyzer/parser.go +++ b/pkg/analyzer/parser.go @@ -28,9 +28,16 @@ import ( "mime/multipart" "net/mail" "net/textproto" + "os" "strings" ) +var hostname = "" + +func init() { + hostname, _ = os.Hostname() +} + // EmailMessage represents a parsed email message type EmailMessage struct { Header mail.Header @@ -211,8 +218,27 @@ func buildRawHeaders(header mail.Header) string { } // GetAuthenticationResults extracts Authentication-Results headers +// If hostname is provided, only returns headers that begin with that hostname func (e *EmailMessage) GetAuthenticationResults() []string { - return e.Header[textproto.CanonicalMIMEHeaderKey("Authentication-Results")] + allResults := e.Header[textproto.CanonicalMIMEHeaderKey("Authentication-Results")] + + // If no hostname specified, return all results + if hostname == "" { + return allResults + } + + // Filter results that begin with the specified hostname + var filtered []string + prefix := hostname + ";" + for _, result := range allResults { + // Trim whitespace and check if it starts with hostname; + trimmed := strings.TrimSpace(result) + if strings.HasPrefix(trimmed, prefix) { + filtered = append(filtered, result) + } + } + + return filtered } // GetSpamAssassinHeaders extracts SpamAssassin-related headers