Skip to content
Merged
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
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ module.exports = function (bodyParser) {
return next(err);
}

req.body = xml || req.body;
// Prevent setting __proto__ and constructor.prototype
if (xml) {
// Guard against prototype pollution
delete xml.__proto__;
delete xml.constructor;
delete xml.prototype;

req.body = xml;
}
next();
});
});
Expand Down
27 changes: 27 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,31 @@ describe('XML Body Parser', function () {
.send('x<foo>test</foo><bar>test</bar></data>')
.expect(400, done);
});

it('should not set/change prototype using __proto__', function (done) {
createServer();
request(app)
.post('/')
.set('Content-Type', 'application/xml')
.send('<__proto__><name>Bob</name></__proto__>')
.expect(200, { parsed: {} }, done);
});

it('should not set/change using __proto__', function (done) {
createServer();
request(app)
.post('/')
.set('Content-Type', 'application/xml')
.send('<prototype><name>Bob</name></prototype>')
.expect(200, { parsed: {} }, done);
});

it('should not set/change using constructor', function (done) {
createServer();
request(app)
.post('/')
.set('Content-Type', 'application/xml')
.send('<constructor><name>Bob</name></constructor>')
.expect(200, { parsed: {} }, done);
});
});