From 62ceab5710859d39828c6a88e3602ab6df1a589f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Thu, 3 Jun 2021 09:39:51 +0800 Subject: [PATCH] fix(ext-plugin): missing schema check (#4359) Signed-off-by: spacewander --- apisix/plugins/ext-plugin-post-req.lua | 6 +++ apisix/plugins/ext-plugin-pre-req.lua | 6 +++ apisix/plugins/ext-plugin/init.lua | 3 +- t/plugin/ext-plugin/sanity.t | 63 ++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/apisix/plugins/ext-plugin-post-req.lua b/apisix/plugins/ext-plugin-post-req.lua index 07ac6bd3238b..3f7aaf51c50a 100644 --- a/apisix/plugins/ext-plugin-post-req.lua +++ b/apisix/plugins/ext-plugin-post-req.lua @@ -14,6 +14,7 @@ -- See the License for the specific language governing permissions and -- limitations under the License. -- +local core = require("apisix.core") local ext = require("apisix.plugins.ext-plugin.init") @@ -25,6 +26,11 @@ local _M = { } +function _M.check_schema(conf) + return core.schema.check(_M.schema, conf) +end + + function _M.access(conf, ctx) return ext.communicate(conf, ctx) end diff --git a/apisix/plugins/ext-plugin-pre-req.lua b/apisix/plugins/ext-plugin-pre-req.lua index 3695ec5de545..8903e12ca2a7 100644 --- a/apisix/plugins/ext-plugin-pre-req.lua +++ b/apisix/plugins/ext-plugin-pre-req.lua @@ -14,6 +14,7 @@ -- See the License for the specific language governing permissions and -- limitations under the License. -- +local core = require("apisix.core") local ext = require("apisix.plugins.ext-plugin.init") @@ -25,6 +26,11 @@ local _M = { } +function _M.check_schema(conf) + return core.schema.check(_M.schema, conf) +end + + function _M.rewrite(conf, ctx) return ext.communicate(conf, ctx) end diff --git a/apisix/plugins/ext-plugin/init.lua b/apisix/plugins/ext-plugin/init.lua index 7be360f8ee57..53e1d1cc751d 100644 --- a/apisix/plugins/ext-plugin/init.lua +++ b/apisix/plugins/ext-plugin/init.lua @@ -79,7 +79,8 @@ local schema = { value = { type = "string", }, - } + }, + required = {"name", "value"} }, minItems = 1, }, diff --git a/t/plugin/ext-plugin/sanity.t b/t/plugin/ext-plugin/sanity.t index bde3fcc28d5f..aacb7b32d38d 100644 --- a/t/plugin/ext-plugin/sanity.t +++ b/t/plugin/ext-plugin/sanity.t @@ -382,3 +382,66 @@ env MY_ENV_VAR=foo; } --- error_log MY_ENV_VAR foo + + + +=== TEST 14: bad conf +--- config + location /t { + content_by_lua_block { + local json = require("toolkit.json") + local t = require("lib.test_admin") + + local code, message, res = t.test('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/hello", + "plugins": { + "ext-plugin-pre-req": { + "conf": [ + {"value":"bar"} + ] + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + } + }]] + ) + + if code >= 300 then + ngx.say(message) + end + + local code, message, res = t.test('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/hello", + "plugins": { + "ext-plugin-post-req": { + "conf": [ + {"name":"bar"} + ] + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + } + }]] + ) + + if code >= 300 then + ngx.print(message) + end + } + } +--- response_body +{"error_msg":"failed to check the configuration of plugin ext-plugin-pre-req err: property \"conf\" validation failed: failed to validate item 1: property \"name\" is required"} + +{"error_msg":"failed to check the configuration of plugin ext-plugin-post-req err: property \"conf\" validation failed: failed to validate item 1: property \"value\" is required"}