forked from Northgate-Systems/RemitX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
89 lines (79 loc) · 2.55 KB
/
Copy pathmiddleware.ts
File metadata and controls
89 lines (79 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { verifyToken } from "@/lib/jwt";
import { applySecurityHeaders, logSecurityEvent } from "@/lib/security-edge";
const publicPaths = [
"/",
"/login",
"/api/auth/login",
"/api/auth/register",
"/api/auth/forgot-password",
"/api/auth/reset-password",
"/api/public/",
"/api/analytics",
"/thank-you",
"/legal/",
"/_next/",
"/favicon.ico",
"/robots.txt",
"/sitemap.xml",
];
const isDev = process.env.NODE_ENV !== "production";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const response = NextResponse.next();
// Apply security headers to all responses
applySecurityHeaders(response);
// Allow public paths before any auth/CORS checks
if (publicPaths.some((p) => pathname.startsWith(p))) {
return response;
}
// CORS lockdown for authenticated API routes - only in production
// In dev, we don't know the exact origin (localhost:3000 vs 127.0.0.1 etc)
if (!isDev && pathname.startsWith("/api/")) {
const origin = request.headers.get("origin");
const allowed = [
"https://remitx.app",
"https://remitx.vercel.app",
];
if (origin && !allowed.includes(origin)) {
logSecurityEvent("csrf_blocked", { origin, pathname, reason: "disallowed_origin" });
return NextResponse.json(
{ success: false, error: "Origin not allowed" },
{ status: 403 }
);
}
}
// Check for session cookie
const token = request.cookies.get("remitx_session")?.value;
if (!token) {
// Redirect to login for page routes, return 401 for API routes
if (pathname.startsWith("/api/")) {
logSecurityEvent("unauthorized_access", { pathname, reason: "no_token" });
return NextResponse.json(
{ success: false, error: "Unauthorized" },
{ status: 401 }
);
}
return NextResponse.redirect(new URL("/login", request.url));
}
const payload = verifyToken(token);
if (!payload) {
if (pathname.startsWith("/api/")) {
logSecurityEvent("invalid_token", { pathname, reason: "invalid_signature" });
return NextResponse.json(
{ success: false, error: "Unauthorized" },
{ status: 401 }
);
}
return NextResponse.redirect(new URL("/login", request.url));
}
return response;
}
export const config = {
matcher: [
// Apply to all routes except static files
"/((?!static|public|_next/static|_next/image|.*\\.png$|.*\\.svg$|.*\\.jpg$|.*\\.ico$|.*\\.css$|.*\\.js$).*)",
"/api/:path*",
],
};