CookieContainer: when a Set-Cookie has both Expires and Max-Age, the first one wins instead of Max-Age
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 74/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- csharp
- 領域
- networking
調査の方向性
まず src/libraries/Common/src/System/Net/CookieParser.cs の CookieToken.Expires と CookieToken.MaxAge の分岐から始めてください。この2つは単一の expiresSet フラグを共有しています。正しく解析された Max-Age がどちらの順序でも優先されるように、2つの属性を別々に追跡してください。まず issue のスニペットで再現し、Expires が先のヘッダーと、Max-Age=0 による削除ケースを確認してください。正の Max-Age の場合と Max-Age=0 の場合について、両方の属性順序に対する回帰テストを追加してください。両方の順序で同じ有効期限になり、削除ケースで Cookie が残らなければ完了です。
索引モデルが issue の本文から書いたものです。
説明
Problem
When a Set-Cookie header contains valid Expires and Max-Age attributes, CookieContainer.SetCookies uses whichever appears first. This also affects HttpClient when its handler uses the cookie container. PHP's setcookie() emits Expires before Max-Age when it sends both.
A short-lived cookie can be dropped on receipt if the server's clock is far enough behind the client's that Expires is already in the past. In the synthetic deletion example below, a future Expires causes the container to keep a cookie with an empty value even though Max-Age=0 asks it to remove the cookie.
using System.Net;
var uri = new Uri("https://example.com/");
string expires = DateTime.UtcNow.AddMinutes(-10 + 5).ToString("R");
foreach (var header in new[] {
$"state=xyz; expires={expires}; Max-Age=300; path=/", // Expires first (PHP's order)
$"state=xyz; Max-Age=300; expires={expires}; path=/" }) // Max-Age first
{
var jar = new CookieContainer();
jar.SetCookies(uri, header);
Console.WriteLine($"{jar.GetCookies(uri).Count} cookie(s) kept for: {header}");
}
var jar2 = new CookieContainer();
jar2.SetCookies(uri, "session=abc; path=/");
jar2.SetCookies(uri, $"session=; expires={DateTime.UtcNow.AddDays(1):R}; Max-Age=0; path=/");
Console.WriteLine($"after Max-Age=0: {jar2.GetCookies(uri).Count} cookie(s)");
Output on .NET 8.0.31 and .NET 10.0.12:
0 cookie(s) kept for: state=xyz; expires=Wed, 25 Nov 2026 00:03:49 GMT; Max-Age=300; path=/
1 cookie(s) kept for: state=xyz; Max-Age=300; expires=Wed, 25 Nov 2026 00:03:49 GMT; path=/
after Max-Age=0: 1 cookie(s)
The first two headers differ only in attribute order. CookieContainer drops the Expires-first cookie and keeps the Max-Age-first cookie. The precedence rule was raised in a comment on #74794. That issue was closed after #76848 addressed the two-digit-year parsing problem in the Baidu repro.
Expected
RFC 6265, section 4.1.2.2:
If a cookie has both the Max-Age and the Expires attribute, the Max-Age attribute has precedence and controls the expiration date of the cookie.
Section 5.3, step 3, gives the same rule for user agents, whatever the order:
If the cookie-attribute-list contains an attribute with an attribute-name of "Max-Age": [...] Set the cookie's expiry-time to attribute-value of the last attribute in the cookie-attribute-list with an attribute-name of "Max-Age".
Otherwise, if the cookie-attribute-list contains an attribute with an attribute-name of "Expires" (and does not contain an attribute with an attribute-name of "Max-Age"): [...]
The first two headers should both set an expiration 300 seconds after receipt. The third should remove the existing session cookie.
Cause
In src/libraries/Common/src/System/Net/CookieParser.cs, Expires and Max-Age share one expiresSet flag, so the first one seen sets cookie.Expires and the second is skipped:
case CookieToken.Expires:
if (!expiresSet)
{
expiresSet = true;
...
case CookieToken.MaxAge:
if (!expiresSet)
{
expiresSet = true;
if (int.TryParse(CheckQuotedSpan(_tokenizer.Value), out int parsed))
{
cookie!.Expires = DateTime.UtcNow.AddSeconds(parsed);
Suggested fix
Suggested behavior
Use a successfully parsed Max-Age in preference to Expires in either attribute order. Add regression tests for both orders with a positive Max-Age and with Max-Age=0.
Tested with
- .NET 8.0.31 and .NET 10.0.12 on macOS arm64. The same code is on
main(0a4458e8e9, 30 Oct 2026):CookieParsershares oneexpiresSetflag between the two attributes. - In a separate .NET 10.0.12 run, I tested all 154 orders of zero or one
Expiresvalue (one hour ahead, one year ahead, 1970, or 9999), zero or oneMax-Agevalue (3600, 31536000, 0, or -1), and optionalHttpOnly, withPath=/fixed. The retained-cookie count or expiration differed from the RFC expectation in 48 cases, all withExpiresbeforeMax-Age.
Found with a new AI-assisted methodology from a Pullboard discovery. A paper describing it will follow.
Links
- Research: https://github.com/coreyolson/research
- Pullboard: https://pullboard.dev
- Pullboard notice: https://github.com/pullboard-dev/pullboard/issues/46
- 主要言語
- C#
- スター
- 18.5k
- フォーク
- 5.8k
- 平均マージ
- 2日 16時間
- マージ済み PR(30日)
- 611
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
dotnet/runtime のほかの issue
-
area-Tools-ILLink untriaged
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
dotnet/runtime#138255 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area-CodeGen-coreclr untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
dotnet/runtime#138181 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area-System.Collections untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
dotnet/runtime#138090 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area-System.Text.Json untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
dotnet/runtime#138089 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area-Setup untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
dotnet/runtime#137376 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 2 日以内に返信
-
area-fundamentals bug untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
dotnet/extensions#7979 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
QuantConnect/Lean#10236 ·
メンテナーはふだん 1 日以内に返信
-
priority:P2
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
area: core bug help wanted ready
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
ZaidX88/sluice#265 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信