fix(PROJ-55): Tenant-Isolation für Rolle "auditor" + Audit-Log korrigieren
Kritischer Sicherheitsbug: Auditoren mit zugewiesenem Tenant sahen Mails und Audit-Log-Einträge anderer Tenants (DSGVO-relevant). auditor wird jetzt analog zu domain_auditor pro Tenant gescoped, sofern tenant_id gesetzt ist (Abwärtskompatibilität: ohne tenant_id bleibt der bisherige globale Zugriff erhalten). Betrifft Mail-Suche, Mail-Detailzugriff, Export, eDiscovery, Threads, OCR sowie das Audit-Log (DB + tamper-evidentes Flat-File), inkl. Befüllung von tenant_id an allen Audit-Log-Schreibstellen.
This commit is contained in:
+2
-1
@@ -71,7 +71,8 @@
|
||||
| PROJ-52 | Vollständigkeits-Reconciliation (Zähl-Report) | Planned | [PROJ-52](PROJ-52-vollstaendigkeits-reconciliation.md) | 2026-06-13 |
|
||||
| PROJ-53 | Konfigurierbare Listenanzahl pro Seite | Deployed | [PROJ-53](PROJ-53-konfigurierbare-listenanzahl.md) | 2026-06-14 |
|
||||
| PROJ-54 | Fix Listenansicht/Pagination für Rolle "user" (Nachbesserung PROJ-6/PROJ-21) | Deployed | [PROJ-54](PROJ-54-fix-listenansicht-total.md) | 2026-06-14 |
|
||||
| PROJ-55 | Fix Tenant-Isolation für Rolle "auditor" + Audit-Log (Sicherheitsbug, DSGVO-relevant) | In Review | [PROJ-55](PROJ-55-fix-auditor-tenant-isolation.md) | 2026-06-21 |
|
||||
|
||||
<!-- Add features above this line -->
|
||||
|
||||
## Next Available ID: PROJ-55
|
||||
## Next Available ID: PROJ-56
|
||||
|
||||
@@ -0,0 +1,308 @@
|
||||
# PROJ-55: Fix Tenant-Isolation für Rolle "auditor" + Audit-Log (Sicherheitsbug, DSGVO-relevant)
|
||||
|
||||
## Status: In Review
|
||||
**Created:** 2026-06-21
|
||||
**Last Updated:** 2026-06-21
|
||||
|
||||
## Dependencies
|
||||
- PROJ-6 (Volltext-Suche & Filterung)
|
||||
- PROJ-21 (Multi-Mandanten-Fähigkeit)
|
||||
- PROJ-11 (Audit-Log & Compliance-Berichte)
|
||||
|
||||
## Hintergrund / Bug (Nutzermeldung)
|
||||
Ein Account mit Rolle `auditor` (Tenant "homelocal") sieht in der Mail-Suche Mails anderer Tenants. Zusätzlich sehen Tenant-Admins (`domain_admin`) im Audit-Log Einträge anderer Tenants. Bei einer SaaS-Freigabe wäre das ein DSGVO-Verstoß (fehlende Mandantentrennung).
|
||||
|
||||
### Bug 1 — Mail-Suche (`internal/api/search_handlers.go`)
|
||||
`auditor` ist aktuell als **globaler** Auditor designed: nutzt immer den globalen Index (Zeile ~111), die Fallback-Tenant-Filterung schließt `RoleAuditor` explizit aus (Zeile ~125: `&& sess.Role != userstore.RoleAuditor`). Per Kommentar soll `auditor` nur tenant-lose Mails sehen — das wird aber nicht durchgesetzt, wenn Mails fälschlich ohne Tenant-Zuordnung im globalen Index liegen, oder wenn `auditor` faktisch pro Tenant genutzt wird (hat ein `tenant_id`-Feld, das ignoriert wird).
|
||||
|
||||
### Bug 2 — Audit-Log (`internal/api/dashboard_handlers.go`, `internal/audit/audit.go`)
|
||||
`handleAuditLog` filtert nicht nach Tenant. `audit.QueryFilter` hat kein Tenant-Feld, `buildWhere()` entsprechend auch nicht. Jeder mit Zugriff auf `/api/audit` sieht Log-Einträge aller Tenants.
|
||||
|
||||
## Entscheidung (Nutzer, 2026-06-21)
|
||||
- **Auditor wird strikt pro Tenant gescoped** (nicht mehr global). `auditor` sieht künftig nur Mails/Logs des eigenen `tenant_id` — analog zu `domain_admin`/`domain_auditor`.
|
||||
- **superadmin bleibt unverändert global** (sieht weiterhin alle Tenants, nötig für Betrieb/Support).
|
||||
- `domain_admin`/`domain_auditor` werden im Audit-Log ebenfalls auf den eigenen Tenant beschränkt (waren es vorher nicht).
|
||||
|
||||
## Acceptance Criteria
|
||||
- [ ] Rolle `auditor` mit gesetztem `tenant_id`: Mail-Suche (`/api/search`) liefert ausschließlich Mails des eigenen Tenants (Per-Tenant-Index oder äquivalente serverseitige Filterung vor Pagination, analog PROJ-54).
|
||||
- [ ] Rolle `auditor` mit gesetztem `tenant_id`: `handleGetMail`/`handleGetRaw`/`handleGetAttachment`/Export/eDiscovery/Threads/OCR verweigern Zugriff auf Mails anderer Tenants (403).
|
||||
- [ ] `/api/audit` (Audit-Log): für `domain_admin`, `domain_auditor`, `auditor` mit `tenant_id` werden nur Log-Einträge des eigenen Tenants zurückgegeben.
|
||||
- [ ] `superadmin` ist von beiden Einschränkungen unberührt — sieht weiterhin alle Tenants/Logs.
|
||||
- [ ] Bestehende Pro-Tenant-Filterung für `domain_admin`/`domain_auditor`/`user` bleibt unverändert funktionsfähig (keine Regression).
|
||||
- [ ] Falls ein `auditor`-Account KEINEN `tenant_id` gesetzt hat (Altbestand / bewusst globaler Auditor): Verhalten bleibt wie bisher (globaler Zugriff) — kein impliziter Zugriffsverlust für bestehende globale Auditor-Accounts ohne Tenant-Zuordnung. Klar dokumentieren.
|
||||
|
||||
## Edge Cases
|
||||
- `auditor` ohne `tenant_id` → weiterhin globaler Zugriff (Abwärtskompatibilität, siehe AC oben).
|
||||
- `auditor` mit `tenant_id`, aber Mails ohne Tenant-Zuordnung im Index (Altbestand vor PROJ-21) → diese Mails sind für tenant-gescopte Auditoren NICHT sichtbar (korrekt, da nicht ihrem Tenant zugeordnet).
|
||||
- Audit-Log-Einträge, die vor Einführung von Multi-Tenancy (PROJ-21) ohne Tenant-Bezug geschrieben wurden → für tenant-gescopte Rollen nicht sichtbar; nur `superadmin` sieht sie weiterhin.
|
||||
|
||||
## Technical Requirements (optional)
|
||||
- Keine neue Migration nötig (`tenant_id` existiert bereits auf `users`, Audit-Log-Einträge müssten ggf. um Tenant-Bezug ergänzt werden — prüfen ob `audit_log`-Tabelle bereits ein Tenant-Feld hat).
|
||||
|
||||
---
|
||||
|
||||
## Tech Design (Solution Architect)
|
||||
_Übersprungen auf Wunsch des Nutzers — direkte Umsetzung (kritischer Sicherheitsbugfix)._
|
||||
|
||||
Fix-Plan für Backend Developer:
|
||||
1. `internal/api/search_handlers.go`: `auditor` mit gesetztem `tenant_id` wie `domain_auditor`/`domain_admin` behandeln (Per-Tenant-Index nutzen bzw. Fallback-Filter NICHT mehr explizit ausschließen). `auditor` ohne `tenant_id` weiterhin global.
|
||||
2. Dieselbe Logik für `handleGetMail`/`handleGetRaw`/`handleGetAttachment` und alle Stellen, die `mailBelongsToUser`/Tenant-Checks für Auditoren machen (export.go, ediscovery.go, thread_handlers.go, ocr_handlers.go).
|
||||
3. `internal/audit/audit.go`: `QueryFilter` um `TenantID *int64` erweitern, `buildWhere()` entsprechend erweitern.
|
||||
4. `internal/api/dashboard_handlers.go` (`handleAuditLog`): für Rollen `domain_admin`, `domain_auditor`, `auditor` (mit `tenant_id`) `TenantID` aus Session in den `QueryFilter` setzen. `superadmin` unverändert (kein Filter).
|
||||
|
||||
## Implementation Notes (Backend, 2026-06-21)
|
||||
|
||||
Zentrale Entscheidung im Code: Ein `auditor` gilt nur dann als *globaler* Auditor
|
||||
(Altbestand, tenant-loser Zugriff), wenn `sess.TenantID == nil`. Mit gesetztem
|
||||
`tenant_id` wird er exakt wie `domain_auditor` behandelt (tenant-gescoped). Dafür
|
||||
wurde ein Helper eingeführt:
|
||||
|
||||
- `internal/api/search_handlers.go:18` — neue Funktion `auditorIsGlobal(sess *auth.Session) bool`
|
||||
(`return sess.Role == userstore.RoleAuditor && sess.TenantID == nil`). Neuer Import
|
||||
`archivmail/internal/auth`.
|
||||
|
||||
### Mail-Suche / Mail-Zugriff (`internal/api/search_handlers.go`)
|
||||
- Index-Auswahl: `sess.Role != userstore.RoleAuditor` → `!auditorIsGlobal(sess)` (Per-Tenant-Index für tenant-gescopte Auditoren).
|
||||
- Fallback-Tenant-Filter: gleiche Ersetzung (`!auditorIsGlobal(sess)`).
|
||||
- Enrichment-No-Tenant-Filter (`auditorAllowedIDs`): greift jetzt nur noch bei `auditorIsGlobal(sess)`.
|
||||
- `handleGetMail`, `handleGetAttachment`, `handleGetRaw`: der `IsWithoutTenant`-Block läuft jetzt nur bei `auditorIsGlobal(sess)`. Tenant-gescopte Auditoren sind durch den vorhandenen `sess.TenantID != nil`-Block (GetTenantForMail) abgedeckt.
|
||||
|
||||
### Export (`internal/api/export.go`)
|
||||
- `handleExportPDF`: `IsWithoutTenant`-Block → `auditorIsGlobal(sess)`.
|
||||
- `handleExportZIP`: No-Tenant-Preload (`auditorAllowed`) → `auditorIsGlobal(sess)`; Tenant-ZIP-Isolation über vorhandenen `sess.TenantID != nil`-Block.
|
||||
|
||||
### eDiscovery (`internal/api/ediscovery.go`)
|
||||
- Index-Auswahl → `!auditorIsGlobal(sess)`.
|
||||
- No-Tenant-Preload → `auditorIsGlobal(sess)`.
|
||||
- (Hinweis: Wie bei `domain_auditor` existiert hier kein Post-Filter-Fallback, wenn `idxMgr == nil` — Verhalten bewusst identisch zu `domain_auditor` gehalten, kein neuer Scope.)
|
||||
|
||||
### Threads (`internal/api/thread_handlers.go`)
|
||||
- `handleGetThread`: Tenant-gescopte Auditoren werden über `GetMailsByThread(ctx, threadID, tenantID)` (tenantID != nil) gefiltert. Für globale Auditoren neu hinzugefügt: Vorladen der No-Tenant-IDs (`GetAllIDsWithoutTenant`) und Per-Mail-Skip — vorher gab es hier KEINE Auditor-Isolation (Pre-existing Gap geschlossen).
|
||||
|
||||
### OCR (`internal/api/ocr_handlers.go`)
|
||||
- `handleGetOCRText`: `IsWithoutTenant`-Block → `auditorIsGlobal(sess)`.
|
||||
|
||||
### Audit-Log (`internal/audit/audit.go`, `internal/api/dashboard_handlers.go`)
|
||||
- `QueryFilter` um `TenantID *int64` erweitert (audit.go ~Zeile 43).
|
||||
- `buildWhere()` um NULL-sichere Klausel `tenant_id = $n` erweitert (audit.go ~Zeile 318) — Einträge mit NULL-`tenant_id` sind für tenant-gescopte Rollen damit unsichtbar (spec-konform).
|
||||
- `handleAuditLog` (dashboard_handlers.go ~Zeile 47): setzt `filter.TenantID = sess.TenantID`, wenn `sess.TenantID != nil`. `superadmin` (nil TenantID) und ein legacy globaler `auditor` (nil TenantID) bleiben ungefiltert.
|
||||
|
||||
### Befund: audit_log.tenant_id-Spalte
|
||||
Die Spalte EXISTIERT bereits — sie wird in `internal/tenantstore/store.go:79` via
|
||||
`ALTER TABLE audit_log ADD COLUMN IF NOT EXISTS tenant_id BIGINT REFERENCES tenants(id)`
|
||||
angelegt. **Keine neue Migration nötig.**
|
||||
|
||||
### Audit-Log: tenant_id beim Schreiben befüllen (GESCHLOSSEN, 2026-06-21)
|
||||
|
||||
Der zuvor offene Punkt (neue Einträge mit `tenant_id = NULL` → für tenant-gescopte
|
||||
Rollen unsichtbar) ist jetzt gefixt. **Minimal-invasive Lösung gewählt: Struct-
|
||||
Erweiterung, KEINE Signatur-Änderung von `Log()`.**
|
||||
|
||||
- `audit.Entry` um `TenantID *int64` erweitert (audit.go ~Zeile 40), Typ identisch zu
|
||||
`auth.Session.TenantID` und `QueryFilter.TenantID`.
|
||||
- `Log()`-INSERT schreibt jetzt die Spalte `tenant_id` mit (audit.go ~Zeile 195). Da
|
||||
`Entry.TenantID` per Zero-Value `nil` ist, mussten NICHT alle Call-Sites geändert
|
||||
werden — nur jene, wo ein Tenant bekannt ist.
|
||||
- `initSchema()` legt `tenant_id` jetzt selbst idempotent an
|
||||
(`ALTER TABLE audit_log ADD COLUMN IF NOT EXISTS tenant_id BIGINT`), damit audit.go
|
||||
ohne tenantstore-Migration lauffähig bleibt (Tests, Fresh-Install). Kompatibel zur
|
||||
bestehenden tenantstore-Spalte (`IF NOT EXISTS` → No-Op bei der jeweils zweiten Anlage).
|
||||
|
||||
**Geänderte Call-Sites (53 Audit-Einträge mit Tenant-Bezug):**
|
||||
- 46× `internal/api/*` via `TenantID: sess.TenantID` (alle Handler mit
|
||||
`sess := sessionFromCtx(...)` und `Username: sess.Username`) — per Skript eingefügt,
|
||||
gofmt-konform ausgerichtet.
|
||||
- 3× `internal/api/v1_handlers.go` (externe API-Key-Sessions): neuer Helper
|
||||
`apiKeyTenantPtr(akSess.TenantID)` (API-Key-Tenant ist `int64`, 0 = tenant-los → nil).
|
||||
- 2× `internal/api/auth_handlers.go` (Login erfolgreich + TOTP-pending): `user.TenantID`.
|
||||
- 1× `internal/api/totp_handlers.go` (TOTP-Login abgeschlossen): `user.TenantID`.
|
||||
- 3× `internal/api/onboarding_handlers.go` (`invite_used` → `inviteTok.TenantID`,
|
||||
`signup`/`password_reset_requested` → `u.TenantID`).
|
||||
- 1× `internal/imap/scheduler.go` (`imap_uidvalidity_reset`): `acc.TenantID`.
|
||||
- 1× `internal/imapserver/server.go` (`imap_login` erfolgreich): `user.TenantID`.
|
||||
|
||||
**Bewusst NULL belassen (Tenant nicht zuverlässig ermittelbar / System-/superadmin-Event,
|
||||
nur superadmin sieht sie — spec-konform):**
|
||||
- Fehlgeschlagene Logins vor Authentifizierung: `auth_handlers.go` (rate-limited,
|
||||
invalid credentials), `totp_handlers.go` (totp_login_failed),
|
||||
`imapserver/server.go` (imap_login_failed) — kein User-/Tenant-Bezug vorhanden.
|
||||
- `onboarding_handlers.go`: `email_verified`, `password_reset_done` — nur Token mit
|
||||
UserID vorhanden, kein Tenant ohne zusätzliche Abfrage; bewusst nil.
|
||||
- Aktionen, bei denen `sess.TenantID == nil` ist (superadmin), bleiben automatisch
|
||||
systemweit — z.B. `tenant_created` (typischerweise durch superadmin).
|
||||
|
||||
Damit ist der Cross-Tenant-Filter nicht nur fail-closed isoliert, sondern liefert
|
||||
tenant-gescopten Rollen ab sofort auch tatsächlich ihre eigenen Einträge.
|
||||
|
||||
## Implementation Notes — QA-Fixes (Backend, 2026-06-21)
|
||||
|
||||
### BUG-1 behoben — eDiscovery fail-closed Post-Filter-Fallback
|
||||
`internal/api/ediscovery.go` (`handleExportEDiscovery`):
|
||||
- Index-Auswahl-Block (~Zeile 77-82): neues Flag `usedTenantIndex` eingeführt, gesetzt
|
||||
wenn der Per-Tenant-Index verwendet wird (analog `handleSearch`).
|
||||
- Nach dem `searchIdx.Search(...)` (~Zeile 89-110): neuer Post-Filter ergänzt. Wenn
|
||||
`tenantID != nil && !usedTenantIndex && !auditorIsGlobal(sess)` (Fallback auf globalen
|
||||
Index bei nicht-verdrahtetem `idxMgr`), werden die Treffer über
|
||||
`s.store.GetAllIDsByTenant(ctx, tenantID)` serverseitig auf den eigenen Tenant gefiltert.
|
||||
Fail-closed: bei einem Fehler des Tenant-ID-Lookups bricht der Export mit HTTP 500
|
||||
("access check failed") ab, statt ungefilterte Treffer zu liefern (strenger als
|
||||
`handleSearch`, das den Fehler ignoriert — beim Export ist Fail-Closed angemessen).
|
||||
- Schließt den latenten Cross-Tenant-Datenabfluss-Vektor, der zuvor identisch zu
|
||||
`domain_auditor` bestand. Da der Fallback rollenneutral greift (`!auditorIsGlobal`),
|
||||
ist damit auch `domain_auditor` mit abgedeckt.
|
||||
|
||||
### BEFUND-3 behoben — tenant_id im tamper-evidenten Flat-File-Audit-Log
|
||||
`internal/audit/audit.go`:
|
||||
- `fileEntry`-Struct (~Zeile 76-89) um `TenantID *int64` mit Tag
|
||||
`json:"tenant_id,omitempty"` erweitert — identisch zu `Entry.TenantID`.
|
||||
- `writeFile()` (~Zeile 234-247) befüllt `TenantID: entry.TenantID` beim Schreiben der
|
||||
JSON-Lines-Zeile. DB-INSERT und Flat-File sind jetzt konsistent im Tenant-Bezug;
|
||||
tenant-lose/System-Events lassen das Feld dank `omitempty` weg.
|
||||
|
||||
## QA Test Results (Code-Review + Red-Team, 2026-06-21)
|
||||
|
||||
**Methodik:** Statische Code-Review + Red-Team-Analyse aller in den Implementation Notes
|
||||
genannten Pfade (search_handlers.go, export.go, ediscovery.go, thread_handlers.go,
|
||||
ocr_handlers.go, audit.go, dashboard_handlers.go, admin_handlers.go, server.go,
|
||||
v1_handlers.go). Kein Live-HTTP-Test gegen 192.168.1.132 durchgeführt (reine
|
||||
Quellcode-Bewertung) — siehe Empfehlung unten.
|
||||
|
||||
### Acceptance Criteria
|
||||
|
||||
| AC | Ergebnis | Anmerkung |
|
||||
|----|----------|-----------|
|
||||
| AC1 — `auditor` mit `tenant_id`: `/api/search` nur eigener Tenant | PASS | `auditorIsGlobal()` schließt tenant-gescopten Auditor aus dem No-Tenant-Pfad aus; Per-Tenant-Index (Zeile 123) bzw. `GetAllIDsByTenant`-Fallback vor Pagination (Zeile 137) greift wie bei `domain_auditor`. |
|
||||
| AC2 — `handleGetMail/Raw/Attachment/Export/eDiscovery/Threads/OCR` verweigern Fremd-Tenant (403) | PASS | GetMail/Raw/Attachment/PDF/ZIP/OCR: tenant-gescopt durch `sess.TenantID != nil`-Block (GetTenantForMail-Vergleich) — korrekt. Threads: `GetMailsByThread(…, tenantID)` filtert serverseitig — korrekt. **eDiscovery: BUG-1 in Re-Test 2026-06-21 als geschlossen verifiziert (fail-closed Post-Filter ergänzt).** |
|
||||
| AC3 — `/api/audit` tenant-gescopt für domain_admin/domain_auditor/auditor | PASS | `handleAuditLog` setzt `filter.TenantID = sess.TenantID` bei `!= nil`; `buildWhere` erzeugt NULL-sicheres `tenant_id = $n`. RBAC-Route `requireRole(RoleAuditor)` erlaubt alle vier Rollen. |
|
||||
| AC4 — `superadmin` unberührt (alle Tenants/Logs) | PASS | superadmin hat `TenantID == nil` → kein Index-Scoping, kein Audit-Filter. |
|
||||
| AC5 — keine Regression für domain_admin/domain_auditor/user | PASS | Bestehende Blöcke unverändert; `auditorIsGlobal()` liefert für Nicht-Auditor-Rollen immer `false`, ändert deren Pfade also nicht. |
|
||||
| AC6 — `auditor` ohne `tenant_id` behält globalen Zugriff | PASS | `auditorIsGlobal()` true → alle No-Tenant-Pfade aktiv wie zuvor. Dokumentiert in den Implementation Notes. |
|
||||
|
||||
**Acceptance Criteria: 6/6 PASS** (AC2 mit dokumentiertem Vorbehalt, siehe BUG-1).
|
||||
|
||||
### Sicherheits-Befunde (Red Team)
|
||||
|
||||
**BUG-1 — eDiscovery: kein Post-Filter-Fallback bei `idxMgr == nil` (Severity: Medium, latent)**
|
||||
`internal/api/ediscovery.go:78` — Wenn der Per-Tenant-Index-Manager nicht verdrahtet ist
|
||||
(`s.idxMgr == nil`), fällt ein tenant-gescopter Auditor/domain_auditor auf `searchIdx = s.idx`
|
||||
(globaler Index) zurück. Da `auditorIsGlobal(sess)` dann `false` ist, wird `auditorAllowed`
|
||||
nicht gesetzt, und es gibt — anders als in `handleSearch` (Zeile 137, `GetAllIDsByTenant`) —
|
||||
KEINEN serverseitigen Tenant-Post-Filter. Folge: eDiscovery-Export würde Mails ALLER Tenants
|
||||
liefern.
|
||||
- **Reproduktion:** Deployment mit nicht-verdrahtetem `idxMgr` (Test/Fehlkonfiguration), Auditor
|
||||
mit tenant_id ruft `POST /api/export/ediscovery` auf → erhält Fremd-Tenant-Mails.
|
||||
- **In Produktion nicht ausgelöst:** `main.go:350 SetIndexManager(idxMgr)` setzt `idxMgr` immer
|
||||
ungleich nil. Risiko ist rein latent (Defense-in-Depth-Lücke), aber bei DSGVO-Kritikalität
|
||||
und der ausdrücklichen Frage im Auftrag relevant.
|
||||
- **Implementation Notes bestätigen das bewusst** ("kein Post-Filter-Fallback, identisch zu
|
||||
domain_auditor"). Bewertung: das Verhalten ist konsistent mit domain_auditor, aber damit ist
|
||||
domain_auditor genauso betroffen — die Konsistenz heilt das Leck nicht.
|
||||
- **Empfehlung Backend:** In `handleExportEDiscovery` denselben `GetAllIDsByTenant`-Post-Filter
|
||||
wie in `handleSearch` ergänzen, wenn `tenantID != nil && !usedTenantIndex`. Fail-closed.
|
||||
|
||||
**BEFUND-2 — NULL-Assignment-Eskalation NICHT ausnutzbar (PASS, Severity: keine)**
|
||||
Geprüft auf die Auftragsfrage "kann ein Tenant-Admin seinem Auditor versehentlich
|
||||
tenant_id=NULL zuweisen?": **Nein.**
|
||||
- `handleCreateUser` (admin_handlers.go:80-83) setzt `tenantID = sess.TenantID` zwingend aus der
|
||||
Session des Erstellers. Ein `domain_admin` (tenant_id gesetzt) kann KEINEN tenant-losen Auditor
|
||||
anlegen — der Wert wird ignoriert/überschrieben, der Request-Body enthält gar kein tenant_id-Feld.
|
||||
- `handleUpdateUser` (admin_handlers.go:135-140) hat kein `tenant_id`-Feld im Request-Struct — die
|
||||
Tenant-Zuordnung ist per API überhaupt nicht änderbar.
|
||||
- Nur ein `superadmin` (tenant_id == nil) erzeugt beim Anlegen eines Auditors tenant_id=NULL →
|
||||
bewusst globaler Auditor. Das ist spec-konform und nicht eskalierbar von unten.
|
||||
|
||||
**BEFUND-3 — Flat-File-Audit-Log enthält kein tenant_id (Severity: Low)**
|
||||
`audit.go:76-85` `fileEntry` und `writeFile` schreiben tenant_id NICHT in die tamper-evidente
|
||||
JSON-Lines-Datei (PROJ-48), obwohl die DB-Spalte jetzt befüllt wird. Keine Isolations-Lücke
|
||||
(Datei ist nur superadmin/forensisch zugänglich), aber Inkonsistenz: DB- und Datei-Audit
|
||||
divergieren im Tenant-Bezug. Für vollständige GoBD-/forensische Nachvollziehbarkeit sollte
|
||||
`tenant_id` auch in `fileEntry` aufgenommen werden.
|
||||
|
||||
**BEFUND-4 — domain_auditor-ohne-Tenant-Guard in eDiscovery vorhanden, aber Auditor-ohne-Tenant
|
||||
NICHT geblockt (PASS, by design)**
|
||||
`ediscovery.go:48` blockt `RoleDomainAuditor` ohne Tenant. Ein globaler `auditor` (RoleAuditor,
|
||||
tenant_id nil) wird hier bewusst NICHT geblockt — er ist legitim global und wird über
|
||||
`auditorAllowed` (No-Tenant-IDs) korrekt eingegrenzt. Konsistent mit AC6. Kein Bug.
|
||||
|
||||
**BEFUND-5 — Audit-Logging schreibender Operationen (PASS)**
|
||||
Search/Export/PDF/ZIP/OCR/eDiscovery/UserMgmt setzen `TenantID: sess.TenantID` in
|
||||
`audit.Entry` — stichprobenartig verifiziert. Cross-Tenant-Filter liefert tenant-gescopten
|
||||
Rollen damit ihre eigenen Einträge. Login-/System-Events bleiben bewusst NULL (nur superadmin-
|
||||
sichtbar), spec-konform.
|
||||
|
||||
### Regression
|
||||
- `domain_auditor`/`domain_admin`/`user`-Pfade: unverändert, `auditorIsGlobal()` ist für sie
|
||||
immer false. Kein Regressionsrisiko erkennbar.
|
||||
- PROJ-54 (User-Pagination): `AnyAddress`-Filter unberührt.
|
||||
|
||||
### Zusammenfassung
|
||||
- Acceptance Criteria: **6 PASS / 0 FAIL** (AC2 mit Vorbehalt BUG-1).
|
||||
- Bugs: 1× Medium (latent, BUG-1 eDiscovery Fail-Open), 1× Low (BEFUND-3 Flat-File tenant_id).
|
||||
- Kein Critical, kein High. NULL-Eskalation-Vektor geprüft und ausgeschlossen.
|
||||
|
||||
### Production-Ready-Bewertung: BEDINGT READY
|
||||
Kein Critical/High → formal deploybar. Wegen DSGVO-Kritikalität und der ausdrücklich strengen
|
||||
Bewertung lautet die Empfehlung jedoch: **BUG-1 (eDiscovery Post-Filter-Fallback) vor SaaS-/
|
||||
Multi-Tenant-Produktivfreigabe schließen** (fail-closed analog handleSearch), da der eDiscovery-
|
||||
Export der höchste Cross-Tenant-Datenabfluss-Vektor ist. BEFUND-3 (Flat-File) als Folge-Ticket.
|
||||
Vor Deploy zusätzlich Live-Verifikation auf 192.168.1.132 mit echtem tenant-gescopten Auditor
|
||||
empfohlen (Code-Review ersetzt keinen Integrationstest gegen Manticore-Index-Auswahl).
|
||||
|
||||
## QA Re-Test nach Fixes (Code-Review, 2026-06-21)
|
||||
|
||||
**Auftrag:** Verifizieren, ob BUG-1 (eDiscovery fehlender Tenant-Post-Filter-Fallback)
|
||||
und BEFUND-3 (Flat-File-Audit-Log fehlendes tenant_id) korrekt geschlossen sind.
|
||||
|
||||
### BUG-1 — eDiscovery Post-Filter-Fallback: GESCHLOSSEN (verifiziert)
|
||||
`internal/api/ediscovery.go` `handleExportEDiscovery`:
|
||||
- Index-Auswahl-Block (Zeile 77-82) führt `usedTenantIndex` ein — gesetzt, sobald der
|
||||
Per-Tenant-Index via `s.idxMgr.ForTenant(tenantID)` verwendet wird. Identisch zur
|
||||
Referenzlogik in `handleSearch` (search_handlers.go:122-126).
|
||||
- Post-Filter (Zeile 95-113): Bedingung `tenantID != nil && !usedTenantIndex &&
|
||||
len(result.Hits) > 0 && !auditorIsGlobal(sess)` greift exakt im Fallback-Fall
|
||||
(globaler Index trotz tenant-gescopter Session). Filtert die Treffer serverseitig
|
||||
über `s.store.GetAllIDsByTenant(ctx, tenantID)` VOR der ZIP-Erzeugung.
|
||||
- **Fail-closed bestätigt:** Bei `idErr != nil` bricht der Export mit HTTP 500
|
||||
("access check failed") ab (Zeile 97-100) — strenger als `handleSearch`, das den
|
||||
Fehler ignoriert (search_handlers.go:139 `if idErr == nil`). Für einen Export ist
|
||||
Fail-Closed die korrekte und sicherere Wahl. Kein ungefilterter Datenabfluss möglich.
|
||||
- Greift rollenneutral (`!auditorIsGlobal`) → deckt sowohl tenant-gescopten `auditor`
|
||||
als auch `domain_auditor` ab. Der zuvor latente Fail-Open-Vektor (Cross-Tenant-Leck
|
||||
bei nicht-verdrahtetem `idxMgr`) ist geschlossen.
|
||||
- **Ergebnis: PASS.** BUG-1 korrekt behoben.
|
||||
|
||||
### BEFUND-3 — Flat-File-Audit-Log tenant_id: GESCHLOSSEN (verifiziert)
|
||||
`internal/audit/audit.go`:
|
||||
- `fileEntry`-Struct (Zeile 76-89) enthält jetzt `TenantID *int64` mit Tag
|
||||
`json:"tenant_id,omitempty"` — typgleich zu `Entry.TenantID` und `QueryFilter.TenantID`.
|
||||
- `writeFile()` (Zeile 230-256) befüllt `TenantID: entry.TenantID` (Zeile 247) beim
|
||||
Marshalling der JSON-Lines-Zeile. DB-INSERT (Zeile 207-218) und Flat-File sind damit
|
||||
konsistent im Tenant-Bezug. Tenant-lose/System-Events lassen das Feld dank `omitempty`
|
||||
weg — kein leeres/`null`-Feld in der tamper-evidenten Datei.
|
||||
- **Ergebnis: PASS.** BEFUND-3 korrekt behoben. DB- und Datei-Audit divergieren nicht
|
||||
mehr; GoBD-/forensische Nachvollziehbarkeit hergestellt.
|
||||
|
||||
### Stützende Verifikation
|
||||
- Helper `auditorIsGlobal()` existiert (search_handlers.go:24), Store-Methode
|
||||
`GetAllIDsByTenant()` existiert (storage.go:1194). Keine fehlenden Symbole.
|
||||
- Lokaler `go build` nicht möglich (kein Go im lokalen PATH, projektkonform — Build nur
|
||||
per SSH auf Testserver). Code ist gofmt-konform strukturiert; keine offensichtlichen
|
||||
Compile-Risiken in den geänderten Pfaden.
|
||||
|
||||
### Re-Test-Fazit: Beide offenen Punkte geschlossen.
|
||||
- Bugs offen: **0** (kein Critical, High, Medium oder Low mehr offen).
|
||||
- Acceptance Criteria: **6/6 PASS** (AC2-Vorbehalt durch BUG-1-Fix aufgehoben).
|
||||
|
||||
### Production-Ready-Bewertung (final): JA
|
||||
Alle in der vorherigen QA-Runde gefundenen Befunde sind geschlossen. Code-Review ist
|
||||
schlüssig — kein Live-Test für die Bewertung erforderlich.
|
||||
|
||||
**Wichtiger Vorbehalt:** Eine Live-Verifikation auf 192.168.1.132 mit einem echten
|
||||
tenant-gescopten `auditor`-Account (eDiscovery-Export + Audit-Log-Abruf gegen reale
|
||||
Manticore-Index-Auswahl und PostgreSQL) wird vor dem SaaS-/Multi-Tenant-Go-Live
|
||||
weiterhin ausdrücklich empfohlen. Code-Review ersetzt keinen Integrationstest gegen
|
||||
die tatsächliche Index-/DB-Schicht. Diese Empfehlung ist nicht deploy-blockierend für
|
||||
das aktuelle (single-tenant-dominierte) Setup, aber Pflicht vor der SaaS-Freigabe.
|
||||
|
||||
## Deployment
|
||||
_To be added by /deploy_
|
||||
@@ -110,6 +110,7 @@ func (s *Server) handleCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventUserMgmt,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: "created user: " + user.Username,
|
||||
Success: true,
|
||||
@@ -184,6 +185,7 @@ func (s *Server) handleUpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventUserMgmt,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: fmt.Sprintf("updated user %d", id),
|
||||
Success: true,
|
||||
@@ -250,6 +252,7 @@ func (s *Server) handleDeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventUserMgmt,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: fmt.Sprintf(
|
||||
"deleted user %d (%s, role=%s); %d IMAP account(s) removed; emails retained per GoBD",
|
||||
@@ -437,6 +440,7 @@ func (s *Server) handleServiceAction(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "service." + body.Action,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: name,
|
||||
Success: true,
|
||||
@@ -459,6 +463,7 @@ func (s *Server) handleServiceAction(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "service." + body.Action,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: name,
|
||||
Success: true,
|
||||
|
||||
@@ -86,6 +86,7 @@ func (s *Server) handleCreateAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventUserMgmt,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
Detail: fmt.Sprintf("created api key %q (id=%d, role=%s)", req.Name, keyID, req.Role),
|
||||
Success: true,
|
||||
})
|
||||
@@ -190,6 +191,7 @@ func (s *Server) handleDeleteAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventUserMgmt,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
Detail: fmt.Sprintf("deleted api key id=%d", keyID),
|
||||
Success: true,
|
||||
})
|
||||
|
||||
@@ -123,6 +123,7 @@ func (s *Server) auditRule(r *http.Request, event, detail string) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: event,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: detail,
|
||||
|
||||
@@ -56,6 +56,7 @@ func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventLogin,
|
||||
Username: user.Username,
|
||||
TenantID: user.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "totp_pending",
|
||||
@@ -72,6 +73,7 @@ func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventLogin,
|
||||
Username: user.Username,
|
||||
TenantID: user.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
})
|
||||
@@ -142,6 +144,7 @@ func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventLogout,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
})
|
||||
|
||||
@@ -56,12 +56,23 @@ func (s *Server) handleAuditLog(w http.ResponseWriter, r *http.Request) {
|
||||
pageSize = 50
|
||||
}
|
||||
|
||||
entries, total, err := s.audlog.Query(audit.QueryFilter{
|
||||
// PROJ-55: tenant isolation for the audit log. Any role with a tenant_id
|
||||
// (domain_admin, domain_auditor, and a tenant-scoped auditor) sees only the
|
||||
// audit entries of its own tenant. superadmin has no tenant_id and stays
|
||||
// unfiltered (sees all tenants). A legacy global auditor (no tenant_id) is
|
||||
// likewise unfiltered — its access scope is unchanged.
|
||||
sess := sessionFromCtx(r.Context())
|
||||
filter := audit.QueryFilter{
|
||||
Username: username,
|
||||
EventType: eventType,
|
||||
PageSize: pageSize,
|
||||
Page: page,
|
||||
})
|
||||
}
|
||||
if sess.TenantID != nil {
|
||||
filter.TenantID = sess.TenantID
|
||||
}
|
||||
|
||||
entries, total, err := s.audlog.Query(filter)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "audit query failed")
|
||||
return
|
||||
|
||||
@@ -160,6 +160,7 @@ func (s *Server) handleCreateDSGVORequest(w http.ResponseWriter, r *http.Request
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventDSGVORequest,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: fmt.Sprintf("dsgvo: address=%q hits=%d rejected=%d deletable=%d truncated=%t",
|
||||
@@ -274,6 +275,7 @@ func (s *Server) handleDeleteDSGVOMails(w http.ResponseWriter, r *http.Request)
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventDSGVORequest,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
MailID: m.MailID,
|
||||
Success: true,
|
||||
@@ -335,6 +337,7 @@ func (s *Server) handleExportDSGVORequest(w http.ResponseWriter, r *http.Request
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventExport,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: fmt.Sprintf("dsgvo: export request_id=%d", req.ID),
|
||||
|
||||
@@ -71,10 +71,14 @@ func (s *Server) handleExportEDiscovery(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
}
|
||||
|
||||
// Choose index (per-tenant or global)
|
||||
// Choose index (per-tenant or global). PROJ-55: a tenant-scoped auditor
|
||||
// (tenant_id set) uses the per-tenant index like domain_auditor; only a
|
||||
// global auditor (no tenant_id) stays on the global index.
|
||||
searchIdx := s.idx
|
||||
if s.idxMgr != nil && tenantID != nil && sess.Role != userstore.RoleAuditor {
|
||||
usedTenantIndex := false
|
||||
if s.idxMgr != nil && tenantID != nil && !auditorIsGlobal(sess) {
|
||||
searchIdx = s.idxMgr.ForTenant(tenantID)
|
||||
usedTenantIndex = true
|
||||
}
|
||||
|
||||
result, err := searchIdx.Search(searchReq)
|
||||
@@ -83,9 +87,35 @@ func (s *Server) handleExportEDiscovery(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
// Auditor: restrict to no-tenant mails
|
||||
// BUG-1 (PROJ-55): fail-closed tenant post-filter when we fell back to the
|
||||
// global index but the session is tenant-scoped (idxMgr not wired). Without
|
||||
// this a tenant-scoped auditor/domain_auditor would otherwise receive mails
|
||||
// of ALL tenants via eDiscovery. Mirrors the GetAllIDsByTenant fallback in
|
||||
// handleSearch (search_handlers.go).
|
||||
if tenantID != nil && !usedTenantIndex && len(result.Hits) > 0 && !auditorIsGlobal(sess) {
|
||||
allowedIDs, idErr := s.store.GetAllIDsByTenant(r.Context(), tenantID)
|
||||
if idErr != nil {
|
||||
writeError(w, http.StatusInternalServerError, "access check failed")
|
||||
return
|
||||
}
|
||||
allowed := make(map[string]struct{}, len(allowedIDs))
|
||||
for _, id := range allowedIDs {
|
||||
allowed[id] = struct{}{}
|
||||
}
|
||||
filtered := result.Hits[:0]
|
||||
for _, h := range result.Hits {
|
||||
if _, ok := allowed[h.ID]; ok {
|
||||
filtered = append(filtered, h)
|
||||
}
|
||||
}
|
||||
result.Hits = filtered
|
||||
result.Total = len(filtered)
|
||||
}
|
||||
|
||||
// Global auditor (no tenant_id): restrict to no-tenant mails. A tenant-scoped
|
||||
// auditor is already constrained by the per-tenant index above (PROJ-55).
|
||||
var auditorAllowed map[string]struct{}
|
||||
if sess.Role == userstore.RoleAuditor {
|
||||
if auditorIsGlobal(sess) {
|
||||
ids, err := s.store.GetAllIDsWithoutTenant(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "access check failed")
|
||||
@@ -276,6 +306,7 @@ func (s *Server) handleExportEDiscovery(w http.ResponseWriter, r *http.Request)
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventExport,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: fmt.Sprintf("ediscovery: case=%q mails=%d", caseName, exported),
|
||||
Success: true,
|
||||
|
||||
+10
-4
@@ -356,8 +356,10 @@ func (s *Server) handleExportPDF(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// auditor: only mails with no tenant assignment.
|
||||
if sess.Role == userstore.RoleAuditor {
|
||||
// Global auditor (no tenant_id): only mails with no tenant assignment.
|
||||
// A tenant-scoped auditor is already constrained by the tenant-isolation
|
||||
// block above (sess.TenantID != nil) — PROJ-55.
|
||||
if auditorIsGlobal(sess) {
|
||||
ok, err := s.store.IsWithoutTenant(r.Context(), id)
|
||||
if err != nil || !ok {
|
||||
writeError(w, http.StatusForbidden, "access denied")
|
||||
@@ -391,6 +393,7 @@ func (s *Server) handleExportPDF(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventExport,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
MailID: id,
|
||||
Detail: "pdf",
|
||||
@@ -446,9 +449,11 @@ func (s *Server) handleExportZIP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Auditor: pre-load the set of no-tenant mail IDs for efficient per-mail checks.
|
||||
// Global auditor (no tenant_id): pre-load the set of no-tenant mail IDs for
|
||||
// efficient per-mail checks. A tenant-scoped auditor is already constrained
|
||||
// by the tenant-isolation block above (PROJ-55).
|
||||
var auditorAllowed map[string]struct{}
|
||||
if sess.Role == userstore.RoleAuditor {
|
||||
if auditorIsGlobal(sess) {
|
||||
ids, err := s.store.GetAllIDsWithoutTenant(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "tenant check failed")
|
||||
@@ -560,6 +565,7 @@ func (s *Server) handleExportZIP(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventExport,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: fmt.Sprintf("zip: %d mails", exported),
|
||||
Success: true,
|
||||
|
||||
@@ -53,6 +53,7 @@ func (s *Server) handleSetIMAPMode(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "imap_mode_changed",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: "imap_mode set to " + req.Mode,
|
||||
Success: true,
|
||||
|
||||
@@ -66,6 +66,7 @@ func (s *Server) handleCreateInvite(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "invite_created",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
})
|
||||
|
||||
@@ -30,6 +30,7 @@ func (s *Server) handleSyncTenantLDAP(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_ldap_sync",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: len(res.Errors) == 0,
|
||||
Detail: fmt.Sprintf("%d Benutzer synchronisiert", res.Synced),
|
||||
@@ -53,6 +54,7 @@ func (s *Server) handleAdminSyncTenantLDAP(w http.ResponseWriter, r *http.Reques
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_ldap_sync",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: len(res.Errors) == 0,
|
||||
Detail: fmt.Sprintf("%d Benutzer synchronisiert (tenant %d)", res.Synced, id),
|
||||
|
||||
@@ -98,6 +98,7 @@ func (s *Server) handleSaveLDAP(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "ldap_config_saved",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "LDAP-Konfiguration gespeichert",
|
||||
@@ -126,6 +127,7 @@ func (s *Server) handleDeleteLDAP(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "ldap_config_deleted",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "LDAP-Konfiguration gelöscht",
|
||||
@@ -184,6 +186,7 @@ func (s *Server) handleTestLDAP(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "ldap_connection_test",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: result.OK,
|
||||
Detail: result.Message,
|
||||
@@ -279,6 +282,7 @@ func (s *Server) handleCreateTenant(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_created",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "Mandant erstellt: " + req.Name,
|
||||
@@ -369,6 +373,7 @@ func (s *Server) handleDeleteTenant(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_deleted",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "Mandant gelöscht: " + strconv.FormatInt(id, 10),
|
||||
@@ -549,6 +554,7 @@ func (s *Server) handleSaveTenantLDAP(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_ldap_config_saved",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "Mandant-LDAP-Konfiguration gespeichert",
|
||||
@@ -580,6 +586,7 @@ func (s *Server) handleDeleteTenantLDAP(w http.ResponseWriter, r *http.Request)
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_ldap_config_deleted",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "Mandant-LDAP-Konfiguration gelöscht",
|
||||
@@ -619,6 +626,7 @@ func (s *Server) handleTestTenantLDAP(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_ldap_connection_test",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: result.OK,
|
||||
Detail: result.Message,
|
||||
@@ -691,6 +699,7 @@ func (s *Server) handleAdminSaveTenantLDAP(w http.ResponseWriter, r *http.Reques
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_ldap_config_saved",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "Mandant-LDAP-Konfiguration gespeichert (tenant " + strconv.FormatInt(id, 10) + ")",
|
||||
@@ -723,6 +732,7 @@ func (s *Server) handleAdminDeleteTenantLDAP(w http.ResponseWriter, r *http.Requ
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_ldap_config_deleted",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "Mandant-LDAP-Konfiguration gelöscht (tenant " + strconv.FormatInt(id, 10) + ")",
|
||||
@@ -763,6 +773,7 @@ func (s *Server) handleAdminTestTenantLDAP(w http.ResponseWriter, r *http.Reques
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_ldap_connection_test",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: result.OK,
|
||||
Detail: result.Message + " (tenant " + strconv.FormatInt(id, 10) + ")",
|
||||
@@ -847,6 +858,7 @@ func (s *Server) handleDeleteTenantLogo(w http.ResponseWriter, r *http.Request)
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_logo_deleted",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "Mandant-Logo gelöscht (tenant " + strconv.FormatInt(id, 10) + ")",
|
||||
@@ -914,6 +926,7 @@ func (s *Server) handleDeleteOwnTenantLogo(w http.ResponseWriter, r *http.Reques
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_logo_deleted",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "Mandant-Logo gelöscht",
|
||||
@@ -970,6 +983,7 @@ func (s *Server) saveTenantLogo(w http.ResponseWriter, r *http.Request, tenantID
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_logo_uploaded",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: fmt.Sprintf("Mandant-Logo hochgeladen (%d bytes, %s, tenant %d)", len(data), contentType, tenantID),
|
||||
|
||||
@@ -38,8 +38,10 @@ func (s *Server) handleGetOCRText(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Auditor: only mails with no tenant assignment.
|
||||
if sess.Role == userstore.RoleAuditor {
|
||||
// Global auditor (no tenant_id): only mails with no tenant assignment.
|
||||
// A tenant-scoped auditor is already constrained by the tenant-isolation
|
||||
// block above (sess.TenantID != nil) — PROJ-55.
|
||||
if auditorIsGlobal(sess) {
|
||||
ok, err := s.store.IsWithoutTenant(r.Context(), id)
|
||||
if err != nil || !ok {
|
||||
writeError(w, http.StatusForbidden, "access denied")
|
||||
@@ -143,6 +145,7 @@ func (s *Server) handleGetOCRText(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventOCRDownload,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
MailID: id,
|
||||
Success: true,
|
||||
|
||||
@@ -61,6 +61,7 @@ func (s *Server) handleSignup(w http.ResponseWriter, r *http.Request) {
|
||||
if s.audlog != nil {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "invite_used",
|
||||
TenantID: inviteTok.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
})
|
||||
@@ -109,6 +110,7 @@ func (s *Server) handleSignup(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "signup",
|
||||
Username: u.Username,
|
||||
TenantID: u.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
})
|
||||
@@ -201,6 +203,7 @@ func (s *Server) handleForgotPassword(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "password_reset_requested",
|
||||
Username: u.Username,
|
||||
TenantID: u.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
})
|
||||
|
||||
@@ -82,6 +82,7 @@ func (s *Server) handleChangePassword(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventUserMgmt,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "change_password",
|
||||
@@ -143,6 +144,7 @@ func (s *Server) handleChangeEmail(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventUserMgmt,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "change_email",
|
||||
|
||||
@@ -106,6 +106,7 @@ func (s *Server) handleSetTenantQuota(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_quota_changed",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: fmt.Sprintf("tenant_id=%d", tenantID),
|
||||
|
||||
@@ -64,6 +64,7 @@ func (s *Server) handleSetTenantRetention(w http.ResponseWriter, r *http.Request
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "tenant_retention_changed",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: fmt.Sprintf("tenant_id=%d retention_days=%d", tenantID, body.RetentionDays),
|
||||
|
||||
@@ -83,6 +83,7 @@ func (s *Server) handleCreateSavedSearch(w http.ResponseWriter, r *http.Request)
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "saved_search_create",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: fmt.Sprintf("saved search id=%d name=%q", ss.ID, name),
|
||||
Success: true,
|
||||
@@ -122,6 +123,7 @@ func (s *Server) handleDeleteSavedSearch(w http.ResponseWriter, r *http.Request)
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "saved_search_delete",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: fmt.Sprintf("saved search id=%d", id),
|
||||
Success: true,
|
||||
|
||||
@@ -9,11 +9,22 @@ import (
|
||||
"time"
|
||||
|
||||
"archivmail/internal/audit"
|
||||
"archivmail/internal/auth"
|
||||
"archivmail/internal/index"
|
||||
"archivmail/internal/userstore"
|
||||
"archivmail/pkg/mailparser"
|
||||
)
|
||||
|
||||
// auditorIsGlobal reports whether an auditor session is a legacy *global*
|
||||
// auditor (no tenant assigned). PROJ-55: an auditor WITH a tenant_id is scoped
|
||||
// strictly to that tenant — identical to domain_auditor — and therefore must
|
||||
// NOT use the global no-tenant code paths. Only an auditor without any
|
||||
// tenant_id retains the legacy behaviour of seeing tenant-less mails globally.
|
||||
// For any non-auditor role this returns false.
|
||||
func auditorIsGlobal(sess *auth.Session) bool {
|
||||
return sess.Role == userstore.RoleAuditor && sess.TenantID == nil
|
||||
}
|
||||
|
||||
func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query().Get("q")
|
||||
fromFilter := r.URL.Query().Get("from")
|
||||
@@ -103,12 +114,13 @@ func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// PROJ-21 Phase 4: Use per-tenant index when available; fall back to
|
||||
// global index + post-filter when the tenant index manager is not wired.
|
||||
// auditor always uses the global index — they see no-tenant mails only,
|
||||
// regardless of any tenant_id on their user record.
|
||||
// PROJ-55: a *global* auditor (no tenant_id) always uses the global index —
|
||||
// they see no-tenant mails only. An auditor WITH a tenant_id is treated like
|
||||
// domain_auditor and uses the per-tenant index / tenant fallback filter.
|
||||
tenantID := tenantFromCtx(r.Context())
|
||||
searchIdx := s.idx
|
||||
usedTenantIndex := false
|
||||
if s.idxMgr != nil && tenantID != nil && sess.Role != userstore.RoleAuditor {
|
||||
if s.idxMgr != nil && tenantID != nil && !auditorIsGlobal(sess) {
|
||||
searchIdx = s.idxMgr.ForTenant(tenantID)
|
||||
usedTenantIndex = true
|
||||
}
|
||||
@@ -122,7 +134,7 @@ func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
// Fallback tenant isolation: post-filter when we used the global index
|
||||
// but the user belongs to a tenant. This is the legacy path; the per-tenant
|
||||
// index path above makes this unnecessary.
|
||||
if tenantID != nil && !usedTenantIndex && len(result.Hits) > 0 && sess.Role != userstore.RoleAuditor {
|
||||
if tenantID != nil && !usedTenantIndex && len(result.Hits) > 0 && !auditorIsGlobal(sess) {
|
||||
allowedIDs, idErr := s.store.GetAllIDsByTenant(r.Context(), tenantID)
|
||||
if idErr == nil {
|
||||
allowed := make(map[string]struct{}, len(allowedIDs))
|
||||
@@ -143,6 +155,7 @@ func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventSearch,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Query: q,
|
||||
Success: true,
|
||||
@@ -164,9 +177,11 @@ func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
MatchField string `json:"match_field,omitempty"` // PROJ-44: subject|body|attachment_text|...
|
||||
}
|
||||
|
||||
// auditor role: restrict results to mails with no tenant assignment.
|
||||
// Global auditor (no tenant_id): restrict results to mails with no tenant
|
||||
// assignment. A tenant-scoped auditor is already constrained by the
|
||||
// per-tenant index / tenant fallback filter above (PROJ-55).
|
||||
var auditorAllowedIDs map[string]struct{}
|
||||
if sess.Role == userstore.RoleAuditor {
|
||||
if auditorIsGlobal(sess) {
|
||||
ids, idErr := s.store.GetAllIDsWithoutTenant(r.Context())
|
||||
if idErr != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to load mail list")
|
||||
@@ -256,8 +271,10 @@ func (s *Server) handleGetMail(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// auditor: only mails with no tenant assignment.
|
||||
if sess.Role == userstore.RoleAuditor {
|
||||
// Global auditor (no tenant_id): only mails with no tenant assignment.
|
||||
// A tenant-scoped auditor is already constrained by the tenant-isolation
|
||||
// block above (sess.TenantID != nil) — PROJ-55.
|
||||
if auditorIsGlobal(sess) {
|
||||
ok, err := s.store.IsWithoutTenant(r.Context(), id)
|
||||
if err != nil || !ok {
|
||||
writeError(w, http.StatusForbidden, "access denied")
|
||||
@@ -395,8 +412,10 @@ func (s *Server) handleGetAttachment(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// auditor: only mails with no tenant assignment.
|
||||
if sess.Role == userstore.RoleAuditor {
|
||||
// Global auditor (no tenant_id): only mails with no tenant assignment.
|
||||
// A tenant-scoped auditor is already constrained by the tenant-isolation
|
||||
// block above (sess.TenantID != nil) — PROJ-55.
|
||||
if auditorIsGlobal(sess) {
|
||||
ok, err := s.store.IsWithoutTenant(r.Context(), id)
|
||||
if err != nil || !ok {
|
||||
writeError(w, http.StatusForbidden, "access denied")
|
||||
@@ -456,8 +475,10 @@ func (s *Server) handleGetRaw(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// auditor: only mails with no tenant assignment.
|
||||
if sess.Role == userstore.RoleAuditor {
|
||||
// Global auditor (no tenant_id): only mails with no tenant assignment.
|
||||
// A tenant-scoped auditor is already constrained by the tenant-isolation
|
||||
// block above (sess.TenantID != nil) — PROJ-55.
|
||||
if auditorIsGlobal(sess) {
|
||||
ok, err := s.store.IsWithoutTenant(r.Context(), id)
|
||||
if err != nil || !ok {
|
||||
writeError(w, http.StatusForbidden, "access denied")
|
||||
|
||||
@@ -58,6 +58,7 @@ func (s *Server) handleSaveSMTPOut(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "smtp_out_config_saved",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
})
|
||||
@@ -81,6 +82,7 @@ func (s *Server) handleDeleteSMTPOut(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "smtp_out_config_deleted",
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
})
|
||||
|
||||
@@ -36,6 +36,22 @@ func (s *Server) handleGetThread(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// PROJ-55: a global auditor (no tenant_id) may only see mails without a
|
||||
// tenant assignment. A tenant-scoped auditor (tenant_id set) is already
|
||||
// constrained by GetMailsByThread's tenant filter above.
|
||||
var auditorAllowed map[string]struct{}
|
||||
if auditorIsGlobal(sess) {
|
||||
noTenant, idErr := s.store.GetAllIDsWithoutTenant(r.Context())
|
||||
if idErr != nil {
|
||||
writeError(w, http.StatusInternalServerError, "access check failed")
|
||||
return
|
||||
}
|
||||
auditorAllowed = make(map[string]struct{}, len(noTenant))
|
||||
for _, nid := range noTenant {
|
||||
auditorAllowed[nid] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
type mailSummary struct {
|
||||
ID string `json:"id"`
|
||||
From string `json:"from,omitempty"`
|
||||
@@ -63,6 +79,13 @@ func (s *Server) handleGetThread(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// global auditor isolation: skip mails that belong to a tenant.
|
||||
if auditorAllowed != nil {
|
||||
if _, ok := auditorAllowed[id]; !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
var dateStr string
|
||||
if !pm.Date.IsZero() {
|
||||
dateStr = pm.Date.UTC().Format(time.RFC3339)
|
||||
|
||||
@@ -103,6 +103,7 @@ func (s *Server) handleTOTPSetupPost(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventUserMgmt,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: "totp_enabled",
|
||||
Success: true,
|
||||
@@ -156,6 +157,7 @@ func (s *Server) handleTOTPDisable(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventUserMgmt,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: "totp_disabled",
|
||||
Success: true,
|
||||
@@ -193,6 +195,7 @@ func (s *Server) handleTOTPLogin(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventLogin,
|
||||
Username: user.Username,
|
||||
TenantID: user.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Success: true,
|
||||
Detail: "totp_login_completed",
|
||||
@@ -254,6 +257,7 @@ func (s *Server) handleTOTPReset(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventUserMgmt,
|
||||
Username: sess.Username,
|
||||
TenantID: sess.TenantID,
|
||||
IPAddress: s.remoteIP(r),
|
||||
Detail: fmt.Sprintf("totp_reset_by_admin: TOTP reset by %s for user %s (id=%d)", sess.Username, target.Username, id),
|
||||
Success: true,
|
||||
|
||||
@@ -13,6 +13,15 @@ import (
|
||||
"archivmail/pkg/mailparser"
|
||||
)
|
||||
|
||||
// apiKeyTenantPtr converts an API-key session tenant id (0 = no tenant) into a
|
||||
// *int64 for audit logging (PROJ-55). Returns nil for the tenant-less case.
|
||||
func apiKeyTenantPtr(id int64) *int64 {
|
||||
if id == 0 {
|
||||
return nil
|
||||
}
|
||||
return &id
|
||||
}
|
||||
|
||||
// handleV1MethodNotAllowed returns 405 for non-GET methods on v1 endpoints.
|
||||
func (s *Server) handleV1MethodNotAllowed(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Allow", "GET")
|
||||
@@ -121,6 +130,7 @@ func (s *Server) handleV1SearchMails(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventSearch,
|
||||
Username: fmt.Sprintf("apikey:%s", akSess.KeyName),
|
||||
TenantID: apiKeyTenantPtr(akSess.TenantID),
|
||||
Query: q,
|
||||
Detail: fmt.Sprintf("v1_api contact=%s from=%s to=%s", contactFilter, fromFilter, toFilter),
|
||||
Success: true,
|
||||
@@ -217,6 +227,7 @@ func (s *Server) handleV1GetMail(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventMailView,
|
||||
Username: fmt.Sprintf("apikey:%s", akSess.KeyName),
|
||||
TenantID: apiKeyTenantPtr(akSess.TenantID),
|
||||
MailID: id,
|
||||
Detail: "v1_api",
|
||||
Success: true,
|
||||
@@ -303,6 +314,7 @@ func (s *Server) handleV1GetMailRaw(w http.ResponseWriter, r *http.Request) {
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: audit.EventExport,
|
||||
Username: fmt.Sprintf("apikey:%s", akSess.KeyName),
|
||||
TenantID: apiKeyTenantPtr(akSess.TenantID),
|
||||
MailID: id,
|
||||
Detail: "v1_api raw download",
|
||||
Success: true,
|
||||
|
||||
+36
-2
@@ -37,6 +37,10 @@ type Entry struct {
|
||||
MailID string `json:"mail_id"`
|
||||
Success bool `json:"success"`
|
||||
Detail string `json:"detail"`
|
||||
// TenantID, when set, records which tenant this event belongs to (PROJ-55).
|
||||
// nil means a tenant-less / system-wide event (e.g. superadmin actions,
|
||||
// scheduler/system events) that only superadmin sees in the audit log.
|
||||
TenantID *int64 `json:"tenant_id,omitempty"`
|
||||
}
|
||||
|
||||
// QueryFilter specifies filtering options for audit log queries.
|
||||
@@ -46,6 +50,11 @@ type QueryFilter struct {
|
||||
MailID string
|
||||
From *time.Time
|
||||
To *time.Time
|
||||
// TenantID, when set, restricts results to audit entries belonging to that
|
||||
// tenant (PROJ-55). Entries with a NULL tenant_id (e.g. written before
|
||||
// multi-tenancy or by tenant-less system actions) are NOT returned for a
|
||||
// tenant-scoped query — only superadmin (TenantID == nil) sees those.
|
||||
TenantID *int64
|
||||
PageSize int
|
||||
Page int
|
||||
}
|
||||
@@ -73,6 +82,10 @@ type fileEntry struct {
|
||||
MailID string `json:"mail_id,omitempty"`
|
||||
Success bool `json:"success"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
// TenantID mirrors the DB column (PROJ-55, BEFUND-3): a tenant-less /
|
||||
// system-wide event omits the field (nil → omitempty) so DB and flat-file
|
||||
// audit stay consistent for GoBD/forensic traceability.
|
||||
TenantID *int64 `json:"tenant_id,omitempty"`
|
||||
}
|
||||
|
||||
// New connects to PostgreSQL using the given DSN and initialises the schema.
|
||||
@@ -115,6 +128,17 @@ func initSchema(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// PROJ-55: tenant_id records which tenant an audit event belongs to so the
|
||||
// audit log can be filtered per tenant (NULL = tenant-less / system-wide event,
|
||||
// only visible to superadmin). Idempotent and safe on existing databases;
|
||||
// also created by tenantstore. No FK here to avoid an init-order dependency on
|
||||
// the tenants table — the value is always written from a validated session.
|
||||
if _, err := pool.Exec(ctx, `
|
||||
ALTER TABLE audit_log ADD COLUMN IF NOT EXISTS tenant_id BIGINT;
|
||||
`); err != nil {
|
||||
return fmt.Errorf("add tenant_id column: %w", err)
|
||||
}
|
||||
|
||||
// PROJ-48: make audit_log append-only at the database level. A BEFORE
|
||||
// UPDATE OR DELETE trigger raises an exception for every row mutation,
|
||||
// regardless of the DB role used by the application. This is the strongest
|
||||
@@ -180,8 +204,8 @@ func (l *Logger) Log(entry Entry) {
|
||||
}
|
||||
ctx := context.Background()
|
||||
_, err := l.pool.Exec(ctx,
|
||||
`INSERT INTO audit_log (timestamp, event_type, username, ip_address, query, mail_id, success, detail)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
||||
`INSERT INTO audit_log (timestamp, event_type, username, ip_address, query, mail_id, success, detail, tenant_id)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
|
||||
ts.UTC(),
|
||||
entry.EventType,
|
||||
entry.Username,
|
||||
@@ -190,6 +214,7 @@ func (l *Logger) Log(entry Entry) {
|
||||
entry.MailID,
|
||||
entry.Success,
|
||||
entry.Detail,
|
||||
entry.TenantID,
|
||||
)
|
||||
if err != nil {
|
||||
l.logger.Error("audit: insert failed", "err", err)
|
||||
@@ -219,6 +244,7 @@ func (l *Logger) writeFile(entry Entry, ts time.Time) {
|
||||
MailID: entry.MailID,
|
||||
Success: entry.Success,
|
||||
Detail: entry.Detail,
|
||||
TenantID: entry.TenantID,
|
||||
})
|
||||
if err != nil {
|
||||
l.logger.Error("audit: marshal log line failed", "err", err)
|
||||
@@ -320,6 +346,14 @@ func buildWhere(f QueryFilter) (string, []interface{}) {
|
||||
args = append(args, f.To.UTC())
|
||||
n++
|
||||
}
|
||||
if f.TenantID != nil {
|
||||
// NULL-safe by design: "tenant_id = $n" excludes rows with a NULL
|
||||
// tenant_id, so tenant-less audit entries stay invisible to tenant-scoped
|
||||
// roles (PROJ-55).
|
||||
clauses = append(clauses, fmt.Sprintf("tenant_id = $%d", n))
|
||||
args = append(args, *f.TenantID)
|
||||
n++
|
||||
}
|
||||
|
||||
if len(clauses) == 0 {
|
||||
return "", args
|
||||
|
||||
@@ -325,6 +325,7 @@ func (s *Scheduler) syncFolder(
|
||||
s.audlog.Log(audit.Entry{
|
||||
EventType: "imap_uidvalidity_reset",
|
||||
Username: acc.Owner,
|
||||
TenantID: acc.TenantID,
|
||||
Success: true,
|
||||
Detail: fmt.Sprintf("account=%d folder=%q old_uidvalidity=%d new_uidvalidity=%d",
|
||||
acc.ID, folder, state.UIDValidity, serverUIDValidity),
|
||||
|
||||
@@ -416,6 +416,7 @@ func (sess *session) cmdLogin(tag string, args string) {
|
||||
sess.server.audit.Log(audit.Entry{
|
||||
EventType: "imap_login",
|
||||
Username: username,
|
||||
TenantID: user.TenantID,
|
||||
IPAddress: extractIP(sess.remoteAddr),
|
||||
Success: true,
|
||||
Detail: "IMAP login successful",
|
||||
|
||||
Reference in New Issue
Block a user