Skip to content

Commit

Permalink
Prevent redirect loop on /home with tags/lastrun filters (apache#42607)…
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaicher authored and joaopamaral committed Oct 21, 2024
1 parent 09c2d4a commit d413f0b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
17 changes: 10 additions & 7 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,19 +814,22 @@ def index(self):
return redirect(url_for("Airflow.index"))

filter_tags_cookie_val = flask_session.get(FILTER_TAGS_COOKIE)
filter_lastrun_cookie_val = flask_session.get(FILTER_LASTRUN_COOKIE)

# update filter args in url from session values if needed
if (not arg_tags_filter and filter_tags_cookie_val) or (
not arg_lastrun_filter and filter_lastrun_cookie_val
):
tags = arg_tags_filter or (filter_tags_cookie_val and filter_tags_cookie_val.split(","))
lastrun = arg_lastrun_filter or filter_lastrun_cookie_val
return redirect(url_for("Airflow.index", tags=tags, lastrun=lastrun))

if arg_tags_filter:
flask_session[FILTER_TAGS_COOKIE] = ",".join(arg_tags_filter)
elif filter_tags_cookie_val:
# If tags exist in cookie, but not URL, add them to the URL
return redirect(url_for("Airflow.index", tags=filter_tags_cookie_val.split(",")))

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

if arg_status_filter is None:
filter_status_cookie_val = flask_session.get(FILTER_STATUS_COOKIE)
Expand Down
38 changes: 38 additions & 0 deletions tests/www/views/test_views_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,41 @@ def test_analytics_pixel(user_client, is_enabled, should_have_pixel):
check_content_in_response("apacheairflow.gateway.scarf.sh", resp)
else:
check_content_not_in_response("apacheairflow.gateway.scarf.sh", resp)


@pytest.mark.parametrize(
"url, filter_tags_cookie_val, filter_lastrun_cookie_val, expected_filter_tags, expected_filter_lastrun",
[
("home", None, None, [], None),
# from url only
("home?tags=example&tags=test", None, None, ["example", "test"], None),
("home?lastrun=running", None, None, [], "running"),
("home?tags=example&tags=test&lastrun=running", None, None, ["example", "test"], "running"),
# from cookie only
("home", "example,test", None, ["example", "test"], None),
("home", None, "running", [], "running"),
("home", "example,test", "running", ["example", "test"], "running"),
# from url and cookie
("home?tags=example", "example,test", None, ["example"], None),
("home?lastrun=failed", None, "running", [], "failed"),
("home?tags=example", None, "running", ["example"], "running"),
("home?lastrun=running", "example,test", None, ["example", "test"], "running"),
("home?tags=example&lastrun=running", "example,test", "failed", ["example"], "running"),
],
)
def test_filter_cookie_eval(
working_dags,
admin_client,
url,
filter_tags_cookie_val,
filter_lastrun_cookie_val,
expected_filter_tags,
expected_filter_lastrun,
):
with admin_client.session_transaction() as flask_session:
flask_session[FILTER_TAGS_COOKIE] = filter_tags_cookie_val
flask_session[FILTER_LASTRUN_COOKIE] = filter_lastrun_cookie_val

resp = admin_client.get(url, follow_redirects=True)
assert resp.request.args.getlist("tags") == expected_filter_tags
assert resp.request.args.get("lastrun") == expected_filter_lastrun

0 comments on commit d413f0b

Please sign in to comment.