Skip to content

Commit 0bce177

Browse files
fix(server): Ensure all JSON responses are protected
1 parent 5d8f8d0 commit 0bce177

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

β€Žserver/lib/mongo-proxy.jsβ€Ž

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ module.exports = function(basePath, apiKey) {
3636
return newReq;
3737
};
3838

39-
// JSON vulnerability protection
40-
// we prepend the data with ")]},\n", which will be stripped by $http in AngularJS
41-
var protectJSON = function(data) {
42-
return ")]}',\n" + data;
43-
};
44-
4539
var proxy = function(req, res, next) {
4640
try {
4741
var options = mapRequest(req);
@@ -55,10 +49,11 @@ module.exports = function(basePath, apiKey) {
5549
data = data + chunk;
5650
});
5751
dbRes.on('end', function() {
52+
res.header('Content-Type', 'application/json');
5853
res.statusCode = dbRes.statusCode;
5954
res.httpVersion = dbRes.httpVersion;
6055
res.trailers = dbRes.trailers;
61-
res.send(protectJSON(data));
56+
res.send(data);
6257
res.end();
6358
});
6459
});

β€Žserver/lib/security.jsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ var security = {
2828
if (req.isAuthenticated()) {
2929
next();
3030
} else {
31-
res.send(401, filterUser(req.user));
31+
res.json(401, filterUser(req.user));
3232
}
3333
},
3434
adminRequired: function(req, res, next) {
3535
console.log('adminRequired');
3636
if (req.user && req.user.admin ) {
3737
next();
3838
} else {
39-
res.send(401, filterUser(req.user));
39+
res.json(401, filterUser(req.user));
4040
}
4141
},
4242
sendCurrentUser: function(req, res, next) {

β€Žserver/server.jsβ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,30 @@ app.use(config.server.staticUrl, function(req, res, next) {
2727
res.send(404); // If we get here then the request for a static file is invalid
2828
});
2929

30+
// JSON vulnerability protection
31+
// we prepend the data with ")]},\n", which will be stripped by $http in AngularJS
32+
app.use(function(req, res, next) {
33+
var _send = res.send;
34+
res.send = function(body) {
35+
var contentType = res.getHeader('Content-Type');
36+
if ( contentType && contentType.indexOf('application/json') !== -1 ) {
37+
if (2 == arguments.length) {
38+
// res.send(body, status) backwards compat
39+
if ('number' != typeof body && 'number' == typeof arguments[1]) {
40+
this.statusCode = arguments[1];
41+
} else {
42+
this.statusCode = body;
43+
body = arguments[1];
44+
}
45+
}
46+
body = ")]}',\n" + body;
47+
return _send.call(res, body);
48+
}
49+
_send.apply(res, arguments);
50+
};
51+
next();
52+
});
53+
3054
app.use(express.logger()); // Log requests to the console
3155
app.use(express.bodyParser()); // Extract the data from the body of the request - this is needed by the LocalStrategy authenticate method
3256
app.use(express.cookieParser(config.server.cookieSecret)); // Hash cookies with this secret

0 commit comments

Comments
 (0)