msgpackr 1.11.8 → 1.11.10

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/index.js CHANGED
@@ -30,13 +30,6 @@
30
30
  var sequentialMode = false;
31
31
  var inlineObjectReadThreshold = 2;
32
32
  var readStruct;
33
- // no-eval build
34
- try {
35
- new Function('');
36
- } catch(error) {
37
- // if eval variants are not supported, do not create inline object readers ever
38
- inlineObjectReadThreshold = Infinity;
39
- }
40
33
 
41
34
  class Unpackr {
42
35
  constructor(options) {
@@ -502,11 +495,18 @@
502
495
  function readObject() {
503
496
  // This initial function is quick to instantiate, but runs slower. After several iterations pay the cost to build the faster function
504
497
  if (readObject.count++ > inlineObjectReadThreshold) {
505
- let readObject = structure.read = (new Function('r', 'return function(){return ' + (currentUnpackr.freezeData ? 'Object.freeze' : '') +
506
- '({' + structure.map(key => key === '__proto__' ? '__proto_:r()' : validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '})}'))(read);
498
+ let optimizedReadObject;
499
+ try {
500
+ optimizedReadObject = structure.read = (new Function('r', 'return function(){return ' + (currentUnpackr.freezeData ? 'Object.freeze' : '') +
501
+ '({' + structure.map(key => key === '__proto__' ? '__proto_:r()' : validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '})}'))(read);
502
+ } catch(error) {
503
+ // in CF workers, the new Function call could begin to fail at any point in time
504
+ inlineObjectReadThreshold = Infinity; // disable going forward
505
+ return readObject(); // recursively try again
506
+ }
507
507
  if (structure.highByte === 0)
508
508
  structure.read = createSecondByteReader(firstId, structure.read);
509
- return readObject() // second byte is already read, if there is one so immediately read object
509
+ return optimizedReadObject() // second byte is already read, if there is one so immediately read object
510
510
  }
511
511
  let object = {};
512
512
  for (let i = 0, l = structure.length; i < l; i++) {
@@ -575,26 +575,45 @@
575
575
  } else if ((byte1 & 0xe0) === 0xc0) {
576
576
  // 2 bytes
577
577
  const byte2 = src[position$1++] & 0x3f;
578
- units.push(((byte1 & 0x1f) << 6) | byte2);
578
+ const codePoint = ((byte1 & 0x1f) << 6) | byte2;
579
+ // Reject overlong encoding: 2-byte sequences must encode values >= 0x80
580
+ if (codePoint < 0x80) {
581
+ units.push(0xFFFD); // replacement character
582
+ } else {
583
+ units.push(codePoint);
584
+ }
579
585
  } else if ((byte1 & 0xf0) === 0xe0) {
580
586
  // 3 bytes
581
587
  const byte2 = src[position$1++] & 0x3f;
582
588
  const byte3 = src[position$1++] & 0x3f;
583
- units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
589
+ const codePoint = ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3;
590
+ // Reject overlong encoding: 3-byte sequences must encode values >= 0x800
591
+ // Also reject surrogates (0xD800-0xDFFF)
592
+ if (codePoint < 0x800 || (codePoint >= 0xD800 && codePoint <= 0xDFFF)) {
593
+ units.push(0xFFFD); // replacement character
594
+ } else {
595
+ units.push(codePoint);
596
+ }
584
597
  } else if ((byte1 & 0xf8) === 0xf0) {
585
598
  // 4 bytes
586
599
  const byte2 = src[position$1++] & 0x3f;
587
600
  const byte3 = src[position$1++] & 0x3f;
588
601
  const byte4 = src[position$1++] & 0x3f;
589
602
  let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
590
- if (unit > 0xffff) {
603
+ // Reject overlong encoding: 4-byte sequences must encode values >= 0x10000
604
+ // Also reject values > 0x10FFFF (maximum valid Unicode)
605
+ if (unit < 0x10000 || unit > 0x10FFFF) {
606
+ units.push(0xFFFD); // replacement character
607
+ } else if (unit > 0xffff) {
591
608
  unit -= 0x10000;
592
609
  units.push(((unit >>> 10) & 0x3ff) | 0xd800);
593
610
  unit = 0xdc00 | (unit & 0x3ff);
611
+ units.push(unit);
612
+ } else {
613
+ units.push(unit);
594
614
  }
595
- units.push(unit);
596
615
  } else {
597
- units.push(byte1);
616
+ units.push(0xFFFD); // replacement character for invalid lead byte
598
617
  }
599
618
 
600
619
  if (units.length >= 0x1000) {