From e1f944f16dff8a5e818426d0c0fef7dff4f43b2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=B2=D1=82=D0=BE=D0=BC=D0=B0=D1=82=D0=B0=20=D0=9A?= =?UTF-8?q?=D0=B0=D0=BB=D0=B0=D1=88=D0=BD=D0=B8=D0=BA=D0=BE=D0=B2=D0=B0?= <3056129056@qq.com> Date: Sat, 17 Oct 2026 16:30:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E9=85=8D=E7=BD=AE=E4=B8=8E?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=99=84=E4=BB=B6=E5=AE=A1=E6=9F=A5=E6=8F=92?= =?UTF-8?q?=E4=BB=B6:=20file=5Fcontent=5Freview.lua=20+=20config.yaml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 13 ++- plugins/file_content_review.lua | 158 ++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 plugins/file_content_review.lua diff --git a/config.yaml b/config.yaml index bb83b70..8bac30e 100644 --- a/config.yaml +++ b/config.yaml @@ -20,16 +20,23 @@ llm: file: models/guard_model load: {device: cpu} limits: - max_input_tokens: 4096 + max_input_tokens: 8192 max_new_tokens: 32 timeout_seconds: 30 queue_size: 8 plugins: - - id: file-read-log - file: plugins/file_read_log.lua + - id: file-content-review + file: plugins/file_content_review.lua match: paths: [/upload] + llm: + allow: [guard_model] + config: + model: guard_model + allowed_extensions: [txt, doc, docx] + max_review_bytes: 8192 + max_files: 8 - id: network-review enabled: false file: plugins/network_review.lua diff --git a/plugins/file_content_review.lua b/plugins/file_content_review.lua new file mode 100644 index 0000000..858b1f2 --- /dev/null +++ b/plugins/file_content_review.lua @@ -0,0 +1,158 @@ +-- file_content_review.lua +-- Moderates the extracted text of uploaded attachments (.txt/.doc/.docx) +-- through the same LLM review path used for user input (see response_guard.lua). +-- +-- Required config: +-- model: reviewer model name for host.llm.call +-- Optional config: +-- allowed_extensions: array of extensions (default {"txt","doc","docx"}) +-- max_review_bytes: per-file cap, default 65536; exceeding files are +-- blocked, never silently truncated +-- max_files: max reviewable attachments per request, default 16 +-- prompt: override the audit prompt +-- +-- Contract: HTTP-equivalent of response_guard's input audit. Any extraction, +-- size, or moderation failure blocks the request (fail-closed). File content +-- is never logged by this plugin. + +local plugin = {} +local settings = {} + +local DEFAULT_EXTENSIONS = { "txt", "doc", "docx" } +local DEFAULT_MAX_REVIEW_BYTES = 65536 +local DEFAULT_MAX_FILES = 16 +local MAX_TOTAL_BYTES = 262144 -- combined review budget across all files + +local FILE_AUDIT_PROMPT = + "Review the following attachment content extracted from user-uploaded " .. + "files for safety before it is used in the conversation." + +local function is_non_empty_string(v) + return type(v) == "string" and v ~= "" +end + +-- ".PDF" -> "pdf"; no extension -> nil (not reviewable, passes by policy) +local function normalize_ext(name) + if type(name) ~= "string" then return nil end + local ext = name:match("%.(%w+)$") + if not ext then return nil end + return ext:lower() +end + +function plugin.init(ctx, options) + if type(options) ~= "table" then + error("file_content_review config must be an object") + end + if not is_non_empty_string(options.model) then + error("file_content_review requires config.model") + end + + local exts = options.allowed_extensions or DEFAULT_EXTENSIONS + if type(exts) ~= "table" or #exts == 0 then + error("file_content_review config.allowed_extensions must be a non-empty array") + end + local allowed, seen = {}, {} + for _, ext in ipairs(exts) do + if not is_non_empty_string(ext) then + error("file_content_review config.allowed_extensions entries must be non-empty strings") + end + local lower = ext:lower() + if seen[lower] then + error("file_content_review config.allowed_extensions contains a duplicate extension") + end + seen[lower] = true + allowed[lower] = true + end + + local max_bytes = tonumber(options.max_review_bytes) or DEFAULT_MAX_REVIEW_BYTES + if max_bytes < 1 or max_bytes % 1 ~= 0 then + error("file_content_review config.max_review_bytes must be a positive integer") + end + + local max_files = tonumber(options.max_files) or DEFAULT_MAX_FILES + if max_files < 1 or max_files % 1 ~= 0 then + error("file_content_review config.max_files must be a positive integer") + end + + local prompt = is_non_empty_string(options.prompt) and options.prompt or FILE_AUDIT_PROMPT + + settings = { + model = options.model, + allowed = allowed, + max_review_bytes = max_bytes, + max_files = max_files, + prompt = prompt, + } + return nil +end + +function plugin.on_request(ctx, request) + if type(request) ~= "table" or request.kind ~= "multipart" then + return { action = "pass" } + end + local files = request.files + if type(files) ~= "table" or #files == 0 then + return { action = "pass" } + end + + local texts, total_bytes = {}, 0 + local reviewable = 0 + + for i, file in ipairs(files or {}) do + if type(file) ~= "table" or not is_non_empty_string(file.id) then + return { action = "block", reason = "invalid attachment metadata at index " .. i } + end + + local ext = normalize_ext(file.name) + if ext and settings.allowed[ext] then + reviewable = reviewable + 1 + if reviewable > settings.max_files then + return { action = "block", reason = "too many reviewable attachments" } + end + + local read_ok, text = pcall(host.files.read_text, file.id) + if not read_ok or type(text) ~= "string" then + return { action = "block", reason = "attachment text extraction failed (index " .. i .. ")" } + end + if #text == 0 then + -- Extraction produced nothing for a reviewable type. That is + -- itself a signal (corrupt doc, unsupported layout, empty + -- wrapper hiding content), so fail closed. + return { action = "block", reason = "attachment produced no reviewable text (index " .. i .. ")" } + end + if #text > settings.max_review_bytes then + return { action = "block", reason = "attachment exceeds review size limit (index " .. i .. ")" } + end + + total_bytes = total_bytes + #text + if total_bytes > MAX_TOTAL_BYTES then + return { action = "block", reason = "combined attachment content exceeds review budget" } + end + texts[#texts + 1] = text + end + -- Non-reviewable extensions pass by policy; type filtering is not + -- this plugin's job. Configure a separate type-allowlist plugin if + -- unknown formats must be rejected. + end + + if #texts == 0 then + return { action = "pass" } -- multipart, but nothing reviewable + end + + -- Single combined review call (same budget pattern as response_guard's + -- multi-turn user audit). Separator is cosmetic; the reviewer sees the + -- full original text. + local combined = table.concat(texts, "\n\n---\n\n") + local ok, result = pcall(host.llm.call, settings.model, { + task = "response_moderation", + prompt = settings.prompt, + response = combined, + }) + if not ok or type(result) ~= "table" or result.safety ~= "Safe" then + return { action = "block", reason = "attachment content moderation failed or unsafe" } + end + + return { action = "pass" } +end + +return plugin