-- 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 -- 新增:路径白名单(可选)。未配置时保持原行为——对所有含 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 } -- 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 -- 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