Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Http/Http/src/Internal/ResponseCookies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ public void Delete(string key, CookieOptions options)
bool pathHasValue = !string.IsNullOrEmpty(options.Path);

Func<string, string, CookieOptions, bool> rejectPredicate;
if (domainHasValue)
if (domainHasValue && pathHasValue)
{
rejectPredicate = (value, encKeyPlusEquals, opts) =>
value.StartsWith(encKeyPlusEquals, StringComparison.OrdinalIgnoreCase) &&
value.IndexOf($"domain={opts.Domain}", StringComparison.OrdinalIgnoreCase) != -1 &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: in the future we can consider removing these string allocs, don't change this PR

value.IndexOf($"path={opts.Path}", StringComparison.OrdinalIgnoreCase) != -1;
}
else if (domainHasValue)
{
rejectPredicate = (value, encKeyPlusEquals, opts) =>
value.StartsWith(encKeyPlusEquals, StringComparison.OrdinalIgnoreCase) &&
Expand Down
59 changes: 59 additions & 0 deletions src/Http/Http/test/ResponseCookiesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -74,6 +75,64 @@ public void DeleteCookieShouldSetDefaultPath()
Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookieHeaderValues[0]);
}

[Fact]
public void DeleteCookieWithDomainAndPathDeletesPriorMatchingCookies()
{
var headers = (IHeaderDictionary)new HeaderDictionary();
var features = MakeFeatures(headers);
var responseCookies = new ResponseCookies(features);

var testCookies = new (string Key, string Path, string Domain)[]
{
new ("key1", "/path1/", null),
new ("key1", "/path2/", null),
new ("key2", "/path1/", "localhost"),
new ("key2", "/path2/", "localhost"),
};

foreach (var cookie in testCookies)
{
responseCookies.Delete(cookie.Key, new CookieOptions() { Domain = cookie.Domain, Path = cookie.Path });
}

var deletedCookies = headers.SetCookie.ToArray();
Assert.Equal(testCookies.Length, deletedCookies.Length);

Assert.Single(deletedCookies, cookie => cookie.StartsWith("key1", StringComparison.InvariantCulture) && cookie.Contains("path=/path1/"));
Assert.Single(deletedCookies, cookie => cookie.StartsWith("key1", StringComparison.InvariantCulture) && cookie.Contains("path=/path2/"));
Assert.Single(deletedCookies, cookie => cookie.StartsWith("key2", StringComparison.InvariantCulture) && cookie.Contains("path=/path1/") && cookie.Contains("domain=localhost"));
Assert.Single(deletedCookies, cookie => cookie.StartsWith("key2", StringComparison.InvariantCulture) && cookie.Contains("path=/path2/") && cookie.Contains("domain=localhost"));
Assert.All(deletedCookies, cookie => Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookie));
}

[Fact]
public void DeleteRemovesCookieWithDomainAndPathCreatedByAdd()
{
var headers = (IHeaderDictionary)new HeaderDictionary();
var features = MakeFeatures(headers);
var responseCookies = new ResponseCookies(features);

var testCookies = new (string Key, string Path, string Domain)[]
{
new ("key1", "/path1/", null),
new ("key1", "/path1/", null),
new ("key2", "/path1/", "localhost"),
new ("key2", "/path1/", "localhost"),
};

foreach (var cookie in testCookies)
{
responseCookies.Append(cookie.Key, cookie.Key, new CookieOptions() { Domain = cookie.Domain, Path = cookie.Path });
responseCookies.Delete(cookie.Key, new CookieOptions() { Domain = cookie.Domain, Path = cookie.Path });
}

var deletedCookies = headers.SetCookie.ToArray();
Assert.Equal(2, deletedCookies.Length);
Assert.Single(deletedCookies, cookie => cookie.StartsWith("key1", StringComparison.InvariantCulture) && cookie.Contains("path=/path1/"));
Assert.Single(deletedCookies, cookie => cookie.StartsWith("key2", StringComparison.InvariantCulture) && cookie.Contains("path=/path1/") && cookie.Contains("domain=localhost"));
Assert.All(deletedCookies, cookie => Assert.Contains("expires=Thu, 01 Jan 1970 00:00:00 GMT", cookie));
}

[Fact]
public void DeleteCookieWithCookieOptionsShouldKeepPropertiesOfCookieOptions()
{
Expand Down