Skip to content
This repository was archived by the owner on Jul 3, 2026. It is now read-only.

Commit 6cec7bd

Browse files
authored
fix: catch possible SAML response signing error (#112)
* fix: catch possible saml response signing error * fix: pin cheerio-select dependency to avoid introduced breaking changes * fix: cleanup test fixture * fix: adapt test for different node versions * fix: avoid case of double callbacks
1 parent c20421d commit 6cec7bd

2 files changed

Lines changed: 52 additions & 7 deletions

File tree

lib/samlp.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,15 @@ function getSamlResponse(samlConfig, user, callback) {
110110
}, function (err, samlAssertion) {
111111
if (err) return callback(err);
112112

113-
var SAMLResponse = buildSamlResponse(Object.assign({}, options, {
114-
samlAssertion: samlAssertion,
115-
samlStatusCode: options.samlStatusCode || constants.STATUS.SUCCESS
116-
}));
113+
var SAMLResponse;
114+
try {
115+
SAMLResponse = buildSamlResponse(Object.assign({}, options, {
116+
samlAssertion: samlAssertion,
117+
samlStatusCode: options.samlStatusCode || constants.STATUS.SUCCESS
118+
}));
119+
} catch (err) {
120+
return callback(err);
121+
}
117122

118123
callback(null, SAMLResponse);
119124
});

test/samlp.tests.js

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,15 +641,23 @@ describe('samlp', function () {
641641
});
642642

643643
describe('response signing', function () {
644-
function doSAMLRequest(testSamlResponse) {
644+
645+
function doRawSAMLRequest(testResponse) {
645646
request.get({
646647
jar: request.jar(),
647648
uri: 'http://localhost:5050/samlp?SAMLRequest=fZBPb4MwDMW%2FSuQ7fxrYhCygYqumVeo0VOgOu2U0WpEgYXGo9vGXwdDaS2%2BO7fi990vX333HztJQq1UGKz8EJlWjj636zOBQP3kJrPOURN8NWIz2pPbya5RkmfunCKdBBqNRqAW1hEr0ktA2WBUvO%2BR%2BiIPRVje6A1YQSWOd0KNWNPbSVNKc20Ye9rsMTtYOhEEgGgK2cQqtEnYytUyO%2F01g241zy6P4zpVEo9wqskLZDHi4irww9nhSc45xhDH3o%2BT%2BHVj5Z%2BShVXO8W64%2F5iXC57ouvfK1qoG9LZjcAsxQcBI3FzRunxULAsh%2FY7lUNKTBxaV8fl3Dzn8A&RelayState=123'
648-
}, function (err, response, body) {
649+
}, function (err, response) {
649650
expect(err).to.equal(null);
651+
652+
testResponse(response);
653+
});
654+
}
655+
656+
function doSAMLRequest(testSamlResponse) {
657+
doRawSAMLRequest(function(response) {
650658
expect(response.statusCode).to.equal(200);
651659

652-
var SAMLResponse = cheerio.load(body)('input[name="SAMLResponse"]').attr('value');
660+
var SAMLResponse = cheerio.load(response.body)('input[name="SAMLResponse"]').attr('value');
653661
var samlResponse = new Buffer(SAMLResponse, 'base64').toString();
654662
var doc = new xmldom.DOMParser().parseFromString(samlResponse);
655663
testSamlResponse(doc);
@@ -670,6 +678,20 @@ describe('samlp', function () {
670678
done();
671679
});
672680
});
681+
682+
describe('when invalid signing key is used', function () {
683+
before(function () {
684+
server.options.key = 'invalid_signing_key';
685+
});
686+
687+
it('should return an error', function (done) {
688+
doRawSAMLRequest(function (response) {
689+
expect(response.statusCode).to.equal(400);
690+
expect(response.body).to.match(/error:\w+:PEM routines:\w+:no start line/);
691+
done();
692+
});
693+
});
694+
});
673695
});
674696

675697
describe('signResponse=true and signAssertion is undefined', function () {
@@ -701,6 +723,24 @@ describe('samlp', function () {
701723
done();
702724
});
703725
});
726+
727+
describe('when invalid signing key is used', function () {
728+
before(function () {
729+
server.options.key = 'invalid_signing_key';
730+
});
731+
732+
after(function () {
733+
delete server.options.key;
734+
});
735+
736+
it('should return an error', function (done) {
737+
doRawSAMLRequest(function (response) {
738+
expect(response.statusCode).to.equal(400);
739+
expect(response.body).to.match(/error:\w+:PEM routines:\w+:no start line/);
740+
done();
741+
});
742+
});
743+
});
704744
});
705745

706746
describe('signResponse=false and signAssertion=true', function () {

0 commit comments

Comments
 (0)