aminghadersohi commented on code in PR #37972: URL: https://github.com/apache/superset/pull/37972#discussion_r2806693053
########## superset/mcp_service/jwt_verifier.py: ########## @@ -0,0 +1,294 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Detailed JWT verification for the MCP service. + +Provides step-by-step JWT validation with specific error messages +instead of the generic "invalid_token" response from the base JWTVerifier. +""" + +import base64 +import logging +import time +from contextvars import ContextVar +from typing import Any, cast + +from authlib.jose.errors import ( + BadSignatureError, + DecodeError, + ExpiredTokenError, + JoseError, +) +from fastmcp.server.auth.auth import AccessToken +from fastmcp.server.auth.providers.jwt import JWTVerifier +from mcp.server.auth.middleware.auth_context import AuthContextMiddleware +from mcp.server.auth.middleware.bearer_auth import BearerAuthBackend +from starlette.authentication import AuthenticationError +from starlette.middleware import Middleware +from starlette.middleware.authentication import AuthenticationMiddleware +from starlette.requests import HTTPConnection +from starlette.responses import JSONResponse + +from superset.utils import json + +logger = logging.getLogger(__name__) + +# Thread-safe storage for the specific JWT failure reason. +# Set by DetailedJWTVerifier.load_access_token() on failure, +# read by DetailedBearerAuthBackend.authenticate() to raise +# an AuthenticationError with the specific reason. +_jwt_failure_reason: ContextVar[str | None] = ContextVar( + "_jwt_failure_reason", default=None +) + + +def _json_auth_error_handler( + conn: HTTPConnection, exc: AuthenticationError +) -> JSONResponse: + """Return a JSON 401 response with the specific JWT failure reason.""" + return JSONResponse( + status_code=401, + content={ + "error": "invalid_token", + "error_description": str(exc), + }, + headers={ + "WWW-Authenticate": f'Bearer error="invalid_token", ' + f'error_description="{exc}"', Review Comment: Good catch — fixed in 8708817. Added `_sanitize_header_value()` that strips CR/LF and replaces quotes before interpolating into the `WWW-Authenticate` header. The JSON body still has the raw reason for debugging, only the header is sanitized. Added test coverage for this. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
