You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "dpgaspar (via GitHub)" <gi...@apache.org> on 2025/03/03 11:47:40 UTC

Re: [PR] fix(lang): patch FAB's LocaleView to redirect to previous page [superset]

dpgaspar commented on code in PR #31692:
URL: https://github.com/apache/superset/pull/31692#discussion_r1977371386


##########
superset/initialization/__init__.py:
##########
@@ -701,3 +702,34 @@
     @expose("/")
     def index(self) -> FlaskResponse:
         return redirect("/superset/welcome/")
+
+    @staticmethod
+    def is_safe_url(target) -> 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
+
+    @expose("/lang/<string:locale>")
+    def patch_flask_locale(self, locale) ->  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:
+            abort(404, description="Locale not supported.")
+        session["locale"] = locale
+        refresh()
+        self.update_redirect()
+
+        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)

Review Comment:
   we only redirect to the referer after checking with `self.is_safe_url` so I think this one is not correct by CodeQL.
   



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org