Skip to content

Commit

Permalink
https: throw Error if required params missing
Browse files Browse the repository at this point in the history
Throw an error when required parameters are missing. Handles ciphers that requires no auth.
Does not throw error If pfx option is provided.
Additional tests added for the same.

Fixes: #3024
PR-URL: #3064
  • Loading branch information
kulkarniankita committed Nov 6, 2015
1 parent 017fc5b commit fae020e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,9 +869,25 @@ Server.prototype.setOptions = function(options) {
}

if (options.pfx) this.pfx = options.pfx;
if (options.key) this.key = options.key;
var defaultCiphers = options.ciphers === tls.DEFAULT_CIPHERS;
if (!options.key) {
if ((options.ciphers === undefined || defaultCiphers) && !options.pfx) {
throw new Error('key is a required parameter for Server.createServer');
}
} else {
this.key = options.key;
}

if (options.passphrase) this.passphrase = options.passphrase;
if (options.cert) this.cert = options.cert;

if (!options.cert) {
if ((options.ciphers === undefined || defaultCiphers) && !options.pfx) {
throw new Error('cert is a required parameter for Server.createServer');
}
} else {
this.cert = options.cert;
}

if (options.ca) this.ca = options.ca;
if (options.secureProtocol) this.secureProtocol = options.secureProtocol;
if (options.crl) this.crl = options.crl;
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-https-pfx.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,28 @@ var options = {
rejectUnauthorized: false
};

var options1 = {
host: '127.0.0.1',
port: common.PORT,
path: '/',
pfx: pfx,
passphrase: 'sample',
requestCert: true
};

var server = https.createServer(options, function(req, res) {
assert.equal(req.socket.authorized, false); // not a client cert
assert.equal(req.socket.authorizationError, 'DEPTH_ZERO_SELF_SIGNED_CERT');
res.writeHead(200);
res.end('OK');
});

assert.doesNotThrow(() => https.createServer(options1, assert.fail),
'cert is a required parameter for Server.createServer');

assert.doesNotThrow(() => https.createServer(options1, assert.fail),
'key is a required parameter for Server.createServer');

server.listen(options.port, options.host, function() {
var data = '';

Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-https-server-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const https = require('https');
const fs = require('fs');

const options1 = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem', 'ascii'),
crt: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem', 'ascii')
};

const options2 = {
ky: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem', 'ascii'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem', 'ascii')
};

assert.throws(() => https.createServer(options1, assert.fail),
'cert is a required parameter for Server.createServer');

assert.throws(() => https.createServer(options2, assert.fail),
'key is a required parameter for Server.createServer');

0 comments on commit fae020e

Please sign in to comment.