只保留不写就会改错的说明(宿主 block 只给固定 403、UTF-8 边界、脱敏长度不变、 messages 需稠密整数表才能编成数组、input_text 绕过等),其余说明性段落删掉。 response_guard 注释 129 → 42 行;config.yaml 注释 51 → 13 行;配置结构未变。
143 lines
4.5 KiB
Lua
143 lines
4.5 KiB
Lua
-- request_rewrite.lua —— 给最后一条 user 消息加前缀打标。
|
|
-- 会重新序列化整个 body,字节级签名校验的下游不要启用。
|
|
|
|
local plugin = {}
|
|
local settings = { prefix = "" }
|
|
|
|
local MAX_PREFIX_BYTES = 1024
|
|
|
|
local function is_valid_utf8(value)
|
|
if type(value) ~= "string" then return false end
|
|
local i, n = 1, #value
|
|
while i <= n do
|
|
local b1 = value:byte(i)
|
|
if b1 <= 0x7F then
|
|
i = i + 1
|
|
elseif b1 >= 0xC2 and b1 <= 0xDF then
|
|
local b2 = value:byte(i + 1)
|
|
if not b2 or b2 < 0x80 or b2 > 0xBF then return false end
|
|
i = i + 2
|
|
elseif b1 == 0xE0 then
|
|
local b2, b3 = value:byte(i + 1, i + 2)
|
|
if not b2 or not b3 or b2 < 0xA0 or b2 > 0xBF or b3 < 0x80 or b3 > 0xBF then
|
|
return false
|
|
end
|
|
i = i + 3
|
|
elseif (b1 >= 0xE1 and b1 <= 0xEC) or (b1 >= 0xEE and b1 <= 0xEF) then
|
|
local b2, b3 = value:byte(i + 1, i + 2)
|
|
if not b2 or not b3 or b2 < 0x80 or b2 > 0xBF or b3 < 0x80 or b3 > 0xBF then
|
|
return false
|
|
end
|
|
i = i + 3
|
|
elseif b1 == 0xED then
|
|
local b2, b3 = value:byte(i + 1, i + 2)
|
|
if not b2 or not b3 or b2 < 0x80 or b2 > 0x9F or b3 < 0x80 or b3 > 0xBF then
|
|
return false
|
|
end
|
|
i = i + 3
|
|
elseif b1 == 0xF0 then
|
|
local b2, b3, b4 = value:byte(i + 1, i + 3)
|
|
if not b2 or not b3 or not b4 or b2 < 0x90 or b2 > 0xBF
|
|
or b3 < 0x80 or b3 > 0xBF or b4 < 0x80 or b4 > 0xBF then
|
|
return false
|
|
end
|
|
i = i + 4
|
|
elseif b1 >= 0xF1 and b1 <= 0xF3 then
|
|
local b2, b3, b4 = value:byte(i + 1, i + 3)
|
|
if not b2 or not b3 or not b4 or b2 < 0x80 or b2 > 0xBF
|
|
or b3 < 0x80 or b3 > 0xBF or b4 < 0x80 or b4 > 0xBF then
|
|
return false
|
|
end
|
|
i = i + 4
|
|
elseif b1 == 0xF4 then
|
|
local b2, b3, b4 = value:byte(i + 1, i + 3)
|
|
if not b2 or not b3 or not b4 or b2 < 0x80 or b2 > 0x8F
|
|
or b3 < 0x80 or b3 > 0xBF or b4 < 0x80 or b4 > 0xBF then
|
|
return false
|
|
end
|
|
i = i + 4
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
-- 只改写最后一条 user 消息;返回是否命中
|
|
local function apply_prefix(message, prefix)
|
|
local content = message.content
|
|
if type(content) == "string" then
|
|
message.content = prefix .. content
|
|
return true
|
|
end
|
|
if type(content) ~= "table" then return false end
|
|
for _, part in ipairs(content) do
|
|
if type(part) == "table" and part.type == "text" and type(part.text) == "string" then
|
|
part.text = prefix .. part.text
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
function plugin.init(ctx, options)
|
|
if type(options) ~= "table" then
|
|
error("request_rewrite config must be an object")
|
|
end
|
|
local prefix = options.prefix
|
|
if prefix == nil then
|
|
prefix = ""
|
|
elseif type(prefix) ~= "string" then
|
|
error("request_rewrite config.prefix must be a string")
|
|
end
|
|
if #prefix > MAX_PREFIX_BYTES then
|
|
error("request_rewrite config.prefix exceeds 1024 bytes")
|
|
end
|
|
if not is_valid_utf8(prefix) then
|
|
error("request_rewrite config.prefix must be valid UTF-8")
|
|
end
|
|
for i = 1, #prefix do
|
|
local byte = prefix:byte(i)
|
|
if byte < 0x20 or byte == 0x7F then
|
|
error("request_rewrite config.prefix must not contain control characters")
|
|
end
|
|
end
|
|
|
|
settings = { prefix = prefix }
|
|
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 object" }
|
|
end
|
|
if settings.prefix == "" then return { action = "pass" } end
|
|
|
|
local ok_decode, body = pcall(host.json.decode, request.body)
|
|
if not ok_decode or type(body) ~= "table" then return { action = "pass" } end
|
|
|
|
local messages = body.messages
|
|
if type(messages) ~= "table" then return { action = "pass" } end
|
|
|
|
local last_user = nil
|
|
for _, message in ipairs(messages) do
|
|
if type(message) == "table" and message.role == "user" then
|
|
last_user = message
|
|
end
|
|
end
|
|
if last_user == nil then return { action = "pass" } end
|
|
if not apply_prefix(last_user, settings.prefix) then return { action = "pass" } end
|
|
|
|
local ok_encode, rewritten_body = pcall(host.json.encode, body)
|
|
if not ok_encode or type(rewritten_body) ~= "string" then
|
|
return { action = "block", reason = "failed to encode rewritten body" }
|
|
end
|
|
|
|
local rewritten = {}
|
|
for key, value in pairs(request) do rewritten[key] = value end
|
|
rewritten.body = rewritten_body
|
|
return { action = "replace", value = rewritten }
|
|
end
|
|
|
|
return plugin
|