Skip to content

Commit 3ccfeb4

Browse files
committed
tls: migrate tls.js to use internal/errors.js
Migrate tls.js to use internal/errors.js as per #11273 PR-URL: #13994 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent f406a7e commit 3ccfeb4

7 files changed

+18
-19
lines changed

lib/internal/errors.js

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
174174
E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed');
175175
E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed');
176176
E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode');
177+
E('ERR_TLS_CERT_ALTNAME_INVALID',
178+
'Hostname/IP does not match certificate\'s altnames: %s');
177179
E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
178180
'Calling transform done when still transforming');
179181
E('ERR_TRANSFORM_WITH_LENGTH_0',

lib/tls.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
'use strict';
2323

24+
const errors = require('internal/errors');
2425
const internalUtil = require('internal/util');
2526
internalUtil.assertCrypto();
2627

@@ -219,8 +220,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
219220
}
220221

221222
if (!valid) {
222-
const err = new Error(
223-
`Hostname/IP doesn't match certificate's altnames: "${reason}"`);
223+
const err = new errors.Error('ERR_TLS_CERT_ALTNAME_INVALID', reason);
224224
err.reason = reason;
225225
err.host = host;
226226
err.cert = cert;

test/parallel/test-https-strict.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,9 @@ function allListening() {
170170

171171
// server1: host 'agent1', signed by ca1
172172
makeReq('/inv1', port1, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE');
173-
makeReq('/inv1-ca1', port1,
174-
'Hostname/IP doesn\'t match certificate\'s altnames: ' +
175-
'"Host: localhost. is not cert\'s CN: agent1"',
173+
makeReq('/inv1-ca1', port1, 'ERR_TLS_CERT_ALTNAME_INVALID',
176174
null, ca1);
177-
makeReq('/inv1-ca1ca2', port1,
178-
'Hostname/IP doesn\'t match certificate\'s altnames: ' +
179-
'"Host: localhost. is not cert\'s CN: agent1"',
175+
makeReq('/inv1-ca1ca2', port1, 'ERR_TLS_CERT_ALTNAME_INVALID',
180176
null, [ca1, ca2]);
181177
makeReq('/val1-ca1', port1, null, 'agent1', ca1);
182178
makeReq('/val1-ca1ca2', port1, null, 'agent1', [ca1, ca2]);
@@ -193,13 +189,8 @@ function allListening() {
193189

194190
// server3: host 'agent3', signed by ca2
195191
makeReq('/inv3', port3, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE');
196-
makeReq('/inv3-ca2', port3,
197-
'Hostname/IP doesn\'t match certificate\'s altnames: ' +
198-
'"Host: localhost. is not cert\'s CN: agent3"',
199-
null, ca2);
200-
makeReq('/inv3-ca1ca2', port3,
201-
'Hostname/IP doesn\'t match certificate\'s altnames: ' +
202-
'"Host: localhost. is not cert\'s CN: agent3"',
192+
makeReq('/inv3-ca2', port3, 'ERR_TLS_CERT_ALTNAME_INVALID', null, ca2);
193+
makeReq('/inv3-ca1ca2', port3, 'ERR_TLS_CERT_ALTNAME_INVALID',
203194
null, [ca1, ca2]);
204195
makeReq('/val3-ca2', port3, null, 'agent3', ca2);
205196
makeReq('/val3-ca1ca2', port3, null, 'agent3', [ca1, ca2]);

test/parallel/test-internal-errors.js

+6
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,9 @@ assert.throws(
229229
code: 'ERR_ASSERTION',
230230
message: /^At least one arg needs to be specified$/
231231
}));
232+
233+
234+
// Test ERR_TLS_CERT_ALTNAME_INVALID
235+
assert.strictEqual(
236+
errors.message('ERR_TLS_CERT_ALTNAME_INVALID', ['altname']),
237+
'Hostname/IP does not match certificate\'s altnames: altname');

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const fs = require('fs');
2929
const path = require('path');
3030
const tls = require('tls');
3131

32-
const hosterr = /Hostname\/IP doesn't match certificate's altnames/;
3332
const testCases =
3433
[{ ca: ['ca1-cert'],
3534
key: 'agent2-key',
@@ -101,7 +100,7 @@ function testServers(index, servers, clientOptions, cb) {
101100
clientOptions.port = this.address().port;
102101
const client = tls.connect(clientOptions, common.mustCall(function() {
103102
const authorized = client.authorized ||
104-
hosterr.test(client.authorizationError);
103+
(client.authorizationError === 'ERR_TLS_CERT_ALTNAME_INVALID');
105104

106105
console.error(`expected: ${ok} authed: ${authorized}`);
107106

test/parallel/test-tls-sni-option.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ function startTest() {
141141
options.port = server.address().port;
142142
const client = tls.connect(options, function() {
143143
clientResults.push(
144-
/Hostname\/IP doesn't/.test(client.authorizationError || ''));
144+
client.authorizationError &&
145+
(client.authorizationError === 'ERR_TLS_CERT_ALTNAME_INVALID'));
145146
client.destroy();
146147

147148
next();

test/parallel/test-tls-sni-server-client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function startTest() {
113113
const client = tls.connect(options, function() {
114114
clientResults.push(
115115
client.authorizationError &&
116-
/Hostname\/IP doesn't/.test(client.authorizationError));
116+
(client.authorizationError === 'ERR_TLS_CERT_ALTNAME_INVALID'));
117117
client.destroy();
118118

119119
// Continue

0 commit comments

Comments
 (0)