From a0b1a4554dc9ce23e877cc1fc58507c795203878 Mon Sep 17 00:00:00 2001 From: Shreemaan Abhishek Date: Mon, 25 Nov 2024 20:24:42 +0545 Subject: [PATCH] fix(body-transformer): add nil check to conf (#11768) --- apisix/plugins/body-transformer.lua | 3 ++ t/plugin/body-transformer2.t | 45 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/apisix/plugins/body-transformer.lua b/apisix/plugins/body-transformer.lua index f63381e01b40..f9d5400e410c 100644 --- a/apisix/plugins/body-transformer.lua +++ b/apisix/plugins/body-transformer.lua @@ -219,6 +219,9 @@ end function _M.body_filter(_, ctx) local conf = ctx.body_transformer_conf + if not conf then + return + end if conf.response then local body = core.response.hold_body_chunk(ctx) if ngx.arg[2] == false and not body then diff --git a/t/plugin/body-transformer2.t b/t/plugin/body-transformer2.t index 748b1fd1fbb4..1db206ccecc0 100644 --- a/t/plugin/body-transformer2.t +++ b/t/plugin/body-transformer2.t @@ -87,3 +87,48 @@ POST /echo {"name": "foo", "address":"LA", "age": 18} -- response_body {"name": "bar", "age": 19} + + + +=== TEST 3: body transformer plugin with key-auth that fails +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin") + local core = require("apisix.core") + local code, body = t.test('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/foobar", + "plugins": { + "body-transformer": { + "request": { + "template": "some-template" + } + }, + "key-auth": {} + }, + "upstream": { + "type": "roundrobin", + "nodes": { + "127.0.0.1:1980": 1 + } + } + }]] + ) + if code >= 300 then + ngx.status = code + return + end + ngx.sleep(0.5) + local http = require("resty.http") + local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/foobar" + local opt = {method = "POST", body = "body", headers = {["Content-Type"] = "application/json"}} + local httpc = http.new() + local res = httpc:request_uri(uri, opt) + assert(res.status == 401) + ngx.say(res.reason) + } + } +--- response_body +Unauthorized