diff options
| author | kirigayakazima <46666711+kirigayakazima@users.noreply.github.com> | 2026-11-26 22:01:17 +0800 |
|---|---|---|
| committer | kirigayakazima <46666711+kirigayakazima@users.noreply.github.com> | 2026-11-26 22:01:17 +0800 |
| commit | d6bb80df42a2ebf3cf827ac5381b68df38aec20c (patch) | |
| tree | 3e76b843169a0b97749ad90deb26ce9b6b7901e7 /scripts | |
| parent | 73516a2902a9872e4d21d1bdbbde4e26059497c5 (diff) | |
merge_check: guard ref option injection, bounds, and git timeouts
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/merge_check.py | 47 |
1 files changed, 40 insertions, 7 deletions
diff --git a/scripts/merge_check.py b/scripts/merge_check.py index e270cbe00..77cb7a917 100644 --- a/scripts/merge_check.py +++ b/scripts/merge_check.py | |||
| @@ -35,15 +35,18 @@ REVERT_SUBJECT = re.compile(r"^Revert\b", re.IGNORECASE) | |||
| 35 | # Where a doubled block is the repeated-merge damage. In code, three equal lines in two functions are ordinary, so a | 35 | # Where a doubled block is the repeated-merge damage. In code, three equal lines in two functions are ordinary, so a |
| 36 | # block there is listed but does not change the verdict. | 36 | # block there is listed but does not change the verdict. |
| 37 | TEXT_SUFFIXES = (".md", ".txt", ".rst") | 37 | TEXT_SUFFIXES = (".md", ".txt", ".rst") |
| 38 | GIT_TIMEOUT = 120 | ||
| 38 | 39 | ||
| 39 | 40 | ||
| 40 | class GitError(Exception): | 41 | class GitError(Exception): |
| 41 | pass | 42 | pass |
| 42 | 43 | ||
| 43 | 44 | ||
| 44 | def git(repo, *args, check=True): | 45 | def git(repo, *args, check=True, timeout=GIT_TIMEOUT): |
| 45 | try: | 46 | try: |
| 46 | r = subprocess.run(["git", "-C", repo, *args], capture_output=True) | 47 | r = subprocess.run(["git", "-C", repo, *args], capture_output=True, timeout=timeout) |
| 48 | except subprocess.TimeoutExpired: | ||
| 49 | raise GitError(f"git {' '.join(args)}: timed out after {timeout}s") from None | ||
| 47 | except OSError as e: # git is not on PATH, or cannot be executed: a usage error (exit 2), not a traceback | 50 | except OSError as e: # git is not on PATH, or cannot be executed: a usage error (exit 2), not a traceback |
| 48 | raise GitError(f"cannot run git: {e.strerror or e}") from None | 51 | raise GitError(f"cannot run git: {e.strerror or e}") from None |
| 49 | # Decode without universal-newline translation: a bare CR inside a Git line is content. | 52 | # Decode without universal-newline translation: a bare CR inside a Git line is content. |
| @@ -177,20 +180,50 @@ def check(repo, main, number, ref, min_length, merges): | |||
| 177 | "duplicate_samples": {p: v[:3] for p, v in dups.items()}} | 180 | "duplicate_samples": {p: v[:3] for p, v in dups.items()}} |
| 178 | 181 | ||
| 179 | 182 | ||
| 183 | def _positive_int(text): | ||
| 184 | try: | ||
| 185 | value = int(text) | ||
| 186 | except ValueError: | ||
| 187 | raise argparse.ArgumentTypeError(f"expected a positive integer, got {text!r}") from None | ||
| 188 | if value < 1: | ||
| 189 | raise argparse.ArgumentTypeError(f"must be at least 1, got {text!r}") | ||
| 190 | return value | ||
| 191 | |||
| 192 | |||
| 193 | def _nonnegative_int(text): | ||
| 194 | try: | ||
| 195 | value = int(text) | ||
| 196 | except ValueError: | ||
| 197 | raise argparse.ArgumentTypeError(f"expected a non-negative integer, got {text!r}") from None | ||
| 198 | if value < 0: | ||
| 199 | raise argparse.ArgumentTypeError(f"must be non-negative, got {text!r}") | ||
| 200 | return value | ||
| 201 | |||
| 202 | |||
| 203 | def _validate_ref(ref, label): | ||
| 204 | if not ref or ref != ref.strip() or ref.startswith("-"): | ||
| 205 | raise argparse.ArgumentTypeError(f"invalid {label} ref: {ref!r}") | ||
| 206 | return ref | ||
| 207 | |||
| 208 | |||
| 180 | def parse_pair(text): | 209 | def parse_pair(text): |
| 181 | number, sep, ref = text.partition("=") | 210 | number, sep, ref = text.partition("=") |
| 182 | if not sep or not number.lstrip("#").isdigit() or not ref: | 211 | raw_num = number[1:] if number.startswith("#") else number |
| 212 | if not sep or not raw_num.isascii() or not raw_num.isdigit() or int(raw_num) < 1: | ||
| 183 | raise argparse.ArgumentTypeError(f"expected PR=REF, got {text!r}") | 213 | raise argparse.ArgumentTypeError(f"expected PR=REF, got {text!r}") |
| 184 | return int(number.lstrip("#")), ref | 214 | return int(raw_num), _validate_ref(ref, "PR target") |
| 185 | 215 | ||
| 186 | 216 | ||
| 187 | def main(argv=None): | 217 | def main(argv=None): |
| 188 | ap = argparse.ArgumentParser(description=__doc__.strip().splitlines()[0]) | 218 | ap = argparse.ArgumentParser(description=__doc__.strip().splitlines()[0]) |
| 189 | ap.add_argument("pairs", nargs="+", type=parse_pair, metavar="PR=REF") | 219 | ap.add_argument("pairs", nargs="+", type=parse_pair, metavar="PR=REF") |
| 190 | ap.add_argument("--main", default="origin/main", help="the branch the round merges into (default origin/main)") | 220 | ap.add_argument("--main", default="origin/main", type=lambda s: _validate_ref(s, "--main"), |
| 221 | help="the branch the round merges into (default origin/main)") | ||
| 191 | ap.add_argument("--repo", default=".", help="repository folder (default: the current one)") | 222 | ap.add_argument("--repo", default=".", help="repository folder (default: the current one)") |
| 192 | ap.add_argument("--min-length", type=int, default=40, help="shortest stripped line counted as a duplicate (40)") | 223 | ap.add_argument("--min-length", type=_nonnegative_int, default=40, |
| 193 | ap.add_argument("--depth", type=int, default=5000, help="first-parent commits searched for earlier merges (5000)") | 224 | help="shortest stripped line counted as a duplicate (40)") |
| 225 | ap.add_argument("--depth", type=_positive_int, default=5000, | ||
| 226 | help="first-parent commits searched for earlier merges (5000)") | ||
| 194 | ap.add_argument("--json", action="store_true", help="one JSON report instead of text") | 227 | ap.add_argument("--json", action="store_true", help="one JSON report instead of text") |
| 195 | args = ap.parse_args(argv) | 228 | args = ap.parse_args(argv) |
| 196 | try: | 229 | try: |