Bug: conflicting auth sources silently prefer ?token= over Authorization header
Summary
If both an Authorization header and a ?token= query parameter are provided, the middleware silently replaces the header with the query token. In production that can hide client misconfiguration and make debugging auth problems harder.
Reproduction
- Send a request with
Authorization: Bearer TOKEN_A. - Also append
?token=TOKEN_Bto the URL. - Observe that the request is executed with
TOKEN_Band the original header is discarded.
Expected
Either:
- reject conflicting auth sources with
400, or - preserve one source deterministically and emit a strong warning.
Actual
The query token silently wins.
Why it matters
This is a realistic integration mistake with browser-hosted clients, local proxies, and copied config snippets.
Found the issue. The TokenQuerystringMiddleware in main.py line 168 silently overwrites the Authorization header with ?token=, discarding the original header without any warning.
In src/openapi_mcp_sdk/main.py lines 163-170:
if token:
# Strip any existing Authorization header and inject the new one.
clean_qs = urlencode(params, doseq=True).encode()
headers = [(k, v) for k, v in scope["headers"] if k.lower() != b"authorization"]
headers.append((b"authorization", f"Bearer {token}".encode()))
scope = {**scope, "headers": headers, "query_string": clean_qs}
When both ?token= and Authorization header are present, the header is unconditionally replaced. The client intended TOKEN_A via header but the server uses TOKEN_B from the query string with no indication.
Fix — reject conflicting auth sources with 400:
if token:
# Check if Authorization header already exists — conflicting auth sources
has_auth_header = any(
k.lower() == b"authorization" for k, _ in scope["headers"]
)
if has_auth_header:
from starlette.responses import JSONResponse
response = JSONResponse(
status_code=400,
content={
"error": "conflicting_auth",
"message": "Both Authorization header and ?token= query parameter are provided. Use only one authentication method.",
},
)
await response(scope, receive, send)
return
# Remove ?token= from the query string so it never appears in
# access logs — the token travels as a header from this point on.
clean_qs = urlencode(params, doseq=True).encode()
headers = [(k, v) for k, v in scope["headers"]]
headers.append((b"authorization", f"Bearer {token}".encode()))
scope = {**scope, "headers": headers, "query_string": clean_qs}
This way:
?token=alone → works as before (lifted to header)Authorizationheader alone → works as before- Both present →
400with clear error message, no silent override
Submitting PR now.
Hi! I investigated this issue and submitted a fix in PR #38.
Root cause: In TokenQuerystringMiddleware.__call__() (main.py), when a ?token= query param is present, the middleware unconditionally strips any existing Authorization header and replaces it with the query token. There is zero conflict detection — ?token= always silently wins.
Fix: Before header replacement, the middleware now checks for an existing Authorization header. If both sources are present, it returns HTTP 400 with a clear JSON error:
{"error": "conflicting_auth", "message": "Both ?token= query parameter and Authorization header are present. Use only one authentication source."}
This is minimal (28 lines added to main.py), backward-compatible (single-source requests work exactly as before), and includes 7 unit tests covering the middleware behavior.
Happy to adjust the approach if you prefer a different resolution (e.g., logging a warning instead of rejecting, or making it configurable via env var).
Hi! Thank you for your valuable contribution — we really appreciate the effort and the clear analysis you provided.
We’ve put the PR under review/moderation and it will be merged soon.
If you’re interested, feel free to also contribute here: https://github.com/openapi/contributors
Thanks again for your great work!