From 88d0ba59539a3e61a4236ea0b667e4f2971f5aa3 Mon Sep 17 00:00:00 2001 From: Hussein Awala Date: Fri, 18 Aug 2023 23:52:33 +0200 Subject: [PATCH] Set strict to True when parsing dates in webserver --- airflow/utils/timezone.py | 5 +++-- airflow/www/views.py | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/airflow/utils/timezone.py b/airflow/utils/timezone.py index f7116cd81f98e..e613570a3375c 100644 --- a/airflow/utils/timezone.py +++ b/airflow/utils/timezone.py @@ -194,16 +194,17 @@ def datetime(*args, **kwargs): return dt.datetime(*args, **kwargs) -def parse(string: str, timezone=None) -> DateTime: +def parse(string: str, timezone=None, strict=False) -> DateTime: """ Parse a time string and return an aware datetime. :param string: time string :param timezone: the timezone + :param strict: if False, it will fall back on the dateutil parser if unable to parse with pendulum """ from airflow.settings import TIMEZONE - return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False) # type: ignore + return pendulum.parse(string, tz=timezone or TIMEZONE, strict=strict) # type: ignore @overload diff --git a/airflow/www/views.py b/airflow/www/views.py index a38752beb13bd..1d7b20c3dce64 100644 --- a/airflow/www/views.py +++ b/airflow/www/views.py @@ -265,17 +265,18 @@ def get_date_time_num_runs_dag_runs_form_data(www_request, session, dag): } -def _safe_parse_datetime(v, allow_empty=False) -> datetime.datetime | None: +def _safe_parse_datetime(v, allow_empty=False, strict=True) -> datetime.datetime | None: """ Parse datetime and return error message for invalid dates. :param v: the string value to be parsed :param allow_empty: Set True to return none if empty str or None + :param strict: if False, it will fall back on the dateutil parser if unable to parse with pendulum """ if allow_empty is True and not v: return None try: - return timezone.parse(v) + return timezone.parse(v, strict=strict) except (TypeError, ParserError): abort(400, f"Invalid datetime: {v!r}") @@ -1630,7 +1631,7 @@ def get_logs_with_metadata(self, session: Session = NEW_SESSION): # Convert string datetime into actual datetime try: - execution_date = timezone.parse(execution_date_str) + execution_date = timezone.parse(execution_date_str, strict=True) except ValueError: error_message = ( f"Given execution date, {execution_date}, could not be identified as a date. " @@ -2103,7 +2104,7 @@ def trigger(self, dag_id: str, session: Session = NEW_SESSION): ) try: - execution_date = timezone.parse(request_execution_date) + execution_date = timezone.parse(request_execution_date, strict=True) except ParserError: flash("Invalid execution date", "error") form = DateTimeForm(data={"execution_date": timezone.utcnow().isoformat()}) @@ -3699,7 +3700,7 @@ def grid_data(self): num_runs = conf.getint("webserver", "default_dag_run_display_number") try: - base_date = timezone.parse(request.args["base_date"]) + base_date = timezone.parse(request.args["base_date"], strict=True) except (KeyError, ValueError): base_date = dag.get_latest_execution_date() or timezone.utcnow()