Skip to content

Fix 403 Forbidden: Add User-Agent header to OpenAI client fetch to prevent WAF/Cloudflare blocks #177

@ID-VerNe

Description

@ID-VerNe

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

  1. Set BASE_URL in ~/.deepcode/settings.json to an API endpoint protected by Cloudflare.
  2. Run deepcode and send a message.
  3. Observe the 403 Your request was blocked error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions