Skip to content

Commit ddc1f95

Browse files
authored
fix(var): patch set_header (#97) (#104)
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: #59 Fix: KAG-5963 Fix: FTI-6406 Signed-off-by: tzssangglass <[email protected]>
1 parent 6672033 commit ddc1f95

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
push:
66

77
env:
8-
KONG_VERSION: master
8+
KONG_VERSION: release/3.9.x
99
BUILD_ROOT: ${{ github.workspace }}/kong/bazel-bin/build
1010

1111
concurrency:

.luacheckrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ globals = {
2020
req = {
2121
set_uri_args = {
2222
read_only = false
23+
},
24+
set_header = {
25+
read_only = false
2326
}
2427
}
2528
}

lualib/resty/kong/var.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ local NGX_DECLINED = ngx.DECLINED
2424

2525
local variable_index = {}
2626
local metatable_patched
27+
local str_replace_char
28+
local replace_dashes_lower
2729

30+
local HTTP_PREFIX = "http_"
2831

2932
--Add back if stream module is implemented to aid readability
3033
--see bottom of: https://luajit.org/ext_ffi_tutorial.html
@@ -50,6 +53,11 @@ if subsystem == "http" then
5053
--ngx_lua_kong_ffi_var_get_by_index = C.ngx_http_lua_kong_ffi_var_get_by_index
5154
--ngx_lua_kong_ffi_var_set_by_index = C.ngx_http_lua_kong_ffi_var_set_by_index
5255
--ngx_lua_kong_ffi_var_load_indexes = C.ngx_http_lua_kong_ffi_var_load_indexes
56+
57+
str_replace_char = require("resty.core.utils").str_replace_char
58+
replace_dashes_lower = function(str)
59+
return str_replace_char(str:lower(), "-", "_")
60+
end
5361
end
5462

5563

@@ -159,6 +167,16 @@ local function patch_functions()
159167
variable_index.args = nil
160168
return orig_set_uri_args(...)
161169
end
170+
171+
local orig_set_header = req.set_header
172+
173+
req.set_header = function(name, value)
174+
local normalized_header = replace_dashes_lower(name)
175+
normalized_header = HTTP_PREFIX .. normalized_header
176+
variable_index[normalized_header] = nil
177+
178+
return orig_set_header(name, value)
179+
end
162180
end
163181

164182

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use Test::Nginx::Socket::Lua;
99

1010
repeat_each(2);
1111

12-
plan tests => repeat_each() * (blocks() * 2 + 9 + 4 + 3);
12+
plan tests => repeat_each() * (blocks() * 2 + 9 + 4 + 3 + 3);
13+
1314

1415
#no_diff();
1516
#no_long_string();
@@ -387,3 +388,57 @@ foo=bar&added=yes
387388
[error]
388389
[crit]
389390
[alert]
391+
392+
393+
394+
=== TEST 14: patch metatable does not invalidate function req.set_header
395+
--- http_config
396+
lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;";
397+
lua_kong_load_var_index default;
398+
399+
init_by_lua_block {
400+
require("resty.kong.var").patch_metatable()
401+
}
402+
403+
--- config
404+
location /auth {
405+
content_by_lua_block {
406+
ngx.say(ngx.var.http_authorization)
407+
ngx.req.set_header("Authorization", "foo")
408+
ngx.say(ngx.var.http_authorization)
409+
ngx.req.set_header("Authorization", "bar")
410+
ngx.say(ngx.var.http_authorization)
411+
ngx.req.set_header("Authorization", "baz")
412+
ngx.say(ngx.var.http_authorization)
413+
ngx.req.set_header("Authorization", "qux")
414+
ngx.say(ngx.var.http_authorization)
415+
416+
ngx.say(ngx.var.http_kong_debug)
417+
ngx.req.set_header("Kong-Debug", "true")
418+
ngx.say(ngx.var.http_kong_debug)
419+
ngx.req.set_header("Kong-Debug", "false")
420+
ngx.say(ngx.var.http_kong_debug)
421+
ngx.req.set_header("Kong-Debug", "true")
422+
ngx.say(ngx.var.http_kong_debug)
423+
ngx.req.set_header("Kong-Debug", "false")
424+
ngx.say(ngx.var.http_kong_debug)
425+
}
426+
}
427+
428+
--- request
429+
GET /auth
430+
--- response_body
431+
nil
432+
foo
433+
bar
434+
baz
435+
qux
436+
nil
437+
true
438+
false
439+
true
440+
false
441+
--- no_error_log
442+
[error]
443+
[crit]
444+
[alert]

0 commit comments

Comments
 (0)