Skip to content

CookieHeaderParserShared throws exception when last cookie contains invalid character #45014

@JmlSaul

Description

@JmlSaul

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Cookie String: keya=valuea; keyb=valueb; errorcookie=dd,:("sa;
the last part of cookie string above is an invalid cookie and the whole cookie string ends with ;,this will throw exception like below, and all cookies fails to be read.

Exception: Nullable object must have a value.

at Microsoft.Net.Http.Headers.CookieHeaderParserShared.TryParseValues(StringValues values, IDictionary`2 store, Boolean enableCookieNameEncoding, Boolean supportsMultipleValues)
at Microsoft.AspNetCore.Http.RequestCookieCollection.ParseInternal(StringValues values, Boolean enableCookieNameEncoding)
at Microsoft.AspNetCore.Http.Features.RequestCookiesFeature.get_Cookies()
... try get value from cookies

Cookie String: keya=valuea; keyb=valueb; errorcookie=dd,:("sa
the last part of this cookie string above is an invalid cookie and the whole cookie string ends without ;,this will not throw exception, the parser will ignore errorcookie

Expected Behavior

error cookie be ignored, and normal cookie still readable.

Steps To Reproduce

No response

Exceptions (if any)

Nullable object must have a value.

at Microsoft.Net.Http.Headers.CookieHeaderParserShared.TryParseValues(StringValues values, IDictionary`2 store, Boolean enableCookieNameEncoding, Boolean supportsMultipleValues)
at Microsoft.AspNetCore.Http.RequestCookieCollection.ParseInternal(StringValues values, Boolean enableCookieNameEncoding)
at Microsoft.AspNetCore.Http.Features.RequestCookiesFeature.get_Cookies()

.NET Version

7.0

Anything else?

visual studio 2022 17.4.0

Metadata

Metadata

Assignees

Labels

area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsbugThis issue describes a behavior which is not expected - a bug.good first issueGood for newcomers.help wantedUp for grabs. We would accept a PR to help resolve this issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions