Skip to content

Commit

Permalink
Add optional ca fallback to tls.rootCertificates (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanMcCurdy authored Sep 27, 2024
1 parent 23a73cf commit c39498d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/protocol/tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ exports.connect = function connect(options, cb) {
if (!('servername' in options)) {
options.servername = options.host;
}
if (!('sslUseDefaultTrustStore' in options) || (options.sslUseDefaultTrustStore === true)) {
if ('ca' in options) {
options.ca = [].concat(options.ca, tls.rootCertificates);
}
}
} else {
createSocket = exports.createSocket;
}
Expand Down
47 changes: 47 additions & 0 deletions test/lib.tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'use strict';

var tcp = require('../lib/protocol/tcp');
var tls = require('tls');
var createSocket = tcp.createSocket;
var createSecureSocket = tcp.createSecureSocket;
var socket = {
Expand Down Expand Up @@ -66,5 +67,51 @@ describe('Lib', function () {
tcp.connect({}, done).should.equal(socket);
});

it('should fallback to default trusted CAs', function (done) {
var testCase = 0;
tcp.createSecureSocket = function tlsConnect(options, cb) {
switch (testCase) {
case 0:
(options.ca === undefined).should.equal(true);
break;
case 1:
options.ca[0].should.equal("DummyCert");
options.ca.length.should.equal(tls.rootCertificates.length + 1);
for(var i = 0; i < tls.rootCertificates.length; ++i) {
options.ca[i+1].should.equal(tls.rootCertificates[i]);
}
break;
case 2:
options.ca[0].should.equal("DummyCert");
options.ca[1].should.equal("DummyCert2");
options.ca.length.should.equal(tls.rootCertificates.length + 2);
for(var i = 0; i < tls.rootCertificates.length; ++i) {
options.ca[i+2].should.equal(tls.rootCertificates[i]);
}
break;
case 3:
options.ca.should.equal("DummyCert");
break;
default:
break;
}
process.nextTick(cb);
return socket;
}
tcp.connect({useTLS: true}, () => {
++testCase; // 1
tcp.connect({ca: "DummyCert"}, () => {
++testCase; // 2
tcp.connect({ca: ["DummyCert", "DummyCert2"], sslUseDefaultTrustStore: true}, () => {
++testCase; // 3
tcp.connect({ca: "DummyCert", sslUseDefaultTrustStore: false}, () => {
tcp.createSecureSocket = createSecureSocket;
done();
}).should.equal(socket);
}).should.equal(socket);
}).should.equal(socket);
});
});

});
});

0 comments on commit c39498d

Please sign in to comment.