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/test.js CHANGED
@@ -1,6 +1,7 @@
1
1
  (function (stream, module, chai, fs) {
2
2
  'use strict';
3
3
 
4
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
4
5
  let decoder;
5
6
  try {
6
7
  decoder = new TextDecoder();
@@ -237,13 +238,22 @@
237
238
  }
238
239
  }
239
240
 
241
+ function endOfCBORError() {
242
+ let error = new Error('Unexpected end of CBOR data');
243
+ error.incomplete = true;
244
+ return error
245
+ }
246
+
240
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()
241
250
  let token = src[position$1++];
242
251
  let majorType = token >> 5;
243
252
  token = token & 0x1f;
244
253
  if (token > 0x17) {
245
254
  switch (token) {
246
255
  case 0x18:
256
+ if (position$1 >= srcEnd) throw endOfCBORError()
247
257
  token = src[position$1++];
248
258
  break
249
259
  case 0x19:
@@ -372,6 +382,9 @@
372
382
  return readFixedString(token)
373
383
  case 4: // array
374
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()
375
388
  let array = new Array(token);
376
389
  //if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
377
390
  //else
@@ -379,6 +392,7 @@
379
392
  return array
380
393
  case 5: // map
381
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
382
396
  if (currentDecoder.mapsAsObjects) {
383
397
  let object = {};
384
398
  if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
@@ -470,11 +484,7 @@
470
484
  throw new Error('Unknown token ' + token)
471
485
  }
472
486
  default: // negative int
473
- if (isNaN(token)) {
474
- let error = new Error('Unexpected end of CBOR data');
475
- error.incomplete = true;
476
- throw error
477
- }
487
+ if (isNaN(token)) throw endOfCBORError()
478
488
  throw new Error('Unknown CBOR token ' + token)
479
489
  }
480
490
  }
@@ -595,43 +605,49 @@
595
605
  units.push(byte1);
596
606
  } else if ((byte1 & 0xe0) === 0xc0) {
597
607
  // 2 bytes
598
- const byte2 = src[position$1++] & 0x3f;
599
- const codePoint = ((byte1 & 0x1f) << 6) | byte2;
600
- // Reject overlong encoding: 2-byte sequences must encode values >= 0x80
601
- if (codePoint < 0x80) {
602
- units.push(0xFFFD); // replacement character
608
+ if (byte1 < 0xc2 || position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
609
+ units.push(0xFFFD);
603
610
  } else {
604
- units.push(codePoint);
611
+ const byte2 = src[position$1++] & 0x3f;
612
+ units.push(((byte1 & 0x1f) << 6) | byte2);
605
613
  }
606
614
  } else if ((byte1 & 0xf0) === 0xe0) {
607
615
  // 3 bytes
608
- const byte2 = src[position$1++] & 0x3f;
609
- const byte3 = src[position$1++] & 0x3f;
610
- const codePoint = ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3;
611
- // Reject overlong encoding: 3-byte sequences must encode values >= 0x800
612
- // Also reject surrogates (0xD800-0xDFFF)
613
- if (codePoint < 0x800 || (codePoint >= 0xD800 && codePoint <= 0xDFFF)) {
614
- units.push(0xFFFD); // replacement character
616
+ const byte2 = position$1 < end ? src[position$1] : 0;
617
+ if (position$1 >= end || (byte2 & 0xc0) !== 0x80 ||
618
+ (byte1 === 0xe0 && byte2 < 0xa0) || (byte1 === 0xed && byte2 >= 0xa0)) {
619
+ units.push(0xFFFD);
615
620
  } else {
616
- units.push(codePoint);
621
+ position$1++;
622
+ if (position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
623
+ units.push(0xFFFD);
624
+ } else {
625
+ const byte3 = src[position$1++] & 0x3f;
626
+ units.push(((byte1 & 0x1f) << 12) | ((byte2 & 0x3f) << 6) | byte3);
627
+ }
617
628
  }
618
629
  } else if ((byte1 & 0xf8) === 0xf0) {
619
630
  // 4 bytes
620
- const byte2 = src[position$1++] & 0x3f;
621
- const byte3 = src[position$1++] & 0x3f;
622
- const byte4 = src[position$1++] & 0x3f;
623
- let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
624
- // Reject overlong encoding: 4-byte sequences must encode values >= 0x10000
625
- // Also reject values > 0x10FFFF (maximum valid Unicode)
626
- if (unit < 0x10000 || unit > 0x10FFFF) {
627
- units.push(0xFFFD); // replacement character
628
- } else if (unit > 0xffff) {
629
- unit -= 0x10000;
630
- units.push(((unit >>> 10) & 0x3ff) | 0xd800);
631
- unit = 0xdc00 | (unit & 0x3ff);
632
- units.push(unit);
631
+ const byte2 = position$1 < end ? src[position$1] : 0;
632
+ if (byte1 > 0xf4 || position$1 >= end || (byte2 & 0xc0) !== 0x80 ||
633
+ (byte1 === 0xf0 && byte2 < 0x90) || (byte1 === 0xf4 && byte2 >= 0x90)) {
634
+ units.push(0xFFFD);
633
635
  } else {
634
- units.push(unit);
636
+ position$1++;
637
+ if (position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
638
+ units.push(0xFFFD);
639
+ } else {
640
+ const byte3 = src[position$1++] & 0x3f;
641
+ if (position$1 >= end || (src[position$1] & 0xc0) !== 0x80) {
642
+ units.push(0xFFFD);
643
+ } else {
644
+ const byte4 = src[position$1++] & 0x3f;
645
+ let unit = ((byte1 & 0x07) << 0x12) | ((byte2 & 0x3f) << 0x0c) | (byte3 << 0x06) | byte4;
646
+ unit -= 0x10000;
647
+ units.push(((unit >>> 10) & 0x3ff) | 0xd800);
648
+ units.push(0xdc00 | (unit & 0x3ff));
649
+ }
650
+ }
635
651
  }
636
652
  } else {
637
653
  units.push(0xFFFD); // replacement character for invalid lead byte
@@ -1129,10 +1145,12 @@
1129
1145
  }
1130
1146
 
1131
1147
  function readJustLength() {
1148
+ if (!(position$1 < srcEnd)) throw endOfCBORError()
1132
1149
  let token = src[position$1++] & 0x1f;
1133
1150
  if (token > 0x17) {
1134
1151
  switch (token) {
1135
1152
  case 0x18:
1153
+ if (position$1 >= srcEnd) throw endOfCBORError()
1136
1154
  token = src[position$1++];
1137
1155
  break
1138
1156
  case 0x19:
@@ -2462,7 +2480,7 @@
2462
2480
  if (typeof require == 'function')
2463
2481
  extractor = require('cbor-extract');
2464
2482
  else
2465
- extractor = module.createRequire((document.currentScript && document.currentScript.src || new URL('test.js', document.baseURI).href))('cbor-extract');
2483
+ extractor = module.createRequire((_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('test.js', document.baseURI).href))('cbor-extract');
2466
2484
  if (extractor)
2467
2485
  setExtractor(extractor.extractStrings);
2468
2486
  } catch (error) {
@@ -2470,7 +2488,7 @@
2470
2488
  }
2471
2489
  }
2472
2490
 
2473
- const sampleData = JSON.parse(fs.readFileSync(new URL('./example4.json', (document.currentScript && document.currentScript.src || new URL('test.js', document.baseURI).href))));
2491
+ const sampleData = JSON.parse(fs.readFileSync(new URL('./example4.json', (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('test.js', document.baseURI).href))));
2474
2492
 
2475
2493
  const senmlData = [
2476
2494
  { bn: '/3303/0/5700', bt: 1278887, v: 35.5 },