Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/connectors/StandaloneConnector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createConnection, IpcNetConnectOpts, TcpNetConnectOpts } from "net";
import { createConnection, IpcNetConnectOpts, TcpNetConnectOpts, isIP } from "net";
import { connect as createTLSConnection, ConnectionOptions } from "tls";
import { NetStream } from "../types";
import { CONNECTION_CLOSED_ERROR_MSG } from "../utils";
Expand Down Expand Up @@ -41,6 +41,12 @@ export default class StandaloneConnector extends AbstractConnector {

if (options.tls) {
Object.assign(connectionOptions, options.tls);
if ("host" in connectionOptions && !("servername" in connectionOptions)) {
const host = (connectionOptions as TcpOptions).host;
if (host && !isIP(host)) {
(connectionOptions as any).servername = host;
}
}
Comment thread
cursor[bot] marked this conversation as resolved.
}
Comment on lines 42 to 50

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding the tls.servername even when tls is undefined would be a good improvement.

In my case, where I'm using only the tls.servername, I would need to set an empty tls: {} to trigger this servername logic, which would be kinda weird.

I believe setting the servername for non-TLS connections isn't a breaking change, right?


// TODO:
Expand Down
35 changes: 35 additions & 0 deletions test/unit/connectors/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,40 @@ describe("StandaloneConnector", () => {
});
connector.disconnect();
});

it("uses host as tls servername when not explicitly provided", async () => {
const spy = sinon.spy(tls, "connect");
const connector = new StandaloneConnector({
host: "cache.internal",
port: 6379,
tls: { rejectUnauthorized: false },
});
await connector.connect(() => {});
expect(spy.calledOnce).to.eql(true);
expect(spy.firstCall.args[0]).to.eql({
host: "cache.internal",
port: 6379,
rejectUnauthorized: false,
servername: "cache.internal",
});
connector.disconnect();
});

it("does not set tls servername from host when host is an IP", async () => {
const spy = sinon.spy(tls, "connect");
const connector = new StandaloneConnector({
host: "127.0.0.1",
port: 6379,
tls: { rejectUnauthorized: false },
});
await connector.connect(() => {});
expect(spy.calledOnce).to.eql(true);
expect(spy.firstCall.args[0]).to.eql({
host: "127.0.0.1",
port: 6379,
rejectUnauthorized: false,
});
connector.disconnect();
});
});
});
Loading