Skip to content

Commit

Permalink
[fix] rejectUnauthorized was not getting honored due to a check to …
Browse files Browse the repository at this point in the history
…see if the connection was authorized (CA signature was verified). By passing this check if `rejectUnauthorized` is set to `false`. (#422)

FIX #421
  • Loading branch information
aricart authored Apr 26, 2021
1 parent bd10f4c commit 18824e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/node_transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ export class NodeTransport implements Transport {

async startTLS(): Promise<TLSSocket> {
let tlsError: Error;
let tlsOpts = { socket: this.socket, servername: this.tlsName };
let tlsOpts = {
socket: this.socket,
servername: this.tlsName,
rejectUnauthorized: true,
};
if (typeof this.options.tls === "object") {
try {
const certOpts = await this.loadClientCerts() || {};
Expand All @@ -224,6 +228,10 @@ export class NodeTransport implements Transport {
tlsError = err;
});
tlsSocket.on("secureConnect", () => {
// socket won't be authorized, if the user disabled it
if (tlsOpts.rejectUnauthorized === false) {
return;
}
if (!tlsSocket.authorized) {
throw tlsSocket.authorizationError;
}
Expand Down
15 changes: 15 additions & 0 deletions test/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ test("tls - connects with proper ca", async (t) => {
t.pass();
});

test("tls - connects with rejectUnauthorized is honored", async (t) => {
const ns = await NatsServer.start(tlsConfig);
const nc = await connect({
servers: `localhost:${ns.port}`,
tls: {
rejectUnauthorized: false,
},
});
await nc.flush();
t.false(nc.protocol.transport.socket.authorized);
await nc.close();
await ns.stop();
t.pass();
});

test("tls - client auth", async (t) => {
const ns = await NatsServer.start(tlsConfig);

Expand Down

0 comments on commit 18824e7

Please sign in to comment.