cbor-x 1.6.0 → 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
@@ -267,6 +267,7 @@
267
267
  }
268
268
  token = dataView.getUint32(position$1);
269
269
  position$1 += 4;
270
+ if (majorType === 1) return -1 - token; // can't safely use negation operator here
270
271
  break
271
272
  case 0x1b:
272
273
  if (majorType == 7) {
@@ -281,8 +282,7 @@
281
282
  } else if (currentDecoder.int64AsNumber) {
282
283
  token = dataView.getUint32(position$1) * 0x100000000;
283
284
  token += dataView.getUint32(position$1 + 4);
284
- } else
285
- token = dataView.getBigUint64(position$1);
285
+ } else token = dataView.getBigUint64(position$1);
286
286
  position$1 += 8;
287
287
  break
288
288
  case 0x1f:
@@ -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) {
@@ -1233,8 +1252,8 @@
1233
1252
  let structures;
1234
1253
  let referenceMap;
1235
1254
  options = options || {};
1236
- let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) {
1237
- return target.utf8Write(string, position, maxBytes)
1255
+ let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1256
+ return target.utf8Write(string, position, target.byteLength - position)
1238
1257
  } : (textEncoder && textEncoder.encodeInto) ?
1239
1258
  function(string, position) {
1240
1259
  return textEncoder.encodeInto(string, target.subarray(position)).written
@@ -1615,7 +1634,7 @@
1615
1634
  targetView.setUint32(position, value);
1616
1635
  position += 4;
1617
1636
  }
1618
- } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer
1637
+ } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer, 31-bit or less
1619
1638
  if (value >= -0x18) {
1620
1639
  target[position++] = 0x1f - value;
1621
1640
  } else if (value >= -0x100) {
@@ -1630,6 +1649,11 @@
1630
1649
  targetView.setUint32(position, ~value);
1631
1650
  position += 4;
1632
1651
  }
1652
+ } else if (!this.alwaysUseFloat && value < 0 && value >= -0x100000000 && Math.floor(value) === value) {
1653
+ // negative integer, 32-bit or less
1654
+ target[position++] = 0x3a;
1655
+ targetView.setUint32(position, -1 - value);
1656
+ position += 4;
1633
1657
  } else {
1634
1658
  let useFloat32;
1635
1659
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
@@ -1671,6 +1695,9 @@
1671
1695
  }
1672
1696
  let constructor = value.constructor;
1673
1697
  if (constructor === Object) {
1698
+ if (this.skipFunction === true) {
1699
+ value = Object.fromEntries([...Object.keys(value).filter(x => typeof value[x] !== "function").map(x => [x, value[x]])]);
1700
+ }
1674
1701
  writeObject(value);
1675
1702
  } else if (constructor === Array) {
1676
1703
  length = value.length;
@@ -2595,6 +2622,16 @@
2595
2622
  assert.equal(deserialized, data);
2596
2623
  });
2597
2624
 
2625
+ test('string overflow', function() {
2626
+ assert.throws(() => decode$1(Buffer.from('7a10000000', 'hex')));
2627
+ });
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
+
2598
2635
  test('encode/decode sample data', function(){
2599
2636
  var data = sampleData;
2600
2637
  var serialized = encode$1(data);
@@ -3082,7 +3119,12 @@
3082
3119
  var deserialized = decode(serialized);
3083
3120
  assert.deepEqual(deserialized, data);
3084
3121
  });
3085
-
3122
+ test('numbers are compact', function(){
3123
+ assert.equal(encode(-256).length, 2);
3124
+ let encoding = encode(-4294967296);
3125
+ assert.equal(encoding.length, 5);
3126
+ assert.equal(decode(encoding), -4294967296);
3127
+ });
3086
3128
  test('encode ArrayBuffer', function() {
3087
3129
  let ua = new Uint8Array([3, 4, 5]);
3088
3130
  let encoded = encode(ua.buffer);
@@ -3181,6 +3223,20 @@
3181
3223
  decodeMultiple(new Uint8Array([1, 2, 3, 4]), value => values.push(value));
3182
3224
  assert.deepEqual(values, [1, 2, 3, 4]);
3183
3225
  });
3226
+ test('skipFunction', () => {
3227
+ var data = {
3228
+ a: 325283295382932843n,
3229
+ f: () => {},
3230
+ };
3231
+ let encoder = new Encoder({
3232
+ int64AsNumber: true,
3233
+ skipFunction: true,
3234
+ });
3235
+ var serialized = encoder.encode(data);
3236
+ var deserialized = encoder.decode(serialized);
3237
+ assert.deepEqual(deserialized.a, 325283295382932843);
3238
+ assert.equal(Object.hasOwn(deserialized, "f"), false);
3239
+ });
3184
3240
  test('bad input', function() {
3185
3241
  let badInput = Buffer.from('7b2273657269616c6e6f223a2265343a30222c226970223a223139322e3136382e312e3335222c226b6579223a226770735f736563726574227d', 'hex');
3186
3242
  assert.throws(function(){ decode(badInput); }); // should throw, not crash