diff --git a/lib/passport-saml/saml.js b/lib/passport-saml/saml.js index e9ca8a2b..a06c906d 100644 --- a/lib/passport-saml/saml.js +++ b/lib/passport-saml/saml.js @@ -504,15 +504,23 @@ SAML.prototype.validateSignature = function (fullXml, currentNode, cert) { SAML.prototype.validatePostResponse = function (container, callback) { var self = this; - var xml = new Buffer(container.SAMLResponse, 'base64').toString('utf8'); - var doc = new xmldom.DOMParser().parseFromString(xml); - var inResponseTo = xpath(doc, "/*[local-name()='Response']/@InResponseTo"); - if(inResponseTo){ - inResponseTo = inResponseTo.length ? inResponseTo[0].nodeValue : null; - } + var xml, doc, inResponseTo; Q.fcall(function(){ + xml = new Buffer(container.SAMLResponse, 'base64').toString('utf8'); + doc = new xmldom.DOMParser({ + }).parseFromString(xml); + + if (!doc.hasOwnProperty('documentElement')) + throw new Error('SAMLResponse is not valid base64-encoded XML'); + + inResponseTo = xpath(doc, "/*[local-name()='Response']/@InResponseTo"); + + if(inResponseTo){ + inResponseTo = inResponseTo.length ? inResponseTo[0].nodeValue : null; + } + if(self.options.validateInResponseTo){ if (inResponseTo) { return Q.ninvoke(self.cacheProvider, 'get', inResponseTo) diff --git a/test/tests.js b/test/tests.js index 89279373..96a2a1ef 100644 --- a/test/tests.js +++ b/test/tests.js @@ -700,6 +700,15 @@ describe( 'passport-saml /', function() { }); describe("validatePostResponse checks /", function() { + it('response with junk content should explain the XML or base64 is not valid', function(done) { + var samlObj = new SAML( { cert: TEST_CERT }); + samlObj.validatePostResponse({SAMLResponse: "BOOM"} , function( err, profile, logout ) { + should.exist( err ); + err.message.should.match( /SAMLResponse is not valid base64-encoded XML/ ); +// should.exist( err.statusXml ); + done(); + }); + }); it('response with error status message should generate appropriate error', function(done) { var xml = 'https://idp.testshib.org/idp/shibbolethRequired NameID format not supported'; var base64xml = new Buffer( xml ).toString('base64');