-
Notifications
You must be signed in to change notification settings - Fork 109
Fix 403 Forbidden: Add User-Agent header to OpenAI client fetch to prevent WAF/Cloudflare blocks #177
Description
Description
When using deepcode-cli with custom/proxy API endpoints (e.g., endpoints routed through Cloudflare or strict enterprise WAFs), requests immediately fail with a 403 Your request was blocked error.
Root Cause
In the initialization of the OpenAI client, the custom fetch implementation using undiciFetch does not provide a default User-Agent.
Many CDNs and WAFs (like Cloudflare) automatically block HTTP requests missing a User-Agent as a basic anti-bot measure.
ypescript // Current implementation fetch: (url, init) => undiciFetch(url, { ...init, dispatcher: keepAliveAgent })
Proposed Solution
Inject a standard or tool-specific User-Agent into the headers before passing them to undiciFetch.
` ypescript
fetch: (url, init) => {
const headers = new Headers(init?.headers);
if (!headers.has("User-Agent")) {
headers.set("User-Agent", "deepcode-cli/0.1.x (Node.js)");
}
// Note: undiciFetch expects a plain object or an array of arrays for headers in some older TS setups,
// so converting Headers back to an object might be necessary depending on the exact undici version used.
const headersObj = Object.fromEntries(headers.entries());
return undiciFetch(url, {
...init,
headers: headersObj,
dispatcher: keepAliveAgent
});
}
`
Steps to Reproduce
- Set
BASE_URLin~/.deepcode/settings.jsonto an API endpoint protected by Cloudflare. - Run
deepcodeand send a message. - Observe the
403 Your request was blockederror.