fix(运行时): 将插件路径匹配移到宿主
新增宿主侧精确路径匹配和插件级 match.paths 配置。\n按请求路径固定插件会话链,移除 Lua 插件内的路径判断与 bypass 状态,并迁移现有插件配置和测试。
This commit is contained in:
1 parent
f905aa22cc
commit
d1f5f69454
12 files changed
+126
-200
No files matched your search
@@ -457,29 +457,12 @@ function plugin.init(ctx, options)
|
||||
end
|
||||
end
|
||||
|
||||
-- 新增:路径白名单(可选)。语义与 network_review.paths 一致:
|
||||
-- 未配置时审核所有请求;配置后只有命中路径的请求才进入审核流程,
|
||||
-- 其余请求登记 bypass 标记后放行(供响应阶段钩子识别)。
|
||||
local paths = {}
|
||||
if candidate.paths ~= nil then
|
||||
if type(candidate.paths) ~= "table" then
|
||||
error("response_guard config.paths must be an array")
|
||||
end
|
||||
for _, p in ipairs(candidate.paths) do
|
||||
if type(p) ~= "string" or p == "" or p:sub(1, 1) ~= "/" then
|
||||
error("response_guard config.paths entries must be non-empty path strings")
|
||||
end
|
||||
paths[p] = true
|
||||
end
|
||||
end
|
||||
|
||||
local normalized = {}
|
||||
for k, v in pairs(candidate) do
|
||||
normalized[k] = v
|
||||
end
|
||||
normalized.content_path = path
|
||||
normalized.non_text_events = events
|
||||
normalized.paths = paths
|
||||
config = normalized
|
||||
|
||||
-- Do not reset requests here: hosts may invoke init during request
|
||||
@@ -494,18 +477,6 @@ function plugin.on_request(ctx, request)
|
||||
return { action = "block", reason = "missing request_id" }
|
||||
end
|
||||
|
||||
-- 新增:路径白名单判断。未命中白名单的请求(非 AI 流量)不做审核,
|
||||
-- 但必须登记 bypass 标记后才能放行——响应阶段的 on_event/on_response
|
||||
-- 依靠 requests[key] 区分"不需要审核"(bypass)和"状态丢失"(block)。
|
||||
if next(config.paths) ~= nil then
|
||||
local path = type(request) == "table" and request.path or nil
|
||||
if path == nil or not config.paths[path] then
|
||||
clear(key)
|
||||
requests[key] = { bypass = true }
|
||||
return { action = "pass" }
|
||||
end
|
||||
end
|
||||
|
||||
local body = type(request) == "table" and request.body or ""
|
||||
local ok, decoded = pcall(host.json.decode, body or "")
|
||||
if not ok or type(decoded) ~= "table" then
|
||||
@@ -616,13 +587,6 @@ function plugin.on_response(ctx, response)
|
||||
|
||||
local state = requests[key]
|
||||
|
||||
-- 新增:请求阶段已判定为非 AI 路径(bypass),本插件不参与其响应处理。
|
||||
-- 非流式响应一次性结束,顺手清掉标记,避免残留状态。
|
||||
if state and state.bypass then
|
||||
clear(key)
|
||||
return nil
|
||||
end
|
||||
|
||||
if not state then
|
||||
return { action = "block", reason = "request state missing for non-streaming response" }
|
||||
end
|
||||
@@ -691,13 +655,6 @@ function plugin.on_event(ctx, event)
|
||||
local key = key_for(ctx)
|
||||
local state = key and requests[key] or nil
|
||||
|
||||
-- 新增:请求阶段已判定为非 AI 路径(bypass),事件流不归本插件管。
|
||||
-- 注意 bypass 判断必须放在 "request state missing" 之前,否则
|
||||
-- 非 AI 流量的事件会被当作状态丢失而 fail-closed 拦截。
|
||||
if state and state.bypass then
|
||||
return nil
|
||||
end
|
||||
|
||||
local event_name = type(event.event) == "string" and event.event or nil
|
||||
if not state then
|
||||
-- Preserve pre-request control frames only when they contain no text at
|
||||
@@ -753,14 +710,6 @@ function plugin.on_end(ctx)
|
||||
local key = key_for(ctx)
|
||||
local state = key and requests[key] or nil
|
||||
|
||||
-- bypass 请求全程未缓冲,清理标记后直接放行。
|
||||
-- 不能落入 moderate_and_release:bypass 状态没有 pending 字段,
|
||||
-- 对 #state.pending 取长度会抛 runtime error。
|
||||
if state and state.bypass then
|
||||
clear(key)
|
||||
return nil
|
||||
end
|
||||
|
||||
return moderate_and_release(key, true)
|
||||
end
|
||||
|
||||
|
||||
Reference in new issue
Block a user