From 366e0f8635b5efae6026444de96d41474d1177c0 Mon Sep 17 00:00:00 2001 From: georgiedekker Date: Sun, 15 Nov 2026 09:32:38 +0000 Subject: [PATCH] fix: replace pytest count regexes with a linear scanner py/polynomial-redos (alerts 1-3): the (\d+) patterns re-searched untrusted pytest output, rescanning a digit run at every start position. Scan once with str.find and a backwards digit walk; parity-tested against the old regexes plus an adversarial 500k-digit timing budget. Co-Authored-By: Claude Code --- src/test_service.py | 40 ++++++++++++++++---- tests/unit/test_test_service.py | 65 +++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 7 deletions(-) diff --git a/src/test_service.py b/src/test_service.py index 4233d5b..387f549 100644 --- a/src/test_service.py +++ b/src/test_service.py @@ -7,7 +7,6 @@ import logging import os import py_compile -import re import sys import time from datetime import UTC, datetime @@ -96,17 +95,44 @@ class TestResponse(BaseModel): # 0 all passed · 1 tests failed · 2 interrupted · 3 internal error # 4 usage error · 5 NO TESTS COLLECTED _PYTEST_NO_TESTS_COLLECTED = 5 -_PYTEST_PASSED = re.compile(r"(\d+) passed") -_PYTEST_FAILED = re.compile(r"(\d+) failed") -_PYTEST_ERRORS = re.compile(r"(\d+) errors?") + + +def _count_before(output: str, suffix: str) -> int: + """First integer immediately preceding ``suffix`` in ``output``, else 0. + + Linear-time equivalent of ``re.search(r"(\\d+)" + suffix, output)``, which + CodeQL flagged as py/polynomial-redos: at every start position the greedy + ``\\d+`` rescans the remainder of a digit run before backtracking, so output + such as ``"9" * n`` with no suffix costs O(n²). This scans each character a + constant number of times. ``str.isdecimal()`` preserves Python's ``\\d`` + semantics (Unicode category Nd). + """ + search_from = 0 + while True: + end = output.find(suffix, search_from) + if end == -1: + return 0 + start = end + while start > 0 and output[start - 1].isdecimal(): + start -= 1 + if start < end: + try: + return int(output[start:end]) + except ValueError: + # More digits than CPython's int() will convert (default limit: + # 4300). The documented contract is a 0 fallback; the old regex + # reached the same int() and let ValueError escape as a 500. + return 0 + search_from = end + 1 def _parse_pytest_counts(output: str) -> tuple[int, int, int]: """Parse (passed, failed, run) from a pytest summary line. Defensive: a count that cannot be parsed falls back to 0 — the verdict still follows the exit code.""" - passed = int(m.group(1)) if (m := _PYTEST_PASSED.search(output)) else 0 - failed = int(m.group(1)) if (m := _PYTEST_FAILED.search(output)) else 0 - errors = int(m.group(1)) if (m := _PYTEST_ERRORS.search(output)) else 0 + passed = _count_before(output, " passed") + failed = _count_before(output, " failed") + # " error" also covers " errors": the old regex's optional ``s`` added no capture. + errors = _count_before(output, " error") failed_total = failed + errors return passed, failed_total, passed + failed_total diff --git a/tests/unit/test_test_service.py b/tests/unit/test_test_service.py index 71f0b89..c5a100d 100644 --- a/tests/unit/test_test_service.py +++ b/tests/unit/test_test_service.py @@ -2,6 +2,9 @@ from __future__ import annotations +import re +import sys +import time from unittest.mock import AsyncMock, MagicMock, patch import pytest @@ -11,6 +14,7 @@ HealthResponse, TestRequest, TestResponse, + _parse_pytest_counts, app, ) @@ -475,3 +479,64 @@ def test_main_block_runs_uvicorn(self): ) finally: _sys.path[:] = original_path + + +# --------------------------------------------------------------------------- +# _parse_pytest_counts — linear-time scanner (py/polynomial-redos) +# --------------------------------------------------------------------------- +class TestParsePytestCounts: + """The scanner replaced three regexes CodeQL flagged as py/polynomial-redos. + These tests pin behavioural parity with the old regexes and a time budget + that the old implementation cannot meet on adversarial digit runs.""" + + LEGACY = ( + re.compile(r"(\d+) passed"), + re.compile(r"(\d+) failed"), + re.compile(r"(\d+) errors?"), + ) + + def _legacy_counts(self, output: str) -> tuple[int, int, int]: + passed = int(m.group(1)) if (m := self.LEGACY[0].search(output)) else 0 + failed = int(m.group(1)) if (m := self.LEGACY[1].search(output)) else 0 + errors = int(m.group(1)) if (m := self.LEGACY[2].search(output)) else 0 + return passed, failed + errors, passed + failed + errors + + PARITY_CORPUS = [ + "", + "no counts here", + "1 passed", + "3 passed, 1 failed, 2 errors", + "1 error", + "2 errors", + "12 passed 4 passed", + "bypassed 7 passed", + "12 passed", + "1errors", + " passed", + "0 passed", + "1 passed, 1 failed, 1 error", + "1 passed\n2 failed\n3 errors in 0.4s", + "5 passed, 1 warning", + "1.5 passed", + "12345678901234567890 passed", + "٣ passed", # \d is Unicode Nd; the scanner must agree + ] + + @pytest.mark.parametrize("output", PARITY_CORPUS) + def test_parity_with_legacy_regexes(self, output): + assert _parse_pytest_counts(output) == self._legacy_counts(output) + + def test_adversarial_digit_run_is_linear(self): + # The old (\d+) scan is O(n²) here: every start position rescans the + # run looking for a suffix that never appears. + started = time.perf_counter() + counts = _parse_pytest_counts("9" * 500_000) + elapsed = time.perf_counter() - started + assert counts == (0, 0, 0) + assert elapsed < 1.0, f"took {elapsed:.3f}s — polynomial backtracking is back" + + def test_count_beyond_int_limit_falls_back_to_zero(self): + limit = sys.get_int_max_str_digits() + if limit == 0: # the interpreter was started with the check disabled + pytest.skip("int string conversion limit is disabled") + assert _parse_pytest_counts("9" * (limit + 1) + " passed") == (0, 0, 0)