Skip to content

Commit

Permalink
Sanitize url_for arguments before they are passed (apache#29039)
Browse files Browse the repository at this point in the history
The url_for of flask has special arguments that start with `_` and we
should sanitize the ones that come with the request before passing them.
  • Loading branch information
potiuk authored Jan 19, 2023
1 parent 26b16c9 commit 7f2b065
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ def truncate_task_duration(task_duration):
return int(task_duration) if task_duration > 10.0 else round(task_duration, 3)


def sanitize_args(args: dict[str, str]) -> dict[str, str]:
"""
Remove all parameters starting with `_`
:param args: arguments of request
:return: copy of the dictionary passed as input with args starting with `_` removed.
"""
return {key: value for key, value in args.items() if not key.startswith("_")}


def get_safe_url(url):
"""Given a user-supplied URL, ensure it points to our web server"""
if not url:
Expand Down Expand Up @@ -1169,7 +1179,7 @@ def last_dagruns(self, session=None):
)
def legacy_code(self):
"""Redirect from url param."""
return redirect(url_for("Airflow.code", **request.args))
return redirect(url_for("Airflow.code", **sanitize_args(request.args)))

@expose("/dags/<string:dag_id>/code")
@auth.has_access(
Expand Down Expand Up @@ -1216,7 +1226,7 @@ def code(self, dag_id, session=None):
)
def legacy_dag_details(self):
"""Redirect from url param."""
return redirect(url_for("Airflow.dag_details", **request.args))
return redirect(url_for("Airflow.dag_details", **sanitize_args(request.args)))

@expose("/dags/<string:dag_id>/details")
@auth.has_access(
Expand Down Expand Up @@ -2628,7 +2638,7 @@ def success(self):
@action_logging
def dag(self, dag_id):
"""Redirect to default DAG view."""
kwargs = {**request.args, "dag_id": dag_id}
kwargs = {**sanitize_args(request.args), "dag_id": dag_id}
return redirect(url_for("Airflow.grid", **kwargs))

@expose("/legacy_tree")
Expand All @@ -2643,7 +2653,7 @@ def dag(self, dag_id):
@action_logging
def legacy_tree(self):
"""Redirect to the replacement - grid view."""
return redirect(url_for("Airflow.grid", **request.args))
return redirect(url_for("Airflow.grid", **sanitize_args(request.args)))

@expose("/tree")
@auth.has_access(
Expand All @@ -2657,7 +2667,7 @@ def legacy_tree(self):
@action_logging
def tree(self):
"""Redirect to the replacement - grid view. Kept for backwards compatibility."""
return redirect(url_for("Airflow.grid", **request.args))
return redirect(url_for("Airflow.grid", **sanitize_args(request.args)))

@expose("/dags/<string:dag_id>/grid")
@auth.has_access(
Expand Down Expand Up @@ -2736,7 +2746,7 @@ def grid(self, dag_id, session=None):
@action_logging
def legacy_calendar(self):
"""Redirect from url param."""
return redirect(url_for("Airflow.calendar", **request.args))
return redirect(url_for("Airflow.calendar", **sanitize_args(request.args)))

@expose("/dags/<string:dag_id>/calendar")
@auth.has_access(
Expand Down Expand Up @@ -2877,7 +2887,7 @@ def _convert_to_date(session, column):
@action_logging
def legacy_graph(self):
"""Redirect from url param."""
return redirect(url_for("Airflow.graph", **request.args))
return redirect(url_for("Airflow.graph", **sanitize_args(request.args)))

@expose("/dags/<string:dag_id>/graph")
@auth.has_access(
Expand Down Expand Up @@ -2994,7 +3004,7 @@ class GraphForm(DateTimeWithNumRunsWithDagRunsForm):
@action_logging
def legacy_duration(self):
"""Redirect from url param."""
return redirect(url_for("Airflow.duration", **request.args))
return redirect(url_for("Airflow.duration", **sanitize_args(request.args)))

@expose("/dags/<string:dag_id>/duration")
@auth.has_access(
Expand Down Expand Up @@ -3155,7 +3165,7 @@ def duration(self, dag_id, session=None):
@action_logging
def legacy_tries(self):
"""Redirect from url param."""
return redirect(url_for("Airflow.tries", **request.args))
return redirect(url_for("Airflow.tries", **sanitize_args(request.args)))

@expose("/dags/<string:dag_id>/tries")
@auth.has_access(
Expand Down Expand Up @@ -3250,7 +3260,7 @@ def tries(self, dag_id, session=None):
@action_logging
def legacy_landing_times(self):
"""Redirect from url param."""
return redirect(url_for("Airflow.landing_times", **request.args))
return redirect(url_for("Airflow.landing_times", **sanitize_args(request.args)))

@expose("/dags/<string:dag_id>/landing-times")
@auth.has_access(
Expand Down Expand Up @@ -3372,7 +3382,7 @@ def paused(self):
@action_logging
def legacy_gantt(self):
"""Redirect from url param."""
return redirect(url_for("Airflow.gantt", **request.args))
return redirect(url_for("Airflow.gantt", **sanitize_args(request.args)))

@expose("/dags/<string:dag_id>/gantt")
@auth.has_access(
Expand Down Expand Up @@ -3820,7 +3830,7 @@ def robots(self):
)
def legacy_audit_log(self):
"""Redirect from url param."""
return redirect(url_for("Airflow.audit_log", **request.args))
return redirect(url_for("Airflow.audit_log", **sanitize_args(request.args)))

@expose("/dags/<string:dag_id>/audit_log")
@auth.has_access(
Expand Down

0 comments on commit 7f2b065

Please sign in to comment.