Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Update certificate parsing for certificate chains with multiple certificates #50

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
18 changes: 12 additions & 6 deletions lib/service/util/certificate-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ pub.parseCertificate = function (certificate, part) {
throw new Error('Invalid certificate part specified', { givenPart: part, allowedParts: pub.CERTIFICATE_PART });
}
var cleanString = certificate
.replace(certHeaderFooter[part].header, '')// Remove header and footer, we'll re-add them later
.replace(certHeaderFooter[part].footer, '')
.trim() // Trim to be sure we don't inject a bad new line
.replace(new RegExp(certHeaderFooter[part].header, 'g'), 'CERTHEADERFOOTERHEADER') // Remove headers and footers, we'll re-add them later
.replace(new RegExp(certHeaderFooter[part].footer, 'g'), 'CERTHEADERFOOTERFOOTER')

.replace(/\\n/g, '\n') // Replace escaped new line with proper new line
.replace(/ /g, '\n') // Replace space with new line
.trim(); // Add a final trim as a new line might have slipped in at the end
// Rebuild cert with header and footer
return certHeaderFooter[part].header + '\n' + cleanString + '\n' + certHeaderFooter[part].footer;


.replace(/CERTHEADERFOOTERHEADER\n/g, 'CERTHEADERFOOTERHEADER') // Remove any extra newlines that have snuck in
.replace(/\nCERTHEADERFOOTERFOOTER/g, 'CERTHEADERFOOTERFOOTER')

.replace(/CERTHEADERFOOTERHEADER/g, certHeaderFooter[part].header + '\n') // Replace headers and footers
.replace(/CERTHEADERFOOTERFOOTER/g, '\n' + certHeaderFooter[part].footer);

return cleanString;
};

module.exports = pub;
55 changes: 39 additions & 16 deletions tests/unit/service/util/certificate-parser-unit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,46 @@ describe('certificateParser', function () {

describe('ParseCertificate', function () {
describe('Parse CHAIN part', function () {
it('should give a valid certificate with newline', function (done) {
var certificate = '-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----';
var parsedCertificate = testSubject.parseCertificate(certificate, testSubject.CERTIFICATE_PART.CHAIN);
expect(parsedCertificate).to.equal('-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----');
done();
});
it('should give a valid certificate with space', function (done) {
var certificate = '-----BEGIN CERTIFICATE----- line1 line2 -----END CERTIFICATE-----';
var parsedCertificate = testSubject.parseCertificate(certificate, testSubject.CERTIFICATE_PART.CHAIN);
expect(parsedCertificate).to.equal('-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----');
done();
describe('with one certificate', function () {
it('should give a valid certificate with newline', function (done) {
var certificate = '-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----';
var parsedCertificate = testSubject.parseCertificate(certificate, testSubject.CERTIFICATE_PART.CHAIN);
expect(parsedCertificate).to.equal('-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----');
done();
});
it('should give a valid certificate with space', function (done) {
var certificate = '-----BEGIN CERTIFICATE----- line1 line2 -----END CERTIFICATE-----';
var parsedCertificate = testSubject.parseCertificate(certificate, testSubject.CERTIFICATE_PART.CHAIN);
expect(parsedCertificate).to.equal('-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----');
done();
});
it('should give a valid certificate with escaped newline', function (done) {
var certificate = '-----BEGIN CERTIFICATE-----\\nline1\\nline2\\n-----END CERTIFICATE-----';
var parsedCertificate = testSubject.parseCertificate(certificate, testSubject.CERTIFICATE_PART.CHAIN);
expect(parsedCertificate).to.equal('-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----');
done();
});
});
it('should give a valid certificate with escaped newline', function (done) {
var certificate = '-----BEGIN CERTIFICATE-----\\nline1\\nline2\\n-----END CERTIFICATE-----';
var parsedCertificate = testSubject.parseCertificate(certificate, testSubject.CERTIFICATE_PART.CHAIN);
expect(parsedCertificate).to.equal('-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----');
done();
describe('with multiple certificates', function () {

it('should give a valid certificate with newline', function (done) {
var certificate = '-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----\n-----BEGIN CERTIFICATE-----\nline3\nline4\n-----END CERTIFICATE-----';
var parsedCertificate = testSubject.parseCertificate(certificate, testSubject.CERTIFICATE_PART.CHAIN);
expect(parsedCertificate).to.equal('-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----\n-----BEGIN CERTIFICATE-----\nline3\nline4\n-----END CERTIFICATE-----');
done();
});
it('should give a valid certificate with space', function (done) {
var certificate = '-----BEGIN CERTIFICATE----- line1 line2 -----END CERTIFICATE----- -----BEGIN CERTIFICATE----- line3 line4 -----END CERTIFICATE-----';
var parsedCertificate = testSubject.parseCertificate(certificate, testSubject.CERTIFICATE_PART.CHAIN);
expect(parsedCertificate).to.equal('-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----\n-----BEGIN CERTIFICATE-----\nline3\nline4\n-----END CERTIFICATE-----');
done();
});
it('should give a valid certificate with escaped newline', function (done) {
var certificate = '-----BEGIN CERTIFICATE-----\\nline1\\nline2\\n-----END CERTIFICATE-----\\n-----BEGIN CERTIFICATE-----\\nline3\\nline4\\n-----END CERTIFICATE-----';
var parsedCertificate = testSubject.parseCertificate(certificate, testSubject.CERTIFICATE_PART.CHAIN);
expect(parsedCertificate).to.equal('-----BEGIN CERTIFICATE-----\nline1\nline2\n-----END CERTIFICATE-----\n-----BEGIN CERTIFICATE-----\nline3\nline4\n-----END CERTIFICATE-----');
done();
});
});
});
describe('Parse BODY part', function () {
Expand Down