feat: block unknown subdomains via tenant check

This commit is contained in:
Erik Silva
2025-12-09 03:04:28 -03:00
parent 74857bf106
commit 9e80aa1d70
4 changed files with 51 additions and 1 deletions

View File

@@ -14,7 +14,23 @@ export function middleware(request: NextRequest) {
return NextResponse.next();
}
// Se for agência ({subdomain}.localhost) - rotas de tenant
// Se for agência ({subdomain}.localhost) - validar se existe
if (hostname.includes('.')) {
try {
const res = await fetch(`http://backend:8080/api/tenant/check?subdomain=${subdomain}`);
if (res.status === 404) {
// Redireciona para o host base (sem subdomínio)
const baseHost = hostname.split('.').slice(1).join('.') || hostname;
const redirectUrl = new URL(url.toString());
redirectUrl.hostname = baseHost;
redirectUrl.pathname = '/';
return NextResponse.redirect(redirectUrl);
}
} catch (err) {
// Em caso de erro de rede, não bloquear
}
}
// Permitir /dashboard, /login, /clientes, etc.
return NextResponse.next();
}