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

https: throw error if required params missing #3064

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert(options.cert, 'cert is required parameter for ...'), maybe?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@indutny Right now I test this way:
assert.throws(() => https.createServer(options1, assert.fail),'cert is a required parameter for server.createServer');
Is one better than the other? If so I am curious to understand why?

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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if I want to set the certificate authority for reading only but not the cert/private key.
Example: http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-client-side-ssl-authentication.html

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cbshakumar you mean if cert authority is provided then skip this whole cert/key error handling right?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kulkarniankita It has been a while since I've looked at this. Should it be possible to set the certificate authority and not set a cert?

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');