Skip to content

Auto-port 4.1: Stop DateFormatter trailing token from running past the parse end - #16968

Merged
normanmaurer merged 2 commits into
4.1from
auto-port-pr-16958-to-4.1
Jun 18, 2026
Merged

normanmaurer merged 2 commits into
4.1from
auto-port-pr-16958-to-4.1

Conversation

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port of #16958 to 4.1
Cherry-picked commit: 692d23f


Problem

DateFormatter.parseHttpDate(txt, start, end) is documented to parse only the [start, end) substring. Its tokenizer loop correctly stops at end, but the trailing token — the one still open when the loop finishes — was terminated at txt.length() instead of end:

// terminate trailing token
return tokenStart != -1 && parseToken(txt, tokenStart, txt.length());

When end < txt.length() and the date's last token is the one that completes the parse, the trailing token swallows the bytes after end and fails to parse.

This is reachable in practice through cookies. A Set-Cookie header like foo=bar; Expires=<date>; Path=/ makes ClientCookieDecoder call parseHttpDate(header, start, end) with end pointing at the ; before Path. RFC 6265 §5.1.1 cookie-date tokens are order-independent, so a valid date whose year (or day) is the last token — e.g. Sun 08:49:37 06 Nov 1994 — parses fine in isolation but returns null as a substring, silently dropping the expiry and downgrading the cookie to a session cookie.

Standard Sun, 06 Nov 1994 08:49:37 GMT ordering does not trigger it (the time token completes the parse mid-loop, before the trailing GMT), which is why existing tests miss it.

Fix

Terminate the trailing token at end rather than txt.length(). Since end <= txt.length() always, this only ever shrinks the trailing token to the intended bound; the full-string parseHttpDate(txt) path (where end == txt.length()) is unaffected.

Added a DateFormatterTest case that parses such a date both in isolation and as a substring and asserts they agree.

Result

parseHttpDate honours the end bound for the trailing token; valid order-independent cookie dates followed by other attributes now parse correctly. Full DateFormatterTest (14) passes.

…6958)

## Problem

`DateFormatter.parseHttpDate(txt, start, end)` is documented to parse
only the `[start, end)` substring. Its tokenizer loop correctly stops at
`end`, but the **trailing token** — the one still open when the loop
finishes — was terminated at `txt.length()` instead of `end`:

```java
// terminate trailing token
return tokenStart != -1 && parseToken(txt, tokenStart, txt.length());
```

When `end < txt.length()` and the date's *last* token is the one that
completes the parse, the trailing token swallows the bytes after `end`
and fails to parse.

This is reachable in practice through cookies. A `Set-Cookie` header
like `foo=bar; Expires=<date>; Path=/` makes `ClientCookieDecoder` call
`parseHttpDate(header, start, end)` with `end` pointing at the `;`
before `Path`. RFC 6265 §5.1.1 cookie-date tokens are
**order-independent**, so a valid date whose year (or day) is the last
token — e.g. `Sun 08:49:37 06 Nov 1994` — parses fine in isolation but
returns `null` as a substring, silently dropping the expiry and
downgrading the cookie to a session cookie.

Standard `Sun, 06 Nov 1994 08:49:37 GMT` ordering does not trigger it
(the time token completes the parse mid-loop, before the trailing
`GMT`), which is why existing tests miss it.

## Fix

Terminate the trailing token at `end` rather than `txt.length()`. Since
`end <= txt.length()` always, this only ever shrinks the trailing token
to the intended bound; the full-string `parseHttpDate(txt)` path (where
`end == txt.length()`) is unaffected.

Added a `DateFormatterTest` case that parses such a date both in
isolation and as a substring and asserts they agree.

## Result

`parseHttpDate` honours the `end` bound for the trailing token; valid
order-independent cookie dates followed by other attributes now parse
correctly. Full `DateFormatterTest` (14) passes.

(cherry picked from commit 692d23f)
@normanmaurer
normanmaurer merged commit dc68016 into 4.1 Jun 18, 2026
17 of 18 checks passed
@normanmaurer
normanmaurer deleted the auto-port-pr-16958-to-4.1 branch June 18, 2026 10:29
@normanmaurer normanmaurer added this to the 4.1.136.Final milestone Jun 18, 2026
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
…e parse end (netty#16968)

Auto-port of netty#16958 to 4.1
Cherry-picked commit: 692d23f

---
## Problem

`DateFormatter.parseHttpDate(txt, start, end)` is documented to parse
only the `[start, end)` substring. Its tokenizer loop correctly stops at
`end`, but the **trailing token** — the one still open when the loop
finishes — was terminated at `txt.length()` instead of `end`:

```java
// terminate trailing token
return tokenStart != -1 && parseToken(txt, tokenStart, txt.length());
```

When `end < txt.length()` and the date's *last* token is the one that
completes the parse, the trailing token swallows the bytes after `end`
and fails to parse.

This is reachable in practice through cookies. A `Set-Cookie` header
like `foo=bar; Expires=<date>; Path=/` makes `ClientCookieDecoder` call
`parseHttpDate(header, start, end)` with `end` pointing at the `;`
before `Path`. RFC 6265 §5.1.1 cookie-date tokens are
**order-independent**, so a valid date whose year (or day) is the last
token — e.g. `Sun 08:49:37 06 Nov 1994` — parses fine in isolation but
returns `null` as a substring, silently dropping the expiry and
downgrading the cookie to a session cookie.

Standard `Sun, 06 Nov 1994 08:49:37 GMT` ordering does not trigger it
(the time token completes the parse mid-loop, before the trailing
`GMT`), which is why existing tests miss it.

## Fix

Terminate the trailing token at `end` rather than `txt.length()`. Since
`end <= txt.length()` always, this only ever shrinks the trailing token
to the intended bound; the full-string `parseHttpDate(txt)` path (where
`end == txt.length()`) is unaffected.

Added a `DateFormatterTest` case that parses such a date both in
isolation and as a substring and asserts they agree.

## Result

`parseHttpDate` honours the `end` bound for the trailing token; valid
order-independent cookie dates followed by other attributes now parse
correctly. Full `DateFormatterTest` (14) passes.

Co-authored-by: Guimu <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants