nt 0.7.2 → 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/lib/hasher.js CHANGED
@@ -223,7 +223,13 @@ module.exports = class Hasher extends Stream {
223
223
  let pieceIndex = task.piece;
224
224
 
225
225
  // Open file for reading.
226
- 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
+ }
227
233
  const rs = fs.createReadStream(file);
228
234
  this.streamGroup.add(rs);
229
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
  *
@@ -78,7 +94,7 @@ const checkCreationDate = (date) => {
78
94
  * in multi file mode.
79
95
  * @return {!string} Possible error
80
96
  */
81
- const md5sumRegexp = /[a-f0-9]{32}/i;
97
+ const md5sumRegexp = /^[a-f0-9]{32}$/i;
82
98
  const checkmd5sum = (parent) => {
83
99
  if (parent.md5sum != null) {
84
100
  if (!Buffer.isBuffer(parent.md5sum)) return '`md5sum` is not a buffer';
@@ -120,7 +136,7 @@ const buf2str = (torrent, path) => {
120
136
  const checkTorrent = exports.checkTorrent = (torrent) => {
121
137
  let err;
122
138
 
123
- if (typeof torrent !== 'object') return 'Torrent is not hash';
139
+ if (typeof torrent !== 'object' || torrent === null) return 'Torrent is not a hash';
124
140
  if ((err = checkAnnounce(torrent, 'announce'))) return err;
125
141
  if ((err = checkAnnounceList(torrent['announce-list']))) return err;
126
142
  if ((err = checkCreationDate(torrent['creation date']))) return err;
@@ -143,10 +159,14 @@ const checkTorrent = exports.checkTorrent = (torrent) => {
143
159
  if ((err = checkmd5sum(file))) return err;
144
160
  if (!file.path) return '`path` field not found in file';
145
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';
146
163
 
147
- if (!file.path.some(Buffer.isBuffer)) {
164
+ if (!file.path.every(Buffer.isBuffer)) {
148
165
  return '`path` is not a list of buffers in file';
149
166
  }
167
+ if (!file.path.every(isValidPathSegment)) {
168
+ return '`path` contains invalid path segments in file';
169
+ }
150
170
  }
151
171
  if (torrent.info.length != null) {
152
172
  return 'Cannot have `info.length` in multi file mode';
@@ -160,20 +180,27 @@ const checkTorrent = exports.checkTorrent = (torrent) => {
160
180
  if (torrent.info.length == null) {
161
181
  return '`info.length` not found in single file mode';
162
182
  }
163
- if (!util.isInteger(torrent.info.length || torrent.info.length < 0)) {
183
+ if (!util.isInteger(torrent.info.length) || torrent.info.length < 0) {
164
184
  return '`info.length` is not a positive integer in file';
165
185
  }
166
186
  if ((err = checkmd5sum(torrent.info))) return err;
167
187
  }
168
188
 
189
+ if (torrent.info.name == null) return '`info.name` not found';
169
190
  if ((err = checkBuffer(torrent.info, 'name'))) return err;
191
+ if (!isValidPathSegment(torrent.info.name)) {
192
+ return '`name` contains invalid characters';
193
+ }
170
194
  if (torrent.info['piece length'] == null) {
171
195
  return '`info.piece length` not found';
172
196
  }
173
- if (!util.isInteger(torrent.info['piece length']) ||
174
- torrent.info['piece length'] < 0) {
197
+ const pieceLength = torrent.info['piece length'];
198
+ if (!util.isInteger(pieceLength) || pieceLength <= 0) {
175
199
  return '`info.piece length` is not a positive integer';
176
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
+ }
177
204
  if (torrent.info.pieces == null) return '`info.pieces` not found';
178
205
  if (!Buffer.isBuffer(torrent.info.pieces)) {
179
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.2",
9
+ "version": "0.7.3",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "git://github.com/fent/node-torrent.git"