Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/encrypted files #39

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ Effectively implemented as:
return dosDateTimeToDate(this.lastModFileDate, this.lastModFileTime);
```

#### isEncrypted()

Effectively implemented as:

```js
return this.generalPurposeBitFlag & 0x001;
```

### Class: RandomAccessReader

This class is meant to be subclassed by clients and instantiated for the `fromRandomAccessReader()` function.
Expand Down Expand Up @@ -408,8 +416,9 @@ By extension the following zip file fields are ignored by this library and not p

### No Encryption Support

Currently, the presence of encryption is not even checked,
and encrypted zip files will cause undefined behavior.
This library is using zlib to unzip, and as [zlib doesn't support encryption](http://www.zlib.net/zlib_faq.html#faq38), neither does yauzl.
When trying to read an encrypted file using `openReadStream`, its callback will be called with an error.
Yauzl does emit `entry` events for encrypted files though, in case you need to be aware of their existence. You can call `entry.isEncrypted()` to find out if an emitted entry is encrypted.

### Local File Headers Are Ignored

Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ ZipFile.prototype.readEntry = function() {
// 46 - File name
var isUtf8 = entry.generalPurposeBitFlag & 0x800
entry.fileName = bufferToString(buffer, 0, entry.fileNameLength, isUtf8);
var isEncrypted = entry.isEncrypted();

// 46+n - Extra field
var fileCommentStart = entry.fileNameLength + entry.extraFieldLength;
Expand Down Expand Up @@ -375,7 +376,7 @@ ZipFile.prototype.readEntry = function() {
}

// validate file size
if (entry.compressionMethod === 0) {
if (entry.compressionMethod === 0 && !isEncrypted) {
if (entry.compressedSize !== entry.uncompressedSize) {
var msg = "compressed/uncompressed size mismatch for stored file: " + entry.compressedSize + " != " + entry.uncompressedSize;
return emitErrorAndAutoClose(self, new Error(msg));
Expand Down Expand Up @@ -439,6 +440,11 @@ ZipFile.prototype.openReadStream = function(entry, callback) {
} else {
return callback(new Error("unsupported compression method: " + entry.compressionMethod));
}

if (entry.isEncrypted()) {
return callback(new Error("File is encrypted, and cannot be read"));
}

var fileDataStart = localFileHeaderEnd;
var fileDataEnd = fileDataStart + entry.compressedSize;
if (entry.compressedSize !== 0) {
Expand Down Expand Up @@ -490,6 +496,9 @@ function Entry() {
Entry.prototype.getLastModDate = function() {
return dosDateTimeToDate(this.lastModFileDate, this.lastModFileTime);
};
Entry.prototype.isEncrypted = function() {
return this.generalPurposeBitFlag & 0x001;
};

function dosDateTimeToDate(date, time) {
var day = date & 0x1f; // 1-31
Expand Down
Binary file added test/success/onebyte-encrypted.zip
Binary file not shown.
Empty file.
18 changes: 14 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,27 @@ listZipFiles(path.join(__dirname, "success")).forEach(function(zipfilePath) {
if (timestamp > new Date()) throw new Error(messagePrefix + "timestamp in the future: " + timestamp);
var fileNameKey = entry.fileName.replace(/\/$/, "");
var expectedContents = expectedArchiveContents[fileNameKey];
if (expectedContents == null) {
throw new Error(messagePrefix + "not supposed to exist");
}
delete expectedArchiveContents[fileNameKey];
if (entry.fileName !== fileNameKey) {
// directory
if (expectedContents == null) {
throw new Error(messagePrefix + "not supposed to exist");
}

console.log(messagePrefix + "pass");

zipfile.readEntry();
} else {
zipfile.openReadStream(entry, function(err, readStream) {
if (err) throw err;
if (err) {
if (expectedContents == null) {
console.log(messagePrefix + "pass");
// Expected error. Skip to next file
return zipfile.readEntry();
} else {
throw err;
}
}
var buffers = [];
readStream.on("data", function(data) {
buffers.push(data);
Expand Down