Skip to content

Commit 8211a1c

Browse files
Tratcheranalogrelay
authored andcommitted
[2.1] CookieChunkingManager needs to flow the Secure attribute… (#17953)
1 parent 164ddfd commit 8211a1c

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

eng/PatchConfig.props

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,10 @@ Later on, this will be checked using this condition:
5050
Microsoft.AspNetCore.SignalR.Core;
5151
</PackagesInPatch>
5252
</PropertyGroup>
53+
<PropertyGroup Condition=" '$(VersionPrefix)' == '2.1.16' ">
54+
<PackagesInPatch>
55+
Microsoft.AspNetCore.Authentication.Cookies;
56+
Microsoft.AspNetCore.Mvc.Core;
57+
</PackagesInPatch>
58+
</PropertyGroup>
5359
</Project>

src/Security/Authentication/OpenIdConnect/samples/OpenIdConnectSample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Startup(IHostingEnvironment env)
4545

4646
private void CheckSameSite(HttpContext httpContext, CookieOptions options)
4747
{
48-
if (options.SameSite > (SameSiteMode)(-1))
48+
if (options.SameSite == SameSiteMode.None)
4949
{
5050
var userAgent = httpContext.Request.Headers["User-Agent"].ToString();
5151
// TODO: Use your User Agent library of choice here.

src/Security/Authentication/test/WsFederation/WsFederationTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -190,7 +190,7 @@ public async Task RemoteSignoutRequestTriggersSignout()
190190
response.EnsureSuccessStatusCode();
191191

192192
var cookie = response.Headers.GetValues(HeaderNames.SetCookie).Single();
193-
Assert.Equal(".AspNetCore.Cookies=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; samesite=lax", cookie);
193+
Assert.Equal(".AspNetCore.Cookies=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; samesite=lax; httponly", cookie);
194194
Assert.Equal("OnRemoteSignOut", response.Headers.GetValues("EventHeader").Single());
195195
Assert.Equal("", await response.Content.ReadAsStringAsync());
196196
}
@@ -440,4 +440,4 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
440440
}
441441
}
442442
}
443-
}
443+
}

src/Security/CookiePolicy/test/CookieChunkingTests.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@ public void AppendLargeCookie_Appended()
2121
Assert.Equal("TestCookie=" + testString + "; path=/; samesite=lax", values[0]);
2222
}
2323

24+
[Fact]
25+
public void AppendLargeCookie_WithOptions_Appended()
26+
{
27+
HttpContext context = new DefaultHttpContext();
28+
var now = DateTimeOffset.UtcNow;
29+
var options = new CookieOptions
30+
{
31+
Domain = "foo.com",
32+
HttpOnly = true,
33+
SameSite = SameSiteMode.Strict,
34+
Path = "/bar",
35+
Secure = true,
36+
Expires = now.AddMinutes(5),
37+
MaxAge = TimeSpan.FromMinutes(5)
38+
};
39+
var testString = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
40+
new ChunkingCookieManager() { ChunkSize = null }.AppendResponseCookie(context, "TestCookie", testString, options);
41+
42+
var values = context.Response.Headers["Set-Cookie"];
43+
Assert.Single(values);
44+
Assert.Equal($"TestCookie={testString}; expires={now.AddMinutes(5).ToString("R")}; max-age=300; domain=foo.com; path=/bar; secure; samesite=strict; httponly", values[0]);
45+
}
46+
2447
[Fact]
2548
public void AppendLargeCookieWithLimit_Chunked()
2649
{
@@ -112,19 +135,19 @@ public void DeleteChunkedCookieWithOptions_AllDeleted()
112135
HttpContext context = new DefaultHttpContext();
113136
context.Request.Headers.Append("Cookie", "TestCookie=chunks-7");
114137

115-
new ChunkingCookieManager().DeleteCookie(context, "TestCookie", new CookieOptions() { Domain = "foo.com" });
138+
new ChunkingCookieManager().DeleteCookie(context, "TestCookie", new CookieOptions() { Domain = "foo.com", Secure = true });
116139
var cookies = context.Response.Headers["Set-Cookie"];
117140
Assert.Equal(8, cookies.Count);
118141
Assert.Equal(new[]
119142
{
120-
"TestCookie=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; samesite=lax",
121-
"TestCookieC1=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; samesite=lax",
122-
"TestCookieC2=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; samesite=lax",
123-
"TestCookieC3=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; samesite=lax",
124-
"TestCookieC4=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; samesite=lax",
125-
"TestCookieC5=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; samesite=lax",
126-
"TestCookieC6=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; samesite=lax",
127-
"TestCookieC7=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; samesite=lax",
143+
"TestCookie=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; secure; samesite=lax",
144+
"TestCookieC1=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; secure; samesite=lax",
145+
"TestCookieC2=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; secure; samesite=lax",
146+
"TestCookieC3=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; secure; samesite=lax",
147+
"TestCookieC4=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; secure; samesite=lax",
148+
"TestCookieC5=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; secure; samesite=lax",
149+
"TestCookieC6=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; secure; samesite=lax",
150+
"TestCookieC7=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=foo.com; path=/; secure; samesite=lax",
128151
}, cookies);
129152
}
130153
}

src/Shared/ChunkingCookieManager/ChunkingCookieManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public void AppendResponseCookie(HttpContext context, string key, string value,
169169
HttpOnly = options.HttpOnly,
170170
Path = options.Path,
171171
Secure = options.Secure,
172+
MaxAge = options.MaxAge,
172173
};
173174

174175
var templateLength = template.ToString().Length;
@@ -285,8 +286,10 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options)
285286
Path = options.Path,
286287
Domain = options.Domain,
287288
SameSite = options.SameSite,
289+
Secure = options.Secure,
288290
IsEssential = options.IsEssential,
289291
Expires = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc),
292+
HttpOnly = options.HttpOnly,
290293
});
291294

292295
for (int i = 1; i <= chunks; i++)
@@ -300,8 +303,10 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options)
300303
Path = options.Path,
301304
Domain = options.Domain,
302305
SameSite = options.SameSite,
306+
Secure = options.Secure,
303307
IsEssential = options.IsEssential,
304308
Expires = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc),
309+
HttpOnly = options.HttpOnly,
305310
});
306311
}
307312
}

0 commit comments

Comments
 (0)