You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by mo...@apache.org on 2023/04/13 10:13:41 UTC

[apisix] branch master updated: fix: hold_body_chunk should use seperate buffer for each plugin in case of pollution (#9266)

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

monkeydluffy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new f39cadde5 fix: hold_body_chunk should use seperate buffer for each plugin in case of pollution (#9266)
f39cadde5 is described below

commit f39cadde59d02d87919a91e0013bdbd6f6f00655
Author: Sn0rt <wa...@gmail.com>
AuthorDate: Thu Apr 13 18:13:34 2023 +0800

    fix: hold_body_chunk should use seperate buffer for each plugin in case of pollution (#9266)
---
 apisix/core/response.lua   | 13 +++++++++----
 apisix/plugin.lua          |  4 ++++
 t/core/response.t          |  1 +
 t/plugin/grpc-transcode2.t |  2 +-
 4 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/apisix/core/response.lua b/apisix/core/response.lua
index b934d94b9..cfbac1467 100644
--- a/apisix/core/response.lua
+++ b/apisix/core/response.lua
@@ -177,14 +177,19 @@ end
 function _M.hold_body_chunk(ctx, hold_the_copy)
     local body_buffer
     local chunk, eof = arg[1], arg[2]
+
+    if not ctx._body_buffer then
+        ctx._body_buffer = {}
+    end
+
     if type(chunk) == "string" and chunk ~= "" then
-        body_buffer = ctx._body_buffer
+        body_buffer = ctx._body_buffer[ctx._plugin_name]
         if not body_buffer then
             body_buffer = {
                 chunk,
                 n = 1
             }
-            ctx._body_buffer = body_buffer
+            ctx._body_buffer[ctx._plugin_name] = body_buffer
         else
             local n = body_buffer.n + 1
             body_buffer.n = n
@@ -193,13 +198,13 @@ function _M.hold_body_chunk(ctx, hold_the_copy)
     end
 
     if eof then
-        body_buffer = ctx._body_buffer
+        body_buffer = ctx._body_buffer[ctx._plugin_name]
         if not body_buffer then
             return chunk
         end
 
         body_buffer = concat_tab(body_buffer, "", 1, body_buffer.n)
-        ctx._body_buffer = nil
+        ctx._body_buffer[ctx._plugin_name] = nil
         return body_buffer
     end
 
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index 7926e2986..e5e04dc39 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -1093,7 +1093,9 @@ function _M.run_plugin(phase, plugins, api_ctx)
                 end
 
                 plugin_run = true
+                api_ctx._plugin_name = plugins[i]["name"]
                 local code, body = phase_func(conf, api_ctx)
+                api_ctx._plugin_name = nil
                 if code or body then
                     if is_http then
                         if code >= 400 then
@@ -1128,7 +1130,9 @@ function _M.run_plugin(phase, plugins, api_ctx)
         local conf = plugins[i + 1]
         if phase_func and meta_filter(api_ctx, plugins[i]["name"], conf) then
             plugin_run = true
+            api_ctx._plugin_name = plugins[i]["name"]
             phase_func(conf, api_ctx)
+            api_ctx._plugin_name = nil
         end
     end
 
diff --git a/t/core/response.t b/t/core/response.t
index c7cb92a56..dc748a7ad 100644
--- a/t/core/response.t
+++ b/t/core/response.t
@@ -188,6 +188,7 @@ aaa:
         }
         body_filter_by_lua_block {
             local core = require("apisix.core")
+            ngx.ctx._plugin_name = "test"
             local final_body = core.response.hold_body_chunk(ngx.ctx)
             if not final_body then
                 return
diff --git a/t/plugin/grpc-transcode2.t b/t/plugin/grpc-transcode2.t
index 0f7cc8d0b..da91d3ceb 100644
--- a/t/plugin/grpc-transcode2.t
+++ b/t/plugin/grpc-transcode2.t
@@ -486,7 +486,7 @@ GET /grpc_plus?a=1&b=2
 --- response_body eval
 qr/\{"result":3\}/
 --- error_log eval
-qr/request log: \{.*body":\"\\u0000\\u0000\\u0000\\u0000\\u0002\\b\\u0003\\u0000\\u0000\\u0000\\u0000\\u0002\\b\\u0003"/
+qr/request log: \{.*body":\"\\u0000\\u0000\\u0000\\u0000\\u0002\\b\\u0003"/