fix(txn+security): 仓储改 flush + 启动校验/兜底 + job.error 脱敏 + SSE 异常硬化
P0-1 SqlCredentialStore/save_draft 由自提交改 flush,端点/服务统一 commit (新增 CredentialStore.commit() 统一提交点;token 刷新落库显式提交); 补多凭据一请求中途失败整体回滚集成测试。 P0-2 启动校验 _fernet(enc_key) 快速失败 + catch-all Exception → ErrorEnvelope; credential_enc_key 改 SecretStr。 P0-3 run_job 异常分类:AppError 存 code+message,其余存通用文案不泄 str(exc)。 P0-4 评审/正文 SSE 失败先发 error 事件,尾部 commit 包 try/except。 P1-4 max_version 加 FOR UPDATE 行锁消除 TOCTOU。 P1-5 scan_overdue 谓词下推 + 批量 UPDATE RETURNING。 P1-10 移除 OAuth user_code 日志。 P2 provider_deps 改调网关 build_adapter;accept_service Committable Protocol; CORS 白名单收窄;request_id 安全字符集白名单;stdlib 日志接管;读端点 404 校验; httpx timeout;测试用合法 Fernet key;类型化响应模型(JobResponse/DimensionEntry/ ReviewConflictView/selling_points)+路由 ErrorEnvelope responses(供 codegen)。
This commit is contained in:
54
apps/api/tests/test_middleware_request_id.py
Normal file
54
apps/api/tests/test_middleware_request_id.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""P2:request_id 头校验——安全字符集白名单(字母数字 . _ -,1–128 位),非法则生成新 uuid。
|
||||
|
||||
放行 uuid.hex / 常见 trace id / 客户端自定义短 id;丢弃含空白/控制字符/换行/注入序列/
|
||||
超长(>128)的值,防止把未经校验的客户端值原样写进日志/响应头(日志注入防护)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from ww_api.middleware import _sanitize_request_id
|
||||
|
||||
_HEX_32 = re.compile(r"^[0-9a-f]{32}$")
|
||||
|
||||
|
||||
def test_valid_hex_passthrough() -> None:
|
||||
raw = "a" * 32
|
||||
assert _sanitize_request_id(raw) == raw
|
||||
|
||||
|
||||
def test_valid_uppercase_hex_passthrough() -> None:
|
||||
raw = "ABCDEF0123456789"
|
||||
assert _sanitize_request_id(raw) == raw
|
||||
|
||||
|
||||
def test_none_generates_new_uuid() -> None:
|
||||
out = _sanitize_request_id(None)
|
||||
assert _HEX_32.match(out)
|
||||
|
||||
|
||||
def test_short_alnum_passthrough() -> None:
|
||||
# 客户端自定义短 id(如 'abc123')属合法字符集 → 原样透传(与 /health 传播契约一致)。
|
||||
raw = "abc123"
|
||||
assert _sanitize_request_id(raw) == raw
|
||||
|
||||
|
||||
def test_too_long_rejected() -> None:
|
||||
out = _sanitize_request_id("a" * 129) # 129 位 > 128 上限 → 丢弃改生成。
|
||||
assert _HEX_32.match(out)
|
||||
|
||||
|
||||
def test_whitespace_rejected() -> None:
|
||||
# 含空格(日志注入风险,非白名单字符)→ 丢弃改生成。
|
||||
out = _sanitize_request_id("abc 123")
|
||||
assert out != "abc 123"
|
||||
assert _HEX_32.match(out)
|
||||
|
||||
|
||||
def test_injection_rejected() -> None:
|
||||
# 含非十六进制字符(注入/控制字符)→ 丢弃改生成。
|
||||
out = _sanitize_request_id("../../etc/passwd\n")
|
||||
assert "/" not in out
|
||||
assert "\n" not in out
|
||||
assert _HEX_32.match(out)
|
||||
Reference in New Issue
Block a user