Files
starrrLight d1f5f69454 fix(运行时): 将插件路径匹配移到宿主
新增宿主侧精确路径匹配和插件级 match.paths 配置。\n按请求路径固定插件会话链,移除 Lua 插件内的路径判断与 bypass 状态,并迁移现有插件配置和测试。
2026-10-15 15:49:05 +08:00

168 lines
5.1 KiB
Lua

-- network_review.lua
-- Fail-closed inbound request review plugin.
--
-- Required plugin config:
-- url: "https://review.example.com/check"
-- fields: ["messages"] -- explicit top-level request-body fields allowed to leave this plugin
-- Optional:
-- headers:
-- x-api-key: "injected-by-admin"
--
-- Review API contract: HTTP 200 with JSON {"allow":true} permits the request.
-- The plugin forwards only configured fields; it never forwards the original
-- request body wholesale. The host remains responsible for network allowlists,
-- DNS/IP checks, TLS, timeouts, and response-size limits.
local plugin = {}
local settings = {}
local RESERVED_HEADERS = {
["host"] = true,
["content-length"] = true,
["authorization"] = true,
["proxy-authorization"] = true,
["cookie"] = true,
["set-cookie"] = true,
["connection"] = true,
["keep-alive"] = true,
["proxy-connection"] = true,
["transfer-encoding"] = true,
["te"] = true,
["trailer"] = true,
["upgrade"] = true,
["expect"] = true,
["content-type"] = true,
["accept"] = true,
}
local function is_non_empty_string(value)
return type(value) == "string" and value ~= ""
end
local function is_allowed_review_url(value)
if not is_non_empty_string(value) then return false end
local lower = value:lower()
if lower:sub(1, 8) == "https://" then return true end
-- 本地测试例外:允许 http,但仅限回环地址/本地测试域名
if lower:sub(1, 7) == "http://" then
local host_part = value:match("^http://([^:/]+)")
return host_part == "127.0.0.1"
or host_part == "localhost"
or host_part == "mock-review.local"
or host_part:sub(-13) == ".localtest.me"
end
return false
end
function plugin.init(ctx, options)
if type(options) ~= "table" then
error("network_review config must be an object")
end
if not is_allowed_review_url(options.url) then
error("network_review config.url must use https:// (or http:// to a loopback host)")
end
if type(options.fields) ~= "table" or #options.fields == 0 then
error("network_review requires a non-empty config.fields allowlist")
end
if options.headers ~= nil and type(options.headers) ~= "table" then
error("network_review config.headers must be an object")
end
local fields = {}
local seen_fields = {}
for _, field in ipairs(options.fields) do
if not is_non_empty_string(field) then
error("network_review config.fields must contain non-empty strings")
end
if seen_fields[field] then
error("network_review config.fields contains a duplicate field")
end
seen_fields[field] = true
fields[#fields + 1] = field
end
local headers = {}
if options.headers ~= nil then
for name, value in pairs(options.headers) do
if type(name) ~= "string" or name == "" or type(value) ~= "string" then
error("network_review header names and values must be strings")
end
local lower_name = name:lower()
if RESERVED_HEADERS[lower_name] then
error("network_review config.headers contains a reserved header")
end
headers[name] = value
end
end
settings = {
url = options.url,
fields = fields,
headers = headers,
}
return nil
end
function plugin.on_request(ctx, request)
if type(request) ~= "table" or type(request.body) ~= "string" then
return { action = "block", reason = "invalid request body" }
end
-- Decode and validate JSON before constructing a minimal allowlisted payload.
local decode_ok, input = pcall(function() return host.json.decode(request.body) end)
if not decode_ok or type(input) ~= "table" then
return { action = "block", reason = "invalid JSON request body" }
end
local payload = {}
local found_field = false
for _, field in ipairs(settings.fields) do
local value = input[field]
if value ~= nil then
payload[field] = value
found_field = true
end
end
if not found_field then
return { action = "block", reason = "no configured review fields present" }
end
local encode_ok, body = pcall(function() return host.json.encode(payload) end)
if not encode_ok or type(body) ~= "string" then
return { action = "block", reason = "failed to encode review payload" }
end
local headers = {
["content-type"] = "application/json",
["accept"] = "application/json",
}
for name, value in pairs(settings.headers) do
headers[name] = value
end
-- Never log request content, credentials, or external error details.
local http_ok, response = pcall(function()
return host.http.request({
method = "POST",
url = settings.url,
headers = headers,
body = body,
})
end)
if not http_ok or type(response) ~= "table" or response.status ~= 200
or type(response.body) ~= "string" then
return { action = "block", reason = "review service unavailable" }
end
local verdict_ok, verdict = pcall(function() return host.json.decode(response.body) end)
if not verdict_ok or type(verdict) ~= "table" or verdict.allow ~= true then
return { action = "block", reason = "review rejected request" }
end
return { action = "pass" }
end
return plugin