Skip to content

Commit

Permalink
fix(pdk): fix a bug that pdk.log.serialize() does not properly handle…
Browse files Browse the repository at this point in the history
… JSON nulls (#13376)

* fix(pdk): fix a bug that pdk.log.serialize() will throw an error
when json entity set via pdk.log.set_serialize_value contains cjson.null
as value.

* handle cdata null

---------

Co-authored-by: Zachary Hu <6426329+outsinre@users.noreply.github.com>
  • Loading branch information
liverpool8056 and outsinre authored Jul 22, 2024
1 parent 6f5684d commit 163f72a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/pdk-log-error.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Fixed an issue that pdk.log.serialize() will throw an error when JSON entity set by serialize_value contains json.null
type: bugfix
scope: PDK
8 changes: 7 additions & 1 deletion kong/pdk/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local inspect = require("inspect")
local phase_checker = require("kong.pdk.private.phases")
local constants = require("kong.constants")
local clear_tab = require("table.clear")
local ngx_null = ngx.null


local request_id_get = require("kong.observability.tracing.request_id").get
Expand Down Expand Up @@ -640,7 +641,12 @@ do

local function is_valid_value(v, visited)
local t = type(v)
if v == nil or t == "number" or t == "string" or t == "boolean" then

-- cdata is not supported by cjson.encode
if type(v) == 'cdata' then
return false

elseif v == nil or v == ngx_null or t == "number" or t == "string" or t == "boolean" then
return true
end

Expand Down
14 changes: 14 additions & 0 deletions spec/01-unit/10-log_serializer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ describe("kong.log.serialize", function()
assert.not_equal(tostring(ngx.ctx.service),
tostring(res.service))
end)

it("handle 'json.null' and 'cdata null'", function()
kong.log.set_serialize_value("response.body", ngx.null)
local pok, value = pcall(kong.log.serialize, {})
assert.is_true(pok)
assert.is_true(type(value) == "table")

local ffi = require "ffi"
local n = ffi.new("void*")
kong.log.set_serialize_value("response.body", n)
local pok, value = pcall(kong.log.serialize, {})
assert.is_false(pok)
assert.is_true(type(value) == "string")
end)
end)
end)

Expand Down

1 comment on commit 163f72a

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:163f72a53c14ef71c6991762edfbf76b5e822e7f
Artifacts available https://github.com/Kong/kong/actions/runs/10033983922

Please sign in to comment.