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
@@ -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
|
||||
Reference in new issue
Block a user