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 11e7824 feat(uri-blocker): add case insensitive switch (#4868)
11e7824 is described below
commit 11e7824cee0e4ab0145ea7189d991464ade3682a
Author: okaybase <[email protected]>
AuthorDate: Mon Aug 23 18:28:11 2021 +0800
feat(uri-blocker): add case insensitive switch (#4868)
---
apisix/plugins/uri-blocker.lua | 7 +++++
docs/en/latest/plugins/uri-blocker.md | 1 +
docs/zh/latest/plugins/uri-blocker.md | 1 +
t/plugin/uri-blocker.t | 49 +++++++++++++++++++++++++++++++++++
4 files changed, 58 insertions(+)
diff --git a/apisix/plugins/uri-blocker.lua b/apisix/plugins/uri-blocker.lua
index a514e38..4612532 100644
--- a/apisix/plugins/uri-blocker.lua
+++ b/apisix/plugins/uri-blocker.lua
@@ -40,6 +40,10 @@ local schema = {
type = "string",
minLength = 1
},
+ case_insensitive = {
+ type = "boolean",
+ default = false
+ },
},
required = {"block_rules"},
}
@@ -85,6 +89,9 @@ function _M.rewrite(conf, ctx)
end
conf.block_rules_concat = core.table.concat(block_rules, "|")
+ if conf.case_insensitive then
+ conf.block_rules_concat = "(?i)" .. conf.block_rules_concat
+ end
core.log.info("concat block_rules: ", conf.block_rules_concat)
end
diff --git a/docs/en/latest/plugins/uri-blocker.md
b/docs/en/latest/plugins/uri-blocker.md
index d7d4072..1b4cbb7 100644
--- a/docs/en/latest/plugins/uri-blocker.md
+++ b/docs/en/latest/plugins/uri-blocker.md
@@ -40,6 +40,7 @@ The plugin helps we intercept user requests, we only need to
indicate the `block
| block_rules | array[string] | required | | | Regular
filter rule array. Each of these items is a regular rule. If the current
request URI hits any one of them, set the response code to rejected_code to
exit the current user request. Example: `["root.exe", "root.m+"]`. |
| rejected_code | integer | optional | 403 | [200, ...] | The
HTTP status code returned when the request URI hit any of `block_rules`. |
| rejected_msg | string | optional | | non-empty | The HTTP
response body returned when the request URI hit any of `block_rules`. |
+| case_insensitive | boolean | optional | false | | Whether case
insensitive or not. Set true will ignore case when matching the request URI.
Default is false. |
## How To Enable
diff --git a/docs/zh/latest/plugins/uri-blocker.md
b/docs/zh/latest/plugins/uri-blocker.md
index b291d6e..a30cbd5 100644
--- a/docs/zh/latest/plugins/uri-blocker.md
+++ b/docs/zh/latest/plugins/uri-blocker.md
@@ -40,6 +40,7 @@ title: uri-blocker
| block_rules | array[string] | 必须 | | |
正则过滤数组。它们都是正则规则,如果当前请求 URI 命中任何一个,请将响应代码设置为 rejected_code 以退出当前用户请求。例如:
`["root.exe", "root.m+"]`。 |
| rejected_code | integer | 可选 | 403 | [200, ...] | 当请求 URI
命中`block_rules`中的任何一个时,将返回的 HTTP 状态代码。 |
| rejected_msg | string | 可选 | | 非空 | 当请求 URI
命中`block_rules`中的任何一个时,将返回的 HTTP 响应体。 |
+| case_insensitive | boolean | 可选 | false | | 是否忽略大小写。当值为 true
时,在匹配请求 URI 时将忽略大小写。默认值是 false 。 |
## 启用方式
diff --git a/t/plugin/uri-blocker.t b/t/plugin/uri-blocker.t
index 1eb1b60..0d0bce8 100644
--- a/t/plugin/uri-blocker.t
+++ b/t/plugin/uri-blocker.t
@@ -436,3 +436,52 @@ GET /hello?aa=1
{"error_msg":"access is not allowed"}
--- no_error_log
[error]
+
+
+
+=== TEST 19: one block rule, with case insensitive
+--- 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,
+ [[{
+ "plugins": {
+ "uri-blocker": {
+ "block_rules": ["AA"],
+ "rejected_msg": "access is not allowed",
+ "case_insensitive": true
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ },
+ "uri": "/hello"
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.print(body)
+ }
+}
+--- request
+GET /t
+--- no_error_log
+[error]
+
+
+
+=== TEST 20: hit block rule
+--- request
+GET /hello?aa=1
+--- error_code: 403
+--- response_body
+{"error_msg":"access is not allowed"}
+--- no_error_log
+[error]