Skip to content

Commit 69e1dd7

Browse files
jscheffljmaicher
authored andcommitted
Prevent redirect loop on /home with tags/lastrun filters (#42607) (#42609) (#42628)
Closes #42607 (cherry picked from commit be2e48e) Co-authored-by: Julian Maicher <[email protected]>
1 parent 1ad33e2 commit 69e1dd7

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

airflow/www/views.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -815,19 +815,22 @@ def index(self):
815815
return redirect(url_for("Airflow.index"))
816816

817817
filter_tags_cookie_val = flask_session.get(FILTER_TAGS_COOKIE)
818+
filter_lastrun_cookie_val = flask_session.get(FILTER_LASTRUN_COOKIE)
819+
820+
# update filter args in url from session values if needed
821+
if (not arg_tags_filter and filter_tags_cookie_val) or (
822+
not arg_lastrun_filter and filter_lastrun_cookie_val
823+
):
824+
tags = arg_tags_filter or (filter_tags_cookie_val and filter_tags_cookie_val.split(","))
825+
lastrun = arg_lastrun_filter or filter_lastrun_cookie_val
826+
return redirect(url_for("Airflow.index", tags=tags, lastrun=lastrun))
827+
818828
if arg_tags_filter:
819829
flask_session[FILTER_TAGS_COOKIE] = ",".join(arg_tags_filter)
820-
elif filter_tags_cookie_val:
821-
# If tags exist in cookie, but not URL, add them to the URL
822-
return redirect(url_for("Airflow.index", tags=filter_tags_cookie_val.split(",")))
823830

824-
filter_lastrun_cookie_val = flask_session.get(FILTER_LASTRUN_COOKIE)
825831
if arg_lastrun_filter:
826832
arg_lastrun_filter = arg_lastrun_filter.strip().lower()
827833
flask_session[FILTER_LASTRUN_COOKIE] = arg_lastrun_filter
828-
elif filter_lastrun_cookie_val:
829-
# If tags exist in cookie, but not URL, add them to the URL
830-
return redirect(url_for("Airflow.index", lastrun=filter_lastrun_cookie_val))
831834

832835
if arg_status_filter is None:
833836
filter_status_cookie_val = flask_session.get(FILTER_STATUS_COOKIE)

tests/www/views/test_views_home.py

+38
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,41 @@ def test_analytics_pixel(user_client, is_enabled, should_have_pixel):
466466
check_content_in_response("apacheairflow.gateway.scarf.sh", resp)
467467
else:
468468
check_content_not_in_response("apacheairflow.gateway.scarf.sh", resp)
469+
470+
471+
@pytest.mark.parametrize(
472+
"url, filter_tags_cookie_val, filter_lastrun_cookie_val, expected_filter_tags, expected_filter_lastrun",
473+
[
474+
("home", None, None, [], None),
475+
# from url only
476+
("home?tags=example&tags=test", None, None, ["example", "test"], None),
477+
("home?lastrun=running", None, None, [], "running"),
478+
("home?tags=example&tags=test&lastrun=running", None, None, ["example", "test"], "running"),
479+
# from cookie only
480+
("home", "example,test", None, ["example", "test"], None),
481+
("home", None, "running", [], "running"),
482+
("home", "example,test", "running", ["example", "test"], "running"),
483+
# from url and cookie
484+
("home?tags=example", "example,test", None, ["example"], None),
485+
("home?lastrun=failed", None, "running", [], "failed"),
486+
("home?tags=example", None, "running", ["example"], "running"),
487+
("home?lastrun=running", "example,test", None, ["example", "test"], "running"),
488+
("home?tags=example&lastrun=running", "example,test", "failed", ["example"], "running"),
489+
],
490+
)
491+
def test_filter_cookie_eval(
492+
working_dags,
493+
admin_client,
494+
url,
495+
filter_tags_cookie_val,
496+
filter_lastrun_cookie_val,
497+
expected_filter_tags,
498+
expected_filter_lastrun,
499+
):
500+
with admin_client.session_transaction() as flask_session:
501+
flask_session[FILTER_TAGS_COOKIE] = filter_tags_cookie_val
502+
flask_session[FILTER_LASTRUN_COOKIE] = filter_lastrun_cookie_val
503+
504+
resp = admin_client.get(url, follow_redirects=True)
505+
assert resp.request.args.getlist("tags") == expected_filter_tags
506+
assert resp.request.args.get("lastrun") == expected_filter_lastrun

0 commit comments

Comments
 (0)