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.
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// TenantLookup resolves and verifies tenant access for a user.
|
|
// Defined as an interface to avoid circular dependency with services.
|
|
type TenantLookup interface {
|
|
FirstTenantForUser(ctx context.Context, userID uuid.UUID) (*uuid.UUID, error)
|
|
VerifyAccess(ctx context.Context, userID, tenantID uuid.UUID) (bool, error)
|
|
}
|
|
|
|
// TenantResolver is middleware that resolves the tenant from X-Tenant-ID header
|
|
// or defaults to the user's first tenant. Always verifies user has access.
|
|
type TenantResolver struct {
|
|
lookup TenantLookup
|
|
}
|
|
|
|
func NewTenantResolver(lookup TenantLookup) *TenantResolver {
|
|
return &TenantResolver{lookup: lookup}
|
|
}
|
|
|
|
func (tr *TenantResolver) Resolve(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
userID, ok := UserFromContext(r.Context())
|
|
if !ok {
|
|
http.Error(w, `{"error":"unauthorized"}`, http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
var tenantID uuid.UUID
|
|
|
|
if header := r.Header.Get("X-Tenant-ID"); header != "" {
|
|
parsed, err := uuid.Parse(header)
|
|
if err != nil {
|
|
http.Error(w, `{"error":"invalid X-Tenant-ID"}`, http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Verify user has access to this tenant
|
|
hasAccess, err := tr.lookup.VerifyAccess(r.Context(), userID, parsed)
|
|
if err != nil {
|
|
slog.Error("tenant access check failed", "error", err, "user_id", userID, "tenant_id", parsed)
|
|
http.Error(w, `{"error":"internal error"}`, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if !hasAccess {
|
|
http.Error(w, `{"error":"no access to tenant"}`, http.StatusForbidden)
|
|
return
|
|
}
|
|
|
|
tenantID = parsed
|
|
} else {
|
|
// Default to user's first tenant
|
|
first, err := tr.lookup.FirstTenantForUser(r.Context(), userID)
|
|
if err != nil {
|
|
slog.Error("failed to resolve default tenant", "error", err, "user_id", userID)
|
|
http.Error(w, `{"error":"internal error"}`, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if first == nil {
|
|
http.Error(w, `{"error":"no tenant found for user"}`, http.StatusBadRequest)
|
|
return
|
|
}
|
|
tenantID = *first
|
|
}
|
|
|
|
ctx := ContextWithTenantID(r.Context(), tenantID)
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
})
|
|
}
|