From ff60e57996a95b8865820d09a32de676b8801064 Mon Sep 17 00:00:00 2001 From: Yuelin Zheng <2226815922@qq.com> Date: Thu, 14 Jan 2021 16:37:36 +0800 Subject: [PATCH] chore(traffic-split): add additionalProperties field to restrict plugin configuration (#3285) --- apisix/plugins/traffic-split.lua | 6 +- t/plugin/traffic-split.t | 106 +++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/apisix/plugins/traffic-split.lua b/apisix/plugins/traffic-split.lua index d74b08c4..d0bdec70 100644 --- a/apisix/plugins/traffic-split.lua +++ b/apisix/plugins/traffic-split.lua @@ -130,10 +130,12 @@ local schema = { properties = { match = match_schema, weighted_upstreams = upstreams_schema - } + }, + additionalProperties = false } } - } + }, + additionalProperties = false } local plugin_name = "traffic-split" diff --git a/t/plugin/traffic-split.t b/t/plugin/traffic-split.t index dae51cc4..cb05004a 100644 --- a/t/plugin/traffic-split.t +++ b/t/plugin/traffic-split.t @@ -1290,3 +1290,109 @@ qr/upstream_key: roundrobin#route_1_\d/ upstream_key: roundrobin#route_1_1 --- no_error_log [error] + + + +=== TEST 38: schema validation, "additionalProperties = false" to limit the plugin configuration +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.traffic-split") + local ok, err = plugin.check_schema({ + additional_properties = "hello", + rules = { + { + match = { + { + vars = { + {"arg_name", "==", "jack"}, + {"arg_age", "!", "<", "16"} + } + }, + { + vars = { + {"arg_name", "==", "rose"}, + {"arg_age", "!", ">", "32"} + } + } + }, + weighted_upstreams = { + { + upstream = { + name = "upstream_A", + type = "roundrobin", + nodes = {["127.0.0.1:1981"]=2}, + timeout = {connect = 15, send = 15, read = 15} + }, + weight = 2 + } + } + } + } + }) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- request +GET /t +--- response_body eval +qr/additional properties forbidden, found additional_properties/ +--- no_error_log +[error] + + + +=== TEST 39: schema validation, "additionalProperties = false" to limit the "rules" configuration +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.traffic-split") + local ok, err = plugin.check_schema({ + rules = { + { + additional_properties = "hello", + match = { + { + vars = { + {"arg_name", "==", "jack"}, + {"arg_age", "!", "<", "16"} + } + }, + { + vars = { + {"arg_name", "==", "rose"}, + {"arg_age", "!", ">", "32"} + } + } + }, + weighted_upstreams = { + { + upstream = { + name = "upstream_A", + type = "roundrobin", + nodes = {["127.0.0.1:1981"]=2}, + timeout = {connect = 15, send = 15, read = 15} + }, + weight = 2 + } + } + } + } + }) + if not ok then + ngx.say(err) + end + + ngx.say("done") + } + } +--- request +GET /t +--- response_body eval +qr/property "rules" validation failed: failed to validate item 1: additional properties forbidden, found additional_properties/ +--- no_error_log +[error]