Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 59c2fab

Browse files
geoffkizerstephentoub
authored andcommitted
fix SocketsHttpHandler to ignore invalid Set-Cookie (#27865)
* fix SocketsHttpHandler to ignore invalid Set-Cookie * log when invalid cookie is ignored * disable test for netfx
1 parent bce4515 commit 59c2fab

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/CookieHelper.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ public static void ProcessReceivedCookies(HttpResponseMessage response, CookieCo
2222
Uri requestUri = response.RequestMessage.RequestUri;
2323
for (int i = 0; i < valuesArray.Length; i++)
2424
{
25-
cookieContainer.SetCookies(requestUri, valuesArray[i]);
25+
try
26+
{
27+
cookieContainer.SetCookies(requestUri, valuesArray[i]);
28+
}
29+
catch (CookieException)
30+
{
31+
// Ignore invalid Set-Cookie header and continue processing.
32+
if (NetEventSource.IsEnabled)
33+
{
34+
NetEventSource.Info(response, $"Invalid Set-Cookie '{valuesArray[i]}' ignored.");
35+
}
36+
}
2637
}
2738
}
2839
}

src/System.Net.Http/tests/FunctionalTests/HttpCookieProtocolTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,43 @@ await LoopbackServer.CreateServerAsync(async (server, url) =>
447447
});
448448
}
449449

450+
[Fact]
451+
public async Task GetAsync_ReceiveInvalidSetCookieHeader_ValidCookiesAdded()
452+
{
453+
if (IsNetfxHandler)
454+
{
455+
// NetfxHandler incorrectly only processes one valid cookie
456+
return;
457+
}
458+
459+
await LoopbackServer.CreateServerAsync(async (server, url) =>
460+
{
461+
HttpClientHandler handler = CreateHttpClientHandler();
462+
463+
using (HttpClient client = new HttpClient(handler))
464+
{
465+
Task<HttpResponseMessage> getResponseTask = client.GetAsync(url);
466+
Task<List<string>> serverTask = server.AcceptConnectionSendResponseAndCloseAsync(
467+
HttpStatusCode.OK,
468+
$"Set-Cookie: A=1; Path=/;Expires=asdfsadgads\r\n" + // invalid Expires
469+
$"Set-Cookie: B=2; Path=/\r\n" +
470+
$"Set-Cookie: C=3; Path=/\r\n",
471+
s_simpleContent);
472+
await TestHelper.WhenAllCompletedOrAnyFailed(getResponseTask, serverTask);
473+
474+
CookieCollection collection = handler.CookieContainer.GetCookies(url);
475+
Assert.Equal(2, collection.Count);
476+
477+
// Convert to array so we can more easily process contents, since CookieCollection does not implement IEnumerable<Cookie>
478+
Cookie[] cookies = new Cookie[3];
479+
collection.CopyTo(cookies, 0);
480+
481+
Assert.Contains(cookies, c => c.Name == "B" && c.Value == "2");
482+
Assert.Contains(cookies, c => c.Name == "C" && c.Value == "3");
483+
}
484+
});
485+
}
486+
450487
[Fact]
451488
public async Task GetAsyncWithRedirect_ReceiveSetCookie_CookieSent()
452489
{

0 commit comments

Comments
 (0)