Closed chirag-manwani closed 1 year ago
https://github.com/Kong/kong/blob/master/kong/runloop/handler.lua#L1386 I'm currently looking at this piece of code to understand the issue. Will update as soon as I have something.
In this line, is it expected that if a query arg is being set in a Kong plugin using the Kong 3.3.0 PDK, then the request_uri
will be updated to include the query arg?
I tried logging both variables (ngx.var.request_uri
and ngx.ctx.request_uri
) and found that they still have the original value (in our case /first?
) even after setting the query arg using the Kong 3.3.0 PDK. Since the last character is ?
, the code proceeds to set the upstream_uri like so and ignores the set query args.
In both versions of Kong (2.2.0 and 3.3.0) var.is_args
is empty and var.args gets updated after setting a query arg using the PDK.
In Kong 2.2.0 the following code works, as it should.
if var.is_args == "?" or sub(var.request_uri, -1) == "?" then
var.upstream_uri = upstream_uri .. "?" .. (var.args or "")
end
The change was introduced in this commit.
I checked with Kong versions 2.5 and 2.6 to verify, it indeed seems to be the case. It stops working as expected Kong 2.6 onwards.
thank you @chirag-manwani for reporting this. Could you please provide a working example where the variable $is_args
is empty, as you pointed out in your comment above? I tested this with the details you have provided and while I was able to reproduce the behavior you describe, I always found the is_args
variable populated with ?
.
@samugi I think I made a mistake. var.is_args
is set to ?
after a query arg is set using the PDK in case of Kong 2.2.
I logged the values before and after setting the query arg, refer the updated yaml in the issue description. After reading your comment I thought maybe in case of Kong 3.3 var.is_args
is not set in the access phase of the plugin, but somewhere else, after access phase finishes. So I tried logging it in the log phase, and I could see it set to ?
.
is_args
should be actually set by your post-function plugin's custom code, when set_query
is executed. Logging it before the query args are configured is expected to return an empty value. Thank you for confirming that. The reason I was asking is that your specific example is using a combination of set and get uri args functions that had similar issues in the past due to indexing, see for example this issue and this fix, which anyway I just confirmed to be unrelated.
Here is a simplified configuration I used to reproduce this:
_format_version: "1.1"
plugins:
- config:
access:
- kong.service.request.set_query({ injected = 1 })
body_filter: []
certificate: []
header_filter: []
log: []
rewrite: []
enabled: true
name: post-function
protocols:
- grpc
- grpcs
- http
- https
services:
- connect_timeout: 5000
host: httpbin.org
name: kong
path: /anything
port: 80
protocol: http
read_timeout: 5000
retries: 0
routes:
- https_redirect_status_code: 426
name: second
path_handling: v1
paths:
- /echo
preserve_host: false
protocols:
- http
- https
regex_priority: 0
request_buffering: true
response_buffering: true
strip_path: true
write_timeout: 5000
curl localhost:8000/echo
{
"args": {
"injected": "1"
},
[...]
}
curl localhost:8000/echo?
{
"args": {},
[...]
}
Let's continue the discussion in the PR. Thanks again for reporting this.
Is there an existing issue for this?
Kong version (
$ kong version
)3.3.0
Current Behavior
When I send a request to Kong with empty URL parameters but with a
?
and there is a plugin that sets an additional query arg, the proxied request to the backend does not have any query args.Expected Behavior
The proxied request should have the query arg set by the plugin.
(Side Note: I am facing this issue in Kong 3.3.0 but not in Kong 2.2.0)
Steps To Reproduce
Sending a request without the
?
or a?
and at least 1 query arg works as expected.Anything else?
I am facing the above issue in Kong 3.3.0. Kong 2.2.0 is working as expected. I have not tried it for other versions, but I can in case more information is required.
Kong Logs
Response from hitting the admin endpoint
OS: Kong is running on ubuntu inside a docker container. Container on MacOS
I would be more than willing to fix this issue myself, if someone could point me in the correct direction.