This is an automated email from the ASF dual-hosted git repository.

nickva pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-gun.git

commit e548ae09616f6316f9f90ec001efbcf251829ec8
Author: Loïc Hoguin <[email protected]>
AuthorDate: Fri Oct 2 17:36:06 2026 +0200

    Reject a nameless cookie whose value starts with a prefix
---
 src/gun_cookies.erl | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/src/gun_cookies.erl b/src/gun_cookies.erl
index 2038412..41b0865 100644
--- a/src/gun_cookies.erl
+++ b/src/gun_cookies.erl
@@ -297,7 +297,7 @@ set_cookie2(Store, _URI, Attrs, Cookie0) ->
                        set_cookie3(Store, Attrs, Cookie)
        end.
 
-set_cookie3(Store, Attrs, Cookie=#{name := Name,
+set_cookie3(Store, Attrs, Cookie=#{name := Name, value := Value,
                host_only := HostOnly, secure_only := SecureOnly}) ->
        Path = maps:get(path, Attrs, undefined),
        case known_prefix(Name) of
@@ -309,11 +309,18 @@ set_cookie3(Store, Attrs, Cookie=#{name := Name,
                        {error, name_prefix_host_requires_host_only};
                host when Path =/= <<"/">> ->
                        {error, name_prefix_host_requires_top_level_path};
+               none when Name =:= <<>> ->
+                       case known_prefix(Value) of
+                               none ->
+                                       set_cookie_store(Store, Cookie);
+                               _ ->
+                                       {error, nameless_cookie_prefix}
+                       end;
                _ ->
                        set_cookie_store(Store, Cookie)
        end.
 
-%% Prefixes are matched case-insensitively. The rest of the name is not.
+%% Prefixes are matched case-insensitively. The rest is not.
 known_prefix(<<"__", Chars:4/binary, "-", _/bits>>) ->
        case ascii_lower(Chars) of
                <<"host">> -> host;
@@ -1008,6 +1015,25 @@ prefix_case_insensitive_test() ->
        {ok, [#{name := Name}], _} = query(StoreN, Https),
        ok.
 
+%% A nameless cookie whose value begins with a prefix is ignored.
+nameless_cookie_prefix_test() ->
+       URI = #{scheme => <<"https">>, host => <<"example.org">>, path => 
<<"/">>},
+       Reject = [
+               <<"__Secure-abc">>,
+               <<"__secure-abc=123">>,
+               <<"__SEcure-abc">>,
+               <<"__Host-abc">>,
+               <<"__HoSt-abc=123">>
+       ],
+       [begin
+               {error, nameless_cookie_prefix} = set_cookie(
+                       gun_cookies_list:init(), URI, <<>>, Value, #{secure => 
true})
+       end || Value <- Reject],
+       {ok, Store} = set_cookie(gun_cookies_list:init(), URI,
+               <<>>, <<"__Secure">>, #{}),
+       {ok, [#{name := <<>>, value := <<"__Secure">>}], _} = query(Store, URI),
+       ok.
+
 %% SameSite=None is stored only when the cookie is also Secure.
 samesite_none_requires_secure_test() ->
        Https = #{scheme => <<"https">>, host => <<"example.org">>, path => 
<<"/">>},

Reply via email to