diff --git a/apisix/plugin.lua b/apisix/plugin.lua index 18954f47e1d4..fc24778127a8 100644 --- a/apisix/plugin.lua +++ b/apisix/plugin.lua @@ -102,12 +102,27 @@ local function load_plugin(name, plugins_list, is_stream_plugin) return end - if plugin.schema and plugin.schema.type == "object" then - if not plugin.schema.properties or - core.table.nkeys(plugin.schema.properties) == 0 - then - plugin.schema.properties = core.schema.plugin_disable_schema + if type(plugin.schema) ~= "table" then + core.log.error("invalid plugin [", name, "] schema field") + return + end + + if not plugin.schema.properties then + plugin.schema.properties = {} + end + + local properties = plugin.schema.properties + local plugin_injected_schema = core.schema.plugin_injected_schema + + if properties['$comment'] ~= plugin_injected_schema['$comment'] then + if properties.disable then + core.log.error("invalid plugin [", name, + "]: found forbidden 'disable' field in the schema") + return end + + properties.disable = plugin_injected_schema.disable + properties['$comment'] = plugin_injected_schema['$comment'] end plugin.name = name diff --git a/apisix/schema_def.lua b/apisix/schema_def.lua index daa4acea4fa8..7d9cf49a97c9 100644 --- a/apisix/schema_def.lua +++ b/apisix/schema_def.lua @@ -739,8 +739,11 @@ _M.plugins = { _M.id_schema = id_schema -_M.plugin_disable_schema = { - disable = {type = "boolean"} +_M.plugin_injected_schema = { + ["$comment"] = "this is a mark for our injected plugin schema", + disable = { + type = "boolean", + } } diff --git a/t/control/schema.t b/t/control/schema.t index c1197cfc1d2d..ac0c124e70f3 100644 --- a/t/control/schema.t +++ b/t/control/schema.t @@ -60,16 +60,26 @@ __DATA__ "upstream": {"type":"object"}, "upstream_hash_header_schema": {"type":"string"}, "upstream_hash_vars_schema": {"type":"string"}, - }, + },]] .. [[ "plugins": { "example-plugin": { "version": 0.1, "priority": 0, - "schema": {"type":"object"}, + "schema": { + "type":"object", + "properties": { + "disable": {"type": "boolean"} + } + }, "metadata_schema": {"type":"object"} }, "mqtt-proxy": { - "schema": {"type":"object"}, + "schema": { + "type":"object", + "properties": { + "disable": {"type": "boolean"} + } + }, "priority": 1000 }, "basic-auth": {