CookieContainer: when a Set-Cookie has both Expires and Max-Age, the first one wins instead of Max-Age
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 74/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- csharp
- Área
- networking
Línea de trabajo
Empieza en src/libraries/Common/src/System/Net/CookieParser.cs, en los casos CookieToken.Expires y CookieToken.MaxAge, que comparten la única bandera expiresSet. Haz un seguimiento de los dos atributos por separado para que un Max-Age analizado correctamente gane en cualquier orden. Primero reproduce el problema con el fragmento del issue para confirmar la cabecera con Expires primero y el caso de eliminación con Max-Age=0. Añade pruebas de regresión para ambos órdenes de atributos, con un Max-Age positivo y con Max-Age=0. Está terminado cuando ambos órdenes producen la misma expiración y el caso de eliminación no deja ninguna cookie.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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
- Lenguaje dominante
- C#
- Estrellas
- 18.5k
- Forks
- 5.8k
- Merge medio
- 2 d 16 h
- PR fusionados (30 d)
- 611
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de dotnet/runtime
-
area-Tools-ILLink untriaged
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
dotnet/runtime#138255 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
area-CodeGen-coreclr untriaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
dotnet/runtime#138181 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
area-System.Collections untriaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
dotnet/runtime#138090 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
area-System.Text.Json untriaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
dotnet/runtime#138089 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
area-Setup untriaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
dotnet/runtime#137376 · 1 comentario ·
Los mantenedores suelen responder en 1 día
Todos los issues de dotnet/runtime
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 2 días
-
area-fundamentals bug untriaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
dotnet/extensions#7979 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
QuantConnect/Lean#10236 ·
Los mantenedores suelen responder en 1 día
-
priority:P2
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
-
[Core] IPlatformServices.PrefersDarkTheme: the desktop's theme as the signed-in user sees itAbiertoarea: core bug help wanted ready
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
ZaidX88/sluice#265 · 1 comentario ·
Los mantenedores suelen responder en 1 día