nextauthjs / nextauthjs/next-auth

`getServerSession`, if called more than once, generate and appends multiple `next-auth.session_token` to `set-cookie` header to response object

Open
#7,485 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
TypeScript
Stars
28.4k
Forks
4k
PR merge metrics
No merged PRs in 30d

Description

Environment
next 12.1.5
react 18.0.0
next-auth 4.20.1
Reproduction URL
Describe the issue

I have an API route that invokes getServerSession more than once. It's response header set-cookie was bloated with next-auth.session_token to the point HPE_HEADER_OVERFLOW was throw in proxy server.

const handler: NextApiHandler = async (req, res) => {
    const data1 = await fetchData1(req, res)
    const data2 = await fetchData2(req, res)
}

const fetchData1 = async(req, res) => {
    await getServerSession(req, res, conf)
    ...
}
const fetchData2 = async(req, res) => {
    await getServerSession(req, res, conf)
    ...
}

image

How to reproduce
Expected behavior

Consider that method name is "getServerSession", multiple calls should still generate and set to header only once (would be better if we can detect and re-use previous call result)

Code in question

https://github.com/nextauthjs/next-auth/blob/b96f01319c751c7d62433c24fdb9f3f8f1e572c1/packages/next-auth/src/next/index.ts#L210-L222

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at packages/next-auth/src/next/index.ts lines 210-222 and inspect how repeated getServerSession calls update the response headers. Reproduce the issue with an API route that calls getServerSession twice, then verify that the response contains only one next-auth.session_token cookie and does not accumulate headers.

Written by the indexing model from the issue text.

Assessment

Tech stack
next.js, typescript
Domain
api, authentication
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.