Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

normalize line endings before signature validation #498

Merged
merged 1 commit into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});

});

});