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.
69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"mgit.msbls.de/m/KanzlAI-mGMT/internal/auth"
|
|
"mgit.msbls.de/m/KanzlAI-mGMT/internal/services"
|
|
)
|
|
|
|
// CalDAVHandler handles CalDAV sync HTTP endpoints.
|
|
type CalDAVHandler struct {
|
|
svc *services.CalDAVService
|
|
}
|
|
|
|
// NewCalDAVHandler creates a new CalDAV handler.
|
|
func NewCalDAVHandler(svc *services.CalDAVService) *CalDAVHandler {
|
|
return &CalDAVHandler{svc: svc}
|
|
}
|
|
|
|
// TriggerSync handles POST /api/caldav/sync — triggers a full sync for the current tenant.
|
|
func (h *CalDAVHandler) TriggerSync(w http.ResponseWriter, r *http.Request) {
|
|
tenantID, ok := auth.TenantFromContext(r.Context())
|
|
if !ok {
|
|
writeError(w, http.StatusUnauthorized, "no tenant context")
|
|
return
|
|
}
|
|
|
|
cfg, err := h.svc.LoadTenantConfig(tenantID)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "CalDAV not configured for this tenant")
|
|
return
|
|
}
|
|
|
|
status, err := h.svc.SyncTenant(r.Context(), tenantID, *cfg)
|
|
if err != nil {
|
|
// Still return the status — it contains partial results + error info
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"status": "completed_with_errors",
|
|
"sync": status,
|
|
})
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"status": "ok",
|
|
"sync": status,
|
|
})
|
|
}
|
|
|
|
// GetStatus handles GET /api/caldav/status — returns last sync status.
|
|
func (h *CalDAVHandler) GetStatus(w http.ResponseWriter, r *http.Request) {
|
|
tenantID, ok := auth.TenantFromContext(r.Context())
|
|
if !ok {
|
|
writeError(w, http.StatusUnauthorized, "no tenant context")
|
|
return
|
|
}
|
|
|
|
status := h.svc.GetStatus(tenantID)
|
|
if status == nil {
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"status": "no_sync_yet",
|
|
"last_sync_at": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, status)
|
|
}
|