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