Skip to content

Commit 8354683

Browse files
committed
BugFix: Fail gracefully when SAML Response is invalid. Fixes #238
Before, given junk input, validatePostResponse would fail with: TypeError: Cannot read property 'documentElement' of null Now we'll fail with: SAMLResponse is not valid base64-encoded XML To make this work, we primarily just needed to as a simple additional error check, but to throw the error properly, we needed to move a bit of logic into a nearby promise, but keeping some variable definitions in the outer scope where they continue to be expected.
1 parent a84a722 commit 8354683

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

lib/passport-saml/saml.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,23 @@ SAML.prototype.validateSignature = function (fullXml, currentNode, cert) {
504504

505505
SAML.prototype.validatePostResponse = function (container, callback) {
506506
var self = this;
507-
var xml = new Buffer(container.SAMLResponse, 'base64').toString('utf8');
508-
var doc = new xmldom.DOMParser().parseFromString(xml);
509507

510-
var inResponseTo = xpath(doc, "/*[local-name()='Response']/@InResponseTo");
511-
if(inResponseTo){
512-
inResponseTo = inResponseTo.length ? inResponseTo[0].nodeValue : null;
513-
}
508+
var xml, doc, inResponseTo;
514509

515510
Q.fcall(function(){
511+
xml = new Buffer(container.SAMLResponse, 'base64').toString('utf8');
512+
doc = new xmldom.DOMParser({
513+
}).parseFromString(xml);
514+
515+
if (!doc.hasOwnProperty('documentElement'))
516+
throw new Error('SAMLResponse is not valid base64-encoded XML');
517+
518+
inResponseTo = xpath(doc, "/*[local-name()='Response']/@InResponseTo");
519+
520+
if(inResponseTo){
521+
inResponseTo = inResponseTo.length ? inResponseTo[0].nodeValue : null;
522+
}
523+
516524
if(self.options.validateInResponseTo){
517525
if (inResponseTo) {
518526
return Q.ninvoke(self.cacheProvider, 'get', inResponseTo)

test/tests.js

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)