This is an automated email from the ASF dual-hosted git repository.
LiteSun 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 b9419368bb fix(splunk-hec-logging): safely handle non-200 responses
without text field (#14012)
b9419368bb is described below
commit b9419368bb8c019760705b631712bfef1b31e252
Author: Bhuvan Somisetty <[email protected]>
AuthorDate: Fri Nov 13 06:33:32 2026 +0530
fix(splunk-hec-logging): safely handle non-200 responses without text field
(#14012)
---
apisix/plugins/splunk-hec-logging.lua | 7 +--
t/lib/server.lua | 10 ++++
t/plugin/splunk-hec-logging.t | 98 +++++++++++++++++++++++++++++++++++
3 files changed, 112 insertions(+), 3 deletions(-)
diff --git a/apisix/plugins/splunk-hec-logging.lua
b/apisix/plugins/splunk-hec-logging.lua
index 5817f8a46c..d1ed9cc50b 100644
--- a/apisix/plugins/splunk-hec-logging.lua
+++ b/apisix/plugins/splunk-hec-logging.lua
@@ -25,6 +25,7 @@ local table_insert = core.table.insert
local table_concat = core.table.concat
local ipairs = ipairs
local pairs = pairs
+local type = type
local DEFAULT_SPLUNK_HEC_ENTRY_SOURCE = "apache-apisix-splunk-hec-logging"
@@ -171,11 +172,11 @@ local function send_to_splunk(conf, entries)
if res.status ~= 200 then
local body = core.json.decode(res.body)
- if not body then
- return false, "failed to send splunk, http status code: " ..
res.status
- else
+ if type(body) == "table" and type(body.text) == "string" then
return false, "failed to send splunk, " .. body.text
end
+ return false, "failed to send splunk, http status code: " .. res.status
+ .. ", body: " .. (res.body or "")
end
return true
diff --git a/t/lib/server.lua b/t/lib/server.lua
index 33be399269..939167c337 100644
--- a/t/lib/server.lua
+++ b/t/lib/server.lua
@@ -1648,6 +1648,16 @@ function _M.test_params_in_overridden_endpoint()
ngx.say("passed")
end
+function _M.error_json_no_text()
+ ngx.status = 403
+ ngx.say([[{"message":"Forbidden: missing permissions"}]])
+end
+
+function _M.error_json_null_text()
+ ngx.status = 403
+ ngx.say([[{"text":null}]])
+end
+
-- Please add your fake upstream above
function _M.go()
diff --git a/t/plugin/splunk-hec-logging.t b/t/plugin/splunk-hec-logging.t
index 18d6062004..996fb0b9bc 100644
--- a/t/plugin/splunk-hec-logging.t
+++ b/t/plugin/splunk-hec-logging.t
@@ -601,3 +601,101 @@ passed
tail -n 1 ci/pod/vector/splunk.log
--- response_body eval
qr/.*"upstream_host":"127.0.0.1".*/
+
+
+
+=== TEST 17: set route with upstream returning non-200 JSON without text field
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, {
+ uri = "/hello",
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ },
+ plugins = {
+ ["splunk-hec-logging"] = {
+ endpoint = {
+ uri = "http://127.0.0.1:1980/error_json_no_text",
+ token = "BD274822-96AA-4DA6-90EC-18940FB2414C"
+ },
+ batch_max_size = 1,
+ inactive_timeout = 1
+ }
+ }
+ })
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 18: test route with upstream returning non-200 JSON without text field
+--- request
+GET /hello
+--- wait: 2
+--- response_body
+hello world
+--- error_log
+Batch Processor[splunk-hec-logging] failed to process entries: failed to send
splunk, http status code: 403, body: {"message":"Forbidden: missing
permissions"}
+--- no_error_log
+attempt to concatenate field 'text'
+
+
+
+=== TEST 19: set route with upstream returning non-200 JSON with null text
field
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, {
+ uri = "/hello",
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ },
+ plugins = {
+ ["splunk-hec-logging"] = {
+ endpoint = {
+ uri = "http://127.0.0.1:1980/error_json_null_text",
+ token = "BD274822-96AA-4DA6-90EC-18940FB2414C"
+ },
+ batch_max_size = 1,
+ inactive_timeout = 1
+ }
+ }
+ })
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 20: test route with upstream returning non-200 JSON with null text
field
+--- request
+GET /hello
+--- wait: 2
+--- response_body
+hello world
+--- error_log
+Batch Processor[splunk-hec-logging] failed to process entries: failed to send
splunk, http status code: 403, body: {"text":null}
+--- no_error_log
+attempt to concatenate field 'text'