Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast parsed.private from a boolean to an integer before bencoding it. #63

Merged
merged 2 commits into from
May 25, 2018

Conversation

gruns
Copy link
Contributor

@gruns gruns commented May 24, 2018

This PR fixes a bencode warning emitted in encodeTorrentFile() when fed
private torrents created by https://github.com/webtorrent/create-torrent.

WARNING: Possible data corruption detected with value true: Bencoding only
defines support for integers, value was converted to 1.

Sample code that produces the warning:

#!/usr/bin/env node

const WebTorrent = require('webtorrent')
const parseTorrent = require('parse-torrent')
const createTorrent = require('create-torrent')

const filename = 'macho.jpg'
const opts = {
    private: true,
    name: filename,
    announceList: [[]],
}

createTorrent(`./${filename}`, opts, function (err, torrentBuf) {
    const parsedTorrent = parseTorrent(torrentBuf)

    // WARNING: Possible data corruption detected with value "true": Bencoding
    // only defines support for integers, value was converted to "1"
    parseTorrent.toTorrentFile(parsedTorrent)

    // The same warning is also emitted, implicitly, via WebTorrent.add().
    const client = new WebTorrent()
    client.add(torrentBuf, function (torrent) {
        process.exit(0)
    })
})

Which, when run, emits

WARNING: Possible data corruption detected with value "true": Bencoding only defines support for integers, value was converted to "1"
Trace
    at Function.encode.number (/mnt/fett/code/js/testing/node_modules/bencode/lib/encode.js:79:13)
    at Function.encode._encode (/mnt/fett/code/js/testing/node_modules/bencode/lib/encode.js:47:28)
    at Function.encode.dict (/mnt/fett/code/js/testing/node_modules/bencode/lib/encode.js:96:12)
    at Function.encode._encode (/mnt/fett/code/js/testing/node_modules/bencode/lib/encode.js:43:27)
    at Object.encode (/mnt/fett/code/js/testing/node_modules/bencode/lib/encode.js:13:10)
    at Function.encodeTorrentFile [as toTorrentFile] (/mnt/fett/code/js/testing/node_modules/parse-torrent/index.js:227:18)
    at Torrent._processParsedTorrent (/mnt/fett/code/js/testing/node_modules/webtorrent/lib/torrent.js:304:35)
    at Torrent._onParsedTorrent (/mnt/fett/code/js/testing/node_modules/webtorrent/lib/torrent.js:249:8)
    at /mnt/fett/code/js/testing/node_modules/webtorrent/lib/torrent.js:232:12
    at _combinedTickCallback (internal/process/next_tick.js:131:7)

This PR fixes that 🙂.

… The bencode encoding can't encode booleans. This fixes a bencode warning emitted in encodeTorrentFile() on encode: 'WARNING: Possible data corruption detected with value true: Bencoding only defines support for integers, value was converted to 1'.
index.js Outdated
// Cast boolean to integer 1 or 0; BitTorrent's bencoding can't encode
// booleans.
var privateAsInteger = parsed.private & 1
torrent['private'] = privateAsInteger
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to cast the boolean with torrent['private'] = Number(parsed.private) which I think is clearer.

Copy link
Member

@feross feross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and made the change, so this LGTM now.

@feross feross merged commit b675440 into webtorrent:master May 25, 2018
@feross
Copy link
Member

feross commented May 25, 2018

Released as 6.0.1

@gruns
Copy link
Contributor Author

gruns commented May 25, 2018

Even better 👍.

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.

None yet

2 participants