@@ -11,17 +11,17 @@ public class SameSiteCookieManager : ICookieManager
11
11
{
12
12
private readonly ICookieManager _innerManager ;
13
13
14
- public SameSiteCookieManager ( )
15
- : this ( new CookieManager ( ) )
16
- {
14
+ public SameSiteCookieManager ( ) : this ( new CookieManager ( ) )
15
+ {
17
16
}
18
17
19
18
public SameSiteCookieManager ( ICookieManager innerManager )
20
19
{
21
20
_innerManager = innerManager ;
22
21
}
23
22
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 )
25
25
{
26
26
CheckSameSite ( context , options ) ;
27
27
_innerManager . AppendResponseCookie ( context , key , value , options ) ;
@@ -40,23 +40,57 @@ public string GetRequestCookie(IOwinContext context, string key)
40
40
41
41
private void CheckSameSite ( IOwinContext context , CookieOptions options )
42
42
{
43
- if ( DisallowsSameSiteNone ( context ) && options . SameSite == SameSiteMode . None )
43
+ if ( options . SameSite == SameSiteMode . None && DisallowsSameSiteNone ( context ) )
44
44
{
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
47
45
options . SameSite = null ;
48
46
}
49
47
}
50
48
51
- // https://myip.ms/view/comp_browsers/8568/Safari_12.html
52
49
public static bool DisallowsSameSiteNone ( IOwinContext context )
53
50
{
54
51
// TODO: Use your User Agent library of choice here.
55
52
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 ;
60
94
}
61
95
}
62
96
}
0 commit comments