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

rnewson pushed a commit to branch validate-redirect-2
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 529e0b5986268d49ded90c5a567fbf85ac54c807
Author: Robert Newson <[email protected]>
AuthorDate: Thu Oct 29 15:13:01 2026 +0000

    validate redirect location #2
---
 src/chttpd/src/chttpd.erl     |  4 ++--
 src/couch/src/couch_httpd.erl | 20 +++++++++++---------
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl
index 6f8944ceb..4fd809b97 100644
--- a/src/chttpd/src/chttpd.erl
+++ b/src/chttpd/src/chttpd.erl
@@ -795,9 +795,9 @@ absolute_uri(#httpd{mochi_req = MochiReq, absolute_uri = 
undefined}, Path) ->
                         end
                 end
         end,
-    Scheme ++ "://" ++ Host ++ Path;
+    Scheme ++ "://" ++ Host ++ couch_httpd:validate_uri(Path);
 absolute_uri(#httpd{absolute_uri = URI}, Path) ->
-    URI ++ Path.
+    URI ++ couch_httpd:validate_uri(Path).
 
 unquote(UrlEncodedString) ->
     case config:get_boolean("chttpd", "decode_plus_to_space", true) of
diff --git a/src/couch/src/couch_httpd.erl b/src/couch/src/couch_httpd.erl
index d729a6f6c..fe0c4d525 100644
--- a/src/couch/src/couch_httpd.erl
+++ b/src/couch/src/couch_httpd.erl
@@ -47,6 +47,7 @@
 -export([maybe_decompress/2, maybe_decompress/3]).
 -export([peer/1]).
 -export([authentication_handler_match/1]).
+-export([validate_uri/1]).
 
 -define(HANDLER_NAME_IN_MODULE_POS, 6).
 -define(MAX_DRAIN_BYTES, 1048576).
@@ -660,14 +661,7 @@ host_for_request(#httpd{mochi_req = MochiReq}) ->
             Value
     end.
 
-absolute_uri(#httpd{} = Req, [$/ | _] = Path) ->
-    case uri_string:parse(Path) of
-        {error, _Type, _Reason} ->
-            throw({bad_request, "invalid path"});
-        #{} = URIMap ->
-            absolute_uri(Req, URIMap)
-    end;
-absolute_uri(#httpd{mochi_req = MochiReq} = Req, #{path := Path}) ->
+absolute_uri(#httpd{mochi_req = MochiReq} = Req, [$/ | _] = Path) ->
     Host = host_for_request(Req),
     XSsl = chttpd_util:get_chttpd_config("x_forwarded_ssl", "X-Forwarded-Ssl"),
     Scheme =
@@ -689,7 +683,7 @@ absolute_uri(#httpd{mochi_req = MochiReq} = Req, #{path := 
Path}) ->
                         end
                 end
         end,
-    Scheme ++ "://" ++ Host ++ Path;
+    Scheme ++ "://" ++ Host ++ validate_uri(Path);
 absolute_uri(_Req, _Path) ->
     throw({bad_request, "path must begin with a /."}).
 
@@ -1475,6 +1469,14 @@ peer_from_socket(MochiReq) ->
             MochiReq:get(peer)
     end.
 
+validate_uri(URI) ->
+    case uri_string:parse(URI) of
+        {error, _Type, _Reason} ->
+            throw({bad_request, "invalid URI"});
+        #{} = URIMap ->
+            uri_string:recompose(URIMap)
+    end.
+
 %%%%%%%% module tests below %%%%%%%%
 
 -ifdef(TEST).

Reply via email to