Files
sub2api/backend/internal/service/openai_privacy_cf_challenge_test.go
T
xpflyingandClaude Fable 5.1 9eb120dd40 fix(openai): 隐私/账号检查客户端改用 Firefox 指纹,规避 chatgpt.com Cloudflare 质询
chatgpt.com 的 Cloudflare 会对 req 内置的 ImpersonateChrome(TLS/HTTP2 指纹与 UA 均为
Chrome/120)返回 403 cf-mitigated=challenge,导致 accounts/check、subscriptions、
account_user_setting 三个 backend-api 调用长期失败:订阅到期时间不再更新、隐私开关显示 Fail。
仅改 UA 版本号无效(指纹仍是 120),Firefox 伪装在同一出口下稳定通过,故切换。

同时:
- 用 cf-mitigated 响应头识别质询页,隐私设置失败时正确标记为 training_set_cf_blocked
- accounts/check / subscriptions 失败日志由 Debug 提升为 Warn 并附 cf_challenge 字段,不再静默

Closes #4825

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-11 18:10:15 +08:00

26 lines
779 B
Go

package service
import "testing"
func TestIsCloudflareChallengeResponse(t *testing.T) {
cases := []struct {
name string
cfMitigated string
body string
want bool
}{
{"cf-mitigated challenge header", "challenge", "<html>...</html>", true},
{"header case-insensitive with spaces", " Challenge ", "", true},
{"body marker only", "", "<title>Just a moment...</title>", true},
{"plain json error", "", `{"detail":"Unauthorized"}`, false},
{"empty", "", "", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := isCloudflareChallengeResponse(tc.cfMitigated, tc.body); got != tc.want {
t.Fatalf("isCloudflareChallengeResponse(%q, %q) = %v, want %v", tc.cfMitigated, tc.body, got, tc.want)
}
})
}
}