cbor-x 1.6.4 → 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
@@ -3,6 +3,7 @@
3
3
  var stream = require('stream');
4
4
  var module$1 = require('module');
5
5
 
6
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
6
7
  let decoder;
7
8
  try {
8
9
  decoder = new TextDecoder();
@@ -239,13 +240,22 @@ function checkedRead() {
239
240
  }
240
241
  }
241
242
 
243
+ function endOfCBORError() {
244
+ let error = new Error('Unexpected end of CBOR data');
245
+ error.incomplete = true;
246
+ return error
247
+ }
248
+
242
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()
243
252
  let token = src[position$1++];
244
253
  let majorType = token >> 5;
245
254
  token = token & 0x1f;
246
255
  if (token > 0x17) {
247
256
  switch (token) {
248
257
  case 0x18:
258
+ if (position$1 >= srcEnd) throw endOfCBORError()
249
259
  token = src[position$1++];
250
260
  break
251
261
  case 0x19:
@@ -374,6 +384,9 @@ function read() {
374
384
  return readFixedString(token)
375
385
  case 4: // array
376
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()
377
390
  let array = new Array(token);
378
391
  //if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
379
392
  //else
@@ -381,6 +394,7 @@ function read() {
381
394
  return array
382
395
  case 5: // map
383
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
384
398
  if (currentDecoder.mapsAsObjects) {
385
399
  let object = {};
386
400
  if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
@@ -472,11 +486,7 @@ function read() {
472
486
  throw new Error('Unknown token ' + token)
473
487
  }
474
488
  default: // negative int
475
- if (isNaN(token)) {
476
- let error = new Error('Unexpected end of CBOR data');
477
- error.incomplete = true;
478
- throw error
479
- }
489
+ if (isNaN(token)) throw endOfCBORError()
480
490
  throw new Error('Unknown CBOR token ' + token)
481
491
  }
482
492
  }
@@ -600,43 +610,49 @@ function readStringJS(length) {
600
610
  units.push(byte1);
601
611
  } else if ((byte1 & 0xe0) === 0xc0) {
602
612
  // 2 bytes
603
- const byte2 = src[position$1++] & 0x3f;
604
- const codePoint = ((byte1 & 0x1f) << 6) | byte2;
605
- // Reject overlong encoding: 2-byte sequences must encode values >= 0x80
606
- if (codePoint < 0x80) {
607
- units.push(0xFFFD); // replacement character
613
+ if (byte1 < 0xc2 || position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
614
+ units.push(0xFFFD);
608
615
  } else {
609
- units.push(codePoint);
616
+ const byte2 = src[position$1++] & 0x3f;
617
+ units.push(((byte1 & 0x1f) << 6) | byte2);
610
618
  }
611
619
  } else if ((byte1 & 0xf0) === 0xe0) {
612
620
  // 3 bytes
613
- const byte2 = src[position$1++] & 0x3f;
614
- const byte3 = src[position$1++] & 0x3f;
615
- const codePoint = ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3;
616
- // Reject overlong encoding: 3-byte sequences must encode values >= 0x800
617
- // Also reject surrogates (0xD800-0xDFFF)
618
- if (codePoint < 0x800 || (codePoint >= 0xD800 && codePoint <= 0xDFFF)) {
619
- units.push(0xFFFD); // replacement character
621
+ const byte2 = position$1 < end ? src[position$1] : 0;
622
+ if (position$1 >= end || (byte2 & 0xc0) !== 0x80 ||
623
+ (byte1 === 0xe0 && byte2 < 0xa0) || (byte1 === 0xed && byte2 >= 0xa0)) {
624
+ units.push(0xFFFD);
620
625
  } else {
621
- units.push(codePoint);
626
+ position$1++;
627
+ if (position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
628
+ units.push(0xFFFD);
629
+ } else {
630
+ const byte3 = src[position$1++] & 0x3f;
631
+ units.push(((byte1 & 0x1f) << 12) | ((byte2 & 0x3f) << 6) | byte3);
632
+ }
622
633
  }
623
634
  } else if ((byte1 & 0xf8) === 0xf0) {
624
635
  // 4 bytes
625
- const byte2 = src[position$1++] & 0x3f;
626
- const byte3 = src[position$1++] & 0x3f;
627
- const byte4 = src[position$1++] & 0x3f;
628
- let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
629
- // Reject overlong encoding: 4-byte sequences must encode values >= 0x10000
630
- // Also reject values > 0x10FFFF (maximum valid Unicode)
631
- if (unit < 0x10000 || unit > 0x10FFFF) {
632
- units.push(0xFFFD); // replacement character
633
- } else if (unit > 0xffff) {
634
- unit -= 0x10000;
635
- units.push(((unit >>> 10) & 0x3ff) | 0xd800);
636
- unit = 0xdc00 | (unit & 0x3ff);
637
- units.push(unit);
636
+ const byte2 = position$1 < end ? src[position$1] : 0;
637
+ if (byte1 > 0xf4 || position$1 >= end || (byte2 & 0xc0) !== 0x80 ||
638
+ (byte1 === 0xf0 && byte2 < 0x90) || (byte1 === 0xf4 && byte2 >= 0x90)) {
639
+ units.push(0xFFFD);
638
640
  } else {
639
- units.push(unit);
641
+ position$1++;
642
+ if (position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
643
+ units.push(0xFFFD);
644
+ } else {
645
+ const byte3 = src[position$1++] & 0x3f;
646
+ if (position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
647
+ units.push(0xFFFD);
648
+ } else {
649
+ const byte4 = src[position$1++] & 0x3f;
650
+ let unit = ((byte1 & 0x07) << 0x12) | ((byte2 & 0x3f) << 0x0c) | (byte3 << 0x06) | byte4;
651
+ unit -= 0x10000;
652
+ units.push(((unit >>> 10) & 0x3ff) | 0xd800);
653
+ units.push(0xdc00 | (unit & 0x3ff));
654
+ }
655
+ }
640
656
  }
641
657
  } else {
642
658
  units.push(0xFFFD); // replacement character for invalid lead byte
@@ -1134,10 +1150,12 @@ function readBundleExt() {
1134
1150
  }
1135
1151
 
1136
1152
  function readJustLength() {
1153
+ if (!(position$1 < srcEnd)) throw endOfCBORError()
1137
1154
  let token = src[position$1++] & 0x1f;
1138
1155
  if (token > 0x17) {
1139
1156
  switch (token) {
1140
1157
  case 0x18:
1158
+ if (position$1 >= srcEnd) throw endOfCBORError()
1141
1159
  token = src[position$1++];
1142
1160
  break
1143
1161
  case 0x19:
@@ -2621,7 +2639,7 @@ if (!nativeAccelerationDisabled) {
2621
2639
  if (typeof require == 'function')
2622
2640
  extractor = require('cbor-extract');
2623
2641
  else
2624
- extractor = module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('node.cjs', document.baseURI).href)))('cbor-extract');
2642
+ extractor = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('node.cjs', document.baseURI).href)))('cbor-extract');
2625
2643
  if (extractor)
2626
2644
  setExtractor(extractor.extractStrings);
2627
2645
  } catch (error) {