Skip to content

Fix auth header precedence and prevent conflicting --profile/--header#58

Merged
jancurn merged 2 commits intomainfrom
claude/review-auth-precedence-A12kX
Mar 12, 2026
Merged

Fix auth header precedence and prevent conflicting --profile/--header#58
jancurn merged 2 commits intomainfrom
claude/review-auth-precedence-A12kX

Conversation

@jancurn
Copy link
Member

@jancurn jancurn commented Mar 12, 2026

Summary

This PR fixes authentication header handling in the connect command to ensure explicit --header "Authorization: ..." flags take precedence over auto-detected OAuth profiles, and prevents conflicting combinations of --profile and --header "Authorization: ...".

Key Changes

  • Auth header precedence: When a user explicitly provides --header "Authorization: Bearer ...", the CLI now skips OAuth profile auto-detection entirely, allowing the explicit header to take precedence over any default profile that might exist for the server.

  • Conflict detection: Added validation to detect and reject the mutually exclusive combination of --profile and --header "Authorization: ...", returning a clear error message explaining the two valid alternatives.

  • Simplified header storage: Removed the logic that conditionally stripped Authorization headers when a profile was present. Headers are now stored as-is, with the conflict check preventing problematic combinations upfront.

  • Comprehensive test coverage: Added auth-header-precedence.test.sh with four test cases covering:

    • Explicit Authorization header takes precedence over default profile
    • --profile + --header "Authorization: ..." returns a clear error (both text and JSON formats)
    • Non-Authorization headers work fine alongside profiles
    • Sessions with explicit auth headers function correctly

Implementation Details

The fix is implemented in src/cli/commands/sessions.ts in the connectSession function:

  1. Detects if an Authorization header was explicitly provided via --header flag
  2. Detects if --profile was explicitly specified
  3. Throws a ClientError if both are present, with guidance on which approach to use
  4. Skips OAuth profile auto-detection when an explicit Authorization header is provided
  5. Stores headers without special-casing Authorization headers (the conflict check prevents problematic scenarios)

https://claude.ai/code/session_016sDR5PmmuDeiwiVDWr5ymi

claude added 2 commits March 12, 2026 10:11
…to-detect for explicit header

When user provides --header "Authorization: Bearer ..." without --profile,
skip auto-detection of the default OAuth profile so the explicit header
takes precedence (fixes the original PR #57 bug).

When both --profile and --header "Authorization: ..." are specified,
return a clear error instead of silently stripping the header. This
matches the documented auth precedence (README line 434: "--header flag
is highest priority") and follows the "no surprises" design principle.

Includes e2e test covering all auth precedence scenarios.

https://claude.ai/code/session_016sDR5PmmuDeiwiVDWr5ymi
Adds --no-profile option to the connect command, allowing users to force
anonymous connections even when a default OAuth profile exists for the
server. Also works combined with --header "Authorization: ..." for
explicit bearer token without profile interference.

Updates README and CLAUDE.md to document the full auth precedence:
- --header Authorization (without --profile) skips profile auto-detection
- --profile + --header Authorization is an error
- --no-profile skips all profile detection
- Default behavior tries the "default" profile if it exists

Adds e2e tests covering --no-profile scenarios.

https://claude.ai/code/session_016sDR5PmmuDeiwiVDWr5ymi
@jancurn jancurn merged commit 9d82782 into main Mar 12, 2026
6 checks passed
@jancurn jancurn deleted the claude/review-auth-precedence-A12kX branch March 12, 2026 14:17
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.

3 participants