codeant-ai-for-open-source[bot] commented on code in PR #37234: URL: https://github.com/apache/superset/pull/37234#discussion_r2725082406
########## superset/views/oauth.py: ########## @@ -0,0 +1,78 @@ +# 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. + +from typing import Optional +import jwt +from flask import flash, g, redirect, get_flashed_messages, request, session, url_for +from flask_appbuilder import expose +from flask_appbuilder.security.decorators import no_cache +from flask_appbuilder.security.utils import generate_random_string +from flask_appbuilder.security.views import AuthOAuthView, WerkzeugResponse +from flask_appbuilder._compat import as_unicode +from superset.views.base import BaseSupersetView + + +class SupersetOAuthView(BaseSupersetView, AuthOAuthView): + route_base = "/login" + + @expose("/") + @expose("/<provider>") + @no_cache + def login(self, provider: Optional[str] = None) -> WerkzeugResponse: + + get_flashed_messages(category_filter=['danger']) + + if provider is None: + providers = [k for k in self.appbuilder.sm.oauth_remotes.keys()] + if len(providers) == 1: + provider = providers[0] + if g.user is not None and g.user.is_authenticated: + return redirect(self.appbuilder.get_url_for_index) + + if provider is None: + return self.render_template( + self.login_template, + providers=self.appbuilder.sm.oauth_providers, + title=self.title, + appbuilder=self.appbuilder, + ) + + random_state = generate_random_string() + state = jwt.encode( + request.args.to_dict(flat=False), random_state, algorithm="HS256" + ) + session["oauth_state"] = random_state Review Comment: **Suggestion:** The session currently stores `random_state` instead of the encoded `state` token; if downstream verification expects the token returned by the provider to match the stored `state`, this mismatch will break verification. Save the string form of the encoded JWT (`state`) into the session (decode bytes to str if necessary). [logic error] <details> <summary><b>Severity Level:</b> Critical 🚨</summary> ```mdx - ❌ OAuth callback state verification may fail blocking logins. - ⚠️ All OAuth provider logins may be affected. ``` </details> ```suggestion state_str = state.decode("ascii") if isinstance(state, bytes) else state session["oauth_state"] = state_str ``` <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Start the Superset server with the PR changes applied. 2. Open the OAuth initiation URL `GET /login` (SupersetOAuthView.login at `superset/views/oauth.py:35`) when `provider` is resolved (or call `/login/<provider>`). Execution reaches `superset/views/oauth.py:54-57` where `random_state` and `state` (the JWT) are created. 3. The code stores `session["oauth_state"] = random_state` at `superset/views/oauth.py:58` (the random nonce), not the encoded JWT string that is later passed to providers as `state` (lines 62-75 show `state` being sent to providers). 4. When the OAuth provider redirects back to the configured callback (`.oauth_authorized`, referenced in `superset/views/oauth.py:62-73`), the callback receives the `state` value the provider returned (the encoded JWT). Downstream verification logic (the callback implemented by the auth layer / AuthOAuthView) will compare the returned `state` to `session["oauth_state"]`. Because the session holds the raw `random_state` (not the encoded JWT), this comparison will fail and the authorization will be rejected, causing login failure. 5. The mismatch is evidenced by the code paths in `superset/views/oauth.py:54-58` (setting session) and `superset/views/oauth.py:62-75` (sending `state` to provider), showing a concrete, verifiable inconsistency rather than a stylistic issue. ``` </details> <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/views/oauth.py **Line:** 58:58 **Comment:** *Logic Error: The session currently stores `random_state` instead of the encoded `state` token; if downstream verification expects the token returned by the provider to match the stored `state`, this mismatch will break verification. Save the string form of the encoded JWT (`state`) into the session (decode bytes to str if necessary). Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. ``` </details> -- 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]
