Skip to content

Commit

Permalink
fix(balancer): query args not forwarded bug
Browse files Browse the repository at this point in the history
query args that are set in a plugin by kong are not forwarded to the
upstream in case of empty query args in request but with a ?
(e.g. /route?)
also add test to validate the same

Fixes #11325
  • Loading branch information
chirag-manwani committed Aug 5, 2023
1 parent 9b3393c commit 76ff95f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
4 changes: 1 addition & 3 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1389,9 +1389,7 @@ return {
-- We overcome this behavior with our own logic, to preserve user
-- desired semantics.
-- perf: branch usually not taken, don't cache var outside
if byte(ctx.request_uri or var.request_uri, -1) == QUESTION_MARK then
var.upstream_uri = var.upstream_uri .. "?"
elseif var.is_args == "?" then
if byte(ctx.request_uri or var.request_uri, -1) == QUESTION_MARK or var.is_args == "?" then
var.upstream_uri = var.upstream_uri .. "?" .. (var.args or "")
end

Expand Down
69 changes: 69 additions & 0 deletions spec/02-integration/05-proxy/32-query-params_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"

for _, strategy in helpers.each_strategy() do
describe("query args specs [#" .. strategy .. "]", function()
local proxy_client

lazy_setup(function()
local bp = helpers.get_db_utils(strategy, {
"plugins",
"routes",
"services",
})

local service = assert(bp.services:insert({
url = helpers.mock_upstream_url
}))

local route = assert(bp.routes:insert({
service = service,
paths = { "/set-query-arg" }
}))

assert(bp.plugins:insert({
name = "request-transformer",
route = { id = route.id },
config = {
add = {
querystring = {"dummy:1"},
},
},
}))

helpers.start_kong({
database = strategy,
plugins = "bundled",
nginx_conf = "spec/fixtures/custom_nginx.template",
})
end)

lazy_teardown(function()
helpers.stop_kong()
end)

before_each(function()
proxy_client = helpers.proxy_client()
end)

after_each(function()
if proxy_client then
proxy_client:close()
end
end)

it("does proxy set query args if URI does not contain arguments", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/set-query-arg?",
headers = {
["Host"] = "mock_upstream",
},
})

local body = assert.res_status(200, res)
local json = cjson.decode(body)
assert.equal("1", json.uri_args.dummy)
end)
end)
end

0 comments on commit 76ff95f

Please sign in to comment.