Skip to content

Commit

Permalink
normalize line endings before signature validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mhassan1 authored and markstos committed Nov 5, 2020
1 parent b349e4b commit 02c6c5a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/passport-saml/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ class SAML {
if (totalReferencedNodes.length > 1) {
return false;
}
fullXml = fullXml.replace(/\r\n?/g, '\n');
return sig.checkSignature(fullXml);
}

Expand Down
26 changes: 23 additions & 3 deletions test/test-signatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@ describe('Signatures', function() {
done(ex);
}
},
testOneResponse = ( pathToXml, shouldErrorWith, amountOfSignatureChecks = 1 ) => {
testOneResponseBody = ( samlResponseBody, shouldErrorWith, amountOfSignatureChecks = 1 ) => {
return done => {
//== Instantiate new instance before every test
const samlObj = new SAML({ cert });
//== Spy on `validateSignature` to be able to count how many times it has been called
const validateSignatureSpy = sinon.spy(samlObj, 'validateSignature');

//== Create a body bases on an XML an run the test in `func`
samlObj.validatePostResponse(createBody(pathToXml), tryCatchTest(done, function( error ) {
//== Run the test in `func`
samlObj.validatePostResponse(samlResponseBody, tryCatchTest(done, function( error ) {
//== Assert error. If the error is `SAML assertion expired` we made it past the certificate validation
shouldErrorWith ? error.should.eql(new Error(shouldErrorWith)) : error.should.eql(new Error('SAML assertion expired'));
//== Assert times `validateSignature` was called
validateSignatureSpy.callCount.should.eql(amountOfSignatureChecks);
done();
}));
};
},
testOneResponse = ( pathToXml, ...args ) => {
//== Create a body based on an XML and run the test
return testOneResponseBody(createBody(pathToXml), ...args);
};

describe('Signatures on saml:Response - Only 1 saml:Assertion', () => {
Expand Down Expand Up @@ -80,4 +84,20 @@ describe('Signatures', function() {

});

describe('Signature on saml:Response with non-LF line endings', () => {
const samlResponseXml = fs.readFileSync(__dirname + '/static/signatures/valid/response.root-signed.assertion-signed.xml').toString();
const makeBody = str => ({ SAMLResponse: Buffer.from(str).toString('base64') });

it('CRLF line endings', done => {
const body = makeBody(samlResponseXml.replace(/\n/g, '\r\n'));
testOneResponseBody(body, false, 1)(done);
});

it('CR line endings', done => {
const body = makeBody(samlResponseXml.replace(/\n/g, '\r'));
testOneResponseBody(body, false, 1)(done);
});

});

});

0 comments on commit 02c6c5a

Please sign in to comment.