Skip to content

Commit 6d029b4

Browse files
authored
Merge pull request #766 from MichaelAquilina/fix/files-null-prototype
fix: Use a null prototype object for this.files
2 parents b7f472d + bb38812 commit 6d029b4

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

Diff for: lib/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ function JSZip() {
1919
// "folder/" : {...},
2020
// "folder/data.txt" : {...}
2121
// }
22-
this.files = {};
22+
// NOTE: we use a null prototype because we do not
23+
// want filenames like "toString" coming from a zip file
24+
// to overwrite methods and attributes in a normal Object.
25+
this.files = Object.create(null);
2326

2427
this.comment = null;
2528

Diff for: lib/object.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,16 @@ var out = {
179179
*/
180180
forEach: function(cb) {
181181
var filename, relativePath, file;
182+
/* jshint ignore:start */
183+
// ignore warning about unwanted properties because this.files is a null prototype object
182184
for (filename in this.files) {
183-
if (!this.files.hasOwnProperty(filename)) {
184-
continue;
185-
}
186185
file = this.files[filename];
187186
relativePath = filename.slice(this.root.length, filename.length);
188187
if (relativePath && filename.slice(0, this.root.length) === this.root) { // the file is in the current root
189188
cb(relativePath, file); // TODO reverse the parameters ? need to be clean AND consistent with the filter search fn...
190189
}
191190
}
191+
/* jshint ignore:end */
192192
},
193193

194194
/**

Diff for: test/asserts/load.js

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ QUnit.module("load", function () {
1717
})['catch'](JSZipTestUtils.assertNoError);
1818
});
1919

20+
JSZipTestUtils.testZipFile("Load files which shadow Object prototype methods", "ref/pollution.zip", function(assert, file) {
21+
var done = assert.async();
22+
assert.ok(typeof file === "string");
23+
JSZip.loadAsync(file)
24+
.then(function (zip) {
25+
assert.notEqual(Object.getPrototypeOf(zip.files), zip.files.__proto__);
26+
return zip.file("__proto__").async("string"); })
27+
.then(function(result) {
28+
assert.equal(result, "hello\n", "the zip was correctly read.");
29+
done();
30+
})['catch'](JSZipTestUtils.assertNoError);
31+
});
32+
2033
JSZipTestUtils.testZipFile("load(string) handles bytes > 255", "ref/all.zip", function(assert, file) {
2134
var done = assert.async();
2235
// the method used to load zip with ajax will remove the extra bits.

Diff for: test/ref/pollution.zip

480 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)