From 058bb69bb158290da7ce6c6f77d46baa5f4fedab Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Sat, 7 Nov 2026 23:12:21 +0200 Subject: [PATCH] grpc-js: Don't set an IP address as the TLS servername Node.js 25 made DEP0123 End-of-Life, so tls.connect throws when servername is an IP address, and secure channels to an IP target never connect. Skip SNI for IP targets, as Node's https agent does. Fixes #3127 Co-Authored-By: Claude Opus 5.5 --- packages/grpc-js/src/channel-credentials.ts | 5 +++-- .../grpc-js/test/test-channel-credentials.ts | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/grpc-js/src/channel-credentials.ts b/packages/grpc-js/src/channel-credentials.ts index 04b162899..debfbead1 100644 --- a/packages/grpc-js/src/channel-credentials.ts +++ b/packages/grpc-js/src/channel-credentials.ts @@ -27,7 +27,7 @@ import { import { CallCredentials } from './call-credentials'; import { CIPHER_SUITES, getDefaultRootsData } from './tls-helpers'; import { CaCertificateUpdate, CaCertificateUpdateListener, CertificateProvider, IdentityCertificateUpdate, IdentityCertificateUpdateListener } from './certificate-provider'; -import { Socket } from 'net'; +import { isIP, Socket } from 'net'; import { ChannelOptions } from './channel-options'; import { GrpcUri, parseUri, splitHostPort } from './uri-parser'; import { getDefaultAuthority } from './resolver'; @@ -237,7 +237,8 @@ function getConnectionOptions(secureContext: SecureContext, verifyOptions: Verif return originalCheckServerIdentity(sslTargetNameOverride, cert); }; connectionOptions.servername = sslTargetNameOverride; - } else { + } else if (!isIP(remoteHost)) { + // Node.js 25+ throws if servername is an IP address (DEP0123) connectionOptions.servername = remoteHost; } if (options['grpc-node.tls_enable_trace']) { diff --git a/packages/grpc-js/test/test-channel-credentials.ts b/packages/grpc-js/test/test-channel-credentials.ts index c439821d7..c516765a0 100644 --- a/packages/grpc-js/test/test-channel-credentials.ts +++ b/packages/grpc-js/test/test-channel-credentials.ts @@ -209,6 +209,24 @@ describe('ChannelCredentials usage', () => { ); assert2.afterMustCallsSatisfied(done); }); + it('Should connect to an IP address target', done => { + const channelCreds = ChannelCredentials.createSsl(caCert, null, null, { + checkServerIdentity: assert2.mustCall((hostname, cert) => { + assert.strictEqual(hostname, '127.0.0.1'); + return undefined; + }), + }); + const client = new echoService(`127.0.0.1:${portNum}`, channelCreds); + client.echo( + { value: 'test value', value2: 3 }, + assert2.mustCall((error: ServiceError, response: any) => { + client.close(); + assert.ifError(error); + assert.deepStrictEqual(response, { value: 'test value', value2: 3 }); + }) + ); + assert2.afterMustCallsSatisfied(done); + }); it('Should handle certificate providers', done => { const certificateProvider = new FileWatcherCertificateProvider({ caCertificateFile: `${__dirname}/fixtures/ca.pem`,