cbor-x 1.6.5 → 1.6.6

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/dist/index.js CHANGED
@@ -234,13 +234,22 @@
234
234
  }
235
235
  }
236
236
 
237
+ function endOfCBORError() {
238
+ let error = new Error('Unexpected end of CBOR data');
239
+ error.incomplete = true;
240
+ return error
241
+ }
242
+
237
243
  function read() {
244
+ // compared as < so that a NaN position (from a corrupt length) also throws instead of reading undefined
245
+ if (!(position$1 < srcEnd)) throw endOfCBORError()
238
246
  let token = src[position$1++];
239
247
  let majorType = token >> 5;
240
248
  token = token & 0x1f;
241
249
  if (token > 0x17) {
242
250
  switch (token) {
243
251
  case 0x18:
252
+ if (position$1 >= srcEnd) throw endOfCBORError()
244
253
  token = src[position$1++];
245
254
  break
246
255
  case 0x19:
@@ -369,6 +378,9 @@
369
378
  return readFixedString(token)
370
379
  case 4: // array
371
380
  if (token >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
381
+ // every element occupies at least one byte, so a length beyond what remains in the source can
382
+ // never be satisfied; check before allocating so a tiny header can not force a huge allocation
383
+ if (token > srcEnd - position$1) throw endOfCBORError()
372
384
  let array = new Array(token);
373
385
  //if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
374
386
  //else
@@ -376,6 +388,7 @@
376
388
  return array
377
389
  case 5: // map
378
390
  if (token >= maxMapSize) throw new Error(`Map size exceeds ${maxArraySize}`)
391
+ if (token > (srcEnd - position$1) / 2) throw endOfCBORError() // each entry needs at least two bytes
379
392
  if (currentDecoder.mapsAsObjects) {
380
393
  let object = {};
381
394
  if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
@@ -467,11 +480,7 @@
467
480
  throw new Error('Unknown token ' + token)
468
481
  }
469
482
  default: // negative int
470
- if (isNaN(token)) {
471
- let error = new Error('Unexpected end of CBOR data');
472
- error.incomplete = true;
473
- throw error
474
- }
483
+ if (isNaN(token)) throw endOfCBORError()
475
484
  throw new Error('Unknown CBOR token ' + token)
476
485
  }
477
486
  }
@@ -1100,10 +1109,12 @@
1100
1109
  }
1101
1110
 
1102
1111
  function readJustLength() {
1112
+ if (!(position$1 < srcEnd)) throw endOfCBORError()
1103
1113
  let token = src[position$1++] & 0x1f;
1104
1114
  if (token > 0x17) {
1105
1115
  switch (token) {
1106
1116
  case 0x18:
1117
+ if (position$1 >= srcEnd) throw endOfCBORError()
1107
1118
  token = src[position$1++];
1108
1119
  break
1109
1120
  case 0x19: