fix: apply security headers to all routes, not just protected ones#144
fix: apply security headers to all routes, not just protected ones#144rotarymars wants to merge 1 commit intomainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughThe middleware now consistently applies security headers across all routes through a new helper function instead of inline header assignments. Previously, different routes applied headers inconsistently; now Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/middleware.ts (1)
44-47:⚠️ Potential issue | 🟠 MajorWrap the redirect response too.
Line 47 still returns a bare
NextResponse.redirect(loginUrl), so unauthenticated requests to protected routes are the one path that still miss the headers this PR is supposed to apply globally. That leaves the fix incomplete.Proposed fix
if (!sessionId) { // No session cookie, redirect to login const loginUrl = new URL("/login", request.url); - return NextResponse.redirect(loginUrl); + return addSecurityHeaders(NextResponse.redirect(loginUrl)); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/middleware.ts` around lines 44 - 47, The redirect for unauthenticated requests currently returns NextResponse.redirect(loginUrl) directly (see sessionId and loginUrl), so the global headers aren’t applied; change it to create the redirect response (e.g., const res = NextResponse.redirect(loginUrl)), apply the same header-setting logic you use for other responses (the headers added in middleware.ts), and then return that wrapped response so the security/trace headers are included on the redirect path as well.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@src/middleware.ts`:
- Around line 44-47: The redirect for unauthenticated requests currently returns
NextResponse.redirect(loginUrl) directly (see sessionId and loginUrl), so the
global headers aren’t applied; change it to create the redirect response (e.g.,
const res = NextResponse.redirect(loginUrl)), apply the same header-setting
logic you use for other responses (the headers added in middleware.ts), and then
return that wrapped response so the security/trace headers are included on the
redirect path as well.
Description
Applying security headers to all routes.
Type of Change
Changes Made
Testing
npm run devnpm run build)npm run lint)Screenshots (if applicable)
Checklist
Related Issues
Summary by CodeRabbit