Skip to content

Commit e001fcf

Browse files
committed
Update SameSiteCookieManager to match blog, check null
1 parent 9e9c44d commit e001fcf

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

tests/Katana.Sandbox.WebServer/SameSiteCookieManager.cs

+46-12
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ public class SameSiteCookieManager : ICookieManager
1111
{
1212
private readonly ICookieManager _innerManager;
1313

14-
public SameSiteCookieManager()
15-
: this(new CookieManager())
16-
{
14+
public SameSiteCookieManager() : this(new CookieManager())
15+
{
1716
}
1817

1918
public SameSiteCookieManager(ICookieManager innerManager)
2019
{
2120
_innerManager = innerManager;
2221
}
2322

24-
public void AppendResponseCookie(IOwinContext context, string key, string value, CookieOptions options)
23+
public void AppendResponseCookie(IOwinContext context, string key, string value,
24+
CookieOptions options)
2525
{
2626
CheckSameSite(context, options);
2727
_innerManager.AppendResponseCookie(context, key, value, options);
@@ -40,23 +40,57 @@ public string GetRequestCookie(IOwinContext context, string key)
4040

4141
private void CheckSameSite(IOwinContext context, CookieOptions options)
4242
{
43-
if (DisallowsSameSiteNone(context) && options.SameSite == SameSiteMode.None)
43+
if (options.SameSite == SameSiteMode.None && DisallowsSameSiteNone(context))
4444
{
45-
// IOS12 and Mac OS X 10.14 treat SameSite=None as SameSite=Strict. Exclude the option instead.
46-
// https://bugs.webkit.org/show_bug.cgi?id=198181
4745
options.SameSite = null;
4846
}
4947
}
5048

51-
// https://myip.ms/view/comp_browsers/8568/Safari_12.html
5249
public static bool DisallowsSameSiteNone(IOwinContext context)
5350
{
5451
// TODO: Use your User Agent library of choice here.
5552
var userAgent = context.Request.Headers["User-Agent"];
56-
return userAgent.Contains("CPU iPhone OS 12") // Also covers iPod touch
57-
|| userAgent.Contains("iPad; CPU OS 12")
58-
// Safari 12 and 13 are both broken on Mojave
59-
|| userAgent.Contains("Macintosh; Intel Mac OS X 10_14");
53+
return DisallowsSameSiteNone(userAgent);
54+
}
55+
56+
public static bool DisallowsSameSiteNone(string userAgent)
57+
{
58+
if (string.IsNullOrEmpty(userAgent))
59+
{
60+
return false;
61+
}
62+
63+
// Cover all iOS based browsers here. This includes:
64+
// - Safari on iOS 12 for iPhone, iPod Touch, iPad
65+
// - WkWebview on iOS 12 for iPhone, iPod Touch, iPad
66+
// - Chrome on iOS 12 for iPhone, iPod Touch, iPad
67+
// All of which are broken by SameSite=None, because they use the iOS networking stack
68+
if (userAgent.Contains("CPU iPhone OS 12") || userAgent.Contains("iPad; CPU OS 12"))
69+
{
70+
return true;
71+
}
72+
73+
// Cover Mac OS X based browsers that use the Mac OS networking stack. This includes:
74+
// - Safari on Mac OS X.
75+
// This does not include:
76+
// - Chrome on Mac OS X
77+
// Because they do not use the Mac OS networking stack.
78+
if (userAgent.Contains("Macintosh; Intel Mac OS X 10_14") &&
79+
userAgent.Contains("Version/") && userAgent.Contains("Safari"))
80+
{
81+
return true;
82+
}
83+
84+
// Cover Chrome 50-69, because some versions are broken by SameSite=None,
85+
// and none in this range require it.
86+
// Note: this covers some pre-Chromium Edge versions,
87+
// but pre-Chromium Edge does not require SameSite=None.
88+
if (userAgent.Contains("Chrome/5") || userAgent.Contains("Chrome/6"))
89+
{
90+
return true;
91+
}
92+
93+
return false;
6094
}
6195
}
6296
}

0 commit comments

Comments
 (0)