Skip to content

Commit 37933d6

Browse files
KevinKevin
authored andcommitted
node: tls: accept ALPNProtocols for undici compatibility
1 parent 36ed2d9 commit 37933d6

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/node/internal/internal_tls_wrap.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ import {
3232
tryReadStart,
3333
} from 'node-internal:internal_net';
3434
import { JSStreamSocket } from 'node-internal:internal_tls_jsstream';
35-
import { checkServerIdentity } from 'node-internal:internal_tls';
35+
import {
36+
checkServerIdentity,
37+
convertALPNProtocols,
38+
} from 'node-internal:internal_tls';
3639
import type {
3740
ConnectionOptions,
3841
TlsOptions,
@@ -194,8 +197,12 @@ export function TLSSocket(
194197
}
195198

196199
if (tlsOptions.ALPNProtocols !== undefined) {
197-
// Does not apply to Cloudflare Workers.
198-
throw new ERR_OPTION_NOT_IMPLEMENTED('options.ALPNProtocols');
200+
// connect()/startTls() does not expose ALPN selection yet.
201+
// Undici's buildConnector always passes ALPNProtocols for TLS connections,
202+
// so rejecting it breaks clients like @elastic/elasticsearch. Accept the
203+
// option and preserve Node.js input validation behavior.
204+
const normalizedAlpn = { ALPNProtocols: Buffer.alloc(0) };
205+
convertALPNProtocols(tlsOptions.ALPNProtocols, normalizedAlpn);
199206
}
200207

201208
if (tlsOptions.SNICallback !== undefined) {

src/workerd/api/node/tests/tls-nodejs-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,34 @@ export const testConvertALPNProtocols = {
753753
},
754754
};
755755

756+
export const testConnectAcceptsAlpnProtocols = {
757+
async test(_ctrl, env) {
758+
const socket = tls.connect({
759+
port: env.ECHO_SERVER_PORT,
760+
ALPNProtocols: ['http/1.1'],
761+
});
762+
763+
await once(socket, 'secureConnect');
764+
socket.destroy();
765+
},
766+
};
767+
768+
export const testConnectRejectsInvalidAlpnProtocolLength = {
769+
async test() {
770+
throws(
771+
() =>
772+
tls.connect({
773+
port: 42,
774+
lookup() {},
775+
ALPNProtocols: [new String('a').repeat(500)],
776+
}),
777+
{
778+
code: 'ERR_OUT_OF_RANGE',
779+
}
780+
);
781+
},
782+
};
783+
756784
export const testStartTlsBehaviorOnUpgrade = {
757785
async test(ctrl, env) {
758786
const { promise, resolve, reject } = Promise.withResolvers();

0 commit comments

Comments
 (0)