Skip to content

Commit 36ca30c

Browse files
authored
Fix cookie null regression #45665 (#45715)
1 parent 85f905e commit 36ca30c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/Http/Http/test/RequestCookiesCollectionTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,29 @@ public void ParseManyCookies()
4444

4545
Assert.Equal(12, cookies.Count);
4646
}
47+
48+
[Theory]
49+
[InlineData(",", null)]
50+
[InlineData(";", null)]
51+
[InlineData("er=dd,cc,bb", new[] { "dd" })]
52+
[InlineData("er=dd,err=cc,errr=bb", new[] { "dd", "cc", "bb" })]
53+
[InlineData("errorcookie=dd,:(\"sa;", new[] { "dd" })]
54+
[InlineData("s;", null)]
55+
public void ParseInvalidCookies(string cookieToParse, string[] expectedCookieValues)
56+
{
57+
var cookies = RequestCookieCollection.Parse(new StringValues(new[] { cookieToParse }));
58+
59+
if (expectedCookieValues == null)
60+
{
61+
Assert.Equal(0, cookies.Count);
62+
return;
63+
}
64+
65+
Assert.Equal(expectedCookieValues.Length, cookies.Count);
66+
for (int i = 0; i < expectedCookieValues.Length; i++)
67+
{
68+
var value = expectedCookieValues[i];
69+
Assert.Equal(value, cookies.ElementAt(i).Value);
70+
}
71+
}
4772
}

src/Http/Shared/CookieHeaderParserShared.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ public static bool TryParseValues(StringValues values, IDictionary<string, strin
2929
if (TryParseValue(value, ref index, supportsMultipleValues, out var parsedName, out var parsedValue))
3030
{
3131
// The entry may not contain an actual value, like " , "
32-
var name = enableCookieNameEncoding ? Uri.UnescapeDataString(parsedName.Value.Value!) : parsedName.Value.Value!;
33-
store[name] = Uri.UnescapeDataString(parsedValue.Value.Value!);
34-
hasFoundValue = true;
32+
if (parsedName != null && parsedValue != null)
33+
{
34+
var name = enableCookieNameEncoding ? Uri.UnescapeDataString(parsedName.Value.Value!) : parsedName.Value.Value!;
35+
store[name] = Uri.UnescapeDataString(parsedValue.Value.Value!);
36+
hasFoundValue = true;
37+
}
3538
}
3639
else
3740
{

0 commit comments

Comments
 (0)