Passing the monitor's own port to ssl-checker exposed an adjacent gap found in review: a plain-HTTP monitor on a non-default port, such as http://host:8080, now had a TLS handshake attempted against a port that serves no TLS. That surfaced to the client as a 500 carrying a raw OpenSSL error ("packet length too long") instead of the previous silent "N/A". The endpoint never checked the scheme — it was already wrong for http:// monitors, just quietly so, since every such lookup went to port 443. Guard on the protocol and fail with a 400 before opening a socket. Adds tests for both plain-HTTP cases (default and custom port), each asserting the checker is never invoked. Verified by removing the guard: both fail without it.
98 lines
3.4 KiB
TypeScript
98 lines
3.4 KiB
TypeScript
import { describe, expect, it } from "@jest/globals";
|
|
import { fetchMonitorCertificate } from "../../../src/api/controllers/controllerUtils.ts";
|
|
import type { Monitor } from "../../../src/domain/monitors/monitor.type.ts";
|
|
import type { SSLDetails, SSLOptions } from "ssl-checker";
|
|
|
|
// Regression cover for #3646: the certificate probe parsed the monitor URL but
|
|
// dropped its port, so a monitor on a non-standard port had its certificate
|
|
// checked on 443 and surfaced as "N/A" in the UI.
|
|
|
|
const VALID_CERT: SSLDetails = {
|
|
daysRemaining: 30,
|
|
valid: true,
|
|
validFrom: "2026-01-01T00:00:00.000Z",
|
|
validTo: "2026-12-31T00:00:00.000Z",
|
|
validFor: ["example.com"],
|
|
};
|
|
|
|
/** Records the arguments the checker was called with. */
|
|
const spyChecker = (cert: SSLDetails = VALID_CERT) => {
|
|
const calls: Array<{ hostname: string; options?: SSLOptions }> = [];
|
|
const checker = (async (hostname: string, options?: SSLOptions) => {
|
|
calls.push({ hostname, options });
|
|
return cert;
|
|
}) as unknown as typeof import("ssl-checker").default;
|
|
return { checker, calls };
|
|
};
|
|
|
|
const monitorWithUrl = (url: string) => ({ url }) as Monitor;
|
|
|
|
describe("fetchMonitorCertificate", () => {
|
|
it("probes the port named in the monitor URL", async () => {
|
|
const { checker, calls } = spyChecker();
|
|
|
|
await fetchMonitorCertificate(checker, monitorWithUrl("https://example.com:54321"));
|
|
|
|
expect(calls).toHaveLength(1);
|
|
expect(calls[0].hostname).toBe("example.com");
|
|
expect(calls[0].options?.port).toBe(54321);
|
|
});
|
|
|
|
it("falls back to 443 when the URL names no port", async () => {
|
|
const { checker, calls } = spyChecker();
|
|
|
|
await fetchMonitorCertificate(checker, monitorWithUrl("https://example.com"));
|
|
|
|
expect(calls[0].options?.port).toBe(443);
|
|
});
|
|
|
|
it("passes an explicit 443 through unchanged", async () => {
|
|
const { checker, calls } = spyChecker();
|
|
|
|
await fetchMonitorCertificate(checker, monitorWithUrl("https://example.com:443"));
|
|
|
|
expect(calls[0].options?.port).toBe(443);
|
|
});
|
|
|
|
it("never passes NaN as the port", async () => {
|
|
const { checker, calls } = spyChecker();
|
|
|
|
await fetchMonitorCertificate(checker, monitorWithUrl("https://example.com"));
|
|
|
|
expect(Number.isNaN(calls[0].options?.port)).toBe(false);
|
|
});
|
|
|
|
it("rejects a plain-HTTP monitor without opening a TLS connection", async () => {
|
|
const { checker, calls } = spyChecker();
|
|
|
|
// Probing a non-TLS port would surface a raw OpenSSL handshake error.
|
|
await expect(fetchMonitorCertificate(checker, monitorWithUrl("http://example.com:8080"))).rejects.toThrow(
|
|
"Certificate is only available for HTTPS monitors"
|
|
);
|
|
expect(calls).toHaveLength(0);
|
|
});
|
|
|
|
it("rejects a plain-HTTP monitor on the default port", async () => {
|
|
const { checker, calls } = spyChecker();
|
|
|
|
await expect(fetchMonitorCertificate(checker, monitorWithUrl("http://example.com"))).rejects.toThrow(
|
|
"Certificate is only available for HTTPS monitors"
|
|
);
|
|
expect(calls).toHaveLength(0);
|
|
});
|
|
|
|
it("returns the certificate details it received", async () => {
|
|
const { checker } = spyChecker();
|
|
|
|
const cert = await fetchMonitorCertificate(checker, monitorWithUrl("https://example.com:8443"));
|
|
|
|
expect(cert.validTo).toBe(VALID_CERT.validTo);
|
|
});
|
|
|
|
it("throws when the certificate has no expiry", async () => {
|
|
const { checker } = spyChecker({ ...VALID_CERT, validTo: undefined as unknown as string });
|
|
|
|
await expect(fetchMonitorCertificate(checker, monitorWithUrl("https://example.com:8443"))).rejects.toThrow("Certificate not found");
|
|
});
|
|
});
|