nt 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  Read, make, and hash check torrents with node.js!
4
4
 
5
- [![Build Status](https://secure.travis-ci.org/fent/node-torrent.svg)](http://travis-ci.org/fent/node-torrent)
6
- [![Dependency Status](https://david-dm.org/fent/node-torrent.svg)](https://david-dm.org/fent/node-torrent)
5
+ [![npm](https://img.shields.io/npm/v/nt)](https://www.npmjs.com/package/nt)
6
+ [![Node.js CI](https://github.com/fent/node-torrent/actions/workflows/nodejs.yml/badge.svg)](https://github.com/fent/node-torrent/actions/workflows/nodejs.yml)
7
7
  [![codecov](https://codecov.io/gh/fent/node-torrent/branch/master/graph/badge.svg)](https://codecov.io/gh/fent/node-torrent)
8
8
 
9
9
  # Usage
@@ -202,7 +202,7 @@ Hash checking is finished.
202
202
 
203
203
 
204
204
  # Tests
205
- Tests are written with [vows](http://vowsjs.org/)
205
+ Tests are written with [mocha](https://mochajs.org/) and [chai](https://www.chaijs.com/)
206
206
 
207
207
  ```bash
208
208
  npm test
package/lib/hasher.js CHANGED
@@ -3,7 +3,6 @@ const path = require('path');
3
3
  const crypto = require('crypto');
4
4
  const Stream = require('stream').Stream;
5
5
  const parallel = require('async').parallel;
6
- const _ = require('underscore');
7
6
  const StreamSpeed = require('streamspeed');
8
7
 
9
8
  const queue = require('./queue');
@@ -45,11 +44,17 @@ const round = (num, dec) => {
45
44
  * Allows arrays of objects with a `path` property to be
46
45
  * sorted alphabetically
47
46
  *
48
- * @param {Object} obj
47
+ * @param {Array.<Object>} a
48
+ * @param {Array.<Object>} b
49
49
  * @return {string}
50
50
  */
51
- const sortByPath = (obj) => {
52
- return obj.path.join();
51
+ const sortByPath = (a, b) => {
52
+ const len = Math.min(a.path.length, b.path.length);
53
+ for (let i = 0; i < len; i++) {
54
+ if (a.path[i] < b.path[i]) return -1;
55
+ if (a.path[i] > b.path[i]) return 1;
56
+ }
57
+ return a.path.length - b.path.length;
53
58
  };
54
59
 
55
60
 
@@ -95,9 +100,18 @@ module.exports = class Hasher extends Stream {
95
100
  return this.emit('error', err);
96
101
  }
97
102
 
103
+ const fromDisk = typeof list[0] === 'string';
104
+
98
105
  const readTasks = (err) => {
99
- // Sort files alphabetically.
100
- this.files = _.sortBy(this.files, sortByPath);
106
+ // Create shallow copy of the files array.
107
+ this.files = Array.from(this.files);
108
+
109
+ // When creating a torrent from files on disk, sort them alphabetically.
110
+ // File lists from an existing torrent must keep their original order,
111
+ // since the piece hashes depend on it, so they are not sorted.
112
+ if (fromDisk) {
113
+ this.files.sort(sortByPath);
114
+ }
101
115
 
102
116
  if (err) return this.emit('error', err);
103
117
 
@@ -113,7 +127,7 @@ module.exports = class Hasher extends Stream {
113
127
  };
114
128
 
115
129
  // Check if list containts objects or strings.
116
- if (typeof list[0] === 'string') {
130
+ if (fromDisk) {
117
131
 
118
132
  // Create queue to handle having many files opened.
119
133
  const statqueue = queue(fs.stat, this.maxFiles);
@@ -144,9 +158,9 @@ module.exports = class Hasher extends Stream {
144
158
  this.filetasks.push({
145
159
  length : task.length,
146
160
  path : task.path,
147
- // Piece this task will write to.
161
+ // Piece this task will read from or write to.
148
162
  piece,
149
- // Piece buffer offset to start writing at.
163
+ // Piece buffer offset to start reading or writing at.
150
164
  offset,
151
165
  });
152
166
 
@@ -209,7 +223,13 @@ module.exports = class Hasher extends Stream {
209
223
  let pieceIndex = task.piece;
210
224
 
211
225
  // Open file for reading.
212
- const file = path.join(this.dir, ...task.path);
226
+ const baseDir = path.resolve(this.dir);
227
+ const file = path.resolve(baseDir, ...task.path);
228
+ const relative = path.relative(baseDir, file);
229
+ if (relative.startsWith('..') || path.isAbsolute(relative)) {
230
+ this.emit('error', new Error(`Path traversal detected: ${file}`));
231
+ return callback();
232
+ }
213
233
  const rs = fs.createReadStream(file);
214
234
  this.streamGroup.add(rs);
215
235
 
package/lib/schema.js CHANGED
@@ -15,6 +15,22 @@ const checkBuffer = (torrent, field) => {
15
15
  };
16
16
 
17
17
 
18
+ /**
19
+ * Checks that a path segment is safe and does not traverse directories.
20
+ *
21
+ * @param {*} buf
22
+ * @return {boolean}
23
+ */
24
+ const isValidPathSegment = exports.isValidPathSegment = (buf) => {
25
+ if (!Buffer.isBuffer(buf) || buf.length === 0) return false;
26
+ const str = buf.toString('utf8');
27
+ if (str === '.' || str === '..') return false;
28
+ if (str.includes('/') || str.includes('\\') || str.includes('\0')) return false;
29
+ if (/^[a-zA-Z]:/.test(str)) return false;
30
+ return true;
31
+ };
32
+
33
+
18
34
  /**
19
35
  * Checks announce fields are correct.
20
36
  *
@@ -23,11 +39,10 @@ const checkBuffer = (torrent, field) => {
23
39
  */
24
40
  const checkAnnounce = (torrent) => {
25
41
  let err;
26
- if (!torrent.announce && !torrent['announce-list']) {
27
- return '`announce` and `announce-list` fields not found';
42
+ if (torrent.announce) {
43
+ if ((err = checkBuffer(torrent, 'announce'))) return err;
44
+ if (!util.isURL(torrent.announce)) return '`announce` is not a URL';
28
45
  }
29
- if ((err = checkBuffer(torrent, 'announce'))) return err;
30
- if (!util.isURL(torrent.announce)) return '`announce` is not a URL';
31
46
  return null;
32
47
  };
33
48
 
@@ -79,7 +94,7 @@ const checkCreationDate = (date) => {
79
94
  * in multi file mode.
80
95
  * @return {!string} Possible error
81
96
  */
82
- const md5sumRegexp = /[a-f0-9]{32}/i;
97
+ const md5sumRegexp = /^[a-f0-9]{32}$/i;
83
98
  const checkmd5sum = (parent) => {
84
99
  if (parent.md5sum != null) {
85
100
  if (!Buffer.isBuffer(parent.md5sum)) return '`md5sum` is not a buffer';
@@ -121,7 +136,7 @@ const buf2str = (torrent, path) => {
121
136
  const checkTorrent = exports.checkTorrent = (torrent) => {
122
137
  let err;
123
138
 
124
- if (typeof torrent !== 'object') return 'Torrent is not hash';
139
+ if (typeof torrent !== 'object' || torrent === null) return 'Torrent is not a hash';
125
140
  if ((err = checkAnnounce(torrent, 'announce'))) return err;
126
141
  if ((err = checkAnnounceList(torrent['announce-list']))) return err;
127
142
  if ((err = checkCreationDate(torrent['creation date']))) return err;
@@ -144,10 +159,14 @@ const checkTorrent = exports.checkTorrent = (torrent) => {
144
159
  if ((err = checkmd5sum(file))) return err;
145
160
  if (!file.path) return '`path` field not found in file';
146
161
  if (!Array.isArray(file.path)) return '`path` is not a list in file';
162
+ if (file.path.length === 0) return '`path` is empty in file';
147
163
 
148
- if (!file.path.some(Buffer.isBuffer)) {
164
+ if (!file.path.every(Buffer.isBuffer)) {
149
165
  return '`path` is not a list of buffers in file';
150
166
  }
167
+ if (!file.path.every(isValidPathSegment)) {
168
+ return '`path` contains invalid path segments in file';
169
+ }
151
170
  }
152
171
  if (torrent.info.length != null) {
153
172
  return 'Cannot have `info.length` in multi file mode';
@@ -161,20 +180,27 @@ const checkTorrent = exports.checkTorrent = (torrent) => {
161
180
  if (torrent.info.length == null) {
162
181
  return '`info.length` not found in single file mode';
163
182
  }
164
- if (!util.isInteger(torrent.info.length || torrent.info.length < 0)) {
183
+ if (!util.isInteger(torrent.info.length) || torrent.info.length < 0) {
165
184
  return '`info.length` is not a positive integer in file';
166
185
  }
167
186
  if ((err = checkmd5sum(torrent.info))) return err;
168
187
  }
169
188
 
189
+ if (torrent.info.name == null) return '`info.name` not found';
170
190
  if ((err = checkBuffer(torrent.info, 'name'))) return err;
191
+ if (!isValidPathSegment(torrent.info.name)) {
192
+ return '`name` contains invalid characters';
193
+ }
171
194
  if (torrent.info['piece length'] == null) {
172
195
  return '`info.piece length` not found';
173
196
  }
174
- if (!util.isInteger(torrent.info['piece length']) ||
175
- torrent.info['piece length'] < 0) {
197
+ const pieceLength = torrent.info['piece length'];
198
+ if (!util.isInteger(pieceLength) || pieceLength <= 0) {
176
199
  return '`info.piece length` is not a positive integer';
177
200
  }
201
+ if (pieceLength < 16384 || (pieceLength & (pieceLength - 1)) !== 0) {
202
+ return '`info.piece length` must be a power of 2 and at least 16384';
203
+ }
178
204
  if (torrent.info.pieces == null) return '`info.pieces` not found';
179
205
  if (!Buffer.isBuffer(torrent.info.pieces)) {
180
206
  return '`info.pieces` is not a buffer';
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "bittorremt",
7
7
  "file"
8
8
  ],
9
- "version": "0.7.1",
9
+ "version": "0.7.3",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "git://github.com/fent/node-torrent.git"
@@ -21,7 +21,7 @@
21
21
  "lib": "./lib"
22
22
  },
23
23
  "scripts": {
24
- "test": "istanbul cover vows -- --spec test/*-test.js"
24
+ "test": "nyc mocha test/*-test.js"
25
25
  },
26
26
  "dependencies": {
27
27
  "async": "^3.2.0",
@@ -29,12 +29,12 @@
29
29
  "buffers": "~0.1.1",
30
30
  "memorystream": "~0.3.1",
31
31
  "ordered-emitter": "^1.0.0",
32
- "streamspeed": "~2.0.0",
33
- "underscore": "^1.10.2"
32
+ "streamspeed": "~2.0.0"
34
33
  },
35
34
  "devDependencies": {
36
- "istanbul": "^0.4.5",
37
- "vows": "^0.8.2"
35
+ "chai": "6.2.1",
36
+ "mocha": "12.0.3",
37
+ "nyc": "18.0.0"
38
38
  },
39
39
  "engines": {
40
40
  "node": ">=6"