feat(附件): 增加请求级文本读取与上传代理
支持任意 POST 路径的 multipart 暂存、原始字节转发和 TXT、DOCX、DOC 文本提取;通过 host.files.read_text 提供请求内受限读取,禁止插件改写附件和上传正文。 增加附件数量、文本、解压、超时和读取次数限制,覆盖断开、异常及清理场景;补充依赖、LibreOffice 镜像安装和插件接口文档,不修改业务 Lua 插件。 验证:全量测试 128 通过、2 个既有失败、1 个跳过;既有失败为 test_example_redacts_cross_event_phone 和 test_network_example_uses_configuration_and_http_result,均缺少插件必填配置。Ruff 静态检查、改动文件格式检查、pip check 和差异检查通过;全仓格式检查保留两处既有问题。当前环境缺少 LibreOffice 和 Docker,真实 DOC 转换及镜像构建未验证。
This commit is contained in:
1 parent
7f0ae69252
commit
8ad4b29e4c
12 files changed
+1233
-15
No files matched your search
+24
-5
@@ -7,6 +7,7 @@ from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
from .attachments import validate_multipart
|
||||
from .config import Settings
|
||||
from .errors import LimitExceeded, Overloaded, PluginFailure, PolicyBlocked
|
||||
from .llm import LLMRegistry
|
||||
@@ -18,8 +19,9 @@ ADAPTER_ID = "__protocol_adapter__"
|
||||
|
||||
|
||||
class Session:
|
||||
def __init__(self, manager, request_id=None, plugins=None):
|
||||
def __init__(self, manager, request_id=None, plugins=None, attachments=None):
|
||||
self.manager = manager
|
||||
self.attachments = attachments
|
||||
self.plugins = list(manager.plugins if plugins is None else plugins)
|
||||
self.context = {"request_id": request_id or str(uuid4())}
|
||||
self.started = time.monotonic()
|
||||
@@ -86,6 +88,10 @@ class Session:
|
||||
result["value"],
|
||||
self.manager.settings.limits.max_request_bytes,
|
||||
)
|
||||
elif result["kind"] == "files":
|
||||
if self.attachments is None or plugin == ADAPTER_ID:
|
||||
raise PluginFailure()
|
||||
reply["value"] = self.attachments.read_text(result["value"])
|
||||
elif result["kind"] == "log":
|
||||
reply["value"] = None
|
||||
logger.info(
|
||||
@@ -177,6 +183,10 @@ class Session:
|
||||
for item in values:
|
||||
result = await self.invoke(plugin.id, hook, item)
|
||||
output.extend(self.decision(result, item, hook))
|
||||
if hook == "on_request" and self.attachments is not None:
|
||||
for candidate in output:
|
||||
# Validate every stage so later plugins never see forged file metadata.
|
||||
validate_multipart(candidate, item)
|
||||
if hook == "on_event":
|
||||
self.check_events_size(output)
|
||||
values = output
|
||||
@@ -252,8 +262,14 @@ class Session:
|
||||
except Exception:
|
||||
continue
|
||||
finally:
|
||||
await self.kill()
|
||||
self.manager.sessions.discard(self)
|
||||
try:
|
||||
await self.kill()
|
||||
finally:
|
||||
try:
|
||||
if self.attachments is not None:
|
||||
self.attachments.close()
|
||||
finally:
|
||||
self.manager.sessions.discard(self)
|
||||
|
||||
|
||||
class PluginManager:
|
||||
@@ -280,6 +296,7 @@ class PluginManager:
|
||||
limits.max_response_bytes,
|
||||
limits.max_plugin_buffer_bytes,
|
||||
limits.max_sse_frame_bytes,
|
||||
2 * limits.max_attachment_text_bytes,
|
||||
*(p.network.max_response_bytes for p in self.plugins),
|
||||
)
|
||||
+ 65536
|
||||
@@ -295,14 +312,16 @@ class PluginManager:
|
||||
await self.llm.start()
|
||||
self.llm_started = True
|
||||
|
||||
async def open(self, request_id=None, path=None, adapter_only=False, all_plugins=False):
|
||||
async def open(
|
||||
self, request_id=None, path=None, adapter_only=False, all_plugins=False, attachments=None
|
||||
):
|
||||
if self.stopping or len(self.sessions) >= self.settings.limits.max_sessions:
|
||||
raise Overloaded()
|
||||
if path is None and not adapter_only and not all_plugins:
|
||||
raise ValueError("path is required when opening a plugin session")
|
||||
await self.start()
|
||||
plugins = self.plugins if all_plugins else self.plugins_for_path(path)
|
||||
session = Session(self, request_id, plugins)
|
||||
session = Session(self, request_id, plugins, attachments)
|
||||
self.sessions.add(session)
|
||||
try:
|
||||
await session.initialize(adapter_only)
|
||||
|
||||
Reference in new issue
Block a user