Skip to content

Commit f67aa56

Browse files
bidipynerefack
authored andcommitted
errors: migrate tls_wrap to use internal/errors
PR-URL: #13476 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent e36166b commit f67aa56

5 files changed

+38
-21
lines changed

lib/_tls_wrap.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const Timer = process.binding('timer_wrap').Timer;
3636
const tls_wrap = process.binding('tls_wrap');
3737
const TCP = process.binding('tcp_wrap').TCP;
3838
const Pipe = process.binding('pipe_wrap').Pipe;
39+
const errors = require('internal/errors');
3940

4041
function onhandshakestart() {
4142
debug('onhandshakestart');
@@ -59,7 +60,7 @@ function onhandshakestart() {
5960
// state machine and OpenSSL is not re-entrant. We cannot allow the user's
6061
// callback to destroy the connection right now, it would crash and burn.
6162
setImmediate(function() {
62-
var err = new Error('TLS session renegotiation attack detected');
63+
var err = new errors.Error('ERR_TLS_SESSION_ATTACK');
6364
self._emitTLSError(err);
6465
});
6566
}
@@ -77,14 +78,14 @@ function loadSession(self, hello, cb) {
7778
var once = false;
7879
function onSession(err, session) {
7980
if (once)
80-
return cb(new Error('TLS session callback was called 2 times'));
81+
return cb(new errors.Error('ERR_MULTIPLE_CALLBACK'));
8182
once = true;
8283

8384
if (err)
8485
return cb(err);
8586

8687
if (!self._handle)
87-
return cb(new Error('Socket is closed'));
88+
return cb(new errors.Error('ERR_SOCKET_CLOSED'));
8889

8990
self._handle.loadSession(session);
9091
cb(null);
@@ -106,14 +107,14 @@ function loadSNI(self, servername, cb) {
106107
var once = false;
107108
self._SNICallback(servername, function(err, context) {
108109
if (once)
109-
return cb(new Error('TLS SNI callback was called 2 times'));
110+
return cb(new errors.Error('ERR_MULTIPLE_CALLBACK'));
110111
once = true;
111112

112113
if (err)
113114
return cb(err);
114115

115116
if (!self._handle)
116-
return cb(new Error('Socket is closed'));
117+
return cb(new errors.Error('ERR_SOCKET_CLOSED'));
117118

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

158159
if (err)
159160
return cb(err);
160161

161162
if (!self._handle)
162-
return cb(new Error('Socket is closed'));
163+
return cb(new errors.Error('ERR_SOCKET_CLOSED'));
163164

164165
if (response)
165166
self._handle.setOCSPResponse(response);
@@ -192,7 +193,7 @@ function oncertcb(info) {
192193
return self.destroy(err);
193194

194195
if (!self._handle)
195-
return self.destroy(new Error('Socket is closed'));
196+
return self.destroy(new errors.Error('ERR_SOCKET_CLOSED'));
196197

197198
try {
198199
self._handle.certCbDone();
@@ -221,7 +222,7 @@ function onnewsession(key, session) {
221222
once = true;
222223

223224
if (!self._handle)
224-
return self.destroy(new Error('Socket is closed'));
225+
return self.destroy(new errors.Error('ERR_SOCKET_CLOSED'));
225226

226227
self._handle.newSessionDone();
227228

@@ -552,7 +553,7 @@ TLSSocket.prototype.renegotiate = function(options, callback) {
552553
}
553554
if (!this._handle.renegotiate()) {
554555
if (callback) {
555-
process.nextTick(callback, new Error('Failed to renegotiate'));
556+
process.nextTick(callback, new errors.Error('ERR_TLS_RENEGOTIATE'));
556557
}
557558
return false;
558559
}
@@ -578,7 +579,7 @@ TLSSocket.prototype.getTLSTicket = function getTLSTicket() {
578579
};
579580

580581
TLSSocket.prototype._handleTimeout = function() {
581-
this._emitTLSError(new Error('TLS handshake timeout'));
582+
this._emitTLSError(new errors.Error('ERR_TLS_HANDSHAKE_TIMEOUT'));
582583
};
583584

584585
TLSSocket.prototype._emitTLSError = function(err) {
@@ -780,7 +781,7 @@ function Server(options, listener) {
780781
} else if (options == null || typeof options === 'object') {
781782
options = options || {};
782783
} else {
783-
throw new TypeError('options must be an object');
784+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'object');
784785
}
785786

786787

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

813814
if (typeof timeout !== 'number') {
814-
throw new TypeError('handshakeTimeout must be a number');
815+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'timeout', 'number');
815816
}
816817

817818
if (self.sessionTimeout) {
@@ -949,7 +950,7 @@ Server.prototype.setOptions = function(options) {
949950
// SNI Contexts High-Level API
950951
Server.prototype.addContext = function(servername, context) {
951952
if (!servername) {
952-
throw new Error('"servername" is required parameter for Server.addContext');
953+
throw new errors.Error('ERR_TLS_REQUIRED_SERVER_NAME');
953954
}
954955

955956
var re = new RegExp('^' +
@@ -1088,8 +1089,7 @@ exports.connect = function(...args /* [port,] [host,] [options,] [cb] */) {
10881089
// specified in options.
10891090
var ekeyinfo = socket.getEphemeralKeyInfo();
10901091
if (ekeyinfo.type === 'DH' && ekeyinfo.size < options.minDHSize) {
1091-
var err = new Error('DH parameter size ' + ekeyinfo.size +
1092-
' is less than ' + options.minDHSize);
1092+
var err = new errors.Error('ERR_TLS_DH_PARAM_SIZE', ekeyinfo.size);
10931093
socket.emit('error', err);
10941094
socket.destroy();
10951095
return;

lib/internal/errors.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,24 @@ E('ERR_NO_CRYPTO', 'Node.js is not compiled with OpenSSL crypto support');
156156
E('ERR_NO_LONGER_SUPPORTED', '%s is no longer supported');
157157
E('ERR_PARSE_HISTORY_DATA', 'Could not parse history data in %s');
158158
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound');
159+
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
159160
E('ERR_SOCKET_BAD_TYPE',
160161
'Bad socket type specified. Valid types are: udp4, udp6');
161162
E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data');
162-
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
163+
E('ERR_SOCKET_CLOSED', 'Socket is closed');
163164
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
164165
E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed');
165166
E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed');
166167
E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode');
167168
E('ERR_TLS_CERT_ALTNAME_INVALID',
168169
'Hostname/IP does not match certificate\'s altnames: %s');
170+
E('ERR_TLS_DH_PARAM_SIZE', (size) =>
171+
`DH parameter size ${size} is less than 2048`);
172+
E('ERR_TLS_HANDSHAKE_TIMEOUT', 'TLS handshake timeout');
173+
E('ERR_TLS_RENEGOTIATION_FAILED', 'Failed to renegotiate');
174+
E('ERR_TLS_REQUIRED_SERVER_NAME',
175+
'"servername" is required parameter for Server.addContext');
176+
E('ERR_TLS_SESSION_ATTACK', 'TSL session renegotiation attack detected');
169177
E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
170178
'Calling transform done when still transforming');
171179
E('ERR_TRANSFORM_WITH_LENGTH_0',

test/parallel/test-tls-basic-validations.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ assert.throws(() => tls.createServer({ecdhCurve: 1}),
2323
/TypeError: ECDH curve name must be a string/);
2424

2525
assert.throws(() => tls.createServer({handshakeTimeout: 'abcd'}),
26-
/TypeError: handshakeTimeout must be a number/);
26+
common.expectsError({
27+
code: 'ERR_INVALID_ARG_TYPE',
28+
type: TypeError,
29+
message: 'The "timeout" argument must be of type number'
30+
})
31+
);
2732

2833
assert.throws(() => tls.createServer({sessionTimeout: 'abcd'}),
2934
/TypeError: Session timeout must be a 32-bit integer/);

test/parallel/test-tls-client-mindhsize.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ function test(size, err, next) {
5151
if (err) {
5252
client.on('error', function(e) {
5353
nerror++;
54-
assert.strictEqual(e.message,
55-
'DH parameter size 1024 is less than 2048');
54+
assert.strictEqual(e.code, 'ERR_TLS_DH_PARAM_SIZE');
5655
server.close();
5756
});
5857
}

test/parallel/test-tls-no-cert-required.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ tls.createServer({})
4040
.listen(0, common.mustCall(close));
4141

4242
assert.throws(() => tls.createServer('this is not valid'),
43-
/^TypeError: options must be an object$/);
43+
common.expectsError({
44+
code: 'ERR_INVALID_ARG_TYPE',
45+
type: TypeError,
46+
message: 'The "options" argument must be of type object'
47+
})
48+
);
4449

4550
tls.createServer()
4651
.listen(0, common.mustCall(close));

0 commit comments

Comments
 (0)