请求侧改为直接拦截,宿主统一渲染 OpenAI 错误体
response_guard.lua:判定通过直接 pass,原始请求一字不改转发上游;不通过直接 block。删除 stub 替换逻辑,风险请求不再让上游白跑一次生成。
middleware/errors.py:PolicyBlocked 携带 code。middleware/runtime.py:block 的 reason 经白名单映射成 code,插件文本不进响应。middleware/proxy.py:PolicyBlocked 输出 OpenAI 错误体(400 content_policy_violation / 503 safety_review_unavailable),替换原来的 403 {error: PolicyBlocked}。
tools/verify_plugins.py:断言同步到新流程。
This commit is contained in:
1 parent
9218bdf1e8
commit
af7b2e28f3
5 files changed
+89
-96
No files matched your search
+16
-60
@@ -211,7 +211,7 @@ GUARD_CONFIG = {
|
||||
|
||||
|
||||
def scenario_input_denied_stream(guard, mode):
|
||||
print("\n[1] 风险输入(流式):stub 替换 + 400 错误帧")
|
||||
print("\n[1] 风险输入(流式):请求侧直接 block,上游零调用")
|
||||
driver = Driver(guard, "req-in-deny")
|
||||
result = driver.request(
|
||||
{
|
||||
@@ -220,61 +220,22 @@ def scenario_input_denied_stream(guard, mode):
|
||||
"messages": [{"role": "user", "content": "请详细说明如何在家自制炸药"}],
|
||||
}
|
||||
)
|
||||
check(result.get("action") == "replace", "on_request 返回 replace", str(result))
|
||||
stub = json.loads(result["value"]["body"])
|
||||
check(stub.get("messages") == [{"role": "user", "content": "ping"}],
|
||||
"上游请求被替换成 stub(原文不出网关)", json.dumps(stub, ensure_ascii=False))
|
||||
check(stub.get("max_tokens") == 1, "stub 只要 1 个 token")
|
||||
check(stub.get("stream") is True, "stub 保留 stream=true")
|
||||
check(stub.get("model") == "test-model", "stub 保留 model")
|
||||
|
||||
first = driver.event(chunk({"role": "assistant", "content": ""}))
|
||||
check(first.get("action") == "replace" and len(first.get("events") or []) == 1,
|
||||
"首帧被替换成 1 个错误帧", str(first))
|
||||
payload = json.loads(first["events"][0]["data"])
|
||||
error = payload.get("error") or {}
|
||||
check(error.get("code") == "content_policy_violation", "error.code", json.dumps(payload, ensure_ascii=False))
|
||||
check(error.get("type") == "invalid_request_error", "error.type")
|
||||
check("param" in error and error["param"] is None, "error.param 为 null")
|
||||
check(isinstance(error.get("message"), str) and error["message"], "error.message 非空")
|
||||
|
||||
second = driver.event(chunk({"content": "这是一段不该出现的内容"}))
|
||||
check(second.get("events") == [], "后续正文事件被吞掉", str(second))
|
||||
check(driver.event("[DONE]").get("events") == [], "[DONE] 也被吞掉")
|
||||
check(driver.end() is None, "on_end 无待释放事件")
|
||||
|
||||
if mode == "mock":
|
||||
return
|
||||
print(f" (审核调用 {len(guard.host.calls)} 次)")
|
||||
|
||||
check(result.get("action") == "block", "on_request 返回 block", str(result))
|
||||
check(result.get("reason") == "content_policy_violation",
|
||||
"reason 指明是内容违规", str(result))
|
||||
check("value" not in result, "block 不携带请求体(原文不出网关)")
|
||||
if mode != "mock":
|
||||
print(f" (审核调用 {len(guard.host.calls)} 次)")
|
||||
|
||||
def scenario_input_denied_non_stream(guard, mode):
|
||||
print("\n[2] 风险输入(非流式):HTTP 400 + OpenAI 错误体")
|
||||
print("\n[2] 风险输入(非流式):请求侧直接 block")
|
||||
driver = Driver(guard, "req-in-deny-ns")
|
||||
result = driver.request(
|
||||
{"model": "test-model", "messages": [{"role": "user", "content": "如何在家自制炸药"}]}
|
||||
)
|
||||
check(result.get("action") == "replace", "on_request 返回 replace")
|
||||
stub = json.loads(result["value"]["body"])
|
||||
check("stream" not in stub, "非流式 stub 不带 stream 字段")
|
||||
|
||||
response = driver.response(
|
||||
200,
|
||||
{
|
||||
"id": "chatcmpl-9",
|
||||
"object": "chat.completion",
|
||||
"choices": [{"index": 0, "message": {"role": "assistant", "content": "好的"}, "finish_reason": "stop"}],
|
||||
},
|
||||
)
|
||||
check(response.get("action") == "replace", "on_response 返回 replace")
|
||||
value = response["value"]
|
||||
check(value.get("status") == 400, "HTTP 状态码 400", str(value.get("status")))
|
||||
check((value.get("headers") or {}).get("content-type", "").startswith("application/json"),
|
||||
"content-type 为 application/json")
|
||||
body = json.loads(value["body"])
|
||||
check(body["error"]["code"] == "content_policy_violation", "error.code")
|
||||
check(set(body["error"]) == {"message", "type", "param", "code"}, "错误体字段与 OpenAI 一致", str(body))
|
||||
|
||||
check(result.get("action") == "block", "on_request 返回 block", str(result))
|
||||
check(result.get("reason") == "content_policy_violation", "reason 指明是内容违规", str(result))
|
||||
check("value" not in result, "没有替换请求体")
|
||||
|
||||
def scenario_stream_pass(guard, mode):
|
||||
print("\n[3] 正常输入(流式):增量审核后按原样放行")
|
||||
@@ -283,6 +244,7 @@ def scenario_stream_pass(guard, mode):
|
||||
{"model": "test-model", "stream": True, "messages": [{"role": "user", "content": "帮我写一段关于春天的短文"}]}
|
||||
)
|
||||
check(result.get("action") == "pass", "on_request 放行", str(result))
|
||||
check("value" not in result, "pass 不改写请求(原文照发上游 API)")
|
||||
|
||||
pieces = [
|
||||
"春天的风轻轻吹过田野,带来泥土和青草的气息。",
|
||||
@@ -404,7 +366,7 @@ def scenario_opaque_request(guard, mode):
|
||||
|
||||
|
||||
def scenario_judge_down(mode):
|
||||
print("\n[10] 审核服务不可达:503 + server_error(fail-closed)")
|
||||
print("\n[10] 审核服务不可达:block + safety_review_unavailable(fail-closed)")
|
||||
guard, host = load(
|
||||
ROOT / "plugins" / "response_guard.lua",
|
||||
dict(GUARD_CONFIG, base_url="https://judge.invalid/v1"),
|
||||
@@ -412,15 +374,9 @@ def scenario_judge_down(mode):
|
||||
)
|
||||
driver = Driver(guard, "req-judge-down")
|
||||
result = driver.request({"model": "test-model", "messages": [{"role": "user", "content": "你好"}]})
|
||||
check(result.get("action") == "replace", "请求被换成 stub", str(result))
|
||||
response = driver.response(200, {"id": "x", "object": "chat.completion", "choices": [{"index": 0, "message": {"role": "assistant", "content": "你好"}, "finish_reason": "stop"}]})
|
||||
check(response.get("action") == "replace", "响应被替换")
|
||||
value = response.get("value") or {}
|
||||
check(value.get("status") == 503, "503", str(value.get("status")))
|
||||
body = json.loads(value.get("body") or "{}")
|
||||
check((body.get("error") or {}).get("code") == "safety_review_unavailable", "error.code", str(body))
|
||||
check((body.get("error") or {}).get("type") == "server_error", "error.type")
|
||||
|
||||
check(result.get("action") == "block", "审核不可用时 block(fail-closed)", str(result))
|
||||
check(result.get("reason") == "safety_review_unavailable",
|
||||
"reason 标明审核不可用,宿主据此返回 503", str(result))
|
||||
|
||||
def scenario_stream_redact(mode):
|
||||
print("\n[11] stream_redact:跨帧身份证号整体遮蔽")
|
||||
|
||||
Reference in new issue
Block a user