拦截逻辑收回 Lua 层,宿主代码恢复原样
还原 middleware/errors.py、runtime.py、proxy.py:不再依赖宿主渲染错误体,宿主按原样返回 403。网关代码零改动。 response_guard.lua:请求侧判定不通过时,把发往上游的请求体换成最小 stub(原文不出网关),在响应侧用 replace 输出 OpenAI 错误体 400 content_policy_violation;审核不可用时 503 safety_review_unavailable。拦截响应形态完全由插件自己控制。 tools/verify_plugins.py:断言同步回 stub 流程,57 项。
This commit is contained in:
1 parent
554a36cf05
commit
c76972c623
6 files changed
+97
-90
No files matched your search
@@ -7,15 +7,7 @@ class PluginFailure(MiddlewareError):
|
||||
|
||||
|
||||
class PolicyBlocked(MiddlewareError):
|
||||
"""A plugin rejected the request.
|
||||
|
||||
``code`` is one of a closed set of tokens (see runtime.BLOCK_CODES). Plugin-supplied
|
||||
text must never reach the response body, so anything outside that set is normalised.
|
||||
"""
|
||||
|
||||
def __init__(self, code: str = "content_policy_violation") -> None:
|
||||
super().__init__(code)
|
||||
self.code = code
|
||||
pass
|
||||
|
||||
|
||||
class Overloaded(MiddlewareError):
|
||||
|
||||
+2
-34
@@ -28,37 +28,6 @@ async def bounded_body(chunks, maximum):
|
||||
return bytes(data)
|
||||
|
||||
|
||||
# OpenAI-shaped rejection bodies. The set is closed: an unknown code falls back to the
|
||||
# content-policy message, so no plugin-supplied text can reach the client.
|
||||
POLICY_ERRORS = {
|
||||
"content_policy_violation": (
|
||||
400,
|
||||
"Your request was rejected by the content safety policy.",
|
||||
"invalid_request_error",
|
||||
),
|
||||
"safety_review_unavailable": (
|
||||
503,
|
||||
"Content safety review is temporarily unavailable. Please retry later.",
|
||||
"server_error",
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def policy_error(code):
|
||||
status_code, message, kind = POLICY_ERRORS.get(code, POLICY_ERRORS["content_policy_violation"])
|
||||
return (
|
||||
{
|
||||
"error": {
|
||||
"message": message,
|
||||
"type": kind,
|
||||
"param": None,
|
||||
"code": code if code in POLICY_ERRORS else "content_policy_violation",
|
||||
}
|
||||
},
|
||||
status_code,
|
||||
)
|
||||
|
||||
|
||||
async def disconnected(receive):
|
||||
while True:
|
||||
message = await receive()
|
||||
@@ -372,9 +341,8 @@ def create_app(settings: Settings | str = "config.yaml") -> FastAPI:
|
||||
)
|
||||
except Overloaded:
|
||||
return JSONResponse({"error": "Overloaded"}, status_code=503)
|
||||
except PolicyBlocked as exc:
|
||||
payload, status_code = policy_error(exc.code)
|
||||
return JSONResponse(payload, status_code=status_code)
|
||||
except PolicyBlocked:
|
||||
return JSONResponse({"error": "PolicyBlocked"}, status_code=403)
|
||||
except InvalidMultipart:
|
||||
return JSONResponse({"error": "InvalidMultipart"}, status_code=400)
|
||||
except (MiddlewareError, aiohttp.ClientError, TimeoutError, UnicodeError, ValueError):
|
||||
|
||||
+1
-10
@@ -17,15 +17,6 @@ from .worker import json_bytes, worker_main
|
||||
logger = logging.getLogger("middleware.audit")
|
||||
ADAPTER_ID = "__protocol_adapter__"
|
||||
|
||||
# A plugin may only name a rejection from this closed set; anything else falls back to the
|
||||
# default. This keeps plugin-supplied text out of the response body (see errors.PolicyBlocked).
|
||||
BLOCK_CODES = frozenset({"content_policy_violation", "safety_review_unavailable"})
|
||||
DEFAULT_BLOCK_CODE = "content_policy_violation"
|
||||
|
||||
|
||||
def block_code(reason):
|
||||
return reason if reason in BLOCK_CODES else DEFAULT_BLOCK_CODE
|
||||
|
||||
|
||||
class Session:
|
||||
def __init__(self, manager, request_id=None, plugins=None, attachments=None):
|
||||
@@ -242,7 +233,7 @@ class Session:
|
||||
raise PluginFailure()
|
||||
action = result.get("action")
|
||||
if action == "block":
|
||||
raise PolicyBlocked(block_code(result.get("reason")))
|
||||
raise PolicyBlocked()
|
||||
if action == "pass":
|
||||
if "value" in result or "events" in result:
|
||||
raise PluginFailure()
|
||||
|
||||
Reference in new issue
Block a user