-
Notifications
You must be signed in to change notification settings - Fork 34
fix: return 401 for non-GET requests instead of 307 redirect to login #526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shafaladhikari
wants to merge
4
commits into
kinde-oss:main
Choose a base branch
from
shafaladhikari:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ec939bc
fix: return 401 for non-GET requests instead of 307 redirect to login
safal-octagon fefba21
Merge branch 'kinde-oss:main' into main
shafaladhikari 6e57014
fix: guard invitation-code redirect against non-GET method replay
safal-octagon 891f370
Merge branch 'main' of github.com:shafaladhikari/kinde-auth-nextjs
safal-octagon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
In Next.js 15 middleware, what HTTP status code does NextResponse.json return by default when no status option is provided?💡 Result:
In Next.js 15, when you use NextResponse.json without providing a status option, it returns an HTTP 200 status code by default [1][2]. This behavior aligns with the standard Web Response API, which NextResponse extends [3][2].
Citations:
Fix 401 response in
loginRedirect(and ensure invitation-code doesn’t bypass it)loginRedirectreturnsNextResponse.json(...)without a{ status: 401 }option, which results in HTTP 200 by default—so the transport-level 401 contract isn’t met.Also, the invitation-code flow still returns
NextResponse.redirect(...)directly and appears to bypass the non-GET/HEAD method gate; ensure it goes through the sameloginRedirectlogic (or otherwise enforces 401 for non-GET/HEAD).📝 Committable suggestion
🤖 Prompt for AI Agents
🧩 Analysis chain
🌐 Web query:
In Next.js middleware, does NextResponse.redirect default to HTTP 307 and preserve the original HTTP method for POST requests?💡 Result:
Yes. 1) Default status code: In Next.js middleware, NextResponse.redirect defaults to an HTTP 307 Temporary Redirect when you don’t specify a status code. This is shown in the Next.js implementation where it uses init?.status?? 307 [1]. Next.js docs also state that redirect returns a 307 by default [2]. 2) Preservation of method for POST: With an HTTP 307 Temporary Redirect, HTTP semantics require the client to repeat the request with the same method (so a POST stays a POST). MDN describes 307 as “Method and body not changed” [3]. Therefore, when NextResponse.redirect defaults to 307, it preserves the original HTTP method for POST requests in practice/semantics. Note: Next.js redirect helpers can be overridden if you pass a different status (e.g., { status: 308 } for permanent redirect) [1][4], but the default behavior is 307, which preserves method per HTTP semantics [3].
Citations:
🏁 Script executed:
Repository: kinde-oss/kinde-auth-nextjs
Length of output: 163
🏁 Script executed:
Repository: kinde-oss/kinde-auth-nextjs
Length of output: 25355
Guard invitation-code redirects against non-GET/HEAD (default 307 preserves POST method)
handleMiddlewareimmediately returnshandleInvitationCodeRedirect(...)wheninvitation_codeis present (bypassingloginRedirect(req, ...)), andhandleInvitationCodeRedirectperforms a bareNextResponse.redirect(...)with no method check. SinceNextResponse.redirectdefaults to HTTP 307, aPOST /protected?invitation_code=...will keep the POST method and be repeated to the register URL (and to the login URL in the error path).One way to close the gap
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix 401 response in loginRedirect: the finding is technically correct (HTTP 200 with statusCode: 401 in the body is wrong), but fixing it in isolation would make loginRedirect and handleInvitationCodeRedirect diverge from the identical pattern used across all 5 call sites in protect.js. We'd be creating a new inconsistency rather than resolving one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shafaladhikari, that's a fair point — patching justloginRedirecthere while leaving the identical{ statusCode: 401 }+ HTTP 200 pattern intact acrossprotect.js's five call sites would create a worse inconsistency than the one it solves. Deferring to a single coordinated follow-up across both files makes sense.Would you like me to open a follow-up GitHub issue to track the holistic fix (correcting the HTTP status to 401 across
authMiddleware.tsandprotect.js)?✏️ Learnings added