Skip to content

Commit be2e48e

Browse files
authored
Prevent redirect loop on /home with tags/lastrun filters (apache#42607) (apache#42609)
Closes apache#42607
1 parent 05c43ee commit be2e48e

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
@@ -814,19 +814,22 @@ def index(self):
814814
return redirect(url_for("Airflow.index"))
815815

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

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

831834
if arg_status_filter is None:
832835
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)