This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 03e7606b6 fix: can not modify x-forwarded-host in proxy rewrite (#8200)
03e7606b6 is described below

commit 03e7606b6fd0fafa707d9ada6d6318c2e5c30bd8
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)
---
 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 fd45d7f84..cc849884e 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -677,9 +677,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 b98df9186..b0210259f 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 01f4398cc..9c1dde08c 100644
--- a/apisix/init.lua
+++ b/apisix/init.lua
@@ -258,6 +258,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 fc79d0eb9..8c6924cf0 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -771,9 +771,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 4ba236f36..bd95360f8 100644
--- a/t/plugin/proxy-rewrite2.t
+++ b/t/plugin/proxy-rewrite2.t
@@ -186,7 +186,7 @@ localhost
 
 
 
-=== TEST 6: pass duplicate  X-Forwarded-Proto
+=== TEST 6: pass duplicate X-Forwarded-Proto
 --- apisix_yaml
 routes:
   -
diff --git a/t/plugin/proxy-rewrite3.t b/t/plugin/proxy-rewrite3.t
index 88e2a9db1..7ddedefd1 100644
--- a/t/plugin/proxy-rewrite3.t
+++ b/t/plugin/proxy-rewrite3.t
@@ -284,3 +284,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]

Reply via email to