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

nickva pushed a commit to branch 3.5.x-a-few-more-fixes
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 2a539fc058fd03aef276438c19a2c88d8e41db1e
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Sat Oct 3 15:18:45 2026 -0400

    Use incremental gunzip
    
    Use incremental gunzip so we can apply configured request limits better.
    
    Session endpoint already had an implicit 1MB size limit applied so we apply 
it
    explicitly in case it changes upstream in mochiweb at some point.
    
    Co-Authored-By: Robert Newson <[email protected]>
    (cherry picked from commit e0a8e2c3fb089ff856fe90751047c95fb425d797)
---
 src/chttpd/src/chttpd.erl          |  2 +-
 src/couch/src/couch_att.erl        |  2 +-
 src/couch/src/couch_httpd.erl      |  8 ++++++--
 src/couch/src/couch_httpd_auth.erl |  8 +++++---
 src/couch/src/couch_util.erl       | 27 +++++++++++++++++++++++++++
 5 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl
index 485e012c2..13fad2ffe 100644
--- a/src/chttpd/src/chttpd.erl
+++ b/src/chttpd/src/chttpd.erl
@@ -1447,7 +1447,7 @@ maybe_decompress(Httpd, Body) ->
     case header_value(Httpd, "Content-Encoding", "identity") of
         "gzip" ->
             try
-                zlib:gunzip(Body)
+                couch_util:gunzip_with_limit(Body)
             catch
                 error:data_error ->
                     throw({bad_request, "Request body is not properly 
gzipped."})
diff --git a/src/couch/src/couch_att.erl b/src/couch/src/couch_att.erl
index 4c3678b5f..f61acdc98 100644
--- a/src/couch/src/couch_att.erl
+++ b/src/couch/src/couch_att.erl
@@ -547,7 +547,7 @@ to_json(Att, OutputData, DataToFollow, ShowEncoding) ->
             true ->
                 AttData =
                     case Enc of
-                        gzip -> zlib:gunzip(to_binary(Att));
+                        gzip -> couch_util:gunzip_with_limit(to_binary(Att));
                         identity -> to_binary(Att)
                     end,
                 [{<<"data">>, base64:encode(AttData)}]
diff --git a/src/couch/src/couch_httpd.erl b/src/couch/src/couch_httpd.erl
index 5253e4d3c..f38375959 100644
--- a/src/couch/src/couch_httpd.erl
+++ b/src/couch/src/couch_httpd.erl
@@ -45,7 +45,7 @@
 -export([check_max_request_length/1]).
 -export([handle_request/1]).
 -export([set_auth_handlers/0]).
--export([maybe_decompress/2]).
+-export([maybe_decompress/2, maybe_decompress/3]).
 
 -define(HANDLER_NAME_IN_MODULE_POS, 6).
 -define(MAX_DRAIN_BYTES, 1048576).
@@ -723,9 +723,13 @@ json_body_obj(Httpd) ->
     end.
 
 maybe_decompress(Httpd, Body) ->
+    MaxSize = chttpd_util:get_chttpd_config_integer("max_http_request_size", 
4294967296),
+    maybe_decompress(Httpd, Body, MaxSize).
+
+maybe_decompress(Httpd, Body, MaxSize) ->
     case header_value(Httpd, "Content-Encoding", "identity") of
         "gzip" ->
-            zlib:gunzip(Body);
+            couch_util:gunzip_with_limit(Body, MaxSize);
         "identity" ->
             Body;
         Else ->
diff --git a/src/couch/src/couch_httpd_auth.erl 
b/src/couch/src/couch_httpd_auth.erl
index 2af2f7e62..b55892244 100644
--- a/src/couch/src/couch_httpd_auth.erl
+++ b/src/couch/src/couch_httpd_auth.erl
@@ -35,13 +35,15 @@
 -export([jwt_authentication_handler/1]).
 
 -import(couch_httpd, [
-    header_value/2, send_json/2, send_json/4, send_method_not_allowed/2, 
maybe_decompress/2
+    header_value/2, send_json/2, send_json/4, send_method_not_allowed/2, 
maybe_decompress/3
 ]).
 
 -compile({no_auto_import, [integer_to_binary/1, integer_to_binary/2]}).
 
 -define(LOCKOUT_MSG, <<"Account is temporarily locked due to multiple 
authentication failures">>).
 
+-define(MAX_SESSION_BODY_SIZE, 1048576).
+
 party_mode_handler(Req) ->
     case
         chttpd_util:get_chttpd_config_boolean(
@@ -471,14 +473,14 @@ handle_session_req(Req) ->
     handle_session_req(Req, couch_auth_cache).
 
 handle_session_req(#httpd{method = 'POST', mochi_req = MochiReq} = Req, 
AuthModule) ->
-    ReqBody = MochiReq:recv_body(),
+    ReqBody = MochiReq:recv_body(?MAX_SESSION_BODY_SIZE),
     Form =
         case MochiReq:get_primary_header_value("content-type") of
             % content type should be json
             "application/x-www-form-urlencoded" ++ _ ->
                 mochiweb_util:parse_qs(ReqBody);
             "application/json" ++ _ ->
-                {Pairs} = ?JSON_DECODE(maybe_decompress(Req, ReqBody)),
+                {Pairs} = ?JSON_DECODE(maybe_decompress(Req, ReqBody, 
?MAX_SESSION_BODY_SIZE)),
                 lists:map(
                     fun({Key, Value}) ->
                         {?b2l(Key), ?b2l(Value)}
diff --git a/src/couch/src/couch_util.erl b/src/couch/src/couch_util.erl
index 8ee165666..7e3dffd61 100644
--- a/src/couch/src/couch_util.erl
+++ b/src/couch/src/couch_util.erl
@@ -47,6 +47,7 @@
 -export([ejson_to_map/1]).
 -export([new_set/0, set_from_list/1]).
 -export([hibernate_after/1]).
+-export([gunzip_with_limit/1, gunzip_with_limit/2]).
 
 -include_lib("couch/include/couch_db.hrl").
 
@@ -819,3 +820,29 @@ hibernate_after(Module) when is_atom(Module) ->
         Timeout when is_integer(Timeout) ->
             [{hibernate_after, Timeout}]
     end.
+
+gunzip_with_limit(Data) ->
+    Limit = chttpd_util:get_chttpd_config_integer("max_http_request_size", 
4294967296),
+    gunzip_with_limit(Data, Limit).
+
+gunzip_with_limit(Data, Limit) when is_integer(Limit), Limit >= 0 ->
+    Z = zlib:open(),
+    try
+        ok = zlib:inflateInit(Z, 16 + 15),
+        Result = gunzip_loop(Z, Limit, zlib:safeInflate(Z, Data), 0, []),
+        ok = zlib:inflateEnd(Z),
+        iolist_to_binary(lists:reverse(Result))
+    after
+        zlib:close(Z)
+    end.
+
+gunzip_loop(Z, Limit, {Action, Output}, AccLen0, AccChunks) ->
+    AccLen1 = AccLen0 + iolist_size(Output),
+    case AccLen1 > Limit of
+        true ->
+            exit({body_too_large, AccLen1});
+        false when Action == continue ->
+            gunzip_loop(Z, Limit, zlib:safeInflate(Z, []), AccLen1, [Output | 
AccChunks]);
+        false when Action == finished ->
+            [Output | AccChunks]
+    end.

Reply via email to