新增宿主侧精确路径匹配和插件级 match.paths 配置。\n按请求路径固定插件会话链,移除 Lua 插件内的路径判断与 bypass 状态,并迁移现有插件配置和测试。
156 lines
5.2 KiB
Lua
156 lines
5.2 KiB
Lua
-- request_rewrite.lua
|
|
-- Request rewrite example following docs/plugins.md (Lua plugin interface v1).
|
|
--
|
|
-- Example config:
|
|
-- prefix: "[checked] "
|
|
--
|
|
-- Scope:
|
|
-- Intended for route-level use on chat-style JSON requests with a string
|
|
-- "message" field. Requests that are not applicable (invalid/non-object JSON,
|
|
-- or missing/non-string message) pass through unchanged.
|
|
--
|
|
-- Important:
|
|
-- This implementation decodes and re-encodes the complete JSON object after
|
|
-- changing message. Other fields' serialized representation (key order,
|
|
-- whitespace, escapes, numeric representation, etc.) may change. Do not use
|
|
-- it where downstream systems require byte-identical body/signature stability.
|
|
--
|
|
-- Contract:
|
|
-- init(ctx, config) must return nil.
|
|
-- on_request may return pass, replace, or block.
|
|
-- method/path are preserved; a new request table is returned for replace.
|
|
|
|
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 = 1
|
|
local n = #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
|
|
|
|
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
|
|
|
|
-- Atomically replace local settings; don't retain the caller's config table.
|
|
settings = { prefix = prefix }
|
|
|
|
-- plugins.md requires init to return nil.
|
|
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
|
|
|
|
-- This is a rewrite plugin, not a JSON validator or moderation plugin.
|
|
-- Non-applicable requests are passed through unchanged.
|
|
local decode_ok, body = pcall(function() return host.json.decode(request.body) end)
|
|
if not decode_ok or type(body) ~= "table" then
|
|
return { action = "pass" }
|
|
end
|
|
if type(body.message) ~= "string" then
|
|
return { action = "pass" }
|
|
end
|
|
|
|
local new_message = settings.prefix .. body.message
|
|
if not is_valid_utf8(new_message) then
|
|
return { action = "block", reason = "rewritten message is not valid UTF-8" }
|
|
end
|
|
body.message = new_message
|
|
|
|
-- The host codec re-serializes the complete object; see warning above.
|
|
local encode_ok, rewritten_body = pcall(function() return host.json.encode(body) end)
|
|
if not encode_ok or type(rewritten_body) ~= "string" then
|
|
return { action = "block", reason = "failed to encode rewritten body" }
|
|
end
|
|
|
|
-- Construct a new table so this plugin does not mutate the input request.
|
|
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
|