From e817446e018b0e760577e7fdce7fa54120f7cad7 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 11 Dec 2024 21:26:32 +0800 Subject: [PATCH 1/3] fix(var): patch set_header patch the `req.set_header` function to invalidate the relevant `variable_index` entry for the modified header. Specifically, after normalizing the header name, the corresponding `variable_index` entry is set to `nil`. This ensures that subsequent accesses to `ngx.var.http_*` variables will bypass the cached index and fetch the updated header value. same fix way as: https://github.com/Kong/lua-kong-nginx-module/pull/59 Fix: [KAG-5963](https://konghq.atlassian.net/browse/KAG-5963) Signed-off-by: tzssangglass --- .luacheckrc | 3 ++ lualib/resty/kong/var.lua | 17 +++++++++ t/005-indexed-var-openresty-suites.t | 57 +++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/.luacheckrc b/.luacheckrc index 03b9b076..6d02a939 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -20,6 +20,9 @@ globals = { req = { set_uri_args = { read_only = false + }, + set_header = { + read_only = false } } } diff --git a/lualib/resty/kong/var.lua b/lualib/resty/kong/var.lua index a2fa73b1..9c89244a 100644 --- a/lualib/resty/kong/var.lua +++ b/lualib/resty/kong/var.lua @@ -24,6 +24,8 @@ local NGX_DECLINED = ngx.DECLINED local variable_index = {} local metatable_patched +local str_replace_char +local replace_dashes_lower --Add back if stream module is implemented to aid readability @@ -50,6 +52,11 @@ if subsystem == "http" then --ngx_lua_kong_ffi_var_get_by_index = C.ngx_http_lua_kong_ffi_var_get_by_index --ngx_lua_kong_ffi_var_set_by_index = C.ngx_http_lua_kong_ffi_var_set_by_index --ngx_lua_kong_ffi_var_load_indexes = C.ngx_http_lua_kong_ffi_var_load_indexes + + str_replace_char = require("resty.core.utils").str_replace_char + replace_dashes_lower = function(str) + return str_replace_char(str:lower(), "-", "_") + end end @@ -159,6 +166,16 @@ local function patch_functions() variable_index.args = nil return orig_set_uri_args(...) end + + local orig_set_header = req.set_header + + req.set_header = function(...) + local normalized_header = replace_dashes_lower(...) + normalized_header = "http_" .. normalized_header + variable_index[normalized_header] = nil + + return orig_set_header(...) + end end diff --git a/t/005-indexed-var-openresty-suites.t b/t/005-indexed-var-openresty-suites.t index e646fe68..a4bfb60f 100644 --- a/t/005-indexed-var-openresty-suites.t +++ b/t/005-indexed-var-openresty-suites.t @@ -9,7 +9,8 @@ use Test::Nginx::Socket::Lua; repeat_each(2); -plan tests => repeat_each() * (blocks() * 2 + 9 + 4 + 3); +plan tests => repeat_each() * (blocks() * 2 + 9 + 4 + 3 + 3); + #no_diff(); #no_long_string(); @@ -387,3 +388,57 @@ foo=bar&added=yes [error] [crit] [alert] + + + +=== TEST 14: patch metatable does not invalidate function req.set_header +--- http_config + lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;"; + lua_kong_load_var_index default; + + init_by_lua_block { + require("resty.kong.var").patch_metatable() + } + +--- config + location /auth { + content_by_lua_block { + ngx.say(ngx.var.http_authorization) + ngx.req.set_header("Authorization", "foo") + ngx.say(ngx.var.http_authorization) + ngx.req.set_header("Authorization", "bar") + ngx.say(ngx.var.http_authorization) + ngx.req.set_header("Authorization", "baz") + ngx.say(ngx.var.http_authorization) + ngx.req.set_header("Authorization", "qux") + ngx.say(ngx.var.http_authorization) + + ngx.say(ngx.var.http_kong_debug) + ngx.req.set_header("Kong-Debug", "true") + ngx.say(ngx.var.http_kong_debug) + ngx.req.set_header("Kong-Debug", "false") + ngx.say(ngx.var.http_kong_debug) + ngx.req.set_header("Kong-Debug", "true") + ngx.say(ngx.var.http_kong_debug) + ngx.req.set_header("Kong-Debug", "false") + ngx.say(ngx.var.http_kong_debug) + } + } + +--- request +GET /auth +--- response_body +nil +foo +bar +baz +qux +nil +true +false +true +false +--- no_error_log +[error] +[crit] +[alert] From 5e0dcd2def2f3ecc0ec941f743660d2888b36c8d Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Mon, 16 Dec 2024 11:12:02 +0800 Subject: [PATCH 2/3] apply comments Signed-off-by: tzssangglass --- lualib/resty/kong/var.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lualib/resty/kong/var.lua b/lualib/resty/kong/var.lua index 9c89244a..b89a9e1e 100644 --- a/lualib/resty/kong/var.lua +++ b/lualib/resty/kong/var.lua @@ -27,6 +27,7 @@ local metatable_patched local str_replace_char local replace_dashes_lower + local HTTP_PREFIX = "http_" --Add back if stream module is implemented to aid readability --see bottom of: https://luajit.org/ext_ffi_tutorial.html @@ -169,12 +170,12 @@ local function patch_functions() local orig_set_header = req.set_header - req.set_header = function(...) - local normalized_header = replace_dashes_lower(...) - normalized_header = "http_" .. normalized_header + req.set_header = function(name, value) + local normalized_header = replace_dashes_lower(name) + normalized_header = HTTP_PREFIX .. normalized_header variable_index[normalized_header] = nil - return orig_set_header(...) + return orig_set_header(name, value) end end From 6e8ad7b5c1e3b64122ea63bda6555ce01cf4e792 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 18 Dec 2024 17:14:06 +0800 Subject: [PATCH 3/3] chore Signed-off-by: tzssangglass --- lualib/resty/kong/var.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lualib/resty/kong/var.lua b/lualib/resty/kong/var.lua index b89a9e1e..2c0f0748 100644 --- a/lualib/resty/kong/var.lua +++ b/lualib/resty/kong/var.lua @@ -27,7 +27,7 @@ local metatable_patched local str_replace_char local replace_dashes_lower - local HTTP_PREFIX = "http_" +local HTTP_PREFIX = "http_" --Add back if stream module is implemented to aid readability --see bottom of: https://luajit.org/ext_ffi_tutorial.html