Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function middleware(request: NextRequest) {

// Allow access to login page and API routes
if (pathname.startsWith("/login") || pathname.startsWith("/api/auth")) {
return NextResponse.next();
return addSecurityHeaders(NextResponse.next());
}

// Check if accessing protected routes (committee-info, tutorial, events)
Expand All @@ -52,7 +52,7 @@ export function middleware(request: NextRequest) {
}

// Session cookie exists, allow access
// Add cache-control and security headers
// Add cache-control headers for protected pages
const response = NextResponse.next();
response.headers.set(
"Cache-Control",
Expand All @@ -61,21 +61,26 @@ export function middleware(request: NextRequest) {
response.headers.set("Pragma", "no-cache");
response.headers.set("Expires", "0");

// Security headers
response.headers.set("X-Content-Type-Options", "nosniff");
response.headers.set("X-Frame-Options", "DENY");
response.headers.set("X-XSS-Protection", "1; mode=block");
response.headers.set("Referrer-Policy", "strict-origin-when-cross-origin");
response.headers.set(
"Permissions-Policy",
"camera=(), microphone=(), geolocation=()",
);

return response;
return addSecurityHeaders(response);
}

// Allow all other routes
return NextResponse.next();
// Allow all other routes with security headers
return addSecurityHeaders(NextResponse.next());
}

/**
* Adds security headers to a response.
*/
function addSecurityHeaders(response: NextResponse): NextResponse {
response.headers.set("X-Content-Type-Options", "nosniff");
response.headers.set("X-Frame-Options", "DENY");
response.headers.set("X-XSS-Protection", "1; mode=block");
response.headers.set("Referrer-Policy", "strict-origin-when-cross-origin");
response.headers.set(
"Permissions-Policy",
"camera=(), microphone=(), geolocation=()",
);
return response;
}

/**
Expand Down
Loading