Skip to content

Commit cdf8a56

Browse files
committed
fix(var): patch set_uri_args
set_uri_args modifies the $args variable without accessing our patched metatable. For it to work we need to invalidate our index whenever the function is called. fix for Kong/kong#10080
1 parent fec7331 commit cdf8a56

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lualib/resty/kong/var.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local C = ffi.C
66
local ffi_new = ffi.new
77
local ffi_str = ffi.string
88
local var = ngx.var
9+
local req = ngx.req
910
local type = type
1011
local error = error
1112
local assert = assert
@@ -150,6 +151,16 @@ local function var_set_by_index(index, value)
150151
end
151152

152153

154+
local function patch_functions()
155+
local orig_set_uri_args = req.set_uri_args
156+
157+
req.set_uri_args = function(...)
158+
variable_index.args = nil
159+
return orig_set_uri_args(...)
160+
end
161+
end
162+
163+
153164
local function patch_metatable()
154165
if get_phase() ~= "init" then
155166
error("patch_metatable can only be called in init phase")
@@ -184,6 +195,8 @@ local function patch_metatable()
184195

185196
return orig_set(self, name, value)
186197
end
198+
199+
patch_functions()
187200
end
188201

189202

t/005-indexed-var-openresty-suites.t

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,36 @@ variable not changeable
359359
["GET /balancer?port=8091", "GET /balancer?port=8092"]
360360
--- response_body eval
361361
["this is backend peer 8091", "this is backend peer 8092"]
362+
363+
=== TEST 13: patch metatable does not invalidate function req.set_uri_args
364+
--- http_config
365+
lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;";
366+
# this is not required, but set explictly in tests
367+
lua_kong_load_var_index $args;
368+
369+
init_by_lua_block {
370+
local var = require "resty.kong.var"
371+
var.patch_metatable()
372+
}
373+
374+
--- config
375+
set $args 'foo=bar';
376+
377+
location /t {
378+
content_by_lua_block {
379+
local a = ngx.var.args
380+
ngx.req.set_uri_args(a .. "&added=yes")
381+
ngx.say(ngx.var.args)
382+
}
383+
}
384+
385+
--- request
386+
GET /t
387+
--- response_body_like
388+
foo=bar&added=yes
389+
390+
--- error_code: 200
391+
--- no_error_log
392+
[error]
393+
[crit]
394+
[alert]

0 commit comments

Comments
 (0)