fix(network): bypass Cloudflare bot protection using WKWebView#8
fix(network): bypass Cloudflare bot protection using WKWebView#8willsheldon wants to merge 1 commit into
Conversation
Replace URLSession-based NetworkService with WKWebView-based implementation to bypass Cloudflare's bot protection on Claude.ai API endpoints. Problem: - Cloudflare blocks requests from URLSession due to TLS fingerprint mismatch - The cf_clearance cookie is tied to browser TLS fingerprints - Simple HTTP clients (URLSession, curl) cannot pass JS challenges Solution: - Created WebViewNetworkService using WKWebView which shares Safari's TLS stack - WKWebView automatically handles Cloudflare JS challenges - Polls page content until valid JSON is detected (handles challenge delays) Additional fixes: - Added 'capabilities' field to Organization model - Updated org selection to prefer orgs with 'chat' capability - This fixes issues for users with multiple orgs (API-only vs Claude.ai) - Added 'timeout' case to NetworkError enum - Changed NetworkServiceProtocol from Actor to Sendable constraint Files changed: - New: WebViewNetworkService.swift - WKWebView-based network service - Modified: AppModel.swift - Use WebViewNetworkService, fix org selection - Modified: UsageService.swift - Prefer chat-capable organizations - Modified: Organization.swift - Add capabilities field and helper - Modified: NetworkServiceProtocol.swift - Sendable instead of Actor - Modified: NetworkError.swift - Add timeout case Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Just FYI I ran a security analysis of this repo with Copilot and it flagged this PR as a security risk:
|
|
Thanks for this. I’ve been reviewing the change again and wanted to check whether you’re still able to reproduce the Cloudflare blocking issue on the latest release. I haven’t been seeing Cloudflare blocks locally with the current If you are still seeing Cloudflare blocks, could you share:
|
Summary
capabilitiesfield to Organization modelProblem
Cloudflare recently started blocking requests from URLSession due to TLS fingerprint mismatch. The
cf_clearancecookie is tied to browser TLS fingerprints, so simple HTTP clients (URLSession, curl) cannot pass JavaScript challenges even with valid session keys.Solution
Created
WebViewNetworkServiceusing WKWebView which shares Safari's TLS stack. WKWebView automatically handles Cloudflare JS challenges and has the correct TLS fingerprint that Cloudflare expects.The service polls page content until valid JSON is detected, handling the delay while Cloudflare challenges complete.
Additional Fixes
capabilitiesfield to Organization model withhasChatCapabilityhelperUsageServiceandAppModelto prefer organizations with "chat" capabilitytimeoutcase to NetworkError enumActortoSendableconstraint to support @mainactor classesTest plan
🤖 Generated with Claude Code