v1.4: Segurança multi-tenant, file serving via API e UX humanizada
- Validação cross-tenant no login e rotas protegidas
- File serving via /api/files/{bucket}/{path} (eliminação DNS)
- Mensagens de erro humanizadas inline (sem pop-ups)
- Middleware tenant detection via headers customizados
- Upload de logos retorna URLs via API
- README atualizado com changelog v1.4 completo
This commit is contained in:
@@ -2,6 +2,7 @@ package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -59,13 +60,40 @@ func Auth(cfg *config.Config) func(http.Handler) http.Handler {
|
||||
}
|
||||
|
||||
// tenant_id pode ser nil para SuperAdmin
|
||||
var tenantID string
|
||||
var tenantIDFromJWT string
|
||||
if tenantIDClaim, ok := claims["tenant_id"]; ok && tenantIDClaim != nil {
|
||||
tenantID, _ = tenantIDClaim.(string)
|
||||
tenantIDFromJWT, _ = tenantIDClaim.(string)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), UserIDKey, userID)
|
||||
ctx = context.WithValue(ctx, TenantIDKey, tenantID)
|
||||
// VALIDAÇÃO DE SEGURANÇA: Verificar se o tenant_id do JWT corresponde ao subdomínio acessado
|
||||
// Pegar o tenant_id do contexto (detectado pelo TenantDetector middleware ANTES deste)
|
||||
tenantIDFromContext := ""
|
||||
if ctxTenantID := r.Context().Value(TenantIDKey); ctxTenantID != nil {
|
||||
tenantIDFromContext, _ = ctxTenantID.(string)
|
||||
}
|
||||
|
||||
log.Printf("🔐 AUTH VALIDATION: JWT tenant=%s | Context tenant=%s | Path=%s",
|
||||
tenantIDFromJWT, tenantIDFromContext, r.RequestURI)
|
||||
|
||||
// Se o usuário não é SuperAdmin (tem tenant_id) e está acessando uma agência (subdomain detectado)
|
||||
if tenantIDFromJWT != "" && tenantIDFromContext != "" {
|
||||
// Validar se o tenant_id do JWT corresponde ao tenant detectado
|
||||
if tenantIDFromJWT != tenantIDFromContext {
|
||||
log.Printf("❌ CROSS-TENANT ACCESS BLOCKED: User from tenant %s tried to access tenant %s",
|
||||
tenantIDFromJWT, tenantIDFromContext)
|
||||
http.Error(w, "Forbidden: You don't have access to this tenant", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
log.Printf("✅ TENANT VALIDATION PASSED: %s", tenantIDFromJWT)
|
||||
}
|
||||
|
||||
// Preservar TODOS os valores do contexto anterior (incluindo o tenantID do TenantDetector)
|
||||
ctx := r.Context()
|
||||
ctx = context.WithValue(ctx, UserIDKey, userID)
|
||||
// Só sobrescrever o TenantIDKey se vier do JWT (para não perder o do TenantDetector)
|
||||
if tenantIDFromJWT != "" {
|
||||
ctx = context.WithValue(ctx, TenantIDKey, tenantIDFromJWT)
|
||||
}
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -16,15 +16,25 @@ func TenantDetector(tenantRepo *repository.TenantRepository) func(http.Handler)
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Get host from X-Forwarded-Host header (set by Next.js proxy) or Host header
|
||||
host := r.Header.Get("X-Forwarded-Host")
|
||||
if host == "" {
|
||||
host = r.Header.Get("X-Original-Host")
|
||||
}
|
||||
if host == "" {
|
||||
host = r.Host
|
||||
}
|
||||
// Priority order: X-Tenant-Subdomain (set by Next.js middleware) > X-Forwarded-Host > X-Original-Host > Host
|
||||
tenantSubdomain := r.Header.Get("X-Tenant-Subdomain")
|
||||
|
||||
log.Printf("TenantDetector: host = %s (from headers), path = %s", host, r.RequestURI)
|
||||
var host string
|
||||
if tenantSubdomain != "" {
|
||||
// Use direct subdomain from Next.js middleware
|
||||
host = tenantSubdomain
|
||||
log.Printf("TenantDetector: using X-Tenant-Subdomain = %s", tenantSubdomain)
|
||||
} else {
|
||||
// Fallback to extracting from host headers
|
||||
host = r.Header.Get("X-Forwarded-Host")
|
||||
if host == "" {
|
||||
host = r.Header.Get("X-Original-Host")
|
||||
}
|
||||
if host == "" {
|
||||
host = r.Host
|
||||
}
|
||||
log.Printf("TenantDetector: host = %s (from headers), path = %s", host, r.RequestURI)
|
||||
}
|
||||
|
||||
// Extract subdomain
|
||||
// Examples:
|
||||
@@ -33,17 +43,28 @@ func TenantDetector(tenantRepo *repository.TenantRepository) func(http.Handler)
|
||||
// - dash.localhost -> dash (master admin)
|
||||
// - localhost -> (institutional site)
|
||||
|
||||
parts := strings.Split(host, ".")
|
||||
var subdomain string
|
||||
|
||||
if len(parts) >= 2 {
|
||||
// Has subdomain
|
||||
subdomain = parts[0]
|
||||
|
||||
// If we got the subdomain directly from X-Tenant-Subdomain, use it
|
||||
if tenantSubdomain != "" {
|
||||
subdomain = tenantSubdomain
|
||||
// Remove port if present
|
||||
if strings.Contains(subdomain, ":") {
|
||||
subdomain = strings.Split(subdomain, ":")[0]
|
||||
}
|
||||
} else {
|
||||
// Extract from host
|
||||
parts := strings.Split(host, ".")
|
||||
|
||||
if len(parts) >= 2 {
|
||||
// Has subdomain
|
||||
subdomain = parts[0]
|
||||
|
||||
// Remove port if present
|
||||
if strings.Contains(subdomain, ":") {
|
||||
subdomain = strings.Split(subdomain, ":")[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("TenantDetector: extracted subdomain = %s", subdomain)
|
||||
|
||||
Reference in New Issue
Block a user