Cap icon indicating copy to clipboard operation
Cap copied to clipboard

Self-hosted CLI approval returns HTTP 500 when ingress blocks the Vercel Firewall probe

Open moskvini opened this issue 2 weeks ago • 1 comments

Description

Self-hosted production deployments behind an authenticated/private reverse proxy can reject the first Cap CLI authorization attempt as rate-limited. The /cli/authorize approval action returns HTTP 500, the browser displays React's generic server-component error (#441), and cap auth login never receives its callback.

The approval page recognizes the signed-in user. The failure occurs before an authorization code is issued.

Reproduction

  1. Run self-hosted Cap in production with NODE_ENV=production and VERCEL unset.

  2. Put Cap behind an authenticated/private ingress that returns HTTP 403 to server-side requests for /.well-known/vercel/rate-limit-api/rl_agent_authorization. The relevant response is what the Cap server receives, which can differ from a browser's response.

  3. Run cap auth login, open the generated URL in an already authenticated browser, and approve the requested access.

  4. Observe HTTP 500 from the approval action and this server error:

    Error: Too many authorization attempts. Try again later.
        at .../app/cli/authorize/page.js
    

Expected: a first authenticated approval within the local quota issues an authorization code and completes the CLI PKCE flow.

Actual: approval is rejected immediately, even with no previous attempts.

Cause

checkFirewallRateLimit() in apps/web/lib/rate-limit.ts checks NODE_ENV but not whether the deployment is actually on Vercel. @vercel/firewall makes a server-side request to the request host's Vercel-only endpoint. An ingress denial is returned by the SDK as:

{ rateLimited: true, error: "blocked" }

The helper treats this as an authoritative firewall decision. It cannot distinguish this private-ingress denial from a real Vercel Firewall block, so the in-process fallback is never reached.

This remains relevant after #3738: that change handles missing rules and exceptions, but a 403 blocked result still bypasses the fallback. The same decision path is present on upstream main at e3b79135260847cc90b490c355c8d43e17584551.

A narrow fix is to call Vercel Firewall only when VERCEL=1, and otherwise use the existing in-process limiter. Real Vercel allow/block decisions should remain authoritative; ignoring every blocked result would weaken that protection. Other callers of the shared helper may also be affected, although the observed failure was CLI approval.

Additional Context

  • Server: self-hosted Linux/AMD64 Docker deployment.
  • Observed dependency versions: Next.js 16.3.0 and @vercel/firewall 1.2.1.
  • NODE_ENV=production, VERCEL unset.
  • CLI: 0.1.0 bundled with Cap Desktop 0.5.9 on macOS.
  • Existing SSO browser login works.

moskvini avatar Nov 11 '26 13:11 moskvini

CAP-1019

linear[bot] avatar Nov 11 '26 13:11 linear[bot]