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

This commit is contained in:
Book Pauk
2020-10-13 16:24:35 +07:00
parent 2e58cfdb75
commit 39e14d70ee
3 changed files with 17 additions and 3 deletions

View File

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

View File

@@ -766,7 +766,7 @@ ZipEntry.prototype.readDataHeader = function(data) {
}; };
ZipEntry.prototype.read = function(data, offset) { 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(); this.name = this.nameRaw.toString();
var lastChar = data[offset - 1]; var lastChar = data[offset - 1];
this.isDirectory = (lastChar == 47) || (lastChar == 92); this.isDirectory = (lastChar == 47) || (lastChar == 92);

View File

@@ -14,6 +14,14 @@ function fromBase36(data) {
return bs36.decode(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) { function getFileHash(filename, hashName, enc) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const hash = crypto.createHash(hashName); const hash = crypto.createHash(hashName);
@@ -86,6 +94,7 @@ function spawnProcess(cmd, opts) {
module.exports = { module.exports = {
toBase36, toBase36,
fromBase36, fromBase36,
bufferRemoveZeroes,
getFileHash, getFileHash,
sleep, sleep,
randomHexString, randomHexString,