Skip to content

Commit

Permalink
fix(var): patch set_uri_args (#59)
Browse files Browse the repository at this point in the history
set_uri_args modifies the $args variable without accessing our patched
metatable. For it to work we need to invalidate the index whenever this
function is called.

fix for Kong/kong#10080
  • Loading branch information
samugi authored Jan 27, 2023
1 parent fec7331 commit bd5915f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ not_globals = {
ignore = {
"6.", -- ignore whitespace warnings
}


globals = {
ngx = {
req = {
set_uri_args = {
read_only = false
}
}
}
}
13 changes: 13 additions & 0 deletions lualib/resty/kong/var.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local C = ffi.C
local ffi_new = ffi.new
local ffi_str = ffi.string
local var = ngx.var
local req = ngx.req
local type = type
local error = error
local assert = assert
Expand Down Expand Up @@ -150,6 +151,16 @@ local function var_set_by_index(index, value)
end


local function patch_functions()
local orig_set_uri_args = req.set_uri_args

req.set_uri_args = function(...)
variable_index.args = nil
return orig_set_uri_args(...)
end
end


local function patch_metatable()
if get_phase() ~= "init" then
error("patch_metatable can only be called in init phase")
Expand Down Expand Up @@ -184,6 +195,8 @@ local function patch_metatable()

return orig_set(self, name, value)
end

patch_functions()
end


Expand Down
35 changes: 34 additions & 1 deletion t/005-indexed-var-openresty-suites.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Test::Nginx::Socket::Lua;

repeat_each(2);

plan tests => repeat_each() * (blocks() * 2 + 9 + 4);
plan tests => repeat_each() * (blocks() * 2 + 9 + 4 + 3);

#no_diff();
#no_long_string();
Expand Down Expand Up @@ -359,3 +359,36 @@ variable not changeable
["GET /balancer?port=8091", "GET /balancer?port=8092"]
--- response_body eval
["this is backend peer 8091", "this is backend peer 8092"]
=== TEST 13: patch metatable does not invalidate function req.set_uri_args
--- http_config
lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;";
# this is not required, but set explictly in tests
lua_kong_load_var_index $args;
init_by_lua_block {
local var = require "resty.kong.var"
var.patch_metatable()
}
--- config
set $args 'foo=bar';
location /t {
content_by_lua_block {
local a = ngx.var.args
ngx.req.set_uri_args(a .. "&added=yes")
ngx.say(ngx.var.args)
}
}

--- request
GET /t
--- response_body_like
foo=bar&added=yes

--- error_code: 200
--- no_error_log
[error]
[crit]
[alert]

0 comments on commit bd5915f

Please sign in to comment.