Skip to content

gateway: log bearer-token auth state at startup - #255

Merged
FMXExpress merged 2 commits into
mainfrom
claude/nice-dirac-sBwXK
Jun 14, 2026
Merged

FMXExpress merged 2 commits into
mainfrom
claude/nice-dirac-sBwXK

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

Adds one LogInfo at the end of TGatewayServer.Start that summarises the bearer-token auth state, so the typical misconfigurations are visible in the deploy logs without needing to exec into the container. Surfaces four cases:

[info] gateway: bearer-token auth ENABLED (source=env, token len=64, prefix=ab12...)
[info] gateway: bearer-token auth ENABLED (source=config.json, token len=64, prefix=ab12...)
[info] gateway: bearer-token auth MISCONFIGURED -- token field is unresolved template '${PASCLAW_GATEWAY_TOKEN}'; env var not set, so no client token will match
[info] gateway: bearer-token auth DISABLED -- no token configured (every /v1/* route open)

Motivation

The MISCONFIGURED branch is the case that motivated this PR. The typo trap:

  1. Operator sets PASCAL_GATEWAY_TOKEN (no W) in the platform's env-var UI.
  2. ExpandEnvVarsInJSON sees no PASCLAW_GATEWAY_TOKEN, so the literal ${PASCLAW_GATEWAY_TOKEN} survives into Cfg.Gateway.Token.
  3. GetEffectiveGatewayToken returns the literal template string.
  4. Every bearer the web UI sends gets compared against ${PASCLAW_GATEWAY_TOKEN} (the literal characters) — guaranteed mismatch, infinite 401 loop.

Without this log line you can only diagnose by printenv-ing inside the container or grepping the on-disk config.json for the unresolved marker. With it, one redeploy shows the operator exactly what's wrong on App Platform / Docker Hub / k8s — wherever the runtime logs are aggregated.

Token-safety notes

  • The log line never contains the token itself, only prefix=<first 4 chars>... for tokens >= 8 chars (~16 bits of brute-force surface against a hex token) and prefix=<short> for shorter tokens (a 4-byte prefix of a 6-byte token leaks too much).
  • The MISCONFIGURED branch reveals the unresolved template string verbatim — that's a config marker, not a secret.

Files

  • src/pkg/config/PasClaw.Config.pas — new DescribeGatewayAuthState(C: TConfig): string helper next to GetEffectiveGatewayToken. Lives in Config so it can read GEnvGatewayToken without forcing a Gateway -> Auth -> Config dep cycle.
  • src/pkg/gateway/PasClaw.Gateway.Server.pas — TGatewayServer.Start now calls LogInfo('%s', [DescribeGatewayAuthState(FCfg)]) right after the existing "gateway: listening on" line. Suppressed when FMCPOnly is set so the optional /mcp companion listener doesn't double-log.

Test plan

Verified each branch against a real gateway boot with a scratch $PASCLAW_HOME:

  • DISABLED — empty config, no env: bearer-token auth DISABLED -- no token configured (every /v1/* route open)
  • ENABLED (config.json) — gateway.token: "abcdef0123456789ZZ" (18 chars): ENABLED (source=config.json, token len=18, prefix=abcd...)
  • ENABLED (env) — PASCLAW_GATEWAY_TOKEN=<64-char hex>: ENABLED (source=env, token len=64, prefix=cafe...)
  • MISCONFIGURED — gateway.token: "${PASCLAW_GATEWAY_TOKEN}" with the env var unset: MISCONFIGURED -- token field is unresolved template '${PASCLAW_GATEWAY_TOKEN}'; env var not set, so no client token will match
  • Short-token guard — 5-char token: prefix=<short> (no leak of a token where 4 chars would reveal most of it)
  • make clean (FPC 3.2.2)

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

Adds a single LogInfo at the end of TGatewayServer.Start that summarises
the bearer-token auth state without revealing the token. Four cases:

  ENABLED (source=env, token len=N, prefix=ab12...)
  ENABLED (source=config.json, token len=N, prefix=ab12...)
  MISCONFIGURED -- token field is unresolved template '${VAR_NAME}'; ...
  DISABLED -- no token configured (every /v1/* route open)

The MISCONFIGURED branch is the motivating case: operators who misspell
PASCLAW_GATEWAY_TOKEN as PASCAL_GATEWAY_TOKEN (no W) in the platform's
env-var UI never trigger the substitution in ExpandEnvVarsInJSON, the
literal `${PASCLAW_GATEWAY_TOKEN}` survives into Cfg.Gateway.Token, and
every client bearer comparison loses against the literal template
string -- producing an "always 401" symptom that's invisible from the
deploy logs without console access to the container. This line surfaces
the typo on the next restart.

The prefix is the first 4 chars + `...` for tokens >= 8 chars (16 bits
of brute-force surface against a hex token); shorter tokens log
prefix=<short> instead of leaking too high a fraction of the secret.

DescribeGatewayAuthState lives next to GetEffectiveGatewayToken in
PasClaw.Config so it has natural access to GEnvGatewayToken without
adding a Gateway -> Auth -> Config dep cycle.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a178c2f404

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/pkg/config/PasClaw.Config.pas Outdated
Comment on lines +1720 to +1721
Result := 'gateway: bearer-token auth DISABLED -- ' +
'no token configured (every /v1/* route open)';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Include MCP in unauthenticated startup log

When no token is configured, CheckGatewayAuth short-circuits before route checks, and the main listener still dispatches POST /mcp and /v1/mcp/rpc later in PasClaw.Gateway.Server.pas (with --mcp-allow-write, that can expose mutating tools unauthenticated too). This startup message says only /v1/* is open, so operators reading deploy logs can miss that the MCP surface, including a dedicated MCP listener sharing the same config, is also unauthenticated; please make the disabled message cover MCP/all protected routes.

Useful? React with 👍 / 👎.

When no token is configured, the dedicated MCP companion listener
(--mcp-port) and the /v1/mcp/rpc in-main-port path are unauthenticated
alongside the /v1/* surfaces. Old message ("every /v1/* route open")
understated the exposure -- operators reading the log could miss that
the MCP JSON-RPC endpoint is also reachable by any caller. Codex P2.
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