fix(运行时): 将插件路径匹配移到宿主

新增宿主侧精确路径匹配和插件级 match.paths 配置。\n按请求路径固定插件会话链,移除 Lua 插件内的路径判断与 bypass 状态,并迁移现有插件配置和测试。
This commit is contained in:
starrrLight committed 2026-10-15 15:49:05 +08:00
1 parent f905aa22cc
commit d1f5f69454
12 files changed
+126 -200

No files matched your search

-31
View File
@@ -96,46 +96,15 @@ function plugin.init(ctx, options)
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
+1 -32
View File
@@ -108,45 +108,14 @@ function plugin.init(ctx, options)
end
end
-- 新增:路径白名单(可选)。未配置时保持原行为——对所有含 message
-- 字段的请求打标;配置后只有命中白名单的 AI 路径才改写,
-- 非 AI 路径(即使恰好有 message 字段)原样放行,不被加前缀。
local paths = {}
if options.paths ~= nil then
if type(options.paths) ~= "table" then
error("request_rewrite config.paths must be an array")
end
local count = 0
for _, p in ipairs(options.paths) do
if type(p) ~= "string" or p == "" or p:sub(1, 1) ~= "/" then
error("request_rewrite config.paths entries must be non-empty path strings")
end
paths[p] = true
count = count + 1
end
if count == 0 then
error("request_rewrite config.paths must be non-empty when present")
end
end
-- Atomically replace local settings; don't retain the caller's config table.
settings = { prefix = prefix, paths = paths }
settings = { prefix = prefix }
-- plugins.md requires init to return nil.
return nil
end
function plugin.on_request(ctx, request)
-- 新增:路径白名单判断。放在 body 检查之前,保证无 body 的
-- 非 AI 请求(GET /health 等)也能通过,且不会被误加前缀。
-- 未配置 paths 时此分支不生效,行为与原版完全一致。
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 object" }
end
-51
View File
@@ -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
+1 -69
View File
@@ -162,28 +162,6 @@ local function normalize_settings(options)
error("stream_redact: config.content_path must be a dot path or non-empty string array (use * for a dense content array)")
end
-- 新增:路径白名单(可选)。语义与 network_review/response_guard 的
-- paths 一致:未配置时对所有 SSE 流生效;配置后只对命中路径的
-- 请求缓冲/脱敏,其余请求由 on_request 登记 bypass 后全程放行。
local paths = {}
if options.paths ~= nil then
if type(options.paths) ~= "table" then
error("stream_redact: config.paths must be an array")
end
local count = 0
for _, p in ipairs(options.paths) do
if type(p) ~= "string" or p == "" or string.sub(p, 1, 1) ~= "/"
or contains_cr_lf_nul(p) then
error("stream_redact: config.paths entries must be non-empty path strings without CR/LF/NUL")
end
paths[p] = true
count = count + 1
end
if count == 0 then
error("stream_redact: config.paths must be non-empty when present")
end
end
return {
max_buffer_bytes = max_buffer_bytes,
text_event = text_event,
@@ -191,7 +169,6 @@ local function normalize_settings(options)
redact_event_names = redact_event_names,
terminal_error_events = terminal_error_events,
control_event_names = control_event_names,
paths = paths,
}
end
@@ -214,7 +191,6 @@ local function copy_settings(source)
redact_event_names = copy_set(source.redact_event_names),
terminal_error_events = copy_set(source.terminal_error_events),
control_event_names = copy_set(source.control_event_names),
paths = copy_set(source.paths),
}
end
@@ -599,27 +575,6 @@ function plugin.init(ctx, options)
return nil
end
function plugin.on_request(ctx, request)
-- 新增钩子:本插件原本看不到请求路径,靠这个回调在请求阶段判断。
-- 未命中白名单(非 AI 路径)的请求登记 bypass 标记,其 SSE 事件流
-- 与非流式响应在 on_event/on_response/on_end 中直接放行,
-- 不缓冲、不拦截。配置了 paths 时必须保证宿主会调用本钩子。
if next(settings.paths) == nil then
return nil
end
local key = request_key(ctx)
if not key then
return nil -- 与其他请求侧插件不同:这里没有审核职责,不 fail-closed
end
local path = type(request) == "table" and request.path or nil
if path == nil or not settings.paths[path] then
clear_state(key)
states[key] = { bypass = true }
end
return nil
end
function plugin.on_response(ctx, response)
-- This example's guarantee is SSE-only. Refuse ordinary responses rather
-- than silently passing unredacted content through the same enabled rule.
@@ -627,17 +582,6 @@ function plugin.on_response(ctx, response)
return { action = "block", reason = "invalid response object" }
end
-- 新增:请求阶段已判定为非 AI 路径(bypass),本插件不参与其响应处理。
-- 非流式响应一次性结束,顺手清掉标记;流式响应交给 on_event/on_end 收尾。
local key = request_key(ctx)
local state = key and states[key] or nil
if state and state.bypass then
if response.streaming ~= true then
clear_state(key)
end
return nil
end
if response.streaming == true then
return nil
end
@@ -650,13 +594,7 @@ function plugin.on_event(ctx, event)
return { action = "block", reason = "missing request id" }
end
-- 新增:请求阶段已判定为非 AI 路径(bypass),事件流不归本插件管。
-- 必须放在事件合法性检查和状态创建之前,否则非 AI 流量会被
-- 无条件缓冲到流结束,甚至因事件形态不符被 fail-closed 拦截。
local existing = states[key]
if existing and existing.bypass then
return nil
end
local size = estimate_event_bytes(event)
if not size then
@@ -721,12 +659,6 @@ function plugin.on_end(ctx)
return nil
end
-- 新增:bypass 请求全程未缓冲,结束钩子只需清理标记。
if state.bypass then
clear_state(key)
return nil
end
if state.aborted then
clear_state(key)
return nil
@@ -743,4 +675,4 @@ function plugin.close(ctx)
clear_state(request_key(ctx))
end
return plugin
return plugin