fix(PROJ-73): Upload-Job-Status bei Panic + nil-Guards nach GetByUsername

Upload-Job bleibt bei einem Panic im Verarbeitungspfad nicht mehr auf
"running" hängen, sondern zeigt "error" mit generischer Meldung (Panic-
Rohwert nur im Server-Log, ErrMsg geht ans Frontend und könnte sonst
Mail-Inhalt-Fragmente transportieren). Zusätzlich fail-closed nil-Guards
an allen 13 GetByUsername-Aufrufstellen in internal/api/, die das
Ergebnis bisher ungeprüft dereferenzierten.

Verifiziert auf 192.168.1.132: Build und go vet fehlerfrei für die
geänderten Dateien.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019j28kGcaJAhBnrYX34hGdt
This commit is contained in:
sysops
2026-08-05 13:50:36 +02:00
co-authored by Claude Sonnet 5
parent 798cb2817c
commit 88cdc3eb3e
11 changed files with 113 additions and 14 deletions
+1 -1
View File
@@ -103,7 +103,7 @@ func (s *Server) handleMe(w http.ResponseWriter, r *http.Request) {
sess := sessionFromCtx(r.Context())
user, err := s.users.GetByUsername(sess.Username)
if err != nil {
if err != nil || user == nil {
writeError(w, http.StatusInternalServerError, "user lookup failed")
return
}
+1 -1
View File
@@ -131,7 +131,7 @@ func (s *Server) handleExportEDiscovery(w http.ResponseWriter, r *http.Request)
var userEmail string
if sess.Role == userstore.RoleUser {
u, err := s.users.GetByUsername(sess.Username)
if err != nil {
if err != nil || u == nil {
writeError(w, http.StatusInternalServerError, "user lookup failed")
return
}
+2 -2
View File
@@ -370,7 +370,7 @@ func (s *Server) handleExportPDF(w http.ResponseWriter, r *http.Request) {
// user: only own mails; domain_auditor: all tenant mails (no filter)
if sess.Role == userstore.RoleUser {
u, err := s.users.GetByUsername(sess.Username)
if err != nil || !mailBelongsToUser(pm, u.Email) {
if err != nil || u == nil || !mailBelongsToUser(pm, u.Email) {
writeError(w, http.StatusForbidden, "access denied")
return
}
@@ -469,7 +469,7 @@ func (s *Server) handleExportZIP(w http.ResponseWriter, r *http.Request) {
var userEmail string
if sess.Role == userstore.RoleUser {
u, err := s.users.GetByUsername(sess.Username)
if err != nil {
if err != nil || u == nil {
writeError(w, http.StatusInternalServerError, "user lookup failed")
return
}
+1 -1
View File
@@ -63,7 +63,7 @@ func (s *Server) handleGetOCRText(w http.ResponseWriter, r *http.Request) {
return
}
u, err := s.users.GetByUsername(sess.Username)
if err != nil || !mailBelongsToUser(pm, u.Email) {
if err != nil || u == nil || !mailBelongsToUser(pm, u.Email) {
writeError(w, http.StatusForbidden, "access denied")
return
}
+3 -3
View File
@@ -33,7 +33,7 @@ func (s *Server) handleChangePassword(w http.ResponseWriter, r *http.Request) {
// Load user
user, err := s.users.GetByUsername(sess.Username)
if err != nil {
if err != nil || user == nil {
s.logger.Error("change_password: user not found", "err", err, "username", sess.Username)
writeError(w, http.StatusInternalServerError, "user not found")
return
@@ -110,7 +110,7 @@ func (s *Server) handleChangeEmail(w http.ResponseWriter, r *http.Request) {
// Load user
user, err := s.users.GetByUsername(sess.Username)
if err != nil {
if err != nil || user == nil {
s.logger.Error("change_email: user not found", "err", err, "username", sess.Username)
writeError(w, http.StatusInternalServerError, "user not found")
return
@@ -180,7 +180,7 @@ func (s *Server) handleChangePreferences(w http.ResponseWriter, r *http.Request)
}
user, err := s.users.GetByUsername(sess.Username)
if err != nil {
if err != nil || user == nil {
s.logger.Error("change_preferences: user not found", "err", err, "username", sess.Username)
writeError(w, http.StatusInternalServerError, "user not found")
return
+2 -2
View File
@@ -40,7 +40,7 @@ func (s *Server) handleSetRestoreEnabled(w http.ResponseWriter, r *http.Request)
}
user, err := s.users.GetByUsername(sess.Username)
if err != nil {
if err != nil || user == nil {
s.logger.Error("restore_toggle: user not found", "err", err, "username", sess.Username)
writeError(w, http.StatusInternalServerError, "user not found")
return
@@ -137,7 +137,7 @@ func (s *Server) handleRestoreMail(w http.ResponseWriter, r *http.Request) {
}
user, err := s.users.GetByUsername(sess.Username)
if err != nil {
if err != nil || user == nil {
writeError(w, http.StatusInternalServerError, "user lookup failed")
return
}
+2 -2
View File
@@ -426,7 +426,7 @@ func (s *Server) handleGetAttachment(w http.ResponseWriter, r *http.Request) {
// user: only own mails; domain_auditor: all tenant mails (no filter)
if sess.Role == userstore.RoleUser {
u, err := s.users.GetByUsername(sess.Username)
if err != nil || !mailBelongsToUser(pm, u.Email) {
if err != nil || u == nil || !mailBelongsToUser(pm, u.Email) {
writeError(w, http.StatusForbidden, "access denied")
return
}
@@ -494,7 +494,7 @@ func (s *Server) handleGetRaw(w http.ResponseWriter, r *http.Request) {
return
}
u, err := s.users.GetByUsername(sess.Username)
if err != nil || !mailBelongsToUser(pm, u.Email) {
if err != nil || u == nil || !mailBelongsToUser(pm, u.Email) {
writeError(w, http.StatusForbidden, "access denied")
return
}
+1 -1
View File
@@ -100,7 +100,7 @@ func (s *Server) handleTestSMTPOut(w http.ResponseWriter, r *http.Request) {
sess := sessionFromCtx(r.Context())
u, err := s.users.GetByUsername(sess.Username)
if err != nil || u.Email == "" {
if err != nil || u == nil || u.Email == "" {
writeError(w, http.StatusBadRequest, "Keine E-Mail-Adresse für diesen Account")
return
}
+15
View File
@@ -145,6 +145,21 @@ func (s *Server) handleUploadProgress(w http.ResponseWriter, r *http.Request) {
func (s *Server) runUploadJob(job *UploadJob, messages [][]byte, tenantID *int64) {
ctx := context.Background()
// PROJ-73: a panic in the import path must not leave the job stuck on
// "running" forever — surface it as an error state in the upload UI.
// safego.Go still catches the panic afterwards for the stack trace log.
defer func() {
if rec := recover(); rec != nil {
job.mu.Lock()
job.Status = "error"
// No panic value in the message: it can carry mail content
// fragments (DSGVO). Details go to the log via safego.Run.
job.ErrMsg = "Import wegen eines internen Fehlers abgebrochen"
job.mu.Unlock()
panic(rec)
}
}()
for _, raw := range messages {
result := s.importRawMessage(ctx, raw, tenantID)
job.mu.Lock()