From 5c43fb0b23ec4592f922e5b1d98a7571da2210d6 Mon Sep 17 00:00:00 2001 From: "Li, Ian" Date: Wed, 16 Apr 2025 10:40:21 -0700 Subject: [PATCH 1/2] Sanitize on savename --- devops/scripts/benchmarks/main.py | 7 ++++++- devops/scripts/benchmarks/utils/validate.py | 20 +++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/devops/scripts/benchmarks/main.py b/devops/scripts/benchmarks/main.py index 397632e138978..4321e228c0b34 100755 --- a/devops/scripts/benchmarks/main.py +++ b/devops/scripts/benchmarks/main.py @@ -356,7 +356,12 @@ def validate_and_parse_env_args(env_args): ) parser.add_argument( "--save", - type=str, + type=lambda save: Validate.save_name( + save, + throw=argparse.ArgumentTypeError( + "Specified save name is not within characters [a-zA-Z0-9_-]." + ), + ), help="Save the results for comparison under a specified name.", ) parser.add_argument( diff --git a/devops/scripts/benchmarks/utils/validate.py b/devops/scripts/benchmarks/utils/validate.py index b0a2658865562..40055bdfd2a80 100644 --- a/devops/scripts/benchmarks/utils/validate.py +++ b/devops/scripts/benchmarks/utils/validate.py @@ -26,7 +26,17 @@ def runner_name(runner_name: str, throw: Exception = None): """ Returns True if runner_name is clean (no illegal characters). """ - return validate_on_re(runner_name, r"^[a-zA-Z0-9_]+$", throw=throw) + return validate_on_re(runner_name.strip(), r"^[a-zA-Z0-9_]+$", throw=throw) + + @staticmethod + def save_name(save: str, throw: Exception = None): + """ + Returns True if save is within [a-zA-Z0-9_-]. + + If throw argument is specified: return save as is if save satisfies + aforementioned regex, otherwise raise error defined by throw. + """ + return validate_on_re(save.strip(), r"^[a-zA-Z0-9_-]+$", throw=throw) @staticmethod def timestamp(t: str, throw: Exception = None): @@ -37,7 +47,7 @@ def timestamp(t: str, throw: Exception = None): format, otherwise raise error defined by throw. """ return validate_on_re( - t, + t.strip(), r"^\d{4}(0[1-9]|1[0-2])([0-2][0-9]|3[01])_([01][0-9]|2[0-3])[0-5][0-9][0-5][0-9]$", throw=throw, ) @@ -51,7 +61,7 @@ def github_repo(repo: str, throw: Exception = None): aforementioned format, otherwise raise error defined by throw. """ return validate_on_re( - re.sub(r"^https?://github.com/", "", repo), + re.sub(r"^https?://github.com/", "", repo.strip()), r"^[a-zA-Z0-9_-]{1,39}/[a-zA-Z0-9_.-]{1,100}$", throw=throw, ) @@ -67,6 +77,6 @@ def commit_hash(commit: str, throw: Exception = None, trunc: int = 40): """ commit_re = r"^[a-f0-9]{7,40}$" if throw is None: - return validate_on_re(commit, commit_re) + return validate_on_re(commit.strip(), commit_re) else: - return validate_on_re(commit, commit_re, throw=throw)[:trunc] + return validate_on_re(commit.strip(), commit_re, throw=throw)[:trunc] From 3afb4e9cbed8005807aa4ea022b773e3b11dddf5 Mon Sep 17 00:00:00 2001 From: "Li, Ian" Date: Thu, 17 Apr 2025 10:00:28 -0700 Subject: [PATCH 2/2] Resolve stupid way of stripping input --- devops/scripts/benchmarks/utils/validate.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/devops/scripts/benchmarks/utils/validate.py b/devops/scripts/benchmarks/utils/validate.py index 40055bdfd2a80..afdd914eecf63 100644 --- a/devops/scripts/benchmarks/utils/validate.py +++ b/devops/scripts/benchmarks/utils/validate.py @@ -8,7 +8,7 @@ def validate_on_re(val: str, regex: re.Pattern, throw: Exception = None): If `throw` argument is not None: return val as-is if val matches regex, otherwise raise error defined by throw. """ - is_matching: bool = re.compile(regex).match(val) is not None + is_matching: bool = re.compile(regex).match(val.strip()) is not None if throw is None: return is_matching @@ -26,7 +26,7 @@ def runner_name(runner_name: str, throw: Exception = None): """ Returns True if runner_name is clean (no illegal characters). """ - return validate_on_re(runner_name.strip(), r"^[a-zA-Z0-9_]+$", throw=throw) + return validate_on_re(runner_name, r"^[a-zA-Z0-9_]+$", throw=throw) @staticmethod def save_name(save: str, throw: Exception = None): @@ -36,7 +36,7 @@ def save_name(save: str, throw: Exception = None): If throw argument is specified: return save as is if save satisfies aforementioned regex, otherwise raise error defined by throw. """ - return validate_on_re(save.strip(), r"^[a-zA-Z0-9_-]+$", throw=throw) + return validate_on_re(save, r"^[a-zA-Z0-9_-]+$", throw=throw) @staticmethod def timestamp(t: str, throw: Exception = None): @@ -47,7 +47,7 @@ def timestamp(t: str, throw: Exception = None): format, otherwise raise error defined by throw. """ return validate_on_re( - t.strip(), + t, r"^\d{4}(0[1-9]|1[0-2])([0-2][0-9]|3[01])_([01][0-9]|2[0-3])[0-5][0-9][0-5][0-9]$", throw=throw, ) @@ -61,7 +61,7 @@ def github_repo(repo: str, throw: Exception = None): aforementioned format, otherwise raise error defined by throw. """ return validate_on_re( - re.sub(r"^https?://github.com/", "", repo.strip()), + re.sub(r"^https?://github.com/", "", repo), r"^[a-zA-Z0-9_-]{1,39}/[a-zA-Z0-9_.-]{1,100}$", throw=throw, ) @@ -77,6 +77,6 @@ def commit_hash(commit: str, throw: Exception = None, trunc: int = 40): """ commit_re = r"^[a-f0-9]{7,40}$" if throw is None: - return validate_on_re(commit.strip(), commit_re) + return validate_on_re(commit, commit_re) else: - return validate_on_re(commit.strip(), commit_re, throw=throw)[:trunc] + return validate_on_re(commit, commit_re, throw=throw)[:trunc]