Skip to content

Conversation

@leoherzog
Copy link

Fix for Issue #98.

In magnetURIDecode, the xt parameter is stored as-is in the result object:

result.xt = 'urn:btih:2AA4F5A7E209E54B32803D43670971C4C8CAAA05'

The function then extracts the info hash and converts it to lowercase:

if ((m = xt.match(/^urn:btih:(.{40})/))) {
  result.infoHash = m[1].toLowerCase()  // '2aa4f5a7e209e54b32803d43670971c4c8caaa05'
}

So the decoded object has both:

xt: 'urn:btih:2AA4F5A7E209E54B32803D43670971C4C8CAAA05' (original, uppercase)
infoHash: '2aa4f5a7e209e54b32803d43670971c4c8caaa05' (normalized, lowercase)

Then, in magnetURIEncode:

let xts = new Set()
if (obj.xt && typeof obj.xt === 'string') xts.add(obj.xt)  // Adds uppercase version
// ...
if (obj.infoHash) xts.add(`urn:btih:${obj.infoHash}`)      // Adds lowercase version

Since the Set treats these as different strings (case-sensitive), both get added, resulting in duplicate xt parameters.

The issue should be fixed in the decode function by normalizing the xt parameter when it contains a BitTorrent info hash.

This fix ensures that:

  1. All BitTorrent v1 hex info hashes (40 chars) are normalized to lowercase
  2. All BitTorrent v1 base32 info hashes (32 chars) are normalized to uppercase (following base32 convention)
  3. All BitTorrent v2 hex info hashes (64 chars) are normalized to lowercase
  4. The normalization happens during decode, preventing duplicate xt parameters in the encoded output
  5. Other xt URN types are left unchanged

@leoherzog
Copy link
Author

@ThaUnknown Any thoughts?

@leoherzog
Copy link
Author

@ThaUnknown thoughts?

@ThaUnknown
Copy link
Member

ping me again and I'll close and lock this, I don't have time nor interest to look at this as of now

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