diff --git a/dokimi/changelog.d/20261204_120000_napetrov_user-regex-refusal.md b/dokimi/changelog.d/20261204_120000_napetrov_user-regex-refusal.md index 859cd2d50..66e8d7669 100644 --- a/dokimi/changelog.d/20261204_120000_napetrov_user-regex-refusal.md +++ b/dokimi/changelog.d/20261204_120000_napetrov_user-regex-refusal.md @@ -1,3 +1,3 @@ ### Fixed -- **A regex a user writes that Python cannot compile is refused, not a traceback.** `re.compile` raises `OverflowError` for a repeat count that does not fit (`a{999999999999}`) and `RecursionError` for groups nested too deep, not the `re.error` the `process_metrics` and contract loaders caught. Those patterns now refuse as bad input (exit 2, `Error: ...`) instead of crashing: a `process_metrics` counter in the repo manifest and a `trace consistency --contract` pattern. +- **A regex a user writes that Python cannot compile is refused, not a traceback.** `re.compile` raises `OverflowError` for a repeat count that does not fit (`a{999999999999}`) and `RecursionError` for groups nested too deep, not the `re.error` the `process_metrics` and contract loaders caught. A `trace consistency --contract` pattern that triggers either error is refused with exit 2 and `Error: ...`. If a `process_metrics` counter in a repo manifest triggers either error, `eval check` and `repo run` report a manifest error with `✗` and exit 1. diff --git a/dokimi/changelog.d/20261204_130000_napetrov_list-pattern-elements.md b/dokimi/changelog.d/20261204_130000_napetrov_list-pattern-elements.md new file mode 100644 index 000000000..2ef362bfc --- /dev/null +++ b/dokimi/changelog.d/20261204_130000_napetrov_list-pattern-elements.md @@ -0,0 +1,8 @@ + + +### Fixed + +- **A list of regexes in a `trace consistency --contract` action is checked one element at a time.** The list was joined with `|`, each element wrapped in `(?:...)`, and only the joined pattern was compiled, so `["a)|(?:b"]` was accepted (it joins to two groups) though `a)|(?:b` is not a regex. Each element is now compiled on its own, and a bad one is refused by name as `bad regex`. A list whose elements are each a regex compiles to the same pattern as before. The behaviour change: a list in which one element refers to a group of another (`["(a)", "\1"]`, `["(?Pa)", "(?P=x)"]`) is now refused, since that element is not a regex on its own; it was accepted before. The same holds for a list whose elements are each invalid alone but join to a valid pattern (`["(a", "b)"]`): it is now refused. diff --git a/dokimi/dokimi/context/contract.py b/dokimi/dokimi/context/contract.py index 0afde557c..f74e777e7 100644 --- a/dokimi/dokimi/context/contract.py +++ b/dokimi/dokimi/context/contract.py @@ -75,9 +75,18 @@ def _names(value: Any, where: str) -> tuple[str, ...]: def _pattern(value: Any, where: str) -> str: """A regex, or a list of them as one alternation; anything else (a number, a mapping) is refused, - so that `[pytest, tox]` is not read as the character class `['pytest', 'tox']`.""" + so that `[pytest, tox]` is not read as the character class `['pytest', 'tox']`. Each element must be a regex + on its own: joined, `a)|(?:b` would compile as two groups and pass. So a list element cannot refer to a group + of another element (`["(a)", "\\1"]`), though the joined pattern compiled before.""" if isinstance(value, list) and value: - return "|".join(f"(?:{p})" for p in _names(value, where)) + parts = [] + for p in _names(value, where): + try: + compile_user_regex(p) + except ValueError as exc: + raise ValueError(f"{where}: element {p!r}: bad regex ({exc})") from exc + parts.append(f"(?:{p})") + return "|".join(parts) if isinstance(value, str): return value raise ValueError(f"{where} wants a regex or a list of regexes, got {value!r}") diff --git a/dokimi/tests/test_context_consistency.py b/dokimi/tests/test_context_consistency.py index abf9d97e3..ae4b5380e 100644 --- a/dokimi/tests/test_context_consistency.py +++ b/dokimi/tests/test_context_consistency.py @@ -132,6 +132,17 @@ def test_a_pattern_that_is_not_a_regex_is_refused(value): parse_contract({"actions": {"test": {"tool": "Bash", "command": value}}, "rules": []}) +@pytest.mark.parametrize("value", [ + ["a)|(?:b"], [r"\bpytest\b", "a)|(?:b"], ["(", "x"], + ["(a)", "\\1"], ["(?Pa)", "(?P=x)"], # each joins to a pattern that compiled before; alone, neither element does +]) +def test_each_element_of_a_list_pattern_must_be_a_valid_regex_on_its_own(value): + """Joined with `|` and each wrapped in `(?:...)`, `a)|(?:b` compiles; it is not a regex on its own. The + backreference cases are refused on the same rule: `\\1` alone is not a regex.""" + with pytest.raises(ValueError, match="bad regex"): + parse_contract({"actions": {"test": {"tool": "Bash", "command": value}}, "rules": []}) + + @pytest.mark.parametrize("value", ["a{999999999999}", "(" * 2000 + ")" * 2000]) def test_a_regex_that_overflows_or_nests_too_deep_is_refused(value): """re.compile raises OverflowError or RecursionError for these; the contract refuses them as bad input."""