cbor-x 1.6.7 → 1.6.8

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
@@ -212,8 +212,10 @@ class Decoder {
212
212
  }
213
213
  }
214
214
  const nestedDecodeErrors = new WeakSet();
215
+ let arrayAllocationBudget;
215
216
  function checkedRead() {
216
217
  try {
218
+ arrayAllocationBudget = srcEnd - position$1;
217
219
  let result = read();
218
220
  if (bundledStrings$1) {
219
221
  if (position$1 >= bundledStrings$1.postBundlePosition)
@@ -403,7 +405,9 @@ function read() {
403
405
  if (token > maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
404
406
  // every element occupies at least one byte, so a length beyond what remains in the source can
405
407
  // never be satisfied; check before allocating so a tiny header can not force a huge allocation
406
- if (token > srcEnd - position$1) throw endOfCBORError()
408
+ // nested arrays each pass the check above yet still preallocate, so also spend from a
409
+ // per-decode budget of the remaining bytes; the summed lengths of any valid tree stay under it
410
+ if (token > srcEnd - position$1 || (arrayAllocationBudget -= token) < 0) throw endOfCBORError()
407
411
  let array = new Array(token);
408
412
  //if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
409
413
  //else
@@ -1227,6 +1231,7 @@ function saveState(callback) {
1227
1231
  let savedDecoder = currentDecoder;
1228
1232
  let savedSequentialMode = sequentialMode;
1229
1233
  let savedRestoreMapsAsObject = restoreMapsAsObject;
1234
+ let savedArrayAllocationBudget = arrayAllocationBudget;
1230
1235
  restoreMapsAsObject = false;
1231
1236
  try {
1232
1237
  return callback()
@@ -1245,6 +1250,7 @@ function saveState(callback) {
1245
1250
  currentStructures = savedStructures;
1246
1251
  currentDecoder = savedDecoder;
1247
1252
  restoreMapsAsObject = savedRestoreMapsAsObject;
1253
+ arrayAllocationBudget = savedArrayAllocationBudget;
1248
1254
  dataView = new DataView(src.buffer, src.byteOffset, src.byteLength);
1249
1255
  }
1250
1256
  }