Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lang): patch FAB's LocaleView to redirect to previous page #31692

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions superset/initialization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import os
import sys
from typing import Any, Callable, TYPE_CHECKING
from urllib.parse import urlparse

import wtforms_json
from deprecation import deprecated
from flask import Flask, redirect
from flask import abort, Flask, redirect, request, session
from flask_appbuilder import expose, IndexView
from flask_babel import gettext as __
from flask_appbuilder.api import safe
from flask_babel import gettext as __, refresh
from flask_compress import Compress
from flask_session import Session
from werkzeug.middleware.proxy_fix import ProxyFix
Expand Down Expand Up @@ -701,3 +703,34 @@
@expose("/")
def index(self) -> FlaskResponse:
return redirect("/superset/welcome/")

@staticmethod
def is_safe_url(target: str) -> bool:
"""
Is target is a safe URL to redirect to?
"""
ref_url = urlparse(target)
host_url = urlparse(request.host_url)
return ref_url.scheme in ("http", "https") and ref_url.netloc == host_url.netloc

Check warning on line 714 in superset/initialization/__init__.py

View check run for this annotation

Codecov / codecov/patch

superset/initialization/__init__.py#L712-L714

Added lines #L712 - L714 were not covered by tests

@expose("/lang/<string:locale>")
@safe
def patch_flask_locale(self, locale: str) -> FlaskResponse:
"""
Change user's locale and redirect back to the previous page.

Overrides FAB's babel.views.LocaleView so we can use the request
Referrer as the redirect target, in case our previous page was actually
served by the frontend (and thus not added to the session's page_history
stack).
"""
if locale not in self.appbuilder.bm.languages:

Check warning

Code scanning / CodeQL

URL redirection from remote source Medium

Untrusted URL redirection depends on a
user-provided value
.
abort(404, description="Locale not supported.")
session["locale"] = locale
refresh()
self.update_redirect()

Check warning on line 731 in superset/initialization/__init__.py

View check run for this annotation

Codecov / codecov/patch

superset/initialization/__init__.py#L727-L731

Added lines #L727 - L731 were not covered by tests

redirect_to = request.headers.get("Referer")
if not redirect_to or not self.is_safe_url(redirect_to):
redirect_to = self.get_redirect()
return redirect(redirect_to)

Check warning on line 736 in superset/initialization/__init__.py

View check run for this annotation

Codecov / codecov/patch

superset/initialization/__init__.py#L733-L736

Added lines #L733 - L736 were not covered by tests
1 change: 1 addition & 0 deletions tests/integration_tests/security_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,7 @@ def test_views_are_secured(self):
["SecurityApi", "login"],
["SecurityApi", "refresh"],
["SupersetIndexView", "index"],
["SupersetIndexView", "patch_flask_locale"],
["DatabaseRestApi", "oauth2"],
]
unsecured_views = []
Expand Down
Loading