Skip to content

Commit d46ca62

Browse files
committed
Fix prototype pollution vulnerability
2 parents f025695 + cb1609a commit d46ca62

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ module.exports = function (bodyParser) {
4343
return next(err);
4444
}
4545

46-
req.body = xml || req.body;
46+
if (xml) {
47+
// Guard against prototype pollution
48+
delete xml.__proto__;
49+
delete xml.constructor;
50+
delete xml.prototype;
51+
52+
// Set result on the request body
53+
req.body = xml;
54+
}
55+
4756
next();
4857
});
4958
});

test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,31 @@ describe('XML Body Parser', function () {
130130
.send('x<foo>test</foo><bar>test</bar></data>')
131131
.expect(400, done);
132132
});
133+
134+
it('should not set/change prototype using __proto__', function (done) {
135+
createServer();
136+
request(app)
137+
.post('/')
138+
.set('Content-Type', 'application/xml')
139+
.send('<__proto__><name>Bob</name></__proto__>')
140+
.expect(200, { parsed: {} }, done);
141+
});
142+
143+
it('should not set/change using prototype', function (done) {
144+
createServer();
145+
request(app)
146+
.post('/')
147+
.set('Content-Type', 'application/xml')
148+
.send('<prototype><name>Bob</name></prototype>')
149+
.expect(200, { parsed: {} }, done);
150+
});
151+
152+
it('should not set/change using constructor', function (done) {
153+
createServer();
154+
request(app)
155+
.post('/')
156+
.set('Content-Type', 'application/xml')
157+
.send('<constructor><name>Bob</name></constructor>')
158+
.expect(200, { parsed: {} }, done);
159+
});
133160
});

0 commit comments

Comments
 (0)