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

nickva pushed a commit to branch do-not-store-session-resp-in-cache
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 8a5f8d0c957025e50dac734555eab2daa055e2f7
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Wed Nov 11 00:48:13 2026 -0500

    Tell caches to not store session responses
    
    This should keep the responses from _session out of shared caches, even if 
the
    reverse caching proxy is misconfigured.
    
    Since we recently switched away from a _design%2 redirect to directly 
handling
    it, clean up `send_redirect` utility since it was used in that shape only by
    `_utils` handler and nothing else. There was another unused redirect in 
httpd,
    that's cleaned up as well.
    
    https://httpwg.org/specs/rfc9111.html#rfc.section.5.2.2.5
---
 src/chttpd/src/chttpd.erl                      |  6 +----
 src/chttpd/src/chttpd_misc.erl                 |  9 ++++---
 src/chttpd/test/eunit/chttpd_session_tests.erl | 35 +++++++++++++++++++++++++-
 src/couch/src/couch_httpd.erl                  |  6 ++---
 src/couch/src/couch_httpd_auth.erl             | 22 +++++++++++-----
 test/elixir/test/cookie_auth_test.exs          |  2 ++
 6 files changed, 61 insertions(+), 19 deletions(-)

diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl
index 43b569305..751cf1201 100644
--- a/src/chttpd/src/chttpd.erl
+++ b/src/chttpd/src/chttpd.erl
@@ -61,7 +61,6 @@
     send_response_no_cors/4,
     send_method_not_allowed/2,
     send_error/2, send_error/4,
-    send_redirect/2,
     send_chunked_error/2,
     send_json/2, send_json/3, send_json/4,
     validate_ctype/2
@@ -1225,6 +1224,7 @@ error_headers(#httpd{mochi_req = MochiReq} = Req, 401 = 
Code, ErrorStr, ReasonSt
                                                                 
couch_util:url_encode(ReasonStr)
                                                             ]),
                                                             {302, [
+                                                                
{"Cache-Control", "no-store"},
                                                                 {"Location",
                                                                     
absolute_uri(
                                                                         Req, 
RedirectLocation
@@ -1340,10 +1340,6 @@ send_chunked_error(Resp, Error) ->
     send_chunk(Resp, ?l2b([$\n, ?JSON_ENCODE(JsonError), $\n])),
     send_chunk(Resp, []).
 
-send_redirect(Req, Path) ->
-    Headers = [{"Location", absolute_uri(Req, Path)}],
-    send_response(Req, 301, Headers, <<>>).
-
 server_header() ->
     couch_httpd:server_header().
 
diff --git a/src/chttpd/src/chttpd_misc.erl b/src/chttpd/src/chttpd_misc.erl
index e823f1e08..459d6bee9 100644
--- a/src/chttpd/src/chttpd_misc.erl
+++ b/src/chttpd/src/chttpd_misc.erl
@@ -95,19 +95,22 @@ handle_utils_dir_req(Req) ->
 
 handle_utils_dir_req(#httpd{method = 'GET'} = Req, DocumentRoot) ->
     "/" ++ UrlPath = chttpd:path(Req),
+    CachingHeaders = [{"Cache-Control", "private, must-revalidate"}],
     case chttpd:partition(UrlPath) of
         {_ActionKey, "/", RelativePath} ->
             % GET /_utils/path or GET /_utils/
-            CachingHeaders = [{"Cache-Control", "private, must-revalidate"}],
             DefaultValues =
                 "child-src 'self' data: blob:; default-src 'self'; img-src 
'self' data:; font-src 'self'; "
                 "script-src 'self' 'unsafe-eval'; style-src 'self' 
'unsafe-inline'; frame-src https://blog.couchdb.org;";,
             Headers = chttpd_util:maybe_add_csp_header("utils", 
CachingHeaders, DefaultValues),
             chttpd:serve_file(Req, RelativePath, DocumentRoot, Headers);
         {_ActionKey, "", _RelativePath} ->
-            % GET /_utils
+            % redirect /_utils -> /_utils/. Target depends on request host,
+            % specific to this client, so use "private" to keep it outof the
+            % shared caches
             RedirectPath = chttpd:path(Req) ++ "/",
-            chttpd:send_redirect(Req, RedirectPath)
+            Location = {"Location", chttpd:absolute_uri(Req, RedirectPath)},
+            chttpd:send_response(Req, 301, [Location | CachingHeaders], <<>>)
     end;
 handle_utils_dir_req(Req, _) ->
     send_method_not_allowed(Req, "GET,HEAD").
diff --git a/src/chttpd/test/eunit/chttpd_session_tests.erl 
b/src/chttpd/test/eunit/chttpd_session_tests.erl
index 0707090b0..fa619e443 100644
--- a/src/chttpd/test/eunit/chttpd_session_tests.erl
+++ b/src/chttpd/test/eunit/chttpd_session_tests.erl
@@ -43,7 +43,8 @@ session_test_() ->
                 [
                     ?TDEF_FE(session_authentication_db_absent),
                     ?TDEF_FE(session_authentication_db_present),
-                    ?TDEF_FE(session_authentication_gzip_request)
+                    ?TDEF_FE(session_authentication_gzip_request),
+                    ?TDEF_FE(session_cache_control)
                 ]
             }
         }
@@ -70,6 +71,38 @@ session_authentication_gzip_request(Url) ->
     {BodyJson} = jiffy:decode(Body),
     ?assert(lists:member({<<"name">>, list_to_binary(?USER)}, BodyJson)).
 
+session_cache_control(Url) ->
+    CT = {"Content-Type", "application/json"},
+    Auth = {basic_auth, {?USER, ?PASS}},
+    BUser = list_to_binary(?USER),
+    BPass = list_to_binary(?PASS),
+    Good = jiffy:encode({[{name, BUser}, {password, BPass}]}),
+    Bad = jiffy:encode({[{name, BUser}, {password, ~"badpass"}]}),
+    Cases = [
+        {"GET anon", 200, test_request:get(Url)},
+        {"GET auth", 200, test_request:get(Url, [Auth])},
+        {"POST succ 200", 200, test_request:post(Url, [CT], Good)},
+        {"POST succ 302", 302, test_request:post(Url ++ "?next=/_up", [CT], 
Good)},
+        {"POST fail 401", 401, test_request:post(Url, [CT], Bad)},
+        {"POST fail 302", 302, test_request:post(Url ++ "?fail=/_up", [CT], 
Bad)},
+        {"DELETE 200", 200, test_request:delete(Url)},
+        {"DELETE 302", 302, test_request:delete(Url ++ "?next=/_up")}
+    ],
+    lists:foreach(
+        fun({Desc, Expected, {ok, Code, Headers, _}}) ->
+            ?assertEqual({Desc, Expected}, {Desc, Code}),
+            ?assertEqual({Desc, "no-store"}, {Desc, cache_control(Headers)})
+        end,
+        Cases
+    ).
+
+% Headers can be a single or a list if duplicated
+cache_control(Headers) ->
+    case [V || {K, V} <- Headers, K =:= ~"cache-control"] of
+        [Value] -> binary_to_list(Value);
+        Values -> [binary_to_list(V) || V <- Values]
+    end.
+
 session_authentication_db(Url) ->
     {ok, 200, _, Body} = test_request:get(Url, [{basic_auth, {?USER, ?PASS}}]),
     couch_util:get_nested_json_value(
diff --git a/src/couch/src/couch_httpd.erl b/src/couch/src/couch_httpd.erl
index fe0c4d525..51075c6b4 100644
--- a/src/couch/src/couch_httpd.erl
+++ b/src/couch/src/couch_httpd.erl
@@ -33,7 +33,6 @@
     send_response_no_cors/4,
     send_method_not_allowed/2,
     send_error/2, send_error/4,
-    send_redirect/2,
     send_chunked_error/2
 ]).
 -export([send_json/2, send_json/3, send_json/4, last_chunk/1, 
parse_multipart_request/3]).
@@ -1153,6 +1152,8 @@ error_headers(#httpd{mochi_req = MochiReq} = Req, Code, 
ErrorStr, ReasonStr) ->
                                                                         ]
                                                                     ),
                                                                     {302, [
+                                                                        
{"Cache-Control",
+                                                                            
"no-store"},
                                                                         
{"Location",
                                                                             
absolute_uri(
                                                                                
 Req,
@@ -1212,9 +1213,6 @@ send_chunked_error(Resp, Error) ->
     send_chunk(Resp, ?l2b([$\n, ?JSON_ENCODE(JsonError), $\n])),
     last_chunk(Resp).
 
-send_redirect(Req, Path) ->
-    send_response(Req, 301, [{"Location", absolute_uri(Req, Path)}], <<>>).
-
 negotiate_content_type(_Req) ->
     case get(jsonp) of
         no_jsonp -> "application/json";
diff --git a/src/couch/src/couch_httpd_auth.erl 
b/src/couch/src/couch_httpd_auth.erl
index 1a00ccc33..f426430e0 100644
--- a/src/couch/src/couch_httpd_auth.erl
+++ b/src/couch/src/couch_httpd_auth.erl
@@ -43,6 +43,10 @@
 
 -define(MAX_SESSION_BODY_SIZE, 1048576).
 
+% Don't let caches store session responses on redirects, regardless of their
+% own silly policies
+-define(NO_STORE, {"Cache-Control", "no-store"}).
+
 party_mode_handler(Req) ->
     case
         chttpd_util:get_chttpd_config_boolean(
@@ -517,9 +521,11 @@ handle_session_req(#httpd{method = 'POST', mochi_req = 
MochiReq} = Req, AuthModu
             {Code, Headers} =
                 case couch_httpd:qs_value(Req, "next", nil) of
                     nil ->
-                        {200, [Cookie]};
+                        {200, [Cookie, ?NO_STORE]};
                     Redirect ->
-                        {302, [Cookie, {"Location", 
couch_httpd:absolute_uri(Req, Redirect)}]}
+                        {302, [
+                            Cookie, ?NO_STORE, {"Location", 
couch_httpd:absolute_uri(Req, Redirect)}
+                        ]}
                 end,
             send_json(
                 Req#httpd{req_body = ReqBody},
@@ -538,9 +544,11 @@ handle_session_req(#httpd{method = 'POST', mochi_req = 
MochiReq} = Req, AuthModu
             {Code, Headers} =
                 case couch_httpd:qs_value(Req, "fail", nil) of
                     nil ->
-                        {401, [Cookie]};
+                        {401, [Cookie, ?NO_STORE]};
                     Redirect ->
-                        {302, [Cookie, {"Location", 
couch_httpd:absolute_uri(Req, Redirect)}]}
+                        {302, [
+                            Cookie, ?NO_STORE, {"Location", 
couch_httpd:absolute_uri(Req, Redirect)}
+                        ]}
                 end,
             send_json(
                 Req,
@@ -560,6 +568,8 @@ handle_session_req(#httpd{method = 'GET', user_ctx = 
UserCtx} = Req, _AuthModule
         {Name, _} ->
             send_json(
                 Req,
+                200,
+                [?NO_STORE],
                 {[
                     % remove this ok
                     {ok, true},
@@ -595,9 +605,9 @@ handle_session_req(#httpd{method = 'DELETE'} = Req, 
_AuthModule) ->
     {Code, Headers} =
         case couch_httpd:qs_value(Req, "next", nil) of
             nil ->
-                {200, [Cookie]};
+                {200, [Cookie, ?NO_STORE]};
             Redirect ->
-                {302, [Cookie, {"Location", couch_httpd:absolute_uri(Req, 
Redirect)}]}
+                {302, [Cookie, ?NO_STORE, {"Location", 
couch_httpd:absolute_uri(Req, Redirect)}]}
         end,
     send_json(Req, Code, Headers, {[{ok, true}]});
 handle_session_req(Req, _AuthModule) ->
diff --git a/test/elixir/test/cookie_auth_test.exs 
b/test/elixir/test/cookie_auth_test.exs
index fedfcecb2..ec49c5358 100644
--- a/test/elixir/test/cookie_auth_test.exs
+++ b/test/elixir/test/cookie_auth_test.exs
@@ -280,6 +280,7 @@ defmodule CookieAuthTest do
     assert resp.status_code == 302
     assert resp.body["ok"]
     assert String.ends_with?(resp.headers["location"], "/_up")
+    assert resp.headers["cache-control"] == "no-store"
 
     # test redirect on fail
     resp =
@@ -295,6 +296,7 @@ defmodule CookieAuthTest do
     assert resp.status_code == 302
     assert resp.body["error"] == "unauthorized"
     assert String.ends_with?(resp.headers["location"], "/_up")
+    assert resp.headers["cache-control"] == "no-store"
 
     # test redirect with invalid path
     resp =

Reply via email to