-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sanitize requests in the forms controller. Issue #1844
Added a `db.sanitizeResponse` that can be called from any controller. The only problematic function right now is `forms.getForm` because it returned headers coming from nano. Sanitized those headers by removing `uri` and `statusCode` and wrote tests around it. This could potentially leak auth information to the client. See https://github.com/dscape/nano/blob/master/lib/nano.js#L195 TODO: open issue in nano project and patch
- Loading branch information
Milan Andric
committed
Feb 3, 2016
1 parent
5ece6e2
commit beb2a7c
Showing
3 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
var controller = require('../controllers/forms'), | ||
db = require('../db-nano'), | ||
sinon = require('sinon'); | ||
|
||
exports.tearDown = function (callback) { | ||
if (db.request.restore) { | ||
db.request.restore(); | ||
} | ||
if (db.sanitizeResponse.restore) { | ||
db.sanitizeResponse.restore(); | ||
} | ||
if (db.medic.view.restore) { | ||
db.medic.view.restore(); | ||
} | ||
if (db.medic.attachment.get.restore) { | ||
db.medic.attachment.get.restore(); | ||
} | ||
callback(); | ||
}; | ||
|
||
exports['getForm returns error from view query'] = function(test) { | ||
test.expect(2); | ||
var req = sinon.stub(db.medic, 'view').callsArgWith(3, 'icky'); | ||
controller.getForm('', '', function(err, results) { | ||
test.equals(err, 'icky'); | ||
test.equals(req.callCount, 1); | ||
test.done(); | ||
}); | ||
}; | ||
|
||
exports['getForm returns form not found message on empty view query'] = function(test) { | ||
test.expect(2); | ||
var req = sinon.stub(db.medic, 'view').callsArgWith(3, null, {rows: []}); | ||
controller.getForm('', '', function(err, body, headers) { | ||
test.equals(err.message, 'Form not found.'); | ||
test.equals(req.callCount, 1); | ||
test.done(); | ||
}); | ||
}; | ||
|
||
exports['getForm returns error from attachment query'] = function(test) { | ||
test.expect(3); | ||
var req1 = sinon.stub(db.medic, 'view').callsArgWith(3, null, {rows: [1]}); | ||
var req2 = sinon.stub(db.medic.attachment, 'get').callsArgWith(2, 'boop'); | ||
controller.getForm('', '', function(err, body, headers) { | ||
test.equals(err, 'boop'); | ||
test.equals(req1.callCount, 1); | ||
test.equals(req2.callCount, 1); | ||
test.done(); | ||
}); | ||
}; | ||
|
||
exports['getForm returns body and headers from attachment query'] = function(test) { | ||
test.expect(2); | ||
sinon.stub(db.medic, 'view').callsArgWith(3, null, {rows: [1]}); | ||
sinon.stub(db.medic.attachment, 'get').callsArgWith(2, null, 'foo', { | ||
'content-type': 'xml' | ||
}); | ||
controller.getForm('', '', function(err, body, headers) { | ||
test.equals(body, 'foo'); | ||
test.deepEqual(headers, {'content-type': 'xml'}); | ||
test.done(); | ||
}); | ||
}; | ||
|
||
exports['getForm sanitizes bad headers from attachment query'] = function(test) { | ||
test.expect(3); | ||
sinon.stub(db.medic, 'view').callsArgWith(3, null, {rows: [1]}); | ||
sinon.stub(db.medic.attachment, 'get').callsArgWith(2, null, 'foo', { | ||
'content-type': 'xml', | ||
'uri' : 'http://admin:secret@localhost', | ||
'statusCode' : 'junk' | ||
}); | ||
var spy = sinon.spy(db, 'sanitizeResponse'); | ||
controller.getForm('', '', function(err, body, headers) { | ||
test.equals(body, 'foo'); | ||
test.deepEqual(headers, {'content-type': 'xml'}); | ||
test.ok(spy.called); | ||
test.done(); | ||
}); | ||
}; | ||
|
||
exports['listForms returns error from view query'] = function(test) { | ||
test.expect(2); | ||
var req = sinon.stub(db.medic, 'view').callsArgWith(3, 'icky'); | ||
controller.listForms({}, function(err, body, headers) { | ||
test.equals(err, 'icky'); | ||
test.equals(req.callCount, 1); | ||
test.done(); | ||
}); | ||
}; | ||
|
||
exports['listForms sanitizes response'] = function(test) { | ||
test.expect(2); | ||
sinon.stub(db.medic, 'view').callsArgWith(3, null, { | ||
rows: [1] | ||
}); | ||
var spy = sinon.spy(db, 'sanitizeResponse'); | ||
controller.listForms({}, function(err, body, headers) { | ||
test.equals(err, null); | ||
test.equals(spy.callCount, 1); | ||
test.done(); | ||
}); | ||
}; | ||
|
||
exports['listForms sanitizes openrosa response'] = function(test) { | ||
test.expect(2); | ||
sinon.stub(db.medic, 'view').callsArgWith(3, null, { | ||
rows: [1] | ||
}); | ||
var spy = sinon.spy(db, 'sanitizeResponse'); | ||
controller.listForms({'x-openrosa-version': '1.0'}, function(err, body, headers) { | ||
test.equals(err, null); | ||
test.equals(spy.callCount, 1); | ||
test.done(); | ||
}); | ||
}; |