@zip.js/zip.js 2.17.0 → 2.18.1

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.
@@ -3245,8 +3245,13 @@
3245
3245
  if (codecImportFailed) {
3246
3246
  responseError.codecImportFailed = true;
3247
3247
  }
3248
- if (cause && !isErrorObject(responseError.cause)) {
3249
- responseError.cause = Object.assign(new Error(cause.message), { name: cause.name });
3248
+ if (cause) {
3249
+ if (!isErrorObject(responseError.cause)) {
3250
+ responseError.cause = Object.assign(new Error(cause.message), { name: cause.name });
3251
+ }
3252
+ if (cause.code !== UNDEFINED_VALUE && responseError.cause.code !== cause.code) {
3253
+ responseError.cause.code = cause.code;
3254
+ }
3250
3255
  }
3251
3256
  if (errorValue) {
3252
3257
  if (responseError.name !== name) {
@@ -4838,7 +4843,7 @@
4838
4843
  const CHARSET_UTF8 = "utf-8";
4839
4844
  const PROPERTY_NAME_UTF8_SUFFIX = "UTF8";
4840
4845
  const CHARSET_CP437 = "cp437";
4841
- const BITFLAG_AMBIGUITY_MASK = BITFLAG_ENCRYPTED | BITFLAG_DATA_DESCRIPTOR | BITFLAG_LANG_ENCODING_FLAG;
4846
+ const BITFLAG_AMBIGUITY_MASK = BITFLAG_ENCRYPTED | BITFLAG_DATA_DESCRIPTOR | BITFLAG_STRONG_ENCRYPTION | BITFLAG_LANG_ENCODING_FLAG;
4842
4847
  const VENDOR_VERSION_AE_1$1 = 1;
4843
4848
  const ZIP64_PROPERTIES = [
4844
4849
  [PROPERTY_NAME_UNCOMPRESSED_SIZE, MAX_32_BITS],
@@ -5492,12 +5497,12 @@
5492
5497
  fileEntry.gid = gid;
5493
5498
  }
5494
5499
  const checkPasswordOnly = getOptionValue$1(zipEntry, options, OPTION_CHECK_PASSWORD_ONLY);
5495
- const encrypted = zipEntry.encrypted && localDirectory.encrypted && (!passThroughEncryption || checkPasswordOnly);
5500
+ const encrypted = zipEntry.encrypted && (!passThroughEncryption || checkPasswordOnly);
5496
5501
  const zipCrypto = encrypted && !extraFieldAES;
5497
5502
  if (!passThroughEncryption) {
5498
5503
  fileEntry.zipCrypto = zipCrypto;
5499
5504
  }
5500
- if (encrypted && (localDirectory.rawBitFlag & BITFLAG_STRONG_ENCRYPTION) == BITFLAG_STRONG_ENCRYPTION) {
5505
+ if (encrypted && (zipEntry.rawBitFlag & BITFLAG_STRONG_ENCRYPTION) == BITFLAG_STRONG_ENCRYPTION) {
5501
5506
  throw new Error(ERR_UNSUPPORTED_ENCRYPTION);
5502
5507
  }
5503
5508
  const registeredCodec = passThroughCompression ? UNDEFINED_VALUE : getRegisteredCodec(compressionMethod);
@@ -5737,8 +5742,16 @@
5737
5742
  });
5738
5743
  const extraFieldZip64 = extraField.get(EXTRAFIELD_TYPE_ZIP64);
5739
5744
  if (extraFieldZip64) {
5740
- readExtraFieldZip64(extraFieldZip64, directory);
5745
+ if (!readExtraFieldZip64(extraFieldZip64, directory, localDirectory)) {
5746
+ malformedExtraField = true;
5747
+ }
5741
5748
  directory.extraFieldZip64 = extraFieldZip64;
5749
+ } else if (ZIP64_PROPERTIES.some(([propertyName, max]) => directory[propertyName] == max)) {
5750
+ if (localDirectory) {
5751
+ malformedExtraField = true;
5752
+ } else {
5753
+ throw new Error(ERR_EXTRAFIELD_ZIP64_NOT_FOUND);
5754
+ }
5742
5755
  }
5743
5756
  const extraFieldUnicodePath = extraField.get(EXTRAFIELD_TYPE_UNICODE_PATH);
5744
5757
  if (extraFieldUnicodePath) {
@@ -5751,10 +5764,13 @@
5751
5764
  directory.extraFieldUnicodeComment = extraFieldUnicodeComment;
5752
5765
  }
5753
5766
  const extraFieldAES = extraField.get(EXTRAFIELD_TYPE_AES);
5754
- if (extraFieldAES && extraFieldAES.data.length >= 7) {
5767
+ if (extraFieldAES && (compressionMethod == COMPRESSION_METHOD_AES || directory.encrypted) && extraFieldAES.data.length >= 7) {
5755
5768
  readExtraFieldAES(extraFieldAES, directory, compressionMethod);
5756
5769
  directory.extraFieldAES = extraFieldAES;
5757
5770
  } else {
5771
+ if (extraFieldAES) {
5772
+ malformedExtraField = true;
5773
+ }
5758
5774
  directory.compressionMethod = compressionMethod;
5759
5775
  }
5760
5776
  const extraFieldPkwareUnix = extraField.get(EXTRAFIELD_TYPE_PKWARE_UNIX);
@@ -5797,20 +5813,35 @@
5797
5813
  return malformedExtraField;
5798
5814
  }
5799
5815
 
5800
- function readExtraFieldZip64(extraFieldZip64, directory) {
5816
+ function readExtraFieldZip64(extraFieldZip64, directory, localDirectory) {
5801
5817
  directory.zip64 = true;
5802
5818
  const extraFieldView = getDataView(extraFieldZip64.data);
5803
5819
  const missingProperties = ZIP64_PROPERTIES.filter(([propertyName, max]) => directory[propertyName] == max);
5804
5820
  const requiredLength = missingProperties.reduce((length, [, max]) => length + ZIP64_EXTRACTION[max].bytes, 0);
5805
5821
  if (extraFieldZip64.data.length < requiredLength) {
5822
+ if (localDirectory) {
5823
+ return false;
5824
+ }
5806
5825
  throw new Error(ERR_EXTRAFIELD_ZIP64_NOT_FOUND);
5807
5826
  }
5808
- for (let indexMissingProperty = 0, offset = 0; indexMissingProperty < missingProperties.length; indexMissingProperty++) {
5809
- const [propertyName, max] = missingProperties[indexMissingProperty];
5810
- const extraction = ZIP64_EXTRACTION[max];
5811
- directory[propertyName] = extraFieldZip64[propertyName] = extraction.getValue(extraFieldView, offset);
5812
- offset += extraction.bytes;
5827
+ const values = [];
5828
+ try {
5829
+ for (let indexMissingProperty = 0, offset = 0; indexMissingProperty < missingProperties.length; indexMissingProperty++) {
5830
+ const [, max] = missingProperties[indexMissingProperty];
5831
+ const extraction = ZIP64_EXTRACTION[max];
5832
+ values.push(extraction.getValue(extraFieldView, offset));
5833
+ offset += extraction.bytes;
5834
+ }
5835
+ } catch (error) {
5836
+ if (localDirectory) {
5837
+ return false;
5838
+ }
5839
+ throw error;
5813
5840
  }
5841
+ missingProperties.forEach(([propertyName], indexMissingProperty) => {
5842
+ directory[propertyName] = extraFieldZip64[propertyName] = values[indexMissingProperty];
5843
+ });
5844
+ return true;
5814
5845
  }
5815
5846
 
5816
5847
  function readExtraFieldUnicode(extraFieldUnicode, propertyName, rawPropertyName, directory, fileEntry) {
@@ -5992,32 +6023,27 @@
5992
6023
  }) {
5993
6024
  let dataDescriptorLength = 0;
5994
6025
  if (dataDescriptor) {
5995
- if (extraFieldZip64) {
5996
- dataDescriptorLength = DATA_DESCRIPTOR_RECORD_ZIP_64_LENGTH;
5997
- } else {
5998
- dataDescriptorLength = DATA_DESCRIPTOR_RECORD_LENGTH;
5999
- }
6000
- }
6001
- if (dataDescriptorLength) {
6002
- const dataDescriptorArray = await readUint8Array(reader, dataOffset + compressedSize, dataDescriptorLength + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH);
6026
+ const zip64 = Boolean(extraFieldZip64);
6027
+ const dataDescriptorArray = await readUint8Array(reader, dataOffset + compressedSize, DATA_DESCRIPTOR_RECORD_ZIP_64_LENGTH + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH);
6003
6028
  const dataDescriptorView = getDataView(dataDescriptorArray);
6004
- let signature = dataDescriptorArray.length == dataDescriptorLength + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH &&
6005
- getUint32$1(dataDescriptorView, 0) == DATA_DESCRIPTOR_RECORD_SIGNATURE;
6006
- if (signature) {
6007
- const signedDataDescriptor = readDataDescriptor(dataDescriptorView, DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH, extraFieldZip64);
6008
- const matchCrc32 = (fileEntry.encrypted && !fileEntry.zipCrypto) || signedDataDescriptor.crc32 == crc32;
6009
- if (matchCrc32 &&
6010
- signedDataDescriptor.compressedSize == compressedSize &&
6011
- signedDataDescriptor.uncompressedSize == uncompressedSize) {
6012
- dataDescriptorLength += DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH;
6013
- } else {
6014
- signature = false;
6029
+ const ignoreCrc32 = fileEntry.encrypted && !fileEntry.zipCrypto;
6030
+ let localDataDescriptor;
6031
+ for (const [zip64Layout, signature] of [[zip64, true], [zip64, false], [!zip64, true], [!zip64, false]]) {
6032
+ const candidate = readDataDescriptor(dataDescriptorView, zip64Layout, signature);
6033
+ if (candidate && (ignoreCrc32 || candidate.crc32 == crc32) &&
6034
+ candidate.compressedSize == compressedSize && candidate.uncompressedSize == uncompressedSize) {
6035
+ localDataDescriptor = candidate;
6036
+ break;
6015
6037
  }
6016
6038
  }
6017
- if (dataDescriptorArray.length >= dataDescriptorLength) {
6018
- const localDataDescriptor = readDataDescriptor(dataDescriptorView, signature ? DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH : 0, extraFieldZip64);
6019
- localDataDescriptor.signature = signature;
6039
+ if (!localDataDescriptor) {
6040
+ localDataDescriptor = readDataDescriptor(dataDescriptorView, zip64, false);
6041
+ }
6042
+ if (localDataDescriptor) {
6020
6043
  fileEntry.localDirectory.dataDescriptor = localDataDescriptor;
6044
+ dataDescriptorLength = getDataDescriptorLength(localDataDescriptor.zip64, localDataDescriptor.signature);
6045
+ } else {
6046
+ dataDescriptorLength = getDataDescriptorLength(zip64, false);
6021
6047
  }
6022
6048
  }
6023
6049
  const range = {
@@ -6082,18 +6108,31 @@
6082
6108
  return mergedRanges;
6083
6109
  }
6084
6110
 
6085
- function readDataDescriptor(dataDescriptorView, offset, extraFieldZip64) {
6111
+ function readDataDescriptor(dataDescriptorView, zip64, signature) {
6112
+ const offset = signature ? DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH : 0;
6113
+ if (dataDescriptorView.byteLength < getDataDescriptorLength(zip64, signature) ||
6114
+ (signature && getUint32$1(dataDescriptorView, 0) != DATA_DESCRIPTOR_RECORD_SIGNATURE)) {
6115
+ return UNDEFINED_VALUE;
6116
+ }
6086
6117
  const crc32 = getUint32$1(dataDescriptorView, offset);
6087
6118
  let compressedSize;
6088
6119
  let uncompressedSize;
6089
- if (extraFieldZip64) {
6090
- compressedSize = getBigUint64(dataDescriptorView, offset + 4);
6091
- uncompressedSize = getBigUint64(dataDescriptorView, offset + 12);
6092
- } else {
6093
- compressedSize = getUint32$1(dataDescriptorView, offset + 4);
6094
- uncompressedSize = getUint32$1(dataDescriptorView, offset + 8);
6120
+ try {
6121
+ if (zip64) {
6122
+ compressedSize = getBigUint64(dataDescriptorView, offset + 4);
6123
+ uncompressedSize = getBigUint64(dataDescriptorView, offset + 12);
6124
+ } else {
6125
+ compressedSize = getUint32$1(dataDescriptorView, offset + 4);
6126
+ uncompressedSize = getUint32$1(dataDescriptorView, offset + 8);
6127
+ }
6128
+ } catch {
6129
+ return UNDEFINED_VALUE;
6095
6130
  }
6096
- return { crc32, compressedSize, uncompressedSize };
6131
+ return { signature, zip64, crc32, compressedSize, uncompressedSize };
6132
+ }
6133
+
6134
+ function getDataDescriptorLength(zip64, signature) {
6135
+ return (zip64 ? DATA_DESCRIPTOR_RECORD_ZIP_64_LENGTH : DATA_DESCRIPTOR_RECORD_LENGTH) + (signature ? DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH : 0);
6097
6136
  }
6098
6137
 
6099
6138
  function getDiskOffset$1(reader, diskNumber) {
@@ -9014,7 +9053,7 @@
9014
9053
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9015
9054
  */
9016
9055
 
9017
- const VERSION = "2.17.0";
9056
+ const VERSION = "2.18.1";
9018
9057
 
9019
9058
  /*
9020
9059
  Copyright (c) 2025 Gildas Lormeau. All rights reserved.