1. Tenant isolation bypass (CRITICAL): TenantResolver now verifies user has access to X-Tenant-ID via user_tenants lookup before setting context. Added VerifyAccess method to TenantLookup interface and TenantService. 2. Consolidated tenant resolution: Removed duplicate resolveTenant() from helpers.go and tenant resolution from auth middleware. TenantResolver is now the single source of truth. Deadlines and AI handlers use auth.TenantFromContext() instead of direct DB queries. 3. CalDAV credential masking: tenant settings responses now mask CalDAV passwords with "********" via maskSettingsPassword helper. Applied to GetTenant, ListTenants, and UpdateSettings responses. 4. CORS + security headers: New middleware/security.go with CORS (restricted to FRONTEND_ORIGIN) and security headers (X-Frame-Options, X-Content-Type-Options, HSTS, Referrer-Policy, X-XSS-Protection). 5. Internal error leaking: All writeError(w, 500, err.Error()) replaced with internalError() that logs via slog and returns generic "internal error" to client. Same for jsonError in tenant handler. 6. Input validation: Max length on title (500), description (10000), case_number (100), search (200). Pagination clamped to max 100. Content-Disposition filename sanitized against header injection. Regression test added for tenant access denial (403 on unauthorized X-Tenant-ID). All existing tests pass, go vet clean.
208 lines
5.2 KiB
Go
208 lines
5.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"mgit.msbls.de/m/KanzlAI-mGMT/internal/auth"
|
|
"mgit.msbls.de/m/KanzlAI-mGMT/internal/services"
|
|
)
|
|
|
|
// DeadlineHandlers holds handlers for deadline CRUD endpoints
|
|
type DeadlineHandlers struct {
|
|
deadlines *services.DeadlineService
|
|
}
|
|
|
|
// NewDeadlineHandlers creates deadline handlers
|
|
func NewDeadlineHandlers(ds *services.DeadlineService) *DeadlineHandlers {
|
|
return &DeadlineHandlers{deadlines: ds}
|
|
}
|
|
|
|
// Get handles GET /api/deadlines/{deadlineID}
|
|
func (h *DeadlineHandlers) Get(w http.ResponseWriter, r *http.Request) {
|
|
tenantID, ok := auth.TenantFromContext(r.Context())
|
|
if !ok {
|
|
writeError(w, http.StatusForbidden, "missing tenant")
|
|
return
|
|
}
|
|
|
|
deadlineID, err := parsePathUUID(r, "deadlineID")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid deadline ID")
|
|
return
|
|
}
|
|
|
|
deadline, err := h.deadlines.GetByID(tenantID, deadlineID)
|
|
if err != nil {
|
|
internalError(w, "failed to fetch deadline", err)
|
|
return
|
|
}
|
|
if deadline == nil {
|
|
writeError(w, http.StatusNotFound, "deadline not found")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, deadline)
|
|
}
|
|
|
|
// ListAll handles GET /api/deadlines
|
|
func (h *DeadlineHandlers) ListAll(w http.ResponseWriter, r *http.Request) {
|
|
tenantID, ok := auth.TenantFromContext(r.Context())
|
|
if !ok {
|
|
writeError(w, http.StatusForbidden, "missing tenant")
|
|
return
|
|
}
|
|
|
|
deadlines, err := h.deadlines.ListAll(tenantID)
|
|
if err != nil {
|
|
internalError(w, "failed to list deadlines", err)
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, deadlines)
|
|
}
|
|
|
|
// ListForCase handles GET /api/cases/{caseID}/deadlines
|
|
func (h *DeadlineHandlers) ListForCase(w http.ResponseWriter, r *http.Request) {
|
|
tenantID, ok := auth.TenantFromContext(r.Context())
|
|
if !ok {
|
|
writeError(w, http.StatusForbidden, "missing tenant")
|
|
return
|
|
}
|
|
|
|
caseID, err := parsePathUUID(r, "caseID")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid case ID")
|
|
return
|
|
}
|
|
|
|
deadlines, err := h.deadlines.ListForCase(tenantID, caseID)
|
|
if err != nil {
|
|
internalError(w, "failed to list deadlines for case", err)
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, deadlines)
|
|
}
|
|
|
|
// Create handles POST /api/cases/{caseID}/deadlines
|
|
func (h *DeadlineHandlers) Create(w http.ResponseWriter, r *http.Request) {
|
|
tenantID, ok := auth.TenantFromContext(r.Context())
|
|
if !ok {
|
|
writeError(w, http.StatusForbidden, "missing tenant")
|
|
return
|
|
}
|
|
|
|
caseID, err := parsePathUUID(r, "caseID")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid case ID")
|
|
return
|
|
}
|
|
|
|
var input services.CreateDeadlineInput
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
input.CaseID = caseID
|
|
|
|
if input.Title == "" || input.DueDate == "" {
|
|
writeError(w, http.StatusBadRequest, "title and due_date are required")
|
|
return
|
|
}
|
|
if msg := validateStringLength("title", input.Title, maxTitleLen); msg != "" {
|
|
writeError(w, http.StatusBadRequest, msg)
|
|
return
|
|
}
|
|
|
|
deadline, err := h.deadlines.Create(tenantID, input)
|
|
if err != nil {
|
|
internalError(w, "failed to create deadline", err)
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, deadline)
|
|
}
|
|
|
|
// Update handles PUT /api/deadlines/{deadlineID}
|
|
func (h *DeadlineHandlers) Update(w http.ResponseWriter, r *http.Request) {
|
|
tenantID, ok := auth.TenantFromContext(r.Context())
|
|
if !ok {
|
|
writeError(w, http.StatusForbidden, "missing tenant")
|
|
return
|
|
}
|
|
|
|
deadlineID, err := parsePathUUID(r, "deadlineID")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid deadline ID")
|
|
return
|
|
}
|
|
|
|
var input services.UpdateDeadlineInput
|
|
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
deadline, err := h.deadlines.Update(tenantID, deadlineID, input)
|
|
if err != nil {
|
|
internalError(w, "failed to update deadline", err)
|
|
return
|
|
}
|
|
if deadline == nil {
|
|
writeError(w, http.StatusNotFound, "deadline not found")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, deadline)
|
|
}
|
|
|
|
// Complete handles PATCH /api/deadlines/{deadlineID}/complete
|
|
func (h *DeadlineHandlers) Complete(w http.ResponseWriter, r *http.Request) {
|
|
tenantID, ok := auth.TenantFromContext(r.Context())
|
|
if !ok {
|
|
writeError(w, http.StatusForbidden, "missing tenant")
|
|
return
|
|
}
|
|
|
|
deadlineID, err := parsePathUUID(r, "deadlineID")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid deadline ID")
|
|
return
|
|
}
|
|
|
|
deadline, err := h.deadlines.Complete(tenantID, deadlineID)
|
|
if err != nil {
|
|
internalError(w, "failed to complete deadline", err)
|
|
return
|
|
}
|
|
if deadline == nil {
|
|
writeError(w, http.StatusNotFound, "deadline not found")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, deadline)
|
|
}
|
|
|
|
// Delete handles DELETE /api/deadlines/{deadlineID}
|
|
func (h *DeadlineHandlers) Delete(w http.ResponseWriter, r *http.Request) {
|
|
tenantID, ok := auth.TenantFromContext(r.Context())
|
|
if !ok {
|
|
writeError(w, http.StatusForbidden, "missing tenant")
|
|
return
|
|
}
|
|
|
|
deadlineID, err := parsePathUUID(r, "deadlineID")
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid deadline ID")
|
|
return
|
|
}
|
|
|
|
if err := h.deadlines.Delete(tenantID, deadlineID); err != nil {
|
|
writeError(w, http.StatusNotFound, "deadline not found")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
|
}
|