You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by to...@apache.org on 2021/02/18 03:35:10 UTC

[apisix] branch master updated: fix: should assign value to `api_ctx.global_rules` before running global rules (#3595)

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

tokers 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 82018b9  fix: should assign value to `api_ctx.global_rules` before running global rules (#3595)
82018b9 is described below

commit 82018b9cea4ce1ab38138d897bb5fa44f24363ad
Author: nic-chen <33...@users.noreply.github.com>
AuthorDate: Thu Feb 18 11:34:59 2021 +0800

    fix: should assign value to `api_ctx.global_rules` before running global rules (#3595)
    
    related issue #3396
---
 apisix/api_router.lua |   1 -
 apisix/init.lua       |   1 -
 apisix/plugin.lua     |   4 ++
 t/node/global-rule.t  | 108 +++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 111 insertions(+), 3 deletions(-)

diff --git a/apisix/api_router.lua b/apisix/api_router.lua
index e68b261..db23bca 100644
--- a/apisix/api_router.lua
+++ b/apisix/api_router.lua
@@ -125,7 +125,6 @@ function fetch_api_router()
                             if not skip_global_rule then
                                 plugin_mod.run_global_rules(api_ctx,
                                     apisix_router.global_rules, "access")
-                                api_ctx.global_rules = apisix_router.global_rules
                             end
 
                             code, body = route.handler(api_ctx)
diff --git a/apisix/init.lua b/apisix/init.lua
index 38b69ce..f1ec7a6 100644
--- a/apisix/init.lua
+++ b/apisix/init.lua
@@ -298,7 +298,6 @@ function _M.http_access_phase()
 
     -- run global rule
     plugin.run_global_rules(api_ctx, router.global_rules, "access")
-    api_ctx.global_rules = router.global_rules
 
     local route = api_ctx.matched_route
     if not route then
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index 54bdf47..513d7f8 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -689,6 +689,10 @@ function _M.run_global_rules(api_ctx, global_rules, phase_name)
         local orig_conf_version = api_ctx.conf_version
         local orig_conf_id = api_ctx.conf_id
 
+        if phase_name == "access" then
+            api_ctx.global_rules = global_rules
+        end
+
         local plugins = core.tablepool.fetch("plugins", 32, 0)
         local values = global_rules.values
         for _, global_rule in config_util.iterate_values(values) do
diff --git a/t/node/global-rule.t b/t/node/global-rule.t
index f96648b..eda574a 100644
--- a/t/node/global-rule.t
+++ b/t/node/global-rule.t
@@ -174,7 +174,107 @@ GET /apisix/status
 
 
 
-=== TEST 10: delete global rule
+=== TEST 10: update global rule
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/global_rules/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "response-rewrite": {
+                            "headers": {
+                                "X-VERSION":"1.0"
+                            }
+                        },
+                        "uri-blocker": {
+                            "block_rules": ["select.+(from|limit)", "(?:(union(.*?)select))"]
+                        }
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 11: set one more global rule
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/global_rules/2',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "response-rewrite": {
+                            "headers": {
+                                "X-TEST":"test"
+                            }
+                        }
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 12: hit global rules
+--- request
+GET /hello?name=;union%20select%20
+--- error_code: 403
+--- response_headers
+X-VERSION: 1.0
+X-TEST: test
+--- no_error_log
+[error]
+
+
+
+=== TEST 13: hit global rules by internal api
+--- yaml_config
+apisix:
+  global_rule_skip_internal_api: false
+plugins:
+  - response-rewrite
+  - uri-blocker
+  - node-status
+--- request
+GET /apisix/status?name=;union%20select%20
+--- error_code: 403
+--- response_headers
+X-VERSION: 1.0
+X-TEST: test
+--- no_error_log
+[error]
+
+
+
+=== TEST 14: delete global rules
 --- config
     location /t {
         content_by_lua_block {
@@ -186,6 +286,12 @@ GET /apisix/status
             end
             ngx.say(body)
 
+            local code, body = t('/apisix/admin/global_rules/2', ngx.HTTP_DELETE)
+
+            if code >= 300 then
+                ngx.status = code
+            end
+
             local code, body = t('/not_found', ngx.HTTP_GET)
             ngx.say(code)
             local code, body = t('/not_found', ngx.HTTP_GET)