Эх сурвалжийг харах

Исправление бага распаковки Zip-архивов с плохими именами файлов

Book Pauk 4 жил өмнө
parent
commit
39e14d70ee

+ 7 - 2
server/core/FileDecompressor.js

@@ -119,14 +119,19 @@ class FileDecompressor {
         try {
             return await zip.unpack(filename, outputDir, {
                 limitFileSize: this.limitFileSize, 
-                limitFileCount: 1000
-            });
+                limitFileCount: 1000,
+                decodeEntryNameCallback: (nameRaw) => {
+                    return utils.bufferRemoveZeroes(nameRaw);
+                }
+            }
+);
         } catch (e) {
             fs.emptyDir(outputDir);
             return await zip.unpack(filename, outputDir, {
                 limitFileSize: this.limitFileSize, 
                 limitFileCount: 1000,
                 decodeEntryNameCallback: (nameRaw) => {
+                    nameRaw = utils.bufferRemoveZeroes(nameRaw);
                     const enc = textUtils.getEncodingLite(nameRaw);
                     if (enc.indexOf('ISO-8859') < 0) {
                         return iconv.decode(nameRaw, enc);

+ 1 - 1
server/core/Zip/node_stream_zip.js

@@ -766,7 +766,7 @@ ZipEntry.prototype.readDataHeader = function(data) {
 };
 
 ZipEntry.prototype.read = function(data, offset) {
-    this.nameRaw = data.slice(offset, offset += this.fnameLen);
+    this.nameRaw = Buffer.from(data.slice(offset, offset += this.fnameLen));
     this.name = this.nameRaw.toString();
     var lastChar = data[offset - 1];
     this.isDirectory = (lastChar == 47) || (lastChar == 92);

+ 9 - 0
server/core/utils.js

@@ -14,6 +14,14 @@ function fromBase36(data) {
     return bs36.decode(data);
 }
 
+function bufferRemoveZeroes(buf) {
+    const i = buf.indexOf(0);
+    if (i >= 0) {
+        return buf.slice(0, i);
+    }
+    return buf;
+}
+
 function getFileHash(filename, hashName, enc) {
     return new Promise((resolve, reject) => {
         const hash = crypto.createHash(hashName);
@@ -86,6 +94,7 @@ function spawnProcess(cmd, opts) {
 module.exports = {
     toBase36,
     fromBase36,
+    bufferRemoveZeroes,
     getFileHash,
     sleep,
     randomHexString,