mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 12:20:42 +00:00
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
This commit is contained in:
committed by
Peter Steinberger
parent
51606e9889
commit
36dd58ac2a
@@ -10,7 +10,10 @@ const HTTP_STATUS_CODE_PREFIX_RE = new RegExp(
|
||||
"i",
|
||||
);
|
||||
const HTML_ERROR_PREFIX_RE = /^\s*(?:<!doctype\s+html\b|<html\b)/i;
|
||||
const HTML_CLOSE_RE = /<\/html>/i;
|
||||
const CLOUDFLARE_HTML_ERROR_CODES = new Set([521, 522, 523, 524, 525, 526, 530]);
|
||||
const STANDALONE_HTML_ERROR_HINT_RE =
|
||||
/\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;
|
||||
|
||||
type ErrorPayload = Record<string, unknown>;
|
||||
|
||||
@@ -94,6 +97,14 @@ export function isCloudflareOrHtmlErrorPage(raw: string): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
HTML_ERROR_PREFIX_RE.test(trimmed) &&
|
||||
HTML_CLOSE_RE.test(trimmed) &&
|
||||
STANDALONE_HTML_ERROR_HINT_RE.test(trimmed)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const status = extractLeadingHttpStatus(trimmed);
|
||||
if (!status || status.code < 500) {
|
||||
return false;
|
||||
@@ -104,7 +115,7 @@ export function isCloudflareOrHtmlErrorPage(raw: string): boolean {
|
||||
}
|
||||
|
||||
return (
|
||||
status.code < 600 && HTML_ERROR_PREFIX_RE.test(status.rest) && /<\/html>/i.test(status.rest)
|
||||
status.code < 600 && HTML_ERROR_PREFIX_RE.test(status.rest) && HTML_CLOSE_RE.test(status.rest)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -175,6 +186,14 @@ export function formatRawAssistantErrorForUi(raw?: string): string {
|
||||
return `The AI service is temporarily unavailable (HTTP ${leadingStatus.code}). Please try again in a moment.`;
|
||||
}
|
||||
|
||||
if (isCloudflareOrHtmlErrorPage(trimmed)) {
|
||||
return (
|
||||
"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."
|
||||
);
|
||||
}
|
||||
|
||||
const httpMatch = trimmed.match(HTTP_STATUS_PREFIX_RE);
|
||||
if (httpMatch) {
|
||||
const rest = httpMatch[2].trim();
|
||||
|
||||
Reference in New Issue
Block a user