Skip to content

Commit 36dd58a

Browse files
chris-yyausteipete
authored andcommitted
Prevent Codex HTML challenge pages from looking like DNS failures
Cloudflare challenge pages from chatgpt.com/backend-api can arrive as raw HTML without an HTTP status prefix. The transport sanitizer scanned for generic "dns" substrings before HTML detection, so these pages could surface as DNS lookup failures instead of the existing HTML/CDN block message. Constraint: Must preserve DNS transport classification for real ENOTFOUND/getaddrinfo failures Rejected: Treat every bare HTML document as an upstream HTML error | too broad for arbitrary model text/errors Confidence: high Scope-risk: narrow Directive: Keep standalone HTML challenge detection ahead of generic transport keyword matching so CDN block pages do not regress into DNS copy Tested: oxfmt --check on changed files; targeted node --import tsx verification for standalone Cloudflare HTML classification and DNS control case Not-tested: Full Vitest shard run in this environment
1 parent 51606e9 commit 36dd58a

4 files changed

Lines changed: 69 additions & 1 deletion

File tree

‎src/agents/pi-embedded-helpers.formatassistanterrortext.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,23 @@ describe("formatAssistantErrorText", () => {
185185
);
186186
});
187187

188+
it("does not misdiagnose standalone Cloudflare challenge HTML as DNS", () => {
189+
const msg = makeAssistantError(`<!DOCTYPE html>
190+
<html>
191+
<head>
192+
<title>Just a moment...</title>
193+
<link rel="dns-prefetch" href="//chatgpt.com">
194+
</head>
195+
<body>
196+
<span id="challenge-error-text">Enable JavaScript and cookies to continue</span>
197+
<script src="/cdn-cgi/challenge-platform/h/g/orchestrate/chl_page/v1"></script>
198+
</body>
199+
</html>`);
200+
expect(formatAssistantErrorText(msg)).toBe(
201+
"The provider returned an HTML error page instead of an API response. This usually means a CDN or gateway (e.g. Cloudflare) blocked the request. Retry in a moment or check provider status.",
202+
);
203+
});
204+
188205
it("returns a friendly message for empty stream chunk errors", () => {
189206
const msg = makeAssistantError("request ended without sending any chunks");
190207
expect(formatAssistantErrorText(msg)).toBe("LLM request timed out.");
@@ -339,6 +356,21 @@ describe("formatRawAssistantErrorForUi", () => {
339356
"The AI service is temporarily unavailable (HTTP 521). Please try again in a moment.",
340357
);
341358
});
359+
360+
it("formats standalone Cloudflare challenge HTML into a clean provider error", () => {
361+
const htmlError = `<!DOCTYPE html>
362+
<html lang="en-US">
363+
<head><title>Just a moment...</title></head>
364+
<body>
365+
<span id="challenge-error-text">Enable JavaScript and cookies to continue</span>
366+
<script src="/cdn-cgi/challenge-platform/h/g/orchestrate/chl_page/v1"></script>
367+
</body>
368+
</html>`;
369+
370+
expect(formatRawAssistantErrorForUi(htmlError)).toBe(
371+
"The provider returned an HTML error page instead of an API response. This usually means a CDN or gateway (e.g. Cloudflare) blocked the request. Retry in a moment or check provider status.",
372+
);
373+
});
342374
});
343375

344376
describe("raw API error payload helpers", () => {

‎src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,18 @@ describe("isCloudflareOrHtmlErrorPage", () => {
266266
expect(isCloudflareOrHtmlErrorPage(htmlError)).toBe(true);
267267
});
268268

269+
it("detects standalone Cloudflare challenge HTML pages", () => {
270+
const htmlError = `<!DOCTYPE html>
271+
<html lang="en-US">
272+
<head><title>Just a moment...</title></head>
273+
<body>
274+
<span id="challenge-error-text">Enable JavaScript and cookies to continue</span>
275+
<script src="/cdn-cgi/challenge-platform/h/g/orchestrate/chl_page/v1"></script>
276+
</body>
277+
</html>`;
278+
expect(isCloudflareOrHtmlErrorPage(htmlError)).toBe(true);
279+
});
280+
269281
it("does not flag non-HTML status lines", () => {
270282
expect(isCloudflareOrHtmlErrorPage("500 Internal Server Error")).toBe(false);
271283
expect(isCloudflareOrHtmlErrorPage("429 Too Many Requests")).toBe(false);

‎src/agents/pi-embedded-helpers/sanitize-user-facing-text.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ export function formatTransportErrorCopy(raw: string): string | undefined {
102102
if (!raw) {
103103
return undefined;
104104
}
105+
106+
if (isCloudflareOrHtmlErrorPage(raw)) {
107+
return undefined;
108+
}
109+
105110
const lower = normalizeLowercaseStringOrEmpty(raw);
106111

107112
if (

‎src/shared/assistant-error-format.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const HTTP_STATUS_CODE_PREFIX_RE = new RegExp(
1010
"i",
1111
);
1212
const HTML_ERROR_PREFIX_RE = /^\s*(?:<!doctype\s+html\b|<html\b)/i;
13+
const HTML_CLOSE_RE = /<\/html>/i;
1314
const CLOUDFLARE_HTML_ERROR_CODES = new Set([521, 522, 523, 524, 525, 526, 530]);
15+
const STANDALONE_HTML_ERROR_HINT_RE =
16+
/\bcloudflare\b|cdn-cgi\/challenge-platform|challenge-error-text|enable javascript and cookies to continue|access denied|forbidden|service unavailable|bad gateway|web server is down|captcha|attention required/i;
1417

1518
type ErrorPayload = Record<string, unknown>;
1619

@@ -94,6 +97,14 @@ export function isCloudflareOrHtmlErrorPage(raw: string): boolean {
9497
return false;
9598
}
9699

100+
if (
101+
HTML_ERROR_PREFIX_RE.test(trimmed) &&
102+
HTML_CLOSE_RE.test(trimmed) &&
103+
STANDALONE_HTML_ERROR_HINT_RE.test(trimmed)
104+
) {
105+
return true;
106+
}
107+
97108
const status = extractLeadingHttpStatus(trimmed);
98109
if (!status || status.code < 500) {
99110
return false;
@@ -104,7 +115,7 @@ export function isCloudflareOrHtmlErrorPage(raw: string): boolean {
104115
}
105116

106117
return (
107-
status.code < 600 && HTML_ERROR_PREFIX_RE.test(status.rest) && /<\/html>/i.test(status.rest)
118+
status.code < 600 && HTML_ERROR_PREFIX_RE.test(status.rest) && HTML_CLOSE_RE.test(status.rest)
108119
);
109120
}
110121

@@ -175,6 +186,14 @@ export function formatRawAssistantErrorForUi(raw?: string): string {
175186
return `The AI service is temporarily unavailable (HTTP ${leadingStatus.code}). Please try again in a moment.`;
176187
}
177188

189+
if (isCloudflareOrHtmlErrorPage(trimmed)) {
190+
return (
191+
"The provider returned an HTML error page instead of an API response. " +
192+
"This usually means a CDN or gateway (e.g. Cloudflare) blocked the request. " +
193+
"Retry in a moment or check provider status."
194+
);
195+
}
196+
178197
const httpMatch = trimmed.match(HTTP_STATUS_PREFIX_RE);
179198
if (httpMatch) {
180199
const rest = httpMatch[2].trim();

0 commit comments

Comments
 (0)