Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(admin): custom id empty value cause crash #10475

Merged
merged 4 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@
Docker containers if it was not cleanly stopped.
[#10468](https://github.com/Kong/kong/pull/10468)

#### Admin API

- Fix an issue where empty value of URI argument `custom_id` crashes `/consumer`.
[#10475](https://github.com/Kong/kong/pull/10475)

### Changed

#### Core
Expand Down
11 changes: 9 additions & 2 deletions kong/api/routes/consumers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ return {
["/consumers"] = {
GET = function(self, db, helpers, parent)
local args = self.args.uri
local custom_id = args.custom_id

if custom_id and type(custom_id) ~= "string" or custom_id == "" then
return kong.response.exit(400, {
message = "custom_id must be an unempty string",
})
end

-- Search by custom_id: /consumers?custom_id=xxx
if args.custom_id then
if custom_id then
bungle marked this conversation as resolved.
Show resolved Hide resolved
local opts = endpoints.extract_options(args, db.consumers.schema, "select")
local consumer, _, err_t = db.consumers:select_by_custom_id(args.custom_id, opts)
local consumer, _, err_t = db.consumers:select_by_custom_id(custom_id, opts)
if err_t then
return endpoints.handle_error(err_t)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/02-integration/04-admin_api/03-consumers_routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ describe("Admin API (#" .. strategy .. "): ", function()
pages[i] = json
end
end)
it("not crashed by empty custom_id #KAG-867", function()
local res = client:get("/consumers?custom_id=")
StarlightIbuki marked this conversation as resolved.
Show resolved Hide resolved
local body = assert.res_status(400, res)
assert(cjson.decode(body))
res = client:get("/consumers?custom_id")
body = assert.res_status(400, res)
assert(cjson.decode(body))
end)
it("allows filtering by custom_id", function()
local custom_id = gensym()
local c = bp.consumers:insert({ custom_id = custom_id }, { nulls = true })
Expand Down