Skip to content

Cookie Expires parsing does not follow the RFC 6265 cookie-date algorithm #6216

Open

Description

@matthewalberto

parse.js currently uses JavaScript's new Date(attributeValue) to parse the Expires attribute (implementation at eaf7b073). Although the comment references RFC 6265 section 5.1.1, the implementation uses JavaScript's general-purpose date parser rather than the specified cookie-date algorithm.

This causes differences in timezone handling, two-digit-year interpretation and accepted date formats. For example, a timezone-less date is interpreted using the machine's local timezone, explicit offsets are applied, and values that do not satisfy the cookie-date algorithm can still be accepted.

Actual Behavior

The following results were observed with undici 8.12.4 in UTC. The timezone-less example also produces a different result (02:18:14Z) on a UTC+8 machine.

Expires value Actual (UTC) Expected (RFC 6265)
Wed, 09 Jun 2021 10:18:14 10:18:14Z (02:18:14Z in UTC+8) 10:18:14Z
Wed, 09 Jun 2021 10:18:14 +0800 02:18:14Z 10:18:14Z
Thu, 01 Jan 60 00:00:00 GMT 1960-01-01 2060-01-01
1 2001-01-01 Ignore Expires
Jun 9 2021 2021-06-09T00:00Z Ignore Expires
2021-06-09T10:18:14Z Accepted Ignore Expires
01 Jan 1600 00:00:00 GMT Accepted Ignore Expires

Standard IMF-fixdate values with GMT already work correctly. The problem is the difference between JavaScript date parsing and the RFC algorithm, rather than a failure of all existing Expires dates.

Standalone reproduction script

The following standalone node --test script reproduces the issue against undici 8.12.4 and passes with the proposed fix.

'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const { parseCookie } = require('undici')

// RFC 6265 section 5.1.1: the cookie-date is always read as UTC, years 0-69
// are 20xx, and a value without a time, day, month and year is ignored.

test('Expires with no zone is read as UTC', () => {
  const { expires } = parseCookie('id=a; Expires=Wed, 09 Jun 2021 10:18:14')
  assert.strictEqual(expires?.toISOString(), '2021-06-09T10:18:14.000Z')
})

test('Expires ignores a zone offset', () => {
  const { expires } = parseCookie('id=a; Expires=Wed, 09 Jun 2021 10:18:14 +0800')
  assert.strictEqual(expires?.toISOString(), '2021-06-09T10:18:14.000Z')
})

test('Expires two-digit year 60 is 2060', () => {
  const { expires } = parseCookie('id=a; Expires=Thu, 01 Jan 60 00:00:00 GMT')
  assert.strictEqual(expires?.toISOString(), '2060-01-01T00:00:00.000Z')
})

for (const value of ['1', 'Jun 9 2021', '2021-06-09T10:18:14Z', 'Fri, 01 Jan 1600 00:00:00 GMT']) {
  test(`Expires=${value} is ignored`, () => {
    assert.strictEqual(parseCookie(`id=a; Expires=${value}`).expires, undefined)
  })
}
  • Released undici 8.12.4: 6 of 7 tests fail in UTC and 7 of 7 fail in local time (UTC+8).
  • Proposed fix: 7 of 7 tests pass.

Environment

  • OS: Windows 11
  • Node.js: v22.22.2
  • undici: 8.12.4
  • Also checked against main at eaf7b073.

Activity

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