Improve spamassassin report

This commit is contained in:
nemunaire 2025-10-22 14:38:38 +07:00
commit e77bffb04f
6 changed files with 203 additions and 106 deletions

View file

@ -674,7 +674,12 @@ components:
- score
- required_score
- is_spam
- test_details
properties:
version:
type: string
description: SpamAssassin version
example: "SpamAssassin 4.0.1"
score:
type: number
format: float
@ -695,10 +700,44 @@ components:
type: string
description: List of triggered SpamAssassin tests
example: ["BAYES_00", "DKIM_SIGNED"]
test_details:
type: object
additionalProperties:
$ref: '#/components/schemas/SpamTestDetail'
description: Map of test names to their detailed results
example:
BAYES_00:
name: "BAYES_00"
score: -1.9
description: "Bayes spam probability is 0 to 1%"
DKIM_SIGNED:
name: "DKIM_SIGNED"
score: 0.1
description: "Message has a DKIM or DK signature, not necessarily valid"
report:
type: string
description: Full SpamAssassin report
SpamTestDetail:
type: object
required:
- name
- score
properties:
name:
type: string
description: Test name
example: "BAYES_00"
score:
type: number
format: float
description: Score contribution of this test
example: -1.9
description:
type: string
description: Human-readable description of what this test checks
example: "Bayes spam probability is 0 to 1%"
DNSResults:
type: object
required:

View file

@ -63,7 +63,7 @@ type AnalysisResults struct {
DNS *api.DNSResults
Headers *api.HeaderAnalysis
RBL *RBLResults
SpamAssassin *SpamAssassinResult
SpamAssassin *api.SpamAssassinResult
}
// AnalyzeEmail performs complete email analysis
@ -157,21 +157,7 @@ func (r *ReportGenerator) GenerateReport(testID uuid.UUID, results *AnalysisResu
}
// Add SpamAssassin result
if results.SpamAssassin != nil {
report.Spamassassin = &api.SpamAssassinResult{
Score: float32(results.SpamAssassin.Score),
RequiredScore: float32(results.SpamAssassin.RequiredScore),
IsSpam: results.SpamAssassin.IsSpam,
}
if len(results.SpamAssassin.Tests) > 0 {
report.Spamassassin.Tests = &results.SpamAssassin.Tests
}
if results.SpamAssassin.RawReport != "" {
report.Spamassassin.Report = &results.SpamAssassin.RawReport
}
}
report.Spamassassin = results.SpamAssassin
// Add raw headers
if results.Email != nil && results.Email.RawHeaders != "" {

View file

@ -26,6 +26,8 @@ import (
"regexp"
"strconv"
"strings"
"git.happydns.org/happyDeliver/internal/api"
)
// SpamAssassinAnalyzer analyzes SpamAssassin results from email headers
@ -36,33 +38,15 @@ func NewSpamAssassinAnalyzer() *SpamAssassinAnalyzer {
return &SpamAssassinAnalyzer{}
}
// SpamAssassinResult represents parsed SpamAssassin results
type SpamAssassinResult struct {
IsSpam bool
Score float64
RequiredScore float64
Tests []string
TestDetails map[string]SpamTestDetail
Version string
RawReport string
}
// SpamTestDetail contains details about a specific spam test
type SpamTestDetail struct {
Name string
Score float64
Description string
}
// AnalyzeSpamAssassin extracts and analyzes SpamAssassin results from email headers
func (a *SpamAssassinAnalyzer) AnalyzeSpamAssassin(email *EmailMessage) *SpamAssassinResult {
func (a *SpamAssassinAnalyzer) AnalyzeSpamAssassin(email *EmailMessage) *api.SpamAssassinResult {
headers := email.GetSpamAssassinHeaders()
if len(headers) == 0 {
return nil
}
result := &SpamAssassinResult{
TestDetails: make(map[string]SpamTestDetail),
result := &api.SpamAssassinResult{
TestDetails: make(map[string]api.SpamTestDetail),
}
// Parse X-Spam-Status header
@ -73,7 +57,7 @@ func (a *SpamAssassinAnalyzer) AnalyzeSpamAssassin(email *EmailMessage) *SpamAss
// Parse X-Spam-Score header (as fallback if not in X-Spam-Status)
if scoreHeader, ok := headers["X-Spam-Score"]; ok && result.Score == 0 {
if score, err := strconv.ParseFloat(strings.TrimSpace(scoreHeader), 64); err == nil {
result.Score = score
result.Score = float32(score)
}
}
@ -84,13 +68,13 @@ func (a *SpamAssassinAnalyzer) AnalyzeSpamAssassin(email *EmailMessage) *SpamAss
// Parse X-Spam-Report header for detailed test results
if reportHeader, ok := headers["X-Spam-Report"]; ok {
result.RawReport = strings.Replace(reportHeader, " * ", "\n* ", -1)
result.Report = api.PtrTo(strings.Replace(reportHeader, " * ", "\n* ", -1))
a.parseSpamReport(reportHeader, result)
}
// Parse X-Spam-Checker-Version
if versionHeader, ok := headers["X-Spam-Checker-Version"]; ok {
result.Version = strings.TrimSpace(versionHeader)
result.Version = api.PtrTo(strings.TrimSpace(versionHeader))
}
return result
@ -98,7 +82,7 @@ func (a *SpamAssassinAnalyzer) AnalyzeSpamAssassin(email *EmailMessage) *SpamAss
// parseSpamStatus parses the X-Spam-Status header
// Format: Yes/No, score=5.5 required=5.0 tests=TEST1,TEST2,TEST3 autolearn=no
func (a *SpamAssassinAnalyzer) parseSpamStatus(header string, result *SpamAssassinResult) {
func (a *SpamAssassinAnalyzer) parseSpamStatus(header string, result *api.SpamAssassinResult) {
// Check if spam (first word)
parts := strings.SplitN(header, ",", 2)
if len(parts) > 0 {
@ -110,7 +94,7 @@ func (a *SpamAssassinAnalyzer) parseSpamStatus(header string, result *SpamAssass
scoreRe := regexp.MustCompile(`score=(-?\d+\.?\d*)`)
if matches := scoreRe.FindStringSubmatch(header); len(matches) > 1 {
if score, err := strconv.ParseFloat(matches[1], 64); err == nil {
result.Score = score
result.Score = float32(score)
}
}
@ -118,19 +102,19 @@ func (a *SpamAssassinAnalyzer) parseSpamStatus(header string, result *SpamAssass
requiredRe := regexp.MustCompile(`required=(-?\d+\.?\d*)`)
if matches := requiredRe.FindStringSubmatch(header); len(matches) > 1 {
if required, err := strconv.ParseFloat(matches[1], 64); err == nil {
result.RequiredScore = required
result.RequiredScore = float32(required)
}
}
// Extract tests
testsRe := regexp.MustCompile(`tests=([^\s]+)`)
testsRe := regexp.MustCompile(`tests=([^=]+)(?:\s|$)`)
if matches := testsRe.FindStringSubmatch(header); len(matches) > 1 {
testsStr := matches[1]
// Tests can be comma or space separated
tests := strings.FieldsFunc(testsStr, func(r rune) bool {
return r == ',' || r == ' '
})
result.Tests = tests
result.Tests = &tests
}
}
@ -138,17 +122,20 @@ func (a *SpamAssassinAnalyzer) parseSpamStatus(header string, result *SpamAssass
// Format varies, but typically:
// * 1.5 TEST_NAME Description of test
// * 0.0 TEST_NAME2 Description
// Note: mail.Header.Get() joins continuation lines, so newlines are removed.
// We split on '*' to separate individual tests.
func (a *SpamAssassinAnalyzer) parseSpamReport(report string, result *SpamAssassinResult) {
// The report header has been joined by mail.Header.Get(), so we split on '*'
// Each segment starting with '*' is either a test line or continuation
// Multiline descriptions continue on lines starting with * but without score:
// * 0.0 TEST_NAME Description line 1
// * continuation line 2
// * continuation line 3
func (a *SpamAssassinAnalyzer) parseSpamReport(report string, result *api.SpamAssassinResult) {
segments := strings.Split(report, "*")
// Regex to match test lines: score TEST_NAME Description
// Format: " 0.0 TEST_NAME Description" or " -0.1 TEST_NAME Description"
testRe := regexp.MustCompile(`^\s*(-?\d+\.?\d*)\s+(\S+)\s+(.*)$`)
var currentTestName string
var currentDescription strings.Builder
for _, segment := range segments {
segment = strings.TrimSpace(segment)
if segment == "" {
@ -158,22 +145,55 @@ func (a *SpamAssassinAnalyzer) parseSpamReport(report string, result *SpamAssass
// Try to match as a test line
matches := testRe.FindStringSubmatch(segment)
if len(matches) > 3 {
// Save previous test if exists
if currentTestName != "" {
description := strings.TrimSpace(currentDescription.String())
detail := api.SpamTestDetail{
Name: currentTestName,
Score: result.TestDetails[currentTestName].Score,
Description: &description,
}
result.TestDetails[currentTestName] = detail
}
// Start new test
testName := matches[2]
score, _ := strconv.ParseFloat(matches[1], 64)
description := strings.TrimSpace(matches[3])
detail := SpamTestDetail{
Name: testName,
Score: score,
Description: description,
currentTestName = testName
currentDescription.Reset()
currentDescription.WriteString(description)
// Initialize with score
result.TestDetails[testName] = api.SpamTestDetail{
Name: testName,
Score: float32(score),
}
result.TestDetails[testName] = detail
} else if currentTestName != "" {
// This is a continuation line for the current test
// Add a space before appending to ensure proper word separation
if currentDescription.Len() > 0 {
currentDescription.WriteString(" ")
}
currentDescription.WriteString(segment)
}
}
// Save the last test if exists
if currentTestName != "" {
description := strings.TrimSpace(currentDescription.String())
detail := api.SpamTestDetail{
Name: currentTestName,
Score: result.TestDetails[currentTestName].Score,
Description: &description,
}
result.TestDetails[currentTestName] = detail
}
}
// CalculateSpamAssassinScore calculates the SpamAssassin contribution to deliverability
func (a *SpamAssassinAnalyzer) CalculateSpamAssassinScore(result *SpamAssassinResult) int {
func (a *SpamAssassinAnalyzer) CalculateSpamAssassinScore(result *api.SpamAssassinResult) int {
if result == nil {
return 100 // No spam scan results, assume good
}
@ -192,6 +212,6 @@ func (a *SpamAssassinAnalyzer) CalculateSpamAssassinScore(result *SpamAssassinRe
return 0 // Failed spam test
} else {
// Linear scale between 0 and required threshold
return 100 - int(math.Round(score*100/result.RequiredScore))
return 100 - int(math.Round(float64(score*100/result.RequiredScore)))
}
}

View file

@ -26,6 +26,8 @@ import (
"net/mail"
"strings"
"testing"
"git.happydns.org/happyDeliver/internal/api"
)
func TestParseSpamStatus(t *testing.T) {
@ -33,8 +35,8 @@ func TestParseSpamStatus(t *testing.T) {
name string
header string
expectedIsSpam bool
expectedScore float64
expectedReq float64
expectedScore float32
expectedReq float32
expectedTests []string
}{
{
@ -75,8 +77,8 @@ func TestParseSpamStatus(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := &SpamAssassinResult{
TestDetails: make(map[string]SpamTestDetail),
result := &api.SpamAssassinResult{
TestDetails: make(map[string]api.SpamTestDetail),
}
analyzer.parseSpamStatus(tt.header, result)
@ -89,8 +91,12 @@ func TestParseSpamStatus(t *testing.T) {
if result.RequiredScore != tt.expectedReq {
t.Errorf("RequiredScore = %v, want %v", result.RequiredScore, tt.expectedReq)
}
if len(tt.expectedTests) > 0 && !stringSliceEqual(result.Tests, tt.expectedTests) {
t.Errorf("Tests = %v, want %v", result.Tests, tt.expectedTests)
if len(tt.expectedTests) > 0 {
if result.Tests == nil {
t.Errorf("Tests = nil, want %v", tt.expectedTests)
} else if !stringSliceEqual(*result.Tests, tt.expectedTests) {
t.Errorf("Tests = %v, want %v", *result.Tests, tt.expectedTests)
}
}
})
}
@ -109,27 +115,27 @@ func TestParseSpamReport(t *testing.T) {
`
analyzer := NewSpamAssassinAnalyzer()
result := &SpamAssassinResult{
TestDetails: make(map[string]SpamTestDetail),
result := &api.SpamAssassinResult{
TestDetails: make(map[string]api.SpamTestDetail),
}
analyzer.parseSpamReport(report, result)
expectedTests := map[string]SpamTestDetail{
expectedTests := map[string]api.SpamTestDetail{
"BAYES_99": {
Name: "BAYES_99",
Score: 5.0,
Description: "Bayes spam probability is 99 to 100%",
Description: api.PtrTo("Bayes spam probability is 99 to 100%"),
},
"SPOOFED_SENDER": {
Name: "SPOOFED_SENDER",
Score: 3.5,
Description: "From address doesn't match envelope sender",
Description: api.PtrTo("From address doesn't match envelope sender"),
},
"ALL_TRUSTED": {
Name: "ALL_TRUSTED",
Score: -1.0,
Description: "All mail servers are trusted",
Description: api.PtrTo("All mail servers are trusted"),
},
}
@ -142,8 +148,8 @@ func TestParseSpamReport(t *testing.T) {
if detail.Score != expected.Score {
t.Errorf("Test %s score = %v, want %v", testName, detail.Score, expected.Score)
}
if detail.Description != expected.Description {
t.Errorf("Test %s description = %q, want %q", testName, detail.Description, expected.Description)
if *detail.Description != *expected.Description {
t.Errorf("Test %s description = %q, want %q", testName, *detail.Description, *expected.Description)
}
}
}
@ -151,7 +157,7 @@ func TestParseSpamReport(t *testing.T) {
func TestGetSpamAssassinScore(t *testing.T) {
tests := []struct {
name string
result *SpamAssassinResult
result *api.SpamAssassinResult
expectedScore int
minScore int
maxScore int
@ -159,11 +165,11 @@ func TestGetSpamAssassinScore(t *testing.T) {
{
name: "Nil result",
result: nil,
expectedScore: 0,
expectedScore: 100,
},
{
name: "Excellent score (negative)",
result: &SpamAssassinResult{
result: &api.SpamAssassinResult{
Score: -2.5,
RequiredScore: 5.0,
},
@ -171,38 +177,43 @@ func TestGetSpamAssassinScore(t *testing.T) {
},
{
name: "Good score (below threshold)",
result: &SpamAssassinResult{
result: &api.SpamAssassinResult{
Score: 2.0,
RequiredScore: 5.0,
},
minScore: 80,
maxScore: 100,
expectedScore: 60, // 100 - round(2*100/5) = 100 - 40 = 60
},
{
name: "Borderline (just above threshold)",
result: &SpamAssassinResult{
name: "Score at threshold",
result: &api.SpamAssassinResult{
Score: 5.0,
RequiredScore: 5.0,
},
expectedScore: 0, // >= threshold = 0
},
{
name: "Above threshold (spam)",
result: &api.SpamAssassinResult{
Score: 6.0,
RequiredScore: 5.0,
},
minScore: 60,
maxScore: 80,
expectedScore: 0, // >= threshold = 0
},
{
name: "High spam score",
result: &SpamAssassinResult{
result: &api.SpamAssassinResult{
Score: 12.0,
RequiredScore: 5.0,
},
minScore: 20,
maxScore: 50,
expectedScore: 0, // >= threshold = 0
},
{
name: "Very high spam score",
result: &SpamAssassinResult{
result: &api.SpamAssassinResult{
Score: 20.0,
RequiredScore: 5.0,
},
expectedScore: 0,
expectedScore: 0, // >= threshold = 0
},
}
@ -210,7 +221,7 @@ func TestGetSpamAssassinScore(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
score := analyzer.GetSpamAssassinScore(tt.result)
score, _ := analyzer.CalculateSpamAssassinScore(tt.result)
if tt.minScore > 0 || tt.maxScore > 0 {
if score < tt.minScore || score > tt.maxScore {
@ -230,7 +241,7 @@ func TestAnalyzeSpamAssassin(t *testing.T) {
name string
headers map[string]string
expectedIsSpam bool
expectedScore float64
expectedScore float32
expectedHasDetails bool
}{
{
@ -370,24 +381,26 @@ func TestAnalyzeRealEmailExample(t *testing.T) {
}
// Validate score (should be -0.1)
expectedScore := -0.1
var expectedScore float32 = -0.1
if result.Score != expectedScore {
t.Errorf("Score = %v, want %v", result.Score, expectedScore)
}
// Validate required score (should be 5.0)
expectedRequired := 5.0
var expectedRequired float32 = 5.0
if result.RequiredScore != expectedRequired {
t.Errorf("RequiredScore = %v, want %v", result.RequiredScore, expectedRequired)
}
// Validate version
if !strings.Contains(result.Version, "SpamAssassin") {
t.Errorf("Version should contain 'SpamAssassin', got: %s", result.Version)
if result.Version == nil {
t.Errorf("Version should contain 'SpamAssassin', got: nil")
} else if !strings.Contains(*result.Version, "SpamAssassin") {
t.Errorf("Version should contain 'SpamAssassin', got: %s", *result.Version)
}
// Validate that tests were extracted
if len(result.Tests) == 0 {
if len(*result.Tests) == 0 {
t.Error("Expected tests to be extracted, got none")
}
@ -400,7 +413,7 @@ func TestAnalyzeRealEmailExample(t *testing.T) {
"SPF_HELO_NONE": true,
}
for _, testName := range result.Tests {
for _, testName := range *result.Tests {
if expectedTests[testName] {
t.Logf("Found expected test: %s", testName)
}
@ -414,11 +427,11 @@ func TestAnalyzeRealEmailExample(t *testing.T) {
// Log what we actually got for debugging
t.Logf("Parsed %d test details from X-Spam-Report", len(result.TestDetails))
for name, detail := range result.TestDetails {
t.Logf(" %s: score=%v, description=%s", name, detail.Score, detail.Description)
t.Logf(" %s: score=%v, description=%s", name, detail.Score, *detail.Description)
}
// Define expected test details with their scores
expectedTestDetails := map[string]float64{
expectedTestDetails := map[string]float32{
"SPF_PASS": -0.0,
"SPF_HELO_NONE": 0.0,
"DKIM_VALID": -0.1,
@ -439,13 +452,13 @@ func TestAnalyzeRealEmailExample(t *testing.T) {
if detail.Score != expectedScore {
t.Errorf("Test %s score = %v, want %v", testName, detail.Score, expectedScore)
}
if detail.Description == "" {
if detail.Description == nil || *detail.Description == "" {
t.Errorf("Test %s should have a description", testName)
}
}
// Test GetSpamAssassinScore
score := analyzer.GetSpamAssassinScore(result)
score, _ := analyzer.CalculateSpamAssassinScore(result)
if score != 100 {
t.Errorf("GetSpamAssassinScore() = %v, want 100 (excellent score for negative spam score)", score)
}

View file

@ -3,16 +3,25 @@
interface Props {
spamassassin: SpamAssassinResult;
spamScore: number;
}
let { spamassassin }: Props = $props();
let { spamassassin, spamScore }: Props = $props();
</script>
<div class="card">
<div class="card-header bg-warning bg-opacity-10">
<h5 class="mb-0 fw-bold">
<i class="bi bi-bug me-2"></i>SpamAssassin Analysis
</h5>
<div class="card-header bg-white">
<h4 class="mb-0 d-flex justify-content-between align-items-center">
<span>
<i class="bi bi-bug me-2"></i>
SpamAssassin Analysis
</span>
{#if spamScore !== undefined}
<span class="badge bg-secondary">
{spamScore}%
</span>
{/if}
</h4>
</div>
<div class="card-body">
<div class="row mb-3">
@ -30,7 +39,34 @@
</div>
</div>
{#if spamassassin.tests && spamassassin.tests.length > 0}
{#if spamassassin.test_details && Object.keys(spamassassin.test_details).length > 0}
<div class="mb-3">
<div class="table-responsive mt-2">
<table class="table table-sm table-hover">
<thead>
<tr>
<th>Test Name</th>
<th class="text-end">Score</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{#each Object.entries(spamassassin.test_details) as [testName, detail]}
<tr class={detail.score > 0 ? 'table-warning' : detail.score < 0 ? 'table-success' : ''}>
<td class="font-monospace">{testName}</td>
<td class="text-end">
<span class={detail.score > 0 ? 'text-danger fw-bold' : detail.score < 0 ? 'text-success fw-bold' : 'text-muted'}>
{detail.score > 0 ? '+' : ''}{detail.score.toFixed(1)}
</span>
</td>
<td class="small">{detail.description || ''}</td>
</tr>
{/each}
</tbody>
</table>
</div>
</div>
{:else if spamassassin.tests && spamassassin.tests.length > 0}
<div class="mb-2">
<strong>Tests Triggered:</strong>
<div class="mt-2">
@ -43,7 +79,7 @@
{#if spamassassin.report}
<details class="mt-3">
<summary class="cursor-pointer fw-bold">Full Report</summary>
<summary class="cursor-pointer fw-bold">Raw Report</summary>
<pre class="mt-2 small bg-light p-3 rounded">{spamassassin.report}</pre>
</details>
{/if}

View file

@ -197,7 +197,10 @@
{#if report.spamassassin}
<div class="row mb-4" id="spam">
<div class="col-12">
<SpamAssassinCard spamassassin={report.spamassassin} />
<SpamAssassinCard
spamassassin={report.spamassassin}
spamScore={report.summary?.spam_score}
/>
</div>
</div>
{/if}