Skip to content

Commit 87b4e3e

Browse files
qubytecjihrig
authored andcommitted
tls: accept array of protocols in TLSSocket
Brings the ALPNProtocols & NPNProtocols options of TLSSocket in line with the documentation. i.e. an array of strings for protocols may be used, not only a buffer. PR-URL: #16655 Fixes: https://github.com/node/issues/16643 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]>
1 parent 1b090c9 commit 87b4e3e

File tree

2 files changed

+89
-11
lines changed

2 files changed

+89
-11
lines changed

lib/_tls_wrap.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,15 @@ function initRead(tls, wrapped) {
280280
* Provides a wrap of socket stream to do encrypted communication.
281281
*/
282282

283-
function TLSSocket(socket, options) {
284-
if (options === undefined)
285-
this._tlsOptions = {};
286-
else
287-
this._tlsOptions = options;
283+
function TLSSocket(socket, opts) {
284+
const tlsOptions = Object.assign({}, opts);
285+
286+
if (tlsOptions.NPNProtocols)
287+
tls.convertNPNProtocols(tlsOptions.NPNProtocols, tlsOptions);
288+
if (tlsOptions.ALPNProtocols)
289+
tls.convertALPNProtocols(tlsOptions.ALPNProtocols, tlsOptions);
290+
291+
this._tlsOptions = tlsOptions;
288292
this._secureEstablished = false;
289293
this._securePending = false;
290294
this._newSessionPending = false;
@@ -1099,11 +1103,7 @@ exports.connect = function(...args /* [port,] [host,] [options,] [cb] */) {
10991103
'options.minDHSize is not a positive number: ' +
11001104
options.minDHSize);
11011105

1102-
const NPN = {};
1103-
const ALPN = {};
11041106
const context = options.secureContext || tls.createSecureContext(options);
1105-
tls.convertNPNProtocols(options.NPNProtocols, NPN);
1106-
tls.convertALPNProtocols(options.ALPNProtocols, ALPN);
11071107

11081108
var socket = new TLSSocket(options.socket, {
11091109
pipe: !!options.path,
@@ -1112,8 +1112,8 @@ exports.connect = function(...args /* [port,] [host,] [options,] [cb] */) {
11121112
requestCert: true,
11131113
rejectUnauthorized: options.rejectUnauthorized !== false,
11141114
session: options.session,
1115-
NPNProtocols: NPN.NPNProtocols,
1116-
ALPNProtocols: ALPN.ALPNProtocols,
1115+
NPNProtocols: options.NPNProtocols,
1116+
ALPNProtocols: options.ALPNProtocols,
11171117
requestOCSP: options.requestOCSP
11181118
});
11191119

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
3+
// Test that TLSSocket can take arrays of strings for ALPNProtocols and
4+
// NPNProtocols.
5+
6+
const common = require('../common');
7+
8+
if (!common.hasCrypto)
9+
common.skip('missing crypto');
10+
11+
const tls = require('tls');
12+
13+
new tls.TLSSocket(null, {
14+
ALPNProtocols: ['http/1.1'],
15+
NPNProtocols: ['http/1.1']
16+
});
17+
18+
if (!process.features.tls_npn)
19+
common.skip('node compiled without NPN feature of OpenSSL');
20+
21+
if (!process.features.tls_alpn)
22+
common.skip('node compiled without ALPN feature of OpenSSL');
23+
24+
const assert = require('assert');
25+
const net = require('net');
26+
const fixtures = require('../common/fixtures');
27+
28+
const key = fixtures.readKey('agent1-key.pem');
29+
const cert = fixtures.readKey('agent1-cert.pem');
30+
31+
const protocols = [];
32+
33+
const server = net.createServer(common.mustCall((s) => {
34+
const tlsSocket = new tls.TLSSocket(s, {
35+
isServer: true,
36+
server,
37+
key,
38+
cert,
39+
ALPNProtocols: ['http/1.1'],
40+
NPNProtocols: ['http/1.1']
41+
});
42+
43+
tlsSocket.on('secure', common.mustCall(() => {
44+
protocols.push({
45+
alpnProtocol: tlsSocket.alpnProtocol,
46+
npnProtocol: tlsSocket.npnProtocol
47+
});
48+
tlsSocket.end();
49+
}));
50+
}, 2));
51+
52+
server.listen(0, common.mustCall(() => {
53+
const alpnOpts = {
54+
port: server.address().port,
55+
rejectUnauthorized: false,
56+
ALPNProtocols: ['h2', 'http/1.1']
57+
};
58+
const npnOpts = {
59+
port: server.address().port,
60+
rejectUnauthorized: false,
61+
NPNProtocols: ['h2', 'http/1.1']
62+
};
63+
64+
tls.connect(alpnOpts, function() {
65+
this.end();
66+
67+
tls.connect(npnOpts, function() {
68+
this.end();
69+
70+
server.close();
71+
72+
assert.deepStrictEqual(protocols, [
73+
{ alpnProtocol: 'http/1.1', npnProtocol: false },
74+
{ alpnProtocol: false, npnProtocol: 'http/1.1' }
75+
]);
76+
});
77+
});
78+
}));

0 commit comments

Comments
 (0)