You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
setCookie turns finite out-of-range expires values into a Unix-epoch expiry #6123
The Date-range check from #5832 can be bypassed by finite numbers that wrap to an in-range value in the Cookie dictionary's unsigned long long conversion. For example, 2 ** 64 becomes 0, so an invalid expiry is serialized as an immediate expiry at the Unix epoch.
This is a follow-up to merged #5832 / closed #5831, rather than the separate non-finite-input behavior. The existing tests deliberately preserve the conversion of NaN and infinities to zero; those inputs can remain unchanged in a focused fix.
Reproduction
'use strict'constassert=require('node:assert/strict')const{ test }=require('node:test')const{ Headers, setCookie }=require('undici')for(constexpiresof[2**64,-(2**64),Number.MAX_VALUE,-Number.MAX_VALUE]){test(`finite expiry ${expires} outside the Date range`,()=>{constheaders=newHeaders()setCookie(headers,{name: 'session',value: 'value', expires })assert.equal(headers.get('set-cookie'),'session=value')})}
Run with node --test repro.cjs. All four assertions fail on current main. A control with a real numeric zero still correctly produces the Unix-epoch expiry.
Expected Behavior
Ignore a finite numeric expiry outside the Date range, consistent with #5832 and the equivalent invalid Date input. An invalid finite timestamp should not become a valid date through integer wraparound.
Actual Behavior
All four values serialize as session=value; Expires=Thu, 01 Jan 1970 00:00:00 GMT. By comparison, the smaller out-of-range value 8640000000000001 already omits Expires.
The conversion happens in lib/web/cookies/index.js before stringify can inspect the timestamp. A focused correction can let Date validate finite numeric inputs before integer wraparound, while preserving the separately tested non-finite behavior.
Reproduced — 2 ** 64 / Number.MAX_VALUE etc. end up as Expires=Thu, 01 Jan 1970… because of integer wraparound before the Date-range check.
Root cause: the Cookie dictionary converter in lib/web/cookies/index.js runs finite numbers through webidl.converters['unsigned long long']beforestringify checks the Date range. 2 ** 64 becomes 0, which then serializes as a valid epoch expiry. #5832 already ignores out-of-range values in stringify, but wraparound happens earlier.
Fix: reject finite numbers outside ±8640000000000000 in the converter (return null) before the unsigned-long-long conversion. Non-finite inputs still go through the existing converter so NaN/Infinity→0 behavior stays intact:
converter: webidl.nullableConverter((value)=>{if(typeofvalue==='number'){constmaxTimeValue=8640000000000000if(Number.isFinite(value)&&Math.abs(value)>maxTimeValue){returnnull}returnwebidl.converters['unsigned long long'](value)}returnnewDate(value)}),
Verified:
Added test/cookie/expires-out-of-range.js covering 2**64, -(2**64), ±Number.MAX_VALUE, plus a control that expires: 0 still emits the epoch Expires
Activity
hamarshehmhmd commentedon Nov 6, 2026
Reproduced —
2 ** 64/Number.MAX_VALUEetc. end up asExpires=Thu, 01 Jan 1970…because of integer wraparound before the Date-range check.Root cause: the Cookie dictionary converter in
lib/web/cookies/index.jsruns finite numbers throughwebidl.converters['unsigned long long']beforestringifychecks the Date range.2 ** 64becomes0, which then serializes as a valid epoch expiry.#5832already ignores out-of-range values instringify, but wraparound happens earlier.Fix: reject finite numbers outside
±8640000000000000in the converter (returnnull) before the unsigned-long-long conversion. Non-finite inputs still go through the existing converter so NaN/Infinity→0 behavior stays intact:Verified:
test/cookie/expires-out-of-range.jscovering2**64,-(2**64),±Number.MAX_VALUE, plus a control thatexpires: 0still emits the epoch Expiresnode --test test/cookie/expires-out-of-range.js→ 5 passedtest/cookie/cookies.js→ 26 passedHappy to open a PR.
feat(#2): Fix(cookies): ignore finite out-of-range numeric expires va…