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/test.js CHANGED
@@ -238,13 +238,22 @@
238
238
  }
239
239
  }
240
240
 
241
+ function endOfCBORError() {
242
+ let error = new Error('Unexpected end of CBOR data');
243
+ error.incomplete = true;
244
+ return error
245
+ }
246
+
241
247
  function read() {
248
+ // compared as < so that a NaN position (from a corrupt length) also throws instead of reading undefined
249
+ if (!(position$1 < srcEnd)) throw endOfCBORError()
242
250
  let token = src[position$1++];
243
251
  let majorType = token >> 5;
244
252
  token = token & 0x1f;
245
253
  if (token > 0x17) {
246
254
  switch (token) {
247
255
  case 0x18:
256
+ if (position$1 >= srcEnd) throw endOfCBORError()
248
257
  token = src[position$1++];
249
258
  break
250
259
  case 0x19:
@@ -373,6 +382,9 @@
373
382
  return readFixedString(token)
374
383
  case 4: // array
375
384
  if (token >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
385
+ // every element occupies at least one byte, so a length beyond what remains in the source can
386
+ // never be satisfied; check before allocating so a tiny header can not force a huge allocation
387
+ if (token > srcEnd - position$1) throw endOfCBORError()
376
388
  let array = new Array(token);
377
389
  //if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
378
390
  //else
@@ -380,6 +392,7 @@
380
392
  return array
381
393
  case 5: // map
382
394
  if (token >= maxMapSize) throw new Error(`Map size exceeds ${maxArraySize}`)
395
+ if (token > (srcEnd - position$1) / 2) throw endOfCBORError() // each entry needs at least two bytes
383
396
  if (currentDecoder.mapsAsObjects) {
384
397
  let object = {};
385
398
  if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
@@ -471,11 +484,7 @@
471
484
  throw new Error('Unknown token ' + token)
472
485
  }
473
486
  default: // negative int
474
- if (isNaN(token)) {
475
- let error = new Error('Unexpected end of CBOR data');
476
- error.incomplete = true;
477
- throw error
478
- }
487
+ if (isNaN(token)) throw endOfCBORError()
479
488
  throw new Error('Unknown CBOR token ' + token)
480
489
  }
481
490
  }
@@ -1136,10 +1145,12 @@
1136
1145
  }
1137
1146
 
1138
1147
  function readJustLength() {
1148
+ if (!(position$1 < srcEnd)) throw endOfCBORError()
1139
1149
  let token = src[position$1++] & 0x1f;
1140
1150
  if (token > 0x17) {
1141
1151
  switch (token) {
1142
1152
  case 0x18:
1153
+ if (position$1 >= srcEnd) throw endOfCBORError()
1143
1154
  token = src[position$1++];
1144
1155
  break
1145
1156
  case 0x19: