Skip to content

setCookie turns finite out-of-range expires values into a Unix-epoch expiry #6123

Open

Description

@Roshanraj2580

Bug Description

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'

const assert = require('node:assert/strict')
const { test } = require('node:test')
const { Headers, setCookie } = require('undici')

for (const expires of [2 ** 64, -(2 ** 64), Number.MAX_VALUE, -Number.MAX_VALUE]) {
  test(`finite expiry ${expires} outside the Date range`, () => {
    const headers = new Headers()
    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.

Environment

  • Windows amd64
  • Node.js v24.22.2
  • Undici 8.12.3, current main 489bc9d

Additional context

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.

Activity

  1. hamarshehmhmd commented on Nov 6, 2026

    @hamarshehmhmd

    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'] before stringify 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 (typeof value === 'number') {
        const maxTimeValue = 8640000000000000
        if (Number.isFinite(value) && Math.abs(value) > maxTimeValue) {
          return null
        }
        return webidl.converters['unsigned long long'](value)
      }
      return new Date(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
    • node --test test/cookie/expires-out-of-range.js → 5 passed
    • Existing test/cookie/cookies.js → 26 passed

    Happy to open a PR.

  2. added a commit that references this issue on Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions