Skip to content

fix: OIDC accepts valid tokens again (#273) - #283

Merged
jrosskopf merged 3 commits into
mainfrom
fix/gh-273-oidc-decode
Nov 25, 2026
Merged

jrosskopf merged 3 commits into
mainfrom
fix/gh-273-oidc-decode

Conversation

@jrosskopf

@jrosskopf jrosskopf commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • With auth.type: oidc (REST) or mcp.auth.type: oidc (MCP), every token got 401, valid ones included. OIDCAuthHandler::decodeJWT returned std::make_pair(header, payload), i.e. copies of the crow::json::rvalue roots. A copied rvalue does not own the buffer it points into (crow's r_string copy clears owned_). The originals freed that buffer on return, so header.has("alg") read freed memory and the log said JWT header missing 'alg' field. ASAN on the same pattern reports heap-use-after-free.
  • New DecodedJWT type: it holds the owning roots, copying is deleted, and a move hands the buffer over, so the parsed values cannot outlive their text. validateToken reads them by reference. Decode also rejects tokens that are not exactly three parts or whose header/payload are not JSON objects. The unused jwt::decode call is gone.
  • Hardening in the same function: kid/alg/email_verified are type-checked before they are read (.s()/.b() threw on the wrong type), and a token without exp is refused by name instead of through the zero time_point.
  • I checked the rest of src/ for the same pattern: rvalues returned or stored by value, children of a temporary load(), and by-value rvalue parameters. decodeJWT was the only instance. JWKS (oidc_jwks_manager.cpp) and discovery parsing keep their root alive while they read from it.

Test plan

  • New test/integration/test_oidc_e2e.py. It runs a mock issuer on 127.0.0.1 (discovery + JWKS, RSA keys) and the real binary, and signs RS256 tokens itself.
    • Accepted: a valid token gets 200, sub and roles reach the template as auth.username / auth.roles, an audience list works, and a key rotated into the JWKS is picked up.
    • MCP: tools/call with a valid token succeeds, and allowed-roles gates on the OIDC roles (403 without the role).
    • Refused (401): bad signature, a foreign key with a known kid, wrong issuer, wrong audience, expired, missing exp, missing sub, unknown kid (asserts a JWKS refetch happened), alg none, garbage. Wrong audience, expired and no token are also refused over MCP.
    • Red on main: the 5 accept cases fail with 401 (OIDC error: JWT header missing 'alg' field) and every refusal passes. Green with the fix: 20 passed.
  • New test/cpp/oidc_decode_jwt_test.cpp. It reads the decoded values after the decoding scope is gone and the heap has been churned, checks moved objects and malformed tokens, and static_asserts that the type is not copyable.
  • Auth integration suites: 254 passed, 1 skipped (oidc_e2e, oidc_authentication, jwt_empty_secret, mcp_auth_matrix, auth_context_spoofing, mcp_config_tool_auth, rest_auth_and_write_semantics, route_precedence_auth, cors_preflight_and_auth_challenge, g2_auth_metrics_mcp, config_route_authorization, auth_secretsmanager).
  • ctest -R "oidc|auth|decode": 64/64 passed. check_skills.py --static-only: passed.

cryptography is added to the integration test dependencies (uv.lock updated).

Closes #273

🤖 Generated with Claude Code

https://claude.ai/code/session_018txuxF9jXyiKakpUoXrKBi


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

- Run a mock OIDC issuer on 127.0.0.1 (discovery document and a JWKS
  with RSA keys) and the real flapi binary with auth.type oidc for REST
  and mcp.auth.type oidc for MCP
- Sign RS256 tokens in the test: a valid token must get 200 and its sub
  and roles must reach the template as auth.username / auth.roles; a key
  rotated into the JWKS must be picked up; MCP tools/call must succeed
- Refuse a bad signature, a foreign key with a known kid, wrong issuer,
  wrong audience, expired, missing exp, missing sub, unknown kid (after
  a JWKS refetch), alg none and garbage, on REST and MCP
- Fails today: every valid token gets 401, the log says
  "JWT header missing 'alg' field"
- Add cryptography to the integration test dependencies
- decodeJWT returned std::make_pair(header, payload): copies of the crow
  rvalues. A copied rvalue does not own the buffer it points into; the
  originals freed it on return, so header.has("alg") read freed memory
  and every token got 401 ("JWT header missing 'alg' field")
- Add DecodedJWT: it holds the owning roots, cannot be copied, and a move
  hands the buffer over, so the parsed values cannot outlive their text;
  validateToken reads them by reference
- Decode rejects tokens that are not three parts or whose header/payload
  are not JSON objects, and drops the unused jwt::decode call
- Check kid/alg/email_verified types before reading them, and refuse a
  token without exp by name instead of through the zero time_point
- Unit tests read the decoded values after the decoding scope is gone and
  the heap churned; the E2E test now passes (REST and MCP)
- CHANGELOG: OIDC: valid tokens were rejected (#273)
- The authentication guide keeps one line saying v26.11.25 rejected every
  token; the MCP guide points at OIDC as a working option
@jrosskopf
jrosskopf force-pushed the fix/gh-273-oidc-decode branch from 30707ce to 2363c25 Compare November 25, 2026 16:24
@jrosskopf
jrosskopf merged commit 6b30a75 into main Nov 25, 2026
52 of 56 checks passed
@jrosskopf
jrosskopf deleted the fix/gh-273-oidc-decode branch November 25, 2026 18:08
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.

OIDC rejects every valid token: decodeJWT returns crow::json::rvalue pointing into freed strings

1 participant