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`,