Skip to content

Commit f8172e6

Browse files
committed
Revert "fix: narrow Cloudflare challenge classification change"
This reverts commit 035a6a4.
1 parent 035a6a4 commit f8172e6

7 files changed

Lines changed: 30 additions & 35 deletions

File tree

‎src/agents/auth-profiles/usage.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ describe("markAuthProfileFailure — WHAM-aware Codex cooldowns", () => {
10501050
expect(headers.Authorization).toBe("Bearer codex-access-token");
10511051
expect(headers["ChatGPT-Account-Id"]).toBe("acct_test_123");
10521052
expect(headers.originator).toBe("openclaw");
1053-
expect(headers["User-Agent"]).toMatch(/^openclaw\//);
1053+
expect(headers["User-Agent"]).toContain("Mozilla/5.0");
10541054
const stats = store.usageStats?.["openai:default"];
10551055
if (exactBlocked) {
10561056
expect(stats?.blockedUntil).toBe(now + expectedMs);

‎src/agents/auth-profiles/usage.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ async function probeWhamForCooldown(
234234
Accept: "application/json",
235235
originator: "openclaw",
236236
...(version ? { version } : {}),
237-
"User-Agent": `openclaw/${version || "dev"}`,
238237
};
239238
if (profile.accountId) {
240239
defaultHeaders["ChatGPT-Account-Id"] = profile.accountId;
@@ -248,6 +247,12 @@ async function probeWhamForCooldown(
248247
defaultHeaders,
249248
}) ?? defaultHeaders;
250249

250+
// Override User-Agent after attribution policy resolution to use a
251+
// browser-like header for Cloudflare-protected chatgpt.com/backend-api.
252+
// See #94432.
253+
headers["User-Agent"] =
254+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
255+
251256
const res = await fetch(WHAM_USAGE_URL, {
252257
method: "GET",
253258
headers,

‎src/agents/embedded-agent-helpers/errors.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,13 @@ const AUTH_INVALID_TOKEN_HINT_RE =
448448
/\bunauthorized\b|\b(?:invalid|incorrect|expired|stale)[_\s-]?api[_\s-]?key\b|\b(?:invalid|incorrect|expired|stale)\s+(?:token|jwt|credential|api[_\s-]?key)\b|\b(?:token|jwt|credential|api[_\s-]?key)\s+(?:is\s+)?(?:invalid|incorrect|expired|stale)\b/i;
449449
const HTML_BODY_RE = /^\s*(?:<!doctype\s+html\b|<html\b)/i;
450450
const HTML_CLOSE_RE = /<\/html>/i;
451-
// Cloudflare browser-challenge markers mean the CDN/gateway blocked the request,
452-
// so a 403 HTML response should not be reported as an auth failure.
451+
// Cloudflare browser-challenge markers that indicate a CDN/gateway block
452+
// rather than an auth failure. This set aligns with the shared Cloudflare
453+
// detector in src/shared/assistant-error-format.ts (STANDALONE_HTML_ERROR_HINT_RE)
454+
// to avoid gaps where a 403 challenge page with shared markers but without
455+
// the original three patterns would still fall through to auth_html.
453456
const CLOUDFLARE_CHALLENGE_RE =
454-
/cdn-cgi\/challenge-platform|challenge-error-text|Enable\s+JavaScript\s+and\s+cookies\s+to\s+continue|cf-browser-verification|__cf_challenge/i;
457+
/cdn-cgi\/challenge-platform|challenge-error-text|Enable\s+JavaScript\s+and\s+cookies\s+to\s+continue|cf-browser-verification|__cf_challenge|\bcloudflare\b|\bcaptcha\b|attention required/i;
455458
const PROXY_ERROR_RE =
456459
/\bproxyconnect\b|\bhttps?_proxy\b|\b407\b|\bproxy authentication required\b|\btunnel connection failed\b|\bconnect tunnel\b|\bsocks proxy\b|\bproxy error\b/i;
457460
const DNS_ERROR_RE = /\benotfound\b|\beai_again\b|\bgetaddrinfo\b|\bno such host\b|\bdns\b/i;
@@ -1244,6 +1247,8 @@ export function classifyProviderRuntimeFailureKind(
12441247
}
12451248
if (message && isHtmlErrorResponse(message, status)) {
12461249
// Cloudflare browser-challenge pages are CDN/gateway blocks, not auth failures.
1250+
// chatgpt.com/backend-api returns these when the HTTP client lacks browser
1251+
// characteristics (TLS fingerprint, HTTP/2, browser-like headers).
12471252
if (status === 403 && isCloudflareChallengeResponse(message)) {
12481253
return "upstream_html";
12491254
}

‎src/agents/embedded-agent-helpers/provider-error-patterns.test.ts‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,6 @@ describe("Cloudflare / CDN HTML error page classification (#67517)", () => {
182182
"<!doctype html><html><head><title>403 Forbidden</title></head>" +
183183
"<body>Enable JavaScript and cookies to continue." +
184184
"<p>Please stand by, while we are checking your browser...</p></body></html>";
185-
const cloudflareChallengeCdnCgiHtml =
186-
"<!doctype html><html><head><title>403 Forbidden</title></head>" +
187-
'<body><script src="/cdn-cgi/challenge-platform/h/g/orchestrate/chl_page"></script>' +
188-
"<p>Checking your browser...</p></body></html>";
189-
const cloudflareChallengeErrorTextHtml =
190-
"<!doctype html><html><head><title>403 Forbidden</title></head>" +
191-
'<body><span id="challenge-error-text">Enable JavaScript and cookies to continue</span>' +
192-
"<p>Please stand by...</p></body></html>";
193185
const html407 =
194186
"<!doctype html><html><head><title>407 Proxy Authentication Required</title></head>" +
195187
"<body><h1>Proxy Authentication Required</h1></body></html>";
@@ -245,24 +237,6 @@ describe("Cloudflare / CDN HTML error page classification (#67517)", () => {
245237
).toBe("upstream_html");
246238
});
247239

248-
it("classifies Cloudflare challenge-platform 403 HTML as upstream_html", () => {
249-
expect(
250-
classifyProviderRuntimeFailureKind({
251-
status: 403,
252-
message: cloudflareChallengeCdnCgiHtml,
253-
}),
254-
).toBe("upstream_html");
255-
});
256-
257-
it("classifies Cloudflare challenge-error-text 403 HTML as upstream_html", () => {
258-
expect(
259-
classifyProviderRuntimeFailureKind({
260-
status: 403,
261-
message: cloudflareChallengeErrorTextHtml,
262-
}),
263-
).toBe("upstream_html");
264-
});
265-
266240
it("classifies generic 403 HTML runtime failures as auth_html", () => {
267241
expect(classifyProviderRuntimeFailureKind({ status: 403, message: html403 })).toBe("auth_html");
268242
});

‎src/infra/provider-usage.fetch.codex.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe("fetchCodexUsage", () => {
3838
const headers = (init?.headers as Record<string, string> | undefined) ?? {};
3939
expect(headers["ChatGPT-Account-Id"]).toBe("acct-1");
4040
expect(headers.originator).toBe("openclaw");
41-
expect(headers["User-Agent"]).toMatch(/^openclaw\//);
41+
expect(headers["User-Agent"]).toContain("Mozilla/5.0");
4242
return makeResponse(200, {
4343
rate_limit: {
4444
primary_window: {

‎src/infra/provider-usage.fetch.codex.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export async function fetchCodexUsage(
6767
Accept: "application/json",
6868
originator: "openclaw",
6969
...(version ? { version } : {}),
70-
"User-Agent": `openclaw/${version || "dev"}`,
7170
};
7271
if (accountId) {
7372
defaultHeaders["ChatGPT-Account-Id"] = accountId;
@@ -81,6 +80,12 @@ export async function fetchCodexUsage(
8180
defaultHeaders,
8281
}) ?? defaultHeaders;
8382

83+
// Override User-Agent after attribution policy resolution to use a
84+
// browser-like header for Cloudflare-protected chatgpt.com/backend-api.
85+
// See #94432.
86+
headers["User-Agent"] =
87+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
88+
8489
const res = await fetchJson(
8590
"https://chatgpt.com/backend-api/wham/usage",
8691
{ method: "GET", headers },

‎src/llm/providers/openai-chatgpt-responses.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,10 +1595,16 @@ function buildBaseCodexHeaders(
15951595
headers.set("Authorization", `Bearer ${token}`);
15961596
headers.set("chatgpt-account-id", accountId);
15971597
headers.set("originator", "openclaw");
1598+
// chatgpt.com/backend-api is the ChatGPT web-app endpoint protected by
1599+
// Cloudflare bot detection. Non-browser User-Agent and missing browser
1600+
// headers trigger HTML challenge pages. Use a Chrome-like User-Agent
1601+
// and standard browser headers to reduce the bot score.
15981602
const userAgent = os
1599-
? `openclaw (${os.platform()} ${os.release()}; ${os.arch()})`
1600-
: "openclaw (browser)";
1603+
? `Mozilla/5.0 (${os.platform() === "darwin" ? "Macintosh; Intel Mac OS X 10_15_7" : os.platform() === "win32" ? "Windows NT 10.0; Win64; x64" : "X11; Linux x86_64"}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36`
1604+
: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
16011605
headers.set("User-Agent", userAgent);
1606+
headers.set("Accept", "*/*");
1607+
headers.set("Accept-Language", "en-US,en;q=0.9");
16021608
return headers;
16031609
}
16041610

0 commit comments

Comments
 (0)