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

rnewson pushed a commit to branch disable-rewrite-handler-by-default
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit f7f44d97354c89ddec1b6645de8d46ef8b76e477
Author: Robert Newson <[email protected]>
AuthorDate: Thu Nov 19 10:20:03 2026 +0000

    disable rewrite handler by default
---
 rel/overlay/etc/default.ini                   |  3 +++
 src/chttpd/src/chttpd_rewrite.erl             |  8 ++++++++
 src/couch/src/couch_httpd_rewrite.erl         | 10 +++++++++-
 src/couch/test/eunit/couchdb_vhosts_tests.erl |  2 ++
 src/docs/src/whatsnew/3.5.rst                 |  4 ++++
 test/elixir/test/rewrite_js_test.exs          | 17 +++++++++++++++++
 6 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/rel/overlay/etc/default.ini b/rel/overlay/etc/default.ini
index f6bd13ffc..471d203f7 100644
--- a/rel/overlay/etc/default.ini
+++ b/rel/overlay/etc/default.ini
@@ -271,6 +271,9 @@ bind_address = 127.0.0.1
 ; prevent non-admins from accessing /_all_dbs and /_dbs_info
 ;admin_only_all_dbs = true
 
+; Enable/Disable the deprecated _rewrite handler
+;enable_rewrites = false
+
 ; These options are moved from [httpd]
 ;secure_rewrites = true
 ;allow_jsonp = false
diff --git a/src/chttpd/src/chttpd_rewrite.erl 
b/src/chttpd/src/chttpd_rewrite.erl
index 4e77597d4..0cf0df101 100644
--- a/src/chttpd/src/chttpd_rewrite.erl
+++ b/src/chttpd/src/chttpd_rewrite.erl
@@ -25,6 +25,14 @@
 -define(MATCH_ALL, {bind, <<"*">>}).
 
 handle_rewrite_req(#httpd{} = Req, Db, DDoc) ->
+    case config:get_boolean("chttpd", "enable_rewrites", false) of
+        true ->
+            handle_rewrite_req_int(Req, Db, DDoc);
+        false ->
+            throw({forbidden, ~"rewrite handlers are not enabled"})
+    end.
+
+handle_rewrite_req_int(#httpd{} = Req, Db, DDoc) ->
     RewritesSoFar = erlang:get(?REWRITE_COUNT),
     MaxRewrites = chttpd_util:get_chttpd_config_integer("rewrite_limit", 100),
     case RewritesSoFar >= MaxRewrites of
diff --git a/src/couch/src/couch_httpd_rewrite.erl 
b/src/couch/src/couch_httpd_rewrite.erl
index 97f48a4c0..02faa681d 100644
--- a/src/couch/src/couch_httpd_rewrite.erl
+++ b/src/couch/src/couch_httpd_rewrite.erl
@@ -108,7 +108,15 @@
 %% "to": "/some/:foo",
 %%  }}
 
-handle_rewrite_req(
+handle_rewrite_req(#httpd{} = Req, Db, DDoc) ->
+    case config:get_boolean("couch_httpd", "enable_rewrites", false) of
+        true ->
+            handle_rewrite_req_int(Req, Db, DDoc);
+        false ->
+            throw({forbidden, ~"rewrite handlers are not enabled"})
+    end.
+
+handle_rewrite_req_int(
     #httpd{
         path_parts = [DbName, <<"_design">>, DesignName, _Rewrite | PathParts],
         method = Method,
diff --git a/src/couch/test/eunit/couchdb_vhosts_tests.erl 
b/src/couch/test/eunit/couchdb_vhosts_tests.erl
index e005537cb..1e215e284 100644
--- a/src/couch/test/eunit/couchdb_vhosts_tests.erl
+++ b/src/couch/test/eunit/couchdb_vhosts_tests.erl
@@ -19,6 +19,7 @@
 -define(iofmt(S, A), lists:flatten(io_lib:format(S, A))).
 
 setup() ->
+    ok = config:set("couch_httpd", "enable_rewrites", "true"),
     DbName = ?tempdb(),
     {ok, Db} = couch_db:create(DbName, [?ADMIN_CTX]),
     Doc = couch_doc:from_json_obj(
@@ -58,6 +59,7 @@ setup() ->
 
 teardown({_, DbName}) ->
     ok = couch_server:delete(?l2b(DbName), []),
+    ok = config:delete("couch_httpd", "enable_rewrites"),
     ok.
 
 vhosts_test_() ->
diff --git a/src/docs/src/whatsnew/3.5.rst b/src/docs/src/whatsnew/3.5.rst
index d4684a8e0..d420a4095 100644
--- a/src/docs/src/whatsnew/3.5.rst
+++ b/src/docs/src/whatsnew/3.5.rst
@@ -28,6 +28,10 @@ Version 3.5.4
 * Remove support for bare source and target databases to
   ``_replicate`` endpoint.
 
+* As part of the deprecation of the ``_rewrite`` handler, rewrites are
+  now disabled by default. They can be re-enabled by setting
+  ``[chttpd] enable_rewrites = true`` in config.
+
 .. _release/3.5.3:
 
 Version 3.5.3
diff --git a/test/elixir/test/rewrite_js_test.exs 
b/test/elixir/test/rewrite_js_test.exs
index ef4058603..5cb6bfec6 100644
--- a/test/elixir/test/rewrite_js_test.exs
+++ b/test/elixir/test/rewrite_js_test.exs
@@ -213,6 +213,7 @@ defmodule RewriteJSTest do
     @tag with_random_db: db_name
     test "basic js rewrites on #{db_name}", context do
       db_name = context[:db_name]
+      set_config({"chttpd", "enable_rewrites", "true"})
 
       create_doc(db_name, @ddoc)
 
@@ -315,6 +316,7 @@ defmodule RewriteJSTest do
     @tag with_random_db: db_name
     test "early response on #{db_name}", context do
       db_name = context[:db_name]
+      set_config({"chttpd", "enable_rewrites", "true"})
 
       ddoc = %{
         _id: "_design/response",
@@ -346,6 +348,7 @@ defmodule RewriteJSTest do
     @tag with_random_db: db_name
     test "path relative to server on #{db_name}", context do
       db_name = context[:db_name]
+      set_config({"chttpd", "enable_rewrites", "true"})
 
       ddoc = %{
         _id: "_design/relative",
@@ -365,6 +368,7 @@ defmodule RewriteJSTest do
     @tag with_random_db: db_name
     test "loop on #{db_name}", context do
       db_name = context[:db_name]
+      set_config({"chttpd", "enable_rewrites", "true"})
 
       ddoc_loop = %{
         _id: "_design/loop",
@@ -384,6 +388,7 @@ defmodule RewriteJSTest do
     test "requests with body preserve the query string rewrite on #{db_name}",
          context do
       db_name = context[:db_name]
+      set_config({"chttpd", "enable_rewrites", "true"})
 
       ddoc_qs = %{
         _id: "_design/qs",
@@ -404,5 +409,17 @@ defmodule RewriteJSTest do
       assert length(resp.body["results"]) == 1
       assert Enum.at(resp.body["results"], 0)["id"] == "qs2"
     end
+
+    @tag with_random_db: db_name
+    test "rewrite requests blocked by default on #{db_name}",
+         context do
+      db_name = context[:db_name]
+      create_doc(db_name, @ddoc)
+      resp = Couch.get("/#{db_name}/_design/test/_rewrite/foo")
+
+      assert resp.status_code == 403
+      assert resp.body == %{"error" => "forbidden", "reason" => "rewrite 
handlers are not enabled"}
+    end
+
   end
 end

Reply via email to