CWE: CWE-384: Session Fixation
Affected runtime: ASP.NET Core (latest)
Affected source code: CookieHeaderParserShared.cs
The cookie parser in ASP.NET Core allows an attacker to spoof an HttpOnly cookie, enabling unauthorized overwrite of its value.
Although cookies marked as HttpOnly should not be accessible to JavaScript by design, an attacker with a XSS capability could set or overwrite protected cookies, including session identifiers or authentication tokens. This enables session fixation, account impersonation, or persistent access, as outlined in CWE-384 and related session management weaknesses.
ASP.NET Core's cookie parsing logic does not strictly follow RFC 6265 (HTTP State Management Mechanism) which prohibits certain characters in cookie names.
According to the RFC, cookie names must not include the following characters:
()<>@,;:\"/[]?={} \t
Despite this, the parser tolerates malformed or ambiguous input, allowing a specially crafted cookie name to be interpreted in such a way that it overwrites a legitimate HttpOnly cookie.
Assume the backend sets the following cookie:
Set-Cookie: session_id=secure; Path=/; HttpOnly
The attacker, using XSS, injects a spoofed cookie:
document.cookie = 'x@session_id=spoofed; Path=/';The resulting Cookie header becomes:
Cookie: session_id=secure; x@session_id=spoofed
ASP.NET Core's cookie parser processes this as follows:
- parses
session_id=securecorrectly - encounters
x@session_id=spoofedand treats this as a secondsession_identry, overwriting the first one
As a result, the backend sees:
context.Request.Cookies["session_id"] == "spoofed"This behavior bypasses the intended protection of the HttpOnly flag. This technique works with other special characters: [, ,, etc.
I've provided two files:
- Program.cs
- cookie.csproj
Steps to reproduce:
- run with
dotnet runand open http://localhost:5000/ - use the form to set the
session_idcookie (via backend withHttpOnlyflag) - observe the correct value in response from the server
- click "spoof cookie" to inject a second cookie
- observe the
session_idnow equals tospoofeddespiteHttpOnlybeing set
Sanitize and strictly validate cookie names against RFC 6265 in the parsing logic. Cookie names containing illegal characters should be rejected.