Skip to content

Commit 2235749

Browse files
fix: Use a null prototype object for this.files
This approach is taken to prevent overriding object methods that would exist on a normal object Object.create({})
1 parent b7f472d commit 2235749

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

lib/index.js

Lines changed: 4 additions & 1 deletion
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

lib/object.js

Lines changed: 3 additions & 3 deletions
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
/**

0 commit comments

Comments
 (0)