refresh operation throws a TypeError (500) when the session is revoked between the JWT strategy read and its own read

Aperta Adatta ai principianti
#19,867 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@bfs2021 ci sta già lavorando.

Dal 23/11/2026.

  • #19879 di @bfs2021 — aperta
  • #20037 di @alex00010 — aperta
  • #20038 di @shubhammittal92 — aperta

Valutazione

Difficoltà
1/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript

Direzione di ricerca

Inizia in packages/payload/src/auth/operations/refresh.ts alla ricerca user.sessions.find(...) e alla scrittura su existingSession.expiresAt che la segue; la guardia su user.sessions/sid sopra non copre una sessione che è scomparsa. Esegui il repro collegato nell'issue (pnpm repro contro la linea 3.x) per vedere 200/500/500, e leggi l'hook beforeOperation nel suo src/collections/Users.ts per vedere la revoca a metà richiesta. Fatto assomiglia a: il witness case restituisce ancora 200 ed entrambi i casi di revoca restituiscono 403 Forbidden, coerentemente con il modo in cui la strategia JWT tratta una sessione mancante.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area: core Bug Payload OSS
Describe the Bug

With auth.useSessions enabled (the default), POST /api/{auth-collection}/refresh-token answers 500 instead of 403 when the caller's session is removed from the user document after the JWT strategy has authenticated the request and before refreshOperation reads the user again.

packages/payload/src/auth/operations/refresh.ts (3.x) looks the session up and writes on it with no check that it was found:

if (!Array.isArray(user.sessions) || !sid) {
  throw new Forbidden(args.req.t)
}

const existingSession = user.sessions.find(({ id }: { id: number }) => id === sid)

const now = new Date()
const tokenExpInMs = collectionConfig.auth.tokenExpiration * 1000
existingSession.expiresAt = new Date(now.getTime() + tokenExpInMs)

user.sessions is still an array (empty, or holding other sessions), so the first check passes, find returns undefined, and the assignment throws:

TypeError: Cannot set properties of undefined (setting 'expiresAt')

Anything that revokes the session while the request is in flight falls into that window: a logout from another tab, or a server-side revocation (sessions: []) made by another request or by a hook. An admin tab left open sends this request on its own, which is how we met it in our server logs: a 500 and an error alert where a refusal was expected.

Expected: Forbidden (403), as the JWT strategy already treats a missing session as "no user".

Suggested fix, right after the lookup:

if (!existingSession) {
  throw new Forbidden(args.req.t)
}

main no longer has the problem (extendSession followed by if (!session)); this is for the 3.x line. Seen on 3.92.0, reproduced on 3.94.1.

Link to the code that reproduces this issue

https://github.com/JAPellerin/payload-refresh-repro

Reproduction Steps
  1. Clone the repository above (blank template, SQLite adapter, nothing to configure).
  2. pnpm install
  3. pnpm repro

The script logs in through the Local API, then sends the real REST request through handleEndpoints three times. A beforeOperation hook on users (src/collections/Users.ts) removes the caller's session during the refresh operation: it runs after the JWT strategy has accepted the session and before the operation reads the user again.

Output on 3.94.1:

POST /api/users/refresh-token
  200  session untouched (witness)
  500  own session removed mid-request
  500  every session removed mid-request

Expected: 200, 403, 403
Which area(s) are affected?

area: core

Environment Info
Binaries:
  Node: 26.11.1
  npm: 11.20.0
  Yarn: N/A
  pnpm: 11.28.5
Relevant Packages:
  payload: 3.94.1
  next: 16.4.6
  @payloadcms/db-sqlite: 3.94.1
  @payloadcms/drizzle: 3.94.1
  @payloadcms/graphql: 3.94.1
  @payloadcms/next/utilities: 3.94.1
  @payloadcms/richtext-lexical: 3.94.1
  @payloadcms/ui/shared: 3.94.1
  react: 19.2.9
  react-dom: 19.2.9
Operating System:
  Platform: linux
  Arch: x64
Lingua principale
TypeScript
Stelle
46.1k
Fork
4.5k
Merge medio
2g 15h
PR unite (30g)
238

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di payloadcms/payload

Tutte le issue di payloadcms/payload

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.