GitHub advisory · PyPI
python-semver: Super-quadratic ReDoS in _LAST_NUMBER via attacker-controlled version strings
Description
python-semver/python-semver is the Python Semantic Versioning library published on PyPI as semver. At src/semver/version.py:95 it defines:
_LAST_NUMBER = re.compile(r"(?:[^\d]*(\d+)[^\d]*)+")
The pattern is used by bump_build() to find and increment the last numeric component of a version string's build metadata. Its outer repeated group (?:[^\d]*(\d+)[^\d]*)+ contains two unbounded [^\d]* quantifiers plus \d+, and adjacent repetitions can consume the same run of non-digit characters. When the build metadata is a long alphabetic string with no digits, the engine must try an enormous number of ways to divide that run among the quantifiers before the overall match fails, producing super-quadratic catastrophic backtracking, a CWE-1333 (Inefficient Regular Expression Complexity / ReDoS) condition.
The vulnerability is reachable end to end through the public API semver.Version.parse("1.0.0+" + "a" * N).bump_build(): the library's BUILD_IDENTIFIER rule is [0-9a-zA-Z-]+, so an arbitrarily long all-letter build identifier parses successfully and is then passed to the vulnerable search. The untrusted input surface is any version string supplied by an external party: package manifests and dependency constraints, version validation in CI and release pipelines, registry metadata, SBOM imports, update-check services, and multi-tenant version-policy validation. A roughly 32 KB build identifier blocks the parsing process for about 15 seconds, and longer values reach minute scale. Python's regular-expression engine cannot be interrupted by a timeout during that work.
The ambiguity inside _LAST_NUMBER is structural: the trailing [^\d]* of one iteration and the leading [^\d]* of the next iteration can consume the same characters, and there is no separator that forces a unique division. With no digit anywhere in the input, the engine keeps generating new candidate partitions for the outer +, each of which must then fail at the…
Manual fix · PyPI
pip install --upgrade "semver>=3.1.3"Sources