[Bug] 提示词审计将文本内嵌的图片 Base64 逐块送审,容易耗尽总超时预算
使用一张由 Go 标准库生成的 PNG,可以复现提示词审计对两种图片表示的明显差异:正常的结构化 input_image 被跳过;同一张图片位于普通 input_text 内的序列化 JSON 中时,Base64 会完整进入文字审计,产生大量分片,并耗尽共享超时预算。
这类形状可以出现在将含图片的历史对话序列化为文本的摘要/记忆任务中。“仅审最新输入和上一轮输出”无法限制它,因为整个 JSON 字符串已经属于当前这一条 user 文本。
本报告仅使用公开上游源码和本地合成输入,不包含线上请求、生产配置、流量统计或真实图片。
已检查的版本
v0.2.0、最新发布版v0.2.7:使用相应版本未修改的提取、分片、GuardEvaluator 核心代码做隔离复现,两者结果相同。- 当前
main(7c700729c23187d31ed320f6b19c790e2f194826)的prompt_snapshot.go、prompt_scanner.go、prompt_guard.go、coordinator.go与v0.2.7对应文件一致。 - 验证范围是源码级隔离复现,不是最新版本的部署测试或真实模型性能测试。
合成复现结果
使用标准库生成一张有效、确定性的 384×384 PNG,并比较以下两种请求:
| 请求表示 | 实际送审字符数 | 每块 16000 字符时的分片数 | 包含图片 data URI | 模拟扫描结果 |
|---|---|---|---|---|
| user.content 中的 input_text + input_image | 20 | 1 | 否 | 通过 |
| 将上述历史消息 JSON 序列化,放入一条 input_text | 591,016 | 37 | 是 | prompt_guard_unavailable |
测试扫描器在每块耗时 40 ms 后返回 Allow,整次审计预算设为 100 ms。这些值均为合成测试参数,用于稳定展示分片数量与共享 deadline 的关系,不是任何生产服务配置或 Qwen3Guard 吞吐测量。
Go 复现代码:backend/internal/securityaudit/image_audit_repro_test.go
package securityaudit
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"image"
"image/color"
"image/png"
"math/rand"
"strings"
"testing"
"time"
)
// This scanner makes the shared-budget behavior reproducible without a model.
type imageReproScanner struct{}
func (imageReproScanner) Scan(ctx context.Context, endpoint ActiveEndpoint, _ string, _ []string) (*NormalizedResult, error) {
timer := time.NewTimer(40 * time.Millisecond)
defer timer.Stop()
select {
case <-ctx.Done():
return nil, &GuardError{Code: ErrorCodeUnavailable, Retryable: true,
Timeout: errors.Is(ctx.Err(), context.DeadlineExceeded), Cause: ctx.Err()}
case <-timer.C:
return &NormalizedResult{Decision: EventPass, RiskLevel: RiskLow,
Action: ActionAllow, GuardEndpointID: endpoint.ID}, nil
}
}
func TestInlineImageAuditBudgetRepro(t *testing.T) {
// Generate a valid, deterministic PNG; no production image or API is used.
img := image.NewRGBA(image.Rect(0, 0, 384, 384))
rng := rand.New(rand.NewSource(42))
for y := 0; y < 384; y++ {
for x := 0; x < 384; x++ {
img.SetRGBA(x, y, color.RGBA{R: uint8(rng.Intn(256)),
G: uint8(rng.Intn(256)), B: uint8(rng.Intn(256)), A: 255})
}
}
var encoded bytes.Buffer
if err := png.Encode(&encoded, img); err != nil {
t.Fatal(err)
}
uri := "data:image/png;base64," + base64.StdEncoding.EncodeToString(encoded.Bytes())
history := []any{map[string]any{"role": "user", "content": []any{
map[string]any{"type": "input_text", "text": "Describe this image."},
map[string]any{"type": "input_image", "image_url": uri},
}}}
historyJSON, err := json.Marshal(history)
if err != nil {
t.Fatal(err)
}
memoryText := "Analyze this rollout and produce a memory summary.\n" + string(historyJSON)
cases := []struct {
name string
input any
}{
{"structured_image", history},
{"image_in_serialized_history", []any{map[string]any{"role": "user", "content": []any{
map[string]any{"type": "input_text", "text": memoryText},
}}}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
body, err := json.Marshal(map[string]any{"input": tc.input})
if err != nil {
t.Fatal(err)
}
snapshot, err := ExtractBlockingPromptSnapshot(Request{
Protocol: "openai_responses", Body: body, Stage: "http",
}, true)
if err != nil {
t.Fatal(err)
}
t.Logf("scan_chars=%d chunks=%d contains_image_data_uri=%v",
snapshot.PromptLength, len(SplitRunes(snapshot.ScanText, 16000)),
strings.Contains(snapshot.ScanText, "data:image/png;base64,"))
cfg := ActiveConfig{ConfigVersion: 1, Endpoints: []ActiveEndpoint{{
ID: "synthetic-guard", Enabled: true, TimeoutMS: 100, InputLimit: 16000,
}}}
decision, err := NewGuardEvaluator(imageReproScanner{}, nil, nil).
Evaluate(context.Background(), cfg, snapshot)
if err != nil {
t.Fatalf("shared audit budget exhausted while scanning inline image data: %v", err)
}
if decision.Kind != DecisionAllow {
t.Fatalf("unexpected decision: %s", decision.Kind)
}
})
}
}
在 backend/ 下运行:
go test ./internal/securityaudit -run '^TestInlineImageAuditBudgetRepro$' -count=1 -v
当前行为是结构化图片子测试通过,序列化历史子测试失败:
scan_chars=20 chunks=1 contains_image_data_uri=false
scan_chars=591016 chunks=37 contains_image_data_uri=true
shared audit budget exhausted while scanning inline image data: prompt_guard_unavailable
--- PASS: TestInlineImageAuditBudgetRepro/structured_image
--- FAIL: TestInlineImageAuditBudgetRepro/image_in_serialized_history
本地验证采用隔离 harness,复用上述版本未修改的提取、分片和 evaluator 代码,数据库/配置依赖用最小类型替身接入,repo 和 metrics 均为 nil;不是完整后端测试套件。上面的测试文件可直接放入原项目包中运行。
这个测试用于复现当前的输入放大及超时路径;不意味着任何自称为图片的 data URI 都应直接放行。
公开源码中的路径
contentTexts保留 string/input_text 原文,跳过正常的图片内容块,但不处理字符串内部的图片 data URI。GuardEvaluator.Evaluate按字符分片,并在同一个evalCtx的共享 deadline 下串行扫描。coordinator将 unavailable 映射为 HTTP 503。因输入容量而无法完成的审计也会被表示成临时不可用,可能诱发无效重试。
期望行为 / 建议方向
- 为文本内嵌的图片载荷定义有边界的处理方式:保留可读文字,仅对经过结构和格式识别的媒体编码做处理;不按某种任务前缀豁免,也不笼统删除所有 Base64 文本。
- 对处理后仍超出同步审计容量的输入,返回明确的输入超限错误,区分输入容量限制与节点暂时不可用。
- 可考虑记录原始长度、实际送审长度、媒体省略量等不含正文的指标,帮助定位这类问题。
具体采用媒体规范化还是明确拒绝不支持的大输入,可以由项目决定;希望避免将大量图片编码逐块送入文字审核后,仅以通用 503 结束。
重复问题检索
已检索 open/closed issues 与 PR,未找到专门报告这个触发条件的 issue。相关讨论:
- #5216:Codex
output_text未解析导致 latest-turn 选择不符预期。本例只有一条 user 文本,仍可独立复现,触发条件不同。 - #5230 / #5250:相同 latest-turn 内容重复审计与缓存。本例首次出现的图片编码输入也会造成放大和超时。
- #5752:工具参数/结果的解析及二进制过滤。与这里的处理方向相关,但该 PR 的普通
input_text仍按文本保留,可补充本例这种“文本中嵌入序列化历史”的覆盖。