Skip to content

Reject non-ASCII digits and trailing newlines in version parsing - #478

Merged
tomschr merged 1 commit into
python-semver:masterfrom
Str0k:fix/strict-semver-character-validation
Sep 12, 2026
Merged

tomschr merged 1 commit into
python-semver:masterfrom
Str0k:fix/strict-semver-character-validation

Conversation

@Str0k

@Str0k Str0k commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Version.parse() currently accepts characters outside the SemVer grammar. For example, Version.is_valid('1.2.3\n') is true, and Version.parse('1\u0662.2.3') silently converts a mixed ASCII/Arabic-Indic major component to 12.2.3. Non-ASCII digits can also survive in prerelease identifiers.

The shared regex uses Python's Unicode-aware \d and $, which may match before a final newline. Compile both strict and optional-component patterns with re.ASCII and use \Z to require the actual end of the input. These constraints follow the ASCII digit grammar and prerelease requirements in https://semver.org/#backusnaur-form-grammar-for-valid-semver-versions.

This intentionally rejects previously accepted malformed input; callers wanting whitespace tolerance can strip it before parsing. No exploit or CVE is claimed.

Validation, Windows / Python 3.12:

  • All 23 new regression cases fail on the original code.
  • Without the optional native backend: 385 passed, 1 skipped; 99.1% coverage.
  • With fast-semver-rs-backend 0.1.2: 386 passed; 99.7% coverage, including the fallback after native rejection.
  • Ruff passes on the new test file. The changed implementation file has the same 43 existing Ruff diagnostics as base 0f18aa0; mypy has the same existing missing-backend-stub diagnostic on both revisions.
  • The two documentation symlinks under tests/ were temporarily materialized as copies for Windows testing, then restored. They are not part of the diff.

AI assistance: Codex helped implement and test this change. The reproduction and verification results are supplied for maintainer review.

@Str0k
Str0k force-pushed the fix/strict-semver-character-validation branch from a3a30d5 to 188383b Compare September 12, 2026 03:42
@tomschr

tomschr commented Sep 12, 2026

Copy link
Copy Markdown
Member

Thank you very much @Str0k for your contribution! This is awesome! 🎉 😍

Many thanks!

@tomschr
tomschr merged commit 1e87579 into python-semver:master Sep 12, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants