This is an automated email from the ASF dual-hosted git repository. spacewander pushed a commit to branch release/2.15 in repository https://gitbox.apache.org/repos/asf/apisix.git
commit abed6f17010b50ca3ee59852341443f56d61b282 Author: levy liu <[email protected]> AuthorDate: Thu Nov 3 14:36:42 2022 +0800 fix: can not modify x-forwarded-host in proxy rewrite (#8200) Signed-off-by: spacewander <[email protected]> --- apisix/cli/ngx_tpl.lua | 3 --- apisix/core/ctx.lua | 1 + apisix/init.lua | 5 +++++ t/APISIX.pm | 3 --- t/plugin/proxy-rewrite2.t | 2 +- t/plugin/proxy-rewrite3.t | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 60 insertions(+), 7 deletions(-) diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua index dba9917cb..0ec7f50ab 100644 --- a/apisix/cli/ngx_tpl.lua +++ b/apisix/cli/ngx_tpl.lua @@ -716,9 +716,6 @@ http { if ($http_x_forwarded_for != "") { set $var_x_forwarded_for "${http_x_forwarded_for}, ${realip_remote_addr}"; } - if ($http_x_forwarded_host != "") { - set $var_x_forwarded_host $http_x_forwarded_host; - } if ($http_x_forwarded_port != "") { set $var_x_forwarded_port $http_x_forwarded_port; } diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua index 5bf3daa57..03297a2ff 100644 --- a/apisix/core/ctx.lua +++ b/apisix/core/ctx.lua @@ -200,6 +200,7 @@ do var_x_forwarded_proto = true, var_x_forwarded_port = true, + var_x_forwarded_host = true, } -- sort in alphabetical diff --git a/apisix/init.lua b/apisix/init.lua index 3e2b11055..96068894f 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -229,6 +229,11 @@ local function set_upstream_headers(api_ctx, picked_server) if proto then api_ctx.var.var_x_forwarded_proto = proto end + + local x_forwarded_host = api_ctx.var.http_x_forwarded_host + if x_forwarded_host then + api_ctx.var.var_x_forwarded_host = x_forwarded_host + end end diff --git a/t/APISIX.pm b/t/APISIX.pm index bce7c735a..8c7b41bdd 100644 --- a/t/APISIX.pm +++ b/t/APISIX.pm @@ -770,9 +770,6 @@ _EOC_ if (\$http_x_forwarded_for != "") { set \$var_x_forwarded_for "\${http_x_forwarded_for}, \${realip_remote_addr}"; } - if (\$http_x_forwarded_host != "") { - set \$var_x_forwarded_host \$http_x_forwarded_host; - } if (\$http_x_forwarded_port != "") { set \$var_x_forwarded_port \$http_x_forwarded_port; } diff --git a/t/plugin/proxy-rewrite2.t b/t/plugin/proxy-rewrite2.t index fcd4011ba..2ba08d9f8 100644 --- a/t/plugin/proxy-rewrite2.t +++ b/t/plugin/proxy-rewrite2.t @@ -211,7 +211,7 @@ localhost -=== TEST 7: pass duplicate X-Forwarded-Proto +=== TEST 7: pass duplicate X-Forwarded-Proto --- apisix_yaml routes: - diff --git a/t/plugin/proxy-rewrite3.t b/t/plugin/proxy-rewrite3.t index b67ef379e..7501abd1d 100644 --- a/t/plugin/proxy-rewrite3.t +++ b/t/plugin/proxy-rewrite3.t @@ -288,3 +288,56 @@ GET /print_uri_detailed HTTP/1.1 --- response_body ngx.var.uri: /print_uri_detailed ngx.var.request_uri: /print_uri_detailed + + + +=== TEST 12: set route(rewrite X-Forwarded-Host) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "methods": ["GET"], + "plugins": { + "proxy-rewrite": { + "headers": { + "X-Forwarded-Host": "test.com" + } + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/echo" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 13: rewrite X-Forwarded-Host +--- request +GET /echo HTTP/1.1 +--- more_headers +X-Forwarded-Host: apisix.ai +--- response_headers +X-Forwarded-Host: test.com +--- no_error_log +[error]
