You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/11/21 12:07:52 UTC

[apisix] branch master updated: feat(request-validation): add custom rejected_code (#5553)

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 2262e1c  feat(request-validation): add custom rejected_code (#5553)
2262e1c is described below

commit 2262e1c93a516be4f48faabacf6aaf28fd49c0dd
Author: Xunzhuo <mi...@gmail.com>
AuthorDate: Sun Nov 21 20:07:42 2021 +0800

    feat(request-validation): add custom rejected_code (#5553)
---
 apisix/plugins/request-validation.lua        |  11 +-
 docs/en/latest/plugins/request-validation.md |   1 +
 docs/zh/latest/plugins/request-validation.md |   1 +
 t/plugin/request-validation.t                | 163 +++++++++++++++++++++++++++
 4 files changed, 171 insertions(+), 5 deletions(-)

diff --git a/apisix/plugins/request-validation.lua b/apisix/plugins/request-validation.lua
index bdd26fa..8f383bf 100644
--- a/apisix/plugins/request-validation.lua
+++ b/apisix/plugins/request-validation.lua
@@ -26,6 +26,7 @@ local schema = {
     properties = {
         header_schema = {type = "object"},
         body_schema = {type = "object"},
+        rejected_code = {type = "integer", minimum = 200, maximum = 599},
         rejected_msg = {type = "string", minLength = 1, maxLength = 256}
     },
     anyOf = {
@@ -75,7 +76,7 @@ function _M.rewrite(conf)
         local ok, err = core.schema.check(conf.header_schema, headers)
         if not ok then
             core.log.error("req schema validation failed", err)
-            return 400, conf.rejected_msg or err
+            return conf.rejected_code or 400, conf.rejected_msg or err
         end
     end
 
@@ -87,11 +88,11 @@ function _M.rewrite(conf)
         if not body then
             local filename = ngx.req.get_body_file()
             if not filename then
-                return 500, conf.rejected_msg
+                return conf.rejected_code or 500, conf.rejected_msg
             end
             local fd = io.open(filename, 'rb')
             if not fd then
-                return 500, conf.rejected_msg
+                return conf.rejected_code or 500, conf.rejected_msg
             end
             body = fd:read('*a')
         end
@@ -104,13 +105,13 @@ function _M.rewrite(conf)
 
         if not req_body then
           core.log.error('failed to decode the req body', error)
-          return 400, conf.rejected_msg or error
+          return conf.rejected_code or 400, conf.rejected_msg or error
         end
 
         local ok, err = core.schema.check(conf.body_schema, req_body)
         if not ok then
           core.log.error("req schema validation failed", err)
-          return 400, conf.rejected_msg or err
+          return conf.rejected_code or 400, conf.rejected_msg or err
         end
     end
 end
diff --git a/docs/en/latest/plugins/request-validation.md b/docs/en/latest/plugins/request-validation.md
index d2a17b2..42951bd 100644
--- a/docs/en/latest/plugins/request-validation.md
+++ b/docs/en/latest/plugins/request-validation.md
@@ -45,6 +45,7 @@ For more information on schema, refer to [JSON schema](https://github.com/api7/j
 | ---------------- | ------ | ----------- | ------- | ----- | -------------------------- |
 | header_schema    | object | optional    |         |       | schema for the header data |
 | body_schema      | object | optional    |         |       | schema for the body data   |
+| rejected_code | integer | optional    |         |    [200,...,599]   | the custom rejected code |
 | rejected_msg | string | optional    |         |       | the custom rejected message |
 
 ## How To Enable
diff --git a/docs/zh/latest/plugins/request-validation.md b/docs/zh/latest/plugins/request-validation.md
index a1ba46d..e9f5ed8 100644
--- a/docs/zh/latest/plugins/request-validation.md
+++ b/docs/zh/latest/plugins/request-validation.md
@@ -44,6 +44,7 @@ title: request-validation
 | ---------------- | ------ | ----------- | ------- | ----- | --------------------------------- |
 | header_schema    | object | 可选        |         |       | `header` 数据的 `schema` 数据结构 |
 | body_schema      | object | 可选        |         |       | `body` 数据的 `schema` 数据结构   |
+| rejected_code | integer | 可选        |         |    [200,...,599]   | 自定义拒绝状态码 |
 | rejected_msg | string | 可选        |         |       | 自定义拒绝信息 |
 
 ## 如何启用
diff --git a/t/plugin/request-validation.t b/t/plugin/request-validation.t
index 711bf22..6e3e697 100644
--- a/t/plugin/request-validation.t
+++ b/t/plugin/request-validation.t
@@ -1658,3 +1658,166 @@ qr/object matches none of the requireds/
 400
 --- no_error_log
 [error]
+
+
+
+=== TEST 45: add route (test request validation `body_schema.required` success with custom reject code)
+--- 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": {
+                        "request-validation": {
+                            "body_schema": {
+                                "type": "object",
+                                "properties": {
+                                    "test": {
+                                        "type": "string",
+                                        "enum": ["a", "b", "c"]
+                                    }
+                                },
+                                "required": ["test"]
+                            },
+                            "rejected_code": 505
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1982": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/opentracing"
+                }]])
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 46: use empty body to hit custom rejected code rule
+--- request
+GET /opentracing
+--- error_code: 505
+--- no_error_log
+[error]
+
+
+
+=== TEST 47: use bad body value to hit custom rejected code rule
+--- request
+POST /opentracing
+{"test":"abc"}
+--- error_code: 505
+--- error_log eval
+qr/schema validation failed/
+
+
+
+=== TEST 48: pass custom rejected code rule
+--- request
+POST /opentracing
+{"test":"a"}
+--- error_code: 200
+--- response_body eval
+qr/opentracing/
+--- no_error_log
+[error]
+
+
+
+=== TEST 49: add route (test request validation `header_schema.required` failure with custom reject code)
+--- 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": {
+                        "request-validation": {
+                            "header_schema": {
+                                "type": "object",
+                                "properties": {
+                                    "test": {
+                                        "type": "string",
+                                        "enum": ["a", "b", "c"]
+                                    }
+                                },
+                                "required": ["test"]
+                            },
+                            "rejected_code": 10000
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1982": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/plugin/request/validation"
+                }]])
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body_like eval
+qr/expected 10000 to be smaller than 599/
+--- error_code chomp
+400
+--- no_error_log
+[error]
+
+
+
+=== TEST 50: add route (test request validation schema with custom reject code only)
+--- 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": {
+                        "request-validation": {
+                            "rejected_code": 505
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1982": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/plugin/request/validation"
+                }]])
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body_like eval
+qr/object matches none of the requireds/
+--- error_code chomp
+400
+--- no_error_log
+[error]