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