From 57f31a904a40ac0db69d2c906088b4ba35956940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=A9=89=E8=8A=B3?= Date: Tue, 4 Jul 2023 21:06:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/apisix-coraza.lua | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/apisix-coraza.lua b/lib/apisix-coraza.lua index 2befe2a..345a8ff 100644 --- a/lib/apisix-coraza.lua +++ b/lib/apisix-coraza.lua @@ -19,28 +19,21 @@ local core_log = core.log local coraza = require "resty.coraza" - local schema = { type = "object", properties = { - Mode = { - description = "waf running at block mode or monitor mode", + mode = { + description = "waf running at block mode or monitor mode.", type = "string" }, - Rules = { - type = "array", - items = { - type = "string", - minLength = 1, - maxLength = 4096, - }, - uniqueItems = true + rules = { + description = "self waf rules.", + type = "array" }, }, - required = {"Mode"}, + required = {"mode"}, } - local plugin_name = "apisix-coraza" local _M = { @@ -52,12 +45,20 @@ local _M = { function _M.check_schema(conf) core.log.info("check coraza schema") - if conf.Rules ~= nil then - for i, rule in ipairs(conf.Rules) do - coraza.rules_add(rule) + local ok, err = core.schema.check(schema, conf) + if not ok then + return false, err + end + if conf.rules ~= nil then + for i, rule in ipairs(conf.rules) do + local ok, msg = coraza.rules_add(rule) + ngx.log(ngx.ERR, ok) + if not ok then + return false, rule..msg + end end end - return core.schema.check(schema, conf) + return true end function _M.init() @@ -75,7 +76,6 @@ end function _M.header_filter(conf, ctx) core.log.info("plugin access phase, conf: ", core.json.delay_encode(conf)) - -- each connection will be created a transaction coraza.do_header_filter() ngx.status, _ = coraza.do_handle() core.response.clear_header_as_body_modified() @@ -91,4 +91,4 @@ function _M.log(conf, ctx) coraza.do_free() end -return _M \ No newline at end of file +return _M