fix: 收敛 Claude Code 探测拦截并补齐回归测试

This commit is contained in:
shaw
2026-02-07 19:04:08 +08:00
parent da9546ba24
commit 6aaa4aee6a
7 changed files with 290 additions and 26 deletions

View File

@@ -56,7 +56,8 @@ func NewClaudeCodeValidator() *ClaudeCodeValidator {
//
// Step 1: User-Agent 检查 (必需) - 必须是 claude-cli/x.x.x
// Step 2: 对于非 messages 路径,只要 UA 匹配就通过
// Step 3: 对于 messages 路径,进行严格验证
// Step 3: 检查 max_tokens=1 + haiku 探测请求绕过UA 已验证
// Step 4: 对于 messages 路径,进行严格验证:
// - System prompt 相似度检查
// - X-App header 检查
// - anthropic-beta header 检查
@@ -75,14 +76,20 @@ func (v *ClaudeCodeValidator) Validate(r *http.Request, body map[string]any) boo
return true
}
// Step 3: messages 路径,进行严格验证
// Step 3: 检查 max_tokens=1 + haiku 探测请求绕过
// 这类请求用于 Claude Code 验证 API 连通性,不携带 system prompt
if isMaxTokensOneHaiku, ok := r.Context().Value(ctxkey.IsMaxTokensOneHaikuRequest).(bool); ok && isMaxTokensOneHaiku {
return true // 绕过 system prompt 检查UA 已在 Step 1 验证
}
// 3.1 检查 system prompt 相似度
// Step 4: messages 路径,进行严格验证
// 4.1 检查 system prompt 相似度
if !v.hasClaudeCodeSystemPrompt(body) {
return false
}
// 3.2 检查必需的 headers值不为空即可
// 4.2 检查必需的 headers值不为空即可
xApp := r.Header.Get("X-App")
if xApp == "" {
return false
@@ -98,7 +105,7 @@ func (v *ClaudeCodeValidator) Validate(r *http.Request, body map[string]any) boo
return false
}
// 3.3 验证 metadata.user_id
// 4.3 验证 metadata.user_id
if body == nil {
return false
}