Browse Source

Update SameSite cookie attribute to handle "Default"

Loïc Hoguin 2 years ago
parent
commit
0d04cfffa3
1 changed files with 17 additions and 7 deletions
  1. 17 7
      src/cow_cookie.erl

+ 17 - 7
src/cow_cookie.erl

@@ -26,7 +26,7 @@
 	path => binary(),
 	path => binary(),
 	secure => true,
 	secure => true,
 	http_only => true,
 	http_only => true,
-	same_site => strict | lax | none
+	same_site => default | none | strict | lax
 }.
 }.
 -export_type([cookie_attrs/0]).
 -export_type([cookie_attrs/0]).
 
 
@@ -35,7 +35,7 @@
 	http_only => boolean(),
 	http_only => boolean(),
 	max_age => non_neg_integer(),
 	max_age => non_neg_integer(),
 	path => binary(),
 	path => binary(),
-	same_site => strict | lax | none,
+	same_site => default | none | strict | lax,
 	secure => boolean()
 	secure => boolean()
 }.
 }.
 -export_type([cookie_opts/0]).
 -export_type([cookie_opts/0]).
@@ -274,16 +274,15 @@ parse_set_cookie_attr(<<"httponly">>, _) ->
 	{ok, http_only, true};
 	{ok, http_only, true};
 parse_set_cookie_attr(<<"samesite">>, Value) ->
 parse_set_cookie_attr(<<"samesite">>, Value) ->
 	case ?LOWER(Value) of
 	case ?LOWER(Value) of
+		<<"none">> ->
+			{ok, same_site, none};
 		<<"strict">> ->
 		<<"strict">> ->
 			{ok, same_site, strict};
 			{ok, same_site, strict};
 		<<"lax">> ->
 		<<"lax">> ->
 			{ok, same_site, lax};
 			{ok, same_site, lax};
-		%% Clients may have different defaults than "None".
-		<<"none">> ->
-			{ok, same_site, none};
 		%% Unknown values and lack of value are equivalent.
 		%% Unknown values and lack of value are equivalent.
 		_ ->
 		_ ->
-			ignore
+			{ok, same_site, default}
 	end;
 	end;
 parse_set_cookie_attr(_, _) ->
 parse_set_cookie_attr(_, _) ->
 	ignore.
 	ignore.
@@ -302,6 +301,10 @@ parse_set_cookie_test_() ->
 			{ok, <<"a">>, <<"b">>, #{domain => <<"foo.example.org">>}}},
 			{ok, <<"a">>, <<"b">>, #{domain => <<"foo.example.org">>}}},
 		{<<"a=b; Path=/path/to/resource; Path=/">>,
 		{<<"a=b; Path=/path/to/resource; Path=/">>,
 			{ok, <<"a">>, <<"b">>, #{path => <<"/">>}}},
 			{ok, <<"a">>, <<"b">>, #{path => <<"/">>}}},
+		{<<"a=b; SameSite=UnknownValue">>, {ok, <<"a">>, <<"b">>, #{same_site => default}}},
+		{<<"a=b; SameSite=None">>, {ok, <<"a">>, <<"b">>, #{same_site => none}}},
+		{<<"a=b; SameSite=Lax">>, {ok, <<"a">>, <<"b">>, #{same_site => lax}}},
+		{<<"a=b; SameSite=Strict">>, {ok, <<"a">>, <<"b">>, #{same_site => strict}}},
 		{<<"a=b; SameSite=Lax; SameSite=Strict">>,
 		{<<"a=b; SameSite=Lax; SameSite=Strict">>,
 			{ok, <<"a">>, <<"b">>, #{same_site => strict}}}
 			{ok, <<"a">>, <<"b">>, #{same_site => strict}}}
 	],
 	],
@@ -369,9 +372,10 @@ attributes([Opt={max_age, _}|_]) ->
 attributes([{path, Path}|Tail]) -> [<<"; Path=">>, Path|attributes(Tail)];
 attributes([{path, Path}|Tail]) -> [<<"; Path=">>, Path|attributes(Tail)];
 attributes([{secure, false}|Tail]) -> attributes(Tail);
 attributes([{secure, false}|Tail]) -> attributes(Tail);
 attributes([{secure, true}|Tail]) -> [<<"; Secure">>|attributes(Tail)];
 attributes([{secure, true}|Tail]) -> [<<"; Secure">>|attributes(Tail)];
+attributes([{same_site, default}|Tail]) -> attributes(Tail);
+attributes([{same_site, none}|Tail]) -> [<<"; SameSite=None">>|attributes(Tail)];
 attributes([{same_site, lax}|Tail]) -> [<<"; SameSite=Lax">>|attributes(Tail)];
 attributes([{same_site, lax}|Tail]) -> [<<"; SameSite=Lax">>|attributes(Tail)];
 attributes([{same_site, strict}|Tail]) -> [<<"; SameSite=Strict">>|attributes(Tail)];
 attributes([{same_site, strict}|Tail]) -> [<<"; SameSite=Strict">>|attributes(Tail)];
-attributes([{same_site, none}|Tail]) -> [<<"; SameSite=None">>|attributes(Tail)];
 %% Skip unknown options.
 %% Skip unknown options.
 attributes([_|Tail]) -> attributes(Tail).
 attributes([_|Tail]) -> attributes(Tail).
 
 
@@ -393,6 +397,12 @@ setcookie_test_() ->
 			#{secure => false, http_only => false},
 			#{secure => false, http_only => false},
 			<<"Customer=WILE_E_COYOTE">>},
 			<<"Customer=WILE_E_COYOTE">>},
 		{<<"Customer">>, <<"WILE_E_COYOTE">>,
 		{<<"Customer">>, <<"WILE_E_COYOTE">>,
+			#{same_site => default},
+			<<"Customer=WILE_E_COYOTE">>},
+		{<<"Customer">>, <<"WILE_E_COYOTE">>,
+			#{same_site => none},
+			<<"Customer=WILE_E_COYOTE; SameSite=None">>},
+		{<<"Customer">>, <<"WILE_E_COYOTE">>,
 			#{same_site => lax},
 			#{same_site => lax},
 			<<"Customer=WILE_E_COYOTE; SameSite=Lax">>},
 			<<"Customer=WILE_E_COYOTE; SameSite=Lax">>},
 		{<<"Customer">>, <<"WILE_E_COYOTE">>,
 		{<<"Customer">>, <<"WILE_E_COYOTE">>,