Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

add tests

Thiago Araujo da Silva3f4568649165
diff --git a/backend/src/lib/tls/index.test.ts b/backend/src/lib/tls/index.test.ts
new file mode 100644
index 0000000000..25fe56c140
--- /dev/null
+++ b/backend/src/lib/tls/index.test.ts
@@ -0,0 +1,47 @@
+import type { PeerCertificate } from "node:tls";
+
+import { describe, expect, test } from "vitest";
+
+import { getTlsServerNameOptions, stripIpv6Brackets } from "./index";
+
+const certFor = (subjectaltname: string) => ({ subject: { CN: "test" }, subjectaltname }) as unknown as PeerCertificate;
+
+describe("stripIpv6Brackets", () => {
+ test("strips the brackets URL.hostname keeps around an IPv6 literal", () => {
+ expect(stripIpv6Brackets("[2001:db8::1]")).toBe("2001:db8::1");
+ });
+
+ test.each(["10.0.0.5", "2001:db8::1", "vault.example.com", "localhost"])("leaves %s unchanged", (host) => {
+ expect(stripIpv6Brackets(host)).toBe(host);
+ });
+});
+
+describe("getTlsServerNameOptions", () => {
+ test("passes a hostname through as servername", () => {
+ expect(getTlsServerNameOptions("vault.example.com")).toEqual({ servername: "vault.example.com" });
+ });
+
+ test("returns an undefined servername when there is no host", () => {
+ expect(getTlsServerNameOptions(undefined)).toEqual({ servername: undefined });
+ expect(getTlsServerNameOptions("")).toEqual({ servername: "" });
+ });
+
+ test.each(["10.0.0.5", "2001:db8::1", "[2001:db8::1]"])("never sets servername for the IP %s", (host) => {
+ const options = getTlsServerNameOptions(host);
+ expect(options).not.toHaveProperty("servername");
+ expect(options.checkServerIdentity).toBeTypeOf("function");
+ });
+
+ test("verifies an IPv4 host against the certificate, ignoring the dialed hostname", () => {
+ const { checkServerIdentity } = getTlsServerNameOptions("10.0.0.5");
+ expect(checkServerIdentity?.("localhost", certFor("IP Address:10.0.0.5"))).toBeUndefined();
+ expect(checkServerIdentity?.("localhost", certFor("IP Address:10.0.0.6"))).toBeInstanceOf(Error);
+ expect(checkServerIdentity?.("localhost", certFor("DNS:localhost"))).toBeInstanceOf(Error);
+ });
+
+ test("verifies a bracketed IPv6 host against the bare address in the certificate", () => {
+ const { checkServerIdentity } = getTlsServerNameOptions("[2001:db8::1]");
+ expect(checkServerIdentity?.("localhost", certFor("IP Address:2001:db8::1"))).toBeUndefined();
+ expect(checkServerIdentity?.("localhost", certFor("IP Address:2001:db8::2"))).toBeInstanceOf(Error);
+ });
+});