Skip to content

Commit 156d76a

Browse files
committed
Forward-compatible HTTPS options
I recently discovered that `create-servers` (used by `slay`) was not preserving my `requestCert` option. By changing it to pass through all https options by default, it will now enable client certs and other SSL features which are currently blocked (most likely unintentionally). Also changed the code so it does not mutate the original options.
1 parent d8c2ca4 commit 156d76a

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

index.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -122,54 +122,54 @@ module.exports = function createServers(options, listening) {
122122
return onListen('https');
123123
}
124124

125-
var port = +options.https.port || 443,
126-
ssl = options.https,
125+
var ssl = options.https,
126+
port = +ssl.port || 443,
127+
ciphers = ssl.ciphers || CIPHERS,
128+
ca = ssl.ca,
129+
key = fs.readFileSync(path.resolve(ssl.root, ssl.key)),
130+
cert = fs.readFileSync(path.resolve(ssl.root, ssl.cert)),
131+
honorCipherOrder = !!ssl.honorCipherOrder,
127132
server,
128-
args,
129-
ip;
130-
131-
ssl.ciphers = ssl.ciphers || CIPHERS;
133+
args;
132134

133135
//
134136
// Remark: If an array is passed in lets join it like we do the defaults
135137
//
136-
if (Array.isArray(ssl.ciphers)) {
137-
ssl.ciphers = ssl.ciphers.join(':');
138+
if (Array.isArray(ciphers)) {
139+
ciphers = ciphers.join(':');
138140
}
139141

140-
if (ssl.ca && !Array.isArray(ssl.ca)) {
141-
ssl.ca = [ssl.ca];
142+
if (ca && !Array.isArray(ca)) {
143+
ca = [ca];
142144
}
143145

144-
log('https | listening on %d', port);
145-
server = https.createServer({
146+
var finalHttpsOptions = Object.assign({}, ssl, {
146147
//
147148
// Load default SSL key, cert and ca(s).
148149
//
149-
key: fs.readFileSync(path.resolve(ssl.root, ssl.key)),
150-
cert: fs.readFileSync(path.resolve(ssl.root, ssl.cert)),
151-
ca: ssl.ca && ssl.ca.map(
152-
function (file) {
153-
return fs.readFileSync(path.resolve(ssl.root, file));
154-
}
150+
key: key,
151+
cert: cert,
152+
ca: ca && ca.map(
153+
function (file) {
154+
return fs.readFileSync(path.resolve(ssl.root, file));
155+
}
155156
),
156157
//
157158
// Properly expose ciphers for an A+ SSL rating:
158159
// https://certsimple.com/blog/a-plus-node-js-ssl
159160
//
160-
ciphers: ssl.ciphers,
161-
honorCipherOrder: !!ssl.honorCipherOrder,
162-
//
163-
// Optionally support SNI-based SSL.
164-
//
165-
SNICallback: ssl.SNICallback,
161+
ciphers: ciphers,
162+
honorCipherOrder: honorCipherOrder,
166163
//
167164
// Protect against the POODLE attack by disabling SSLv3
168165
// @see http://googleonlinesecurity.blogspot.nl/2014/10/this-poodle-bites-exploiting-ssl-30.html
169166
//
170167
secureProtocol: 'SSLv23_method',
171168
secureOptions: require('constants').SSL_OP_NO_SSLv3
172-
}, ssl.handler || handler);
169+
});
170+
171+
log('https | listening on %d', port);
172+
server = https.createServer(finalHttpsOptions, ssl.handler || handler);
173173

174174
args = [server, port];
175175
if (options.https.host) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"errs": "~0.3.0"
2727
},
2828
"devDependencies": {
29+
"sinon": "^1.17.4",
2930
"tape": "~2.14.0"
3031
}
3132
}

test/create-servers-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
var path = require('path'),
99
http = require('http'),
10+
https = require('https'),
1011
test = require('tape'),
12+
sinon = require('sinon'),
1113
createServers = require('../');
1214

1315
//
@@ -173,3 +175,24 @@ test('http && https with different handlers', function (t) {
173175
});
174176
});
175177
});
178+
179+
test('supports requestCert https option', function (t) {
180+
t.plan(2);
181+
var spy = sinon.spy(https, 'createServer');
182+
createServers({
183+
log: console.log,
184+
https: {
185+
port: 3456,
186+
root: path.join(__dirname, 'fixtures'),
187+
cert: 'agent2-cert.pem',
188+
key: 'agent2-key.pem',
189+
requestCert: true
190+
},
191+
handler: fend
192+
}, function (err, servers) {
193+
t.error(err);
194+
t.equals(spy.lastCall.args[0].requestCert, true, 'should preserve the requestCert option');
195+
servers.https.close();
196+
spy.restore();
197+
});
198+
});

0 commit comments

Comments
 (0)