cbor-x 1.6.3 → 1.6.4

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
@@ -596,26 +596,45 @@
596
596
  } else if ((byte1 & 0xe0) === 0xc0) {
597
597
  // 2 bytes
598
598
  const byte2 = src[position$1++] & 0x3f;
599
- units.push(((byte1 & 0x1f) << 6) | byte2);
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
603
+ } else {
604
+ units.push(codePoint);
605
+ }
600
606
  } else if ((byte1 & 0xf0) === 0xe0) {
601
607
  // 3 bytes
602
608
  const byte2 = src[position$1++] & 0x3f;
603
609
  const byte3 = src[position$1++] & 0x3f;
604
- units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
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
615
+ } else {
616
+ units.push(codePoint);
617
+ }
605
618
  } else if ((byte1 & 0xf8) === 0xf0) {
606
619
  // 4 bytes
607
620
  const byte2 = src[position$1++] & 0x3f;
608
621
  const byte3 = src[position$1++] & 0x3f;
609
622
  const byte4 = src[position$1++] & 0x3f;
610
623
  let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
611
- if (unit > 0xffff) {
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) {
612
629
  unit -= 0x10000;
613
630
  units.push(((unit >>> 10) & 0x3ff) | 0xd800);
614
631
  unit = 0xdc00 | (unit & 0x3ff);
632
+ units.push(unit);
633
+ } else {
634
+ units.push(unit);
615
635
  }
616
- units.push(unit);
617
636
  } else {
618
- units.push(byte1);
637
+ units.push(0xFFFD); // replacement character for invalid lead byte
619
638
  }
620
639
 
621
640
  if (units.length >= 0x1000) {
@@ -2607,6 +2626,12 @@
2607
2626
  assert.throws(() => decode$1(Buffer.from('7a10000000', 'hex')));
2608
2627
  });
2609
2628
 
2629
+ test('overlong encoding', function() {
2630
+ const payload = Buffer.from([0x62, 0xc0, 0xaf]);
2631
+ const result = decode$1(payload);
2632
+ assert.notEqual(result, '/');
2633
+ });
2634
+
2610
2635
  test('encode/decode sample data', function(){
2611
2636
  var data = sampleData;
2612
2637
  var serialized = encode$1(data);