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

errors,tls_wrap: migrate to use internal/errors.js #13476

Merged
merged 1 commit into from
Jul 19, 2017
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
32 changes: 16 additions & 16 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const Timer = process.binding('timer_wrap').Timer;
const tls_wrap = process.binding('tls_wrap');
const TCP = process.binding('tcp_wrap').TCP;
const Pipe = process.binding('pipe_wrap').Pipe;
const errors = require('internal/errors');

function onhandshakestart() {
debug('onhandshakestart');
Expand All @@ -59,7 +60,7 @@ function onhandshakestart() {
// state machine and OpenSSL is not re-entrant. We cannot allow the user's
// callback to destroy the connection right now, it would crash and burn.
setImmediate(function() {
var err = new Error('TLS session renegotiation attack detected');
var err = new errors.Error('ERR_TLS_SESSION_ATTACK');
self._emitTLSError(err);
});
}
Expand All @@ -77,14 +78,14 @@ function loadSession(self, hello, cb) {
var once = false;
function onSession(err, session) {
if (once)
return cb(new Error('TLS session callback was called 2 times'));
return cb(new errors.Error('ERR_MULTIPLE_CALLBACK'));
once = true;

if (err)
return cb(err);

if (!self._handle)
return cb(new Error('Socket is closed'));
return cb(new errors.Error('ERR_SOCKET_CLOSED'));

self._handle.loadSession(session);
cb(null);
Expand All @@ -106,14 +107,14 @@ function loadSNI(self, servername, cb) {
var once = false;
self._SNICallback(servername, function(err, context) {
if (once)
return cb(new Error('TLS SNI callback was called 2 times'));
return cb(new errors.Error('ERR_MULTIPLE_CALLBACK'));
once = true;

if (err)
return cb(err);

if (!self._handle)
return cb(new Error('Socket is closed'));
return cb(new errors.Error('ERR_SOCKET_CLOSED'));

// TODO(indutny): eventually disallow raw `SecureContext`
if (context)
Expand Down Expand Up @@ -152,14 +153,14 @@ function requestOCSP(self, hello, ctx, cb) {
var once = false;
function onOCSP(err, response) {
if (once)
return cb(new Error('TLS OCSP callback was called 2 times'));
return cb(new errors.Error('ERR_MULTIPLE_CALLBACK'));
once = true;

if (err)
return cb(err);

if (!self._handle)
return cb(new Error('Socket is closed'));
return cb(new errors.Error('ERR_SOCKET_CLOSED'));

if (response)
self._handle.setOCSPResponse(response);
Expand Down Expand Up @@ -192,7 +193,7 @@ function oncertcb(info) {
return self.destroy(err);

if (!self._handle)
return self.destroy(new Error('Socket is closed'));
return self.destroy(new errors.Error('ERR_SOCKET_CLOSED'));

try {
self._handle.certCbDone();
Expand Down Expand Up @@ -221,7 +222,7 @@ function onnewsession(key, session) {
once = true;

if (!self._handle)
return self.destroy(new Error('Socket is closed'));
return self.destroy(new errors.Error('ERR_SOCKET_CLOSED'));

self._handle.newSessionDone();

Expand Down Expand Up @@ -552,7 +553,7 @@ TLSSocket.prototype.renegotiate = function(options, callback) {
}
if (!this._handle.renegotiate()) {
if (callback) {
process.nextTick(callback, new Error('Failed to renegotiate'));
process.nextTick(callback, new errors.Error('ERR_TLS_RENEGOTIATE'));
}
return false;
}
Expand All @@ -578,7 +579,7 @@ TLSSocket.prototype.getTLSTicket = function getTLSTicket() {
};

TLSSocket.prototype._handleTimeout = function() {
this._emitTLSError(new Error('TLS handshake timeout'));
this._emitTLSError(new errors.Error('ERR_TLS_HANDSHAKE_TIMEOUT'));
};

TLSSocket.prototype._emitTLSError = function(err) {
Expand Down Expand Up @@ -780,7 +781,7 @@ function Server(options, listener) {
} else if (options == null || typeof options === 'object') {
options = options || {};
} else {
throw new TypeError('options must be an object');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
}


Expand Down Expand Up @@ -811,7 +812,7 @@ function Server(options, listener) {
var timeout = options.handshakeTimeout || (120 * 1000);

if (typeof timeout !== 'number') {
throw new TypeError('handshakeTimeout must be a number');
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'timeout', 'number');
}

if (self.sessionTimeout) {
Expand Down Expand Up @@ -949,7 +950,7 @@ Server.prototype.setOptions = function(options) {
// SNI Contexts High-Level API
Server.prototype.addContext = function(servername, context) {
if (!servername) {
throw new Error('"servername" is required parameter for Server.addContext');
throw new errors.Error('ERR_TLS_REQUIRED_SERVER_NAME');
}

var re = new RegExp('^' +
Expand Down Expand Up @@ -1088,8 +1089,7 @@ exports.connect = function(...args /* [port,] [host,] [options,] [cb] */) {
// specified in options.
var ekeyinfo = socket.getEphemeralKeyInfo();
if (ekeyinfo.type === 'DH' && ekeyinfo.size < options.minDHSize) {
var err = new Error('DH parameter size ' + ekeyinfo.size +
' is less than ' + options.minDHSize);
var err = new errors.Error('ERR_TLS_DH_PARAM_SIZE', ekeyinfo.size);
socket.emit('error', err);
socket.destroy();
return;
Expand Down
10 changes: 9 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,24 @@ E('ERR_NO_CRYPTO', 'Node.js is not compiled with OpenSSL crypto support');
E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported');
E('ERR_PARSE_HISTORY_DATA', 'Could not parse history data in %s');
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound');
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
E('ERR_SOCKET_BAD_TYPE',
'Bad socket type specified. Valid types are: udp4, udp6');
E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data');
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
E('ERR_SOCKET_CLOSED', 'Socket is closed');
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed');
E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed');
E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode');
E('ERR_TLS_CERT_ALTNAME_INVALID',
'Hostname/IP does not match certificate\'s altnames: %s');
E('ERR_TLS_DH_PARAM_SIZE', (size) =>
`DH parameter size ${size} is less than 2048`);
E('ERR_TLS_HANDSHAKE_TIMEOUT', 'TLS handshake timeout');
E('ERR_TLS_RENEGOTIATION_FAILED', 'Failed to renegotiate');
E('ERR_TLS_REQUIRED_SERVER_NAME',
'"servername" is required parameter for Server.addContext');
E('ERR_TLS_SESSION_ATTACK', 'TSL session renegotiation attack detected');
E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
'Calling transform done when still transforming');
E('ERR_TRANSFORM_WITH_LENGTH_0',
Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-tls-basic-validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ assert.throws(() => tls.createServer({ecdhCurve: 1}),
/TypeError: ECDH curve name must be a string/);

assert.throws(() => tls.createServer({handshakeTimeout: 'abcd'}),
/TypeError: handshakeTimeout must be a number/);
common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "timeout" argument must be of type number'
})
);

assert.throws(() => tls.createServer({sessionTimeout: 'abcd'}),
/TypeError: Session timeout must be a 32-bit integer/);
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-tls-client-mindhsize.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ function test(size, err, next) {
if (err) {
client.on('error', function(e) {
nerror++;
assert.strictEqual(e.message,
'DH parameter size 1024 is less than 2048');
assert.strictEqual(e.code, 'ERR_TLS_DH_PARAM_SIZE');
server.close();
});
}
Expand Down
7 changes: 6 additions & 1 deletion test/parallel/test-tls-no-cert-required.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ tls.createServer({})
.listen(0, common.mustCall(close));

assert.throws(() => tls.createServer('this is not valid'),
/^TypeError: options must be an object$/);
common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options" argument must be of type object'
})
);

tls.createServer()
.listen(0, common.mustCall(close));
Expand Down