fix: config.yaml模型路径/加载模式待修改,提交了plugins目录的lua插件,以及config.yaml中的配置

This commit is contained in:
Автомата Калашникова committed 2026-10-13 13:51:12 +08:00
1 parent 45e45948bd
commit d93cf2d5e5
6 files changed
+1917 -114

No files matched your search

+195 -12
View File
@@ -1,15 +1,198 @@
-- 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 config = {}
function plugin.init(ctx, options) config = options end
function plugin.on_request(ctx, request)
local response = host.http.request({
method='POST', url=config.url,
headers={['content-type']='application/json'},
body=request.body,
})
if response.status ~= 200 then return {action='block'} end
local verdict = host.json.decode(response.body)
if verdict.allow ~= true then return {action='block'} end
return {action='pass'}
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
-- 新增:路径白名单(可选)。未配置时保持原行为——审核所有请求;
-- 配置后只有命中白名单的路径才走外发审核,其余请求直接放行。
local paths = {}
if options.paths ~= nil then
if type(options.paths) ~= "table" then
error("network_review config.paths must be an array")
end
local seen_paths = {}
for _, p in ipairs(options.paths) do
if not is_non_empty_string(p) or p:sub(1, 1) ~= "/" then
error("network_review config.paths entries must be non-empty path strings")
end
if seen_paths[p] then
error("network_review config.paths contains a duplicate path")
end
seen_paths[p] = true
paths[p] = true
end
end
settings = {
url = options.url,
fields = fields,
headers = headers,
paths = paths,
}
return nil
end
function plugin.on_request(ctx, request)
-- 新增:路径白名单判断。必须放在 body 检查之前,否则 GET /health 这类
-- 无 body 的非 AI 请求会被 "invalid request body" 拦截。
-- 未配置 paths 时此分支不生效,行为与原版完全一致(fail-closed 审核一切)。
if next(settings.paths) ~= nil then
local path = type(request) == "table" and request.path or nil
if path == nil or not settings.paths[path] then
return { action = "pass" }
end
end
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