Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(var): patch set_header #97

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ globals = {
req = {
set_uri_args = {
read_only = false
},
set_header = {
read_only = false
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions lualib/resty/kong/var.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ local NGX_DECLINED = ngx.DECLINED

local variable_index = {}
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
Expand All @@ -50,6 +53,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


Expand Down Expand Up @@ -159,6 +167,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(name, value)
local normalized_header = replace_dashes_lower(name)
normalized_header = HTTP_PREFIX .. normalized_header
variable_index[normalized_header] = nil

return orig_set_header(name, value)
end
end


Expand Down
57 changes: 56 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,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();
Expand Down Expand Up @@ -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]