Problem
Magnet._INFOHASH_REGEX at torf/_magnet.py:52 has an unwrapped alternation, so the $ only anchors the second branch. The first branch (^[0-9a-f]{40}) matches even when trailing garbage follows, and Magnet.infohash setter (:125) and xt setter (:104) both accept the invalid value.
Steps
from torf import Magnet
bad = 'a' * 40 + 'GARBAGE_HERE!!!'
m = Magnet(xt=bad)
print(repr(m.infohash), len(m.infohash))
Expected
MagnetError: Invalid exact topic ("xt") (same as _XT_REGEX, which correctly wraps its alternation).
Actual
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaGARBAGE_HERE!!!' 55
Fix
r'^([0-9a-f]{40}|[a-z2-7]{32})$' (mirror _XT_REGEX at :53).
Environment
- torf 4.3.1 (master @ 2026-05-01)
- Python 3.9-3.14 per
pyproject.toml
Thanks for maintaining rndusr/torf!
Problem
Magnet._INFOHASH_REGEXattorf/_magnet.py:52has an unwrapped alternation, so the$only anchors the second branch. The first branch (^[0-9a-f]{40}) matches even when trailing garbage follows, andMagnet.infohashsetter (:125) andxtsetter (:104) both accept the invalid value.Steps
Expected
MagnetError: Invalid exact topic ("xt")(same as_XT_REGEX, which correctly wraps its alternation).Actual
Fix
r'^([0-9a-f]{40}|[a-z2-7]{32})$'(mirror_XT_REGEXat:53).Environment
pyproject.tomlThanks for maintaining rndusr/torf!