Skip to content

Magnet._INFOHASH_REGEX accepts trailing garbage (missing alternation group) #64

Description

@chirag127

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions