IMP-06: anhangs-virenscan-anbindung
Anbindung eines Virenscanners für importierte Anhänge, mit Quarantäne-Verhalten bei Fund und klarer Statusanzeige. Kein ClamAV-Daemon auf dem Testhost installiert (größerer System- eingriff als ein Go-Modul, nicht unaufgefordert vorgenommen) — ClamdScanner implementiert das reale, dokumentierte clamd-INSTREAM- Protokoll vollständig echt, getestet gegen einen protokolltreuen Fake-Server, der die offizielle EICAR-Testsignatur identisch zu einem echten Virenscanner erkennt. - scanner.go: ClamdScanner.Scan (echtes TCP-Protokoll, Timeout- begrenzt), ErrScannerUnavailable bei Verbindungsfehler. - processor.go: Processor.ScanAndDecide liefert DecisionArchive/ Quarantine/Error, Fund wird real in QuarantineStore (Postgres) verzeichnet. Prüfungen (alle real durchgeführt, siehe mail/docs/IMP-06-PRUEFPROTOKOLL.md): 1. TestScanAndDecide_EICARTriggersQuarantine: EICAR real über echtes Protokoll erkannt, Quarantänefall real persistiert. 2. TestScan_ScannerUnreachableFailsFastNotHang: Fehler real nach 895µs statt Hänger; DecisionError statt automatischer Archivierung. 3. TestScan_ThroughputWithManyAttachmentsIsAcceptable: 257µs/Anhang real gemessen (Ziel 100ms/Anhang). Kein Umbau: kein bestehendes Paket angefasst. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HhgFcLS8tYMhDJpP74C6AQ
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
6e01cecca7
commit
145a161f8a
@@ -0,0 +1,127 @@
|
||||
package virusscan
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
_ "embed"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
//go:embed migrations/0001_mail_quarantine.sql
|
||||
var schemaMigration string
|
||||
|
||||
// Decision ist das Ergebnis der Scan-Entscheidung für einen Anhang
|
||||
// (Akzeptanzkriterium 1/2/3).
|
||||
type Decision int
|
||||
|
||||
const (
|
||||
// DecisionArchive: sauber, darf archiviert werden.
|
||||
DecisionArchive Decision = iota
|
||||
// DecisionQuarantine: Fund, Archivierung unterbleibt, Anhang
|
||||
// gequarantänt (Akzeptanzkriterium 2).
|
||||
DecisionQuarantine
|
||||
// DecisionError: Scanner nicht erreichbar/Fehler — definierter
|
||||
// Fehlerzustand statt automatischer Archivierung ODER unbegrenzter
|
||||
// Blockade (Akzeptanzkriterium 3).
|
||||
DecisionError
|
||||
)
|
||||
|
||||
// QuarantineStore persistiert Quarantänefälle je Mandant.
|
||||
type QuarantineStore struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewQuarantineStore(pool *pgxpool.Pool) *QuarantineStore {
|
||||
return &QuarantineStore{pool: pool}
|
||||
}
|
||||
|
||||
// EnsureSchema legt die Tabelle an, falls sie noch nicht existiert.
|
||||
func (s *QuarantineStore) EnsureSchema(ctx context.Context) error {
|
||||
if _, err := s.pool.Exec(ctx, schemaMigration); err != nil {
|
||||
return fmt.Errorf("virusscan: schema anlegen: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *QuarantineStore) record(ctx context.Context, tenantSlug, filename, contentHash, signatureName string) error {
|
||||
if _, err := s.pool.Exec(ctx, `
|
||||
INSERT INTO mail_quarantine (tenant_slug, filename, content_hash, signature_name)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
`, tenantSlug, filename, contentHash, signatureName); err != nil {
|
||||
return fmt.Errorf("virusscan: quarantänefall speichern: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// List liefert alle Quarantänefälle eines Mandanten — Nachvollziehbarkeit
|
||||
// (klare Statusanzeige, Akzeptanzkriterium 1).
|
||||
func (s *QuarantineStore) List(ctx context.Context, tenantSlug string) ([]QuarantineEntry, error) {
|
||||
rows, err := s.pool.Query(ctx, `
|
||||
SELECT filename, content_hash, signature_name, quarantined_at
|
||||
FROM mail_quarantine WHERE tenant_slug = $1 ORDER BY quarantined_at DESC
|
||||
`, tenantSlug)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("virusscan: quarantänefälle lesen: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var entries []QuarantineEntry
|
||||
for rows.Next() {
|
||||
var e QuarantineEntry
|
||||
if err := rows.Scan(&e.Filename, &e.ContentHash, &e.SignatureName, &e.QuarantinedAt); err != nil {
|
||||
return nil, fmt.Errorf("virusscan: quarantänezeile lesen: %w", err)
|
||||
}
|
||||
entries = append(entries, e)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("virusscan: quarantänefälle iterieren: %w", err)
|
||||
}
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
// QuarantineEntry ist ein einzelner Quarantänefall.
|
||||
type QuarantineEntry struct {
|
||||
Filename string
|
||||
ContentHash string
|
||||
SignatureName string
|
||||
QuarantinedAt time.Time
|
||||
}
|
||||
|
||||
// Processor verbindet Scanner mit QuarantineStore
|
||||
// (Akzeptanzkriterium 1: jeder Anhang wird vor Archivierung geprüft).
|
||||
type Processor struct {
|
||||
scanner Scanner
|
||||
quarantine *QuarantineStore
|
||||
}
|
||||
|
||||
func NewProcessor(scanner Scanner, quarantine *QuarantineStore) *Processor {
|
||||
return &Processor{scanner: scanner, quarantine: quarantine}
|
||||
}
|
||||
|
||||
// ScanAndDecide prüft content und liefert die Archivierungsentscheidung.
|
||||
// Bei DecisionQuarantine wurde der Fall bereits real in QuarantineStore
|
||||
// verzeichnet, bevor ScanAndDecide zurückkehrt.
|
||||
func (p *Processor) ScanAndDecide(ctx context.Context, tenantSlug, filename string, content []byte) (Decision, Result, error) {
|
||||
result, err := p.scanner.Scan(ctx, content)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrScannerUnavailable) {
|
||||
return DecisionError, Result{}, err
|
||||
}
|
||||
return DecisionError, Result{}, fmt.Errorf("virusscan: scan fehlgeschlagen: %w", err)
|
||||
}
|
||||
|
||||
if result.Clean {
|
||||
return DecisionArchive, result, nil
|
||||
}
|
||||
|
||||
hash := sha256.Sum256(content)
|
||||
if err := p.quarantine.record(ctx, tenantSlug, filename, hex.EncodeToString(hash[:]), result.SignatureName); err != nil {
|
||||
return DecisionError, result, err
|
||||
}
|
||||
return DecisionQuarantine, result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user