diff --git a/lib/index.js b/lib/index.js index b4498772..b4c95bad 100644 --- a/lib/index.js +++ b/lib/index.js @@ -19,7 +19,10 @@ function JSZip() { // "folder/" : {...}, // "folder/data.txt" : {...} // } - this.files = {}; + // NOTE: we use a null prototype because we do not + // want filenames like "toString" coming from a zip file + // to overwrite methods and attributes in a normal Object. + this.files = Object.create(null); this.comment = null; diff --git a/lib/object.js b/lib/object.js index 1c9d8e80..aec3db78 100644 --- a/lib/object.js +++ b/lib/object.js @@ -179,16 +179,16 @@ var out = { */ forEach: function(cb) { var filename, relativePath, file; + /* jshint ignore:start */ + // ignore warning about unwanted properties because this.files is a null prototype object for (filename in this.files) { - if (!this.files.hasOwnProperty(filename)) { - continue; - } file = this.files[filename]; relativePath = filename.slice(this.root.length, filename.length); if (relativePath && filename.slice(0, this.root.length) === this.root) { // the file is in the current root cb(relativePath, file); // TODO reverse the parameters ? need to be clean AND consistent with the filter search fn... } } + /* jshint ignore:end */ }, /** diff --git a/test/asserts/load.js b/test/asserts/load.js index c89978d5..ce073cc3 100644 --- a/test/asserts/load.js +++ b/test/asserts/load.js @@ -17,6 +17,19 @@ QUnit.module("load", function () { })['catch'](JSZipTestUtils.assertNoError); }); + JSZipTestUtils.testZipFile("Load files which shadow Object prototype methods", "ref/pollution.zip", function(assert, file) { + var done = assert.async(); + assert.ok(typeof file === "string"); + JSZip.loadAsync(file) + .then(function (zip) { + assert.notEqual(Object.getPrototypeOf(zip.files), zip.files.__proto__); + return zip.file("__proto__").async("string"); }) + .then(function(result) { + assert.equal(result, "hello\n", "the zip was correctly read."); + done(); + })['catch'](JSZipTestUtils.assertNoError); + }); + JSZipTestUtils.testZipFile("load(string) handles bytes > 255", "ref/all.zip", function(assert, file) { var done = assert.async(); // the method used to load zip with ajax will remove the extra bits. diff --git a/test/ref/pollution.zip b/test/ref/pollution.zip new file mode 100644 index 00000000..c673c0a0 Binary files /dev/null and b/test/ref/pollution.zip differ