Skip to content

gateway: fix PR #246 — accept Bearer scheme via Indy's OnParseAuthentication - #257

Merged
FMXExpress merged 1 commit into
mainfrom
claude/gateway-bearer-auth-fix
Jun 14, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/gateway-bearer-auth-fix

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

PR #246 wired bearer-token auth into OnCommandGet but missed that Indy's TIdCustomHTTPServer natively recognizes only Basic as an auth scheme. Every Authorization: Bearer <tok> request is intercepted by Indy's built-in auth machinery and converted to a 401 with WWW-Authenticate: Basic realm="" before OnCommandGet runs — so CheckGatewayAuth never sees the bearer at all and rejects every authenticated request, even with the correct token.

Repro

Set PASCLAW_GATEWAY_TOKEN=awesome in the container env, start the gateway, then:

$ curl -v -H "Authorization: Bearer $PASCLAW_GATEWAY_TOKEN" http://localhost:8088/v1/status
> Authorization: Bearer awesome
< HTTP/1.1 401 Unauthorized
< WWW-Authenticate: Basic           ← Indy emitted this, not PasClaw
< Server: PasClaw/dev               ← misleading — Indy uses ServerSoftware even in its own exception path

Root cause

Indy walks the Authorization header inside IdCustomHTTPServer.pas at line 1454, calls DoParseAuthentication which only recognizes Basic, and raises EIdHTTPUnsupportedAuthorisationScheme on every other scheme:

// vendor/Indy/Lib/Protocols/IdCustomHTTPServer.pas:1450-1458
s := LRequestInfo.RawHeaders.Values['Authorization'];
if Length(s) > 0 then begin
  LRequestInfo.FAuthType := Fetch(s, ' ');
  LRequestInfo.FAuthExists := DoParseAuthentication(...);
  if not LRequestInfo.FAuthExists then begin
    raise EIdHTTPUnsupportedAuthorisationScheme.Create(...);  // <-- here
  end;
end;

The exception handler at line 1476 converts it to a 401 with the canonical WWW-Authenticate: Basic header — all before DoCommandGet (where PasClaw's middleware lives) ever fires.

Fix

Wire FHTTP.OnParseAuthentication to a handler that just sets VHandled := True for every non-Basic scheme. Indy's built-in Basic parsing path is unchanged; Bearer and any future scheme now fall through to OnCommandGet, where the existing ExtractBearerToken + constant-time comparison in CheckGatewayAuth does the real validation.

Why this hid in PR #246

  • Web UI's gwFetch (webui.html) doesn't attach an Authorization header until a token is stored in localStorage, so for a fresh / token-less deployment no Authorization header ever reaches the gateway.
  • The default token-less config doesn't gate /v1/* at all — so every request was being routed through the no-token-needed path.
  • The first time PR gateway: optional bearer-token auth for /v1/* routes (off by default) #246's auth feature actually got exercised on a real deployment (DO App Platform, PASCLAW_GATEWAY_TOKEN set), every request 401'd.

Verified

Against a live gateway with PASCLAW_GATEWAY_TOKEN=awesome:

no auth header             → 401  (unchanged)
wrong bearer               → 401 with WWW-Authenticate: Bearer realm="pasclaw"
right bearer "awesome"     → 200  ← was 401 before this commit
exempt /v1/health, no auth → 200  (unchanged)

Test plan

  • FPC 3.2.2 build clean
  • All four bearer/no-bearer cases above
  • 401 response now carries WWW-Authenticate: Bearer realm="pasclaw", not Basic
  • Confirm on the reporter's DO App Platform deployment after redeploy

Stacked relationship

Independent of PR #255 (startup auth-state logging). #255 makes the misconfiguration visible in logs; this PR is the actual functional fix. Either can merge first.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

PR #246 wired CheckGatewayAuth into OnCommandGet but missed Indy's
TIdCustomHTTPServer hard-coding "Basic" as the only natively-known
auth scheme. When a request arrives with `Authorization: Bearer <tok>`,
IdCustomHTTPServer.pas line 1454 calls DoParseAuthentication which
recognises only Basic; the next line raises
EIdHTTPUnsupportedAuthorisationScheme, caught at line 1476 and
converted to a 401 with `WWW-Authenticate: Basic realm=""` -- BEFORE
OnCommandGet runs. So PasClaw's CheckGatewayAuth never sees the
bearer at all and rejects every authenticated request.

Symptom: web UI prompts for the token, operator pastes the correct
value, every /v1/* call returns 401 forever. `Server: PasClaw/dev`
in the response is misleading -- Indy uses the configured
ServerSoftware header even when its own exception handler builds
the response, hence why this looked like a PasClaw-issued 401 the
whole time.

Fix: wire OnParseAuthentication to a handler that just sets
VHandled := True for every scheme. Indy's built-in Basic path still
fires first; everything else (Bearer, and future schemes if anyone
adds them) falls through to OnCommandGet where CheckGatewayAuth
parses the raw header directly via ExtractBearerToken.

Verified against a live gateway:
- no auth header             -> 401 (unchanged)
- wrong bearer               -> 401 with WWW-Authenticate: Bearer realm="pasclaw"
- right bearer               -> 200 (was 401 before this commit)
- exempt /v1/health, no auth -> 200 (unchanged)

The reason this hid in PR #246: web UI's gwFetch suppresses the
Authorization header when no token is stored in localStorage, and
the default token-less config doesn't gate /v1/* at all -- so no
deployment exercised the bearer path until [email protected]
turned auth on for his DO App Platform instance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants