tighten validation, add tests
This commit is contained in:
1 parent
272e4b5014
commit
08043a5ea0
7 files changed
+95
-10
No files matched your search
@@ -572,7 +572,7 @@
|
|||||||
"placeholder": "8.8.8.8"
|
"placeholder": "8.8.8.8"
|
||||||
},
|
},
|
||||||
"dnsRecordType": {
|
"dnsRecordType": {
|
||||||
"label": "DNS Record Type",
|
"label": "DNS record type",
|
||||||
"value": {
|
"value": {
|
||||||
"A": "A",
|
"A": "A",
|
||||||
"AAAA": "AAAA",
|
"AAAA": "AAAA",
|
||||||
|
|||||||
@@ -16,11 +16,19 @@ export class DNSProvider implements IStatusProvider<DNSStatusPayload> {
|
|||||||
|
|
||||||
async handle(monitor: Monitor): Promise<MonitorStatusResponse<DNSStatusPayload>> {
|
async handle(monitor: Monitor): Promise<MonitorStatusResponse<DNSStatusPayload>> {
|
||||||
try {
|
try {
|
||||||
const { url: hostname, dnsServer, dnsRecordType = "A" } = monitor;
|
const { url, dnsServer, dnsRecordType = "A" } = monitor;
|
||||||
if (!dnsServer) {
|
if (!dnsServer) {
|
||||||
throw new Error("DNS server is required for DNS monitoring");
|
throw new Error("DNS server is required for DNS monitoring");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let hostname: string;
|
||||||
|
try {
|
||||||
|
const withScheme = /^[a-z]+:\/\//i.test(url) ? url : `http://${url}`; // Ensure URL has a scheme for proper parsing
|
||||||
|
hostname = new URL(withScheme).hostname;
|
||||||
|
} catch {
|
||||||
|
hostname = url;
|
||||||
|
}
|
||||||
|
|
||||||
const resolver = this.createResolver();
|
const resolver = this.createResolver();
|
||||||
resolver.setServers([dnsServer]);
|
resolver.setServers([dnsServer]);
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { booleanCoercion } from "./shared.js";
|
import { booleanCoercion } from "./shared.js";
|
||||||
import { GeoContinents } from "@/types/geoCheck.js";
|
import { GeoContinents } from "@/types/geoCheck.js";
|
||||||
|
import { MonitorTypes } from "@/types/monitor.js";
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Check Validations
|
// Check Validations
|
||||||
@@ -11,7 +12,7 @@ export const getChecksParamValidation = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const getChecksQueryValidation = z.object({
|
export const getChecksQueryValidation = z.object({
|
||||||
type: z.enum(["http", "ping", "pagespeed", "hardware", "docker", "port", "game", "grpc", "websocket", "dns"]).optional(),
|
type: z.enum(MonitorTypes).optional(),
|
||||||
sortOrder: z.enum(["asc", "desc"]),
|
sortOrder: z.enum(["asc", "desc"]),
|
||||||
limit: z.coerce.number().optional(),
|
limit: z.coerce.number().optional(),
|
||||||
dateRange: z.enum(["recent", "hour", "day", "week", "month", "all"]),
|
dateRange: z.enum(["recent", "hour", "day", "week", "month", "all"]),
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { booleanCoercion, dnsServerValidation } from "./shared.js";
|
import { booleanCoercion, dnsHostnameRegex, dnsServerValidation } from "./shared.js";
|
||||||
import { GeoContinents } from "@/types/geoCheck.js";
|
import { GeoContinents } from "@/types/geoCheck.js";
|
||||||
import { DnsRecordTypes, MonitorMatchMethods, MonitorStatuses, MonitorTypes } from "@/types/monitor.js";
|
import { DnsRecordTypes, MonitorMatchMethods, MonitorStatuses, MonitorTypes } from "@/types/monitor.js";
|
||||||
|
|
||||||
@@ -39,7 +39,18 @@ export const getCertificateParamValidation = z.object({
|
|||||||
monitorId: z.string().min(1, "Monitor ID is required"),
|
monitorId: z.string().min(1, "Monitor ID is required"),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const createMonitorBodyValidation = z.object({
|
const refineDnsHostname = (body: { type?: string; url?: string }, ctx: z.RefinementCtx) => {
|
||||||
|
if (body.type === "dns" && body.url && !dnsHostnameRegex.test(body.url)) {
|
||||||
|
ctx.addIssue({
|
||||||
|
code: "custom",
|
||||||
|
path: ["url"],
|
||||||
|
message: "Enter a valid domain (e.g. www.example.com)",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createMonitorBodyValidation = z
|
||||||
|
.object({
|
||||||
_id: z.string().optional(),
|
_id: z.string().optional(),
|
||||||
name: z.string().min(1, "Name is required"),
|
name: z.string().min(1, "Name is required"),
|
||||||
description: z.union([z.string(), z.literal("")]).optional(),
|
description: z.union([z.string(), z.literal("")]).optional(),
|
||||||
@@ -70,9 +81,11 @@ export const createMonitorBodyValidation = z.object({
|
|||||||
geoCheckInterval: z.number().min(300000).optional(),
|
geoCheckInterval: z.number().min(300000).optional(),
|
||||||
dnsServer: dnsServerValidation.optional(),
|
dnsServer: dnsServerValidation.optional(),
|
||||||
dnsRecordType: z.enum(DnsRecordTypes).optional(),
|
dnsRecordType: z.enum(DnsRecordTypes).optional(),
|
||||||
});
|
})
|
||||||
|
.superRefine(refineDnsHostname);
|
||||||
|
|
||||||
export const editMonitorBodyValidation = z.object({
|
export const editMonitorBodyValidation = z
|
||||||
|
.object({
|
||||||
name: z.string().optional(),
|
name: z.string().optional(),
|
||||||
type: z.enum(MonitorTypes).optional(),
|
type: z.enum(MonitorTypes).optional(),
|
||||||
url: z.string().optional(),
|
url: z.string().optional(),
|
||||||
@@ -101,7 +114,8 @@ export const editMonitorBodyValidation = z.object({
|
|||||||
geoCheckInterval: z.number().min(300000).optional(),
|
geoCheckInterval: z.number().min(300000).optional(),
|
||||||
dnsServer: dnsServerValidation.optional(),
|
dnsServer: dnsServerValidation.optional(),
|
||||||
dnsRecordType: z.enum(DnsRecordTypes).optional(),
|
dnsRecordType: z.enum(DnsRecordTypes).optional(),
|
||||||
});
|
})
|
||||||
|
.superRefine(refineDnsHostname);
|
||||||
|
|
||||||
export const pauseMonitorParamValidation = z.object({
|
export const pauseMonitorParamValidation = z.object({
|
||||||
monitorId: z.string().min(1, "Monitor ID is required"),
|
monitorId: z.string().min(1, "Monitor ID is required"),
|
||||||
@@ -124,7 +138,8 @@ export const getUptimeDetailsByIdQueryValidation = z.object({
|
|||||||
normalize: booleanCoercion.optional(),
|
normalize: booleanCoercion.optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const importedMonitorSchema = z.object({
|
const importedMonitorSchema = z
|
||||||
|
.object({
|
||||||
id: z.string().optional(),
|
id: z.string().optional(),
|
||||||
userId: z.string().optional(),
|
userId: z.string().optional(),
|
||||||
teamId: z.string().optional(),
|
teamId: z.string().optional(),
|
||||||
@@ -166,7 +181,8 @@ const importedMonitorSchema = z.object({
|
|||||||
dnsRecordType: z.enum(DnsRecordTypes).optional(),
|
dnsRecordType: z.enum(DnsRecordTypes).optional(),
|
||||||
createdAt: z.string().optional(),
|
createdAt: z.string().optional(),
|
||||||
updatedAt: z.string().optional(),
|
updatedAt: z.string().optional(),
|
||||||
});
|
})
|
||||||
|
.superRefine(refineDnsHostname);
|
||||||
|
|
||||||
export const importMonitorsBodyValidation = z.object({
|
export const importMonitorsBodyValidation = z.object({
|
||||||
monitors: z.array(importedMonitorSchema).min(1, "At least one monitor is required"),
|
monitors: z.array(importedMonitorSchema).min(1, "At least one monitor is required"),
|
||||||
|
|||||||
@@ -20,6 +20,11 @@ export const dnsServerValidation = z
|
|||||||
.min(1, "DNS server is required")
|
.min(1, "DNS server is required")
|
||||||
.refine((v) => z.ipv4().safeParse(v).success || z.ipv6().safeParse(v).success, "Enter a valid IPv4 or IPv6 address (e.g. 8.8.8.8)");
|
.refine((v) => z.ipv4().safeParse(v).success || z.ipv6().safeParse(v).success, "Enter a valid IPv4 or IPv6 address (e.g. 8.8.8.8)");
|
||||||
|
|
||||||
|
// Hostname (FQDN) — labels of 1-63 alphanumerics/hyphens separated by dots,
|
||||||
|
// no scheme, port, path, or whitespace. Total length ≤ 253. Kept in sync with
|
||||||
|
// the client-side regex in client/src/Validation/monitor.ts.
|
||||||
|
export const dnsHostnameRegex = /^(?=.{1,253}$)([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,63}$/;
|
||||||
|
|
||||||
export const booleanCoercion = z.preprocess((val) => {
|
export const booleanCoercion = z.preprocess((val) => {
|
||||||
if (val === "true" || val === true) return true;
|
if (val === "true" || val === true) return true;
|
||||||
if (val === "false" || val === false) return false;
|
if (val === "false" || val === false) return false;
|
||||||
|
|||||||
@@ -187,6 +187,28 @@ describe("DNSProvider", () => {
|
|||||||
await expect(provider.handle(makeMonitor())).rejects.toThrow("Error performing DNS check");
|
await expect(provider.handle(makeMonitor())).rejects.toThrow("Error performing DNS check");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("strips scheme from url before querying the resolver", async () => {
|
||||||
|
const provider = new DNSProvider(createResolver);
|
||||||
|
const result = await provider.handle(makeMonitor({ url: "https://www.google.ca" }));
|
||||||
|
|
||||||
|
expect(mockResolverInstance.resolve4).toHaveBeenCalledWith("www.google.ca");
|
||||||
|
expect(result.payload?.hostname).toBe("www.google.ca");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("strips port and path from url before querying the resolver", async () => {
|
||||||
|
const provider = new DNSProvider(createResolver);
|
||||||
|
await provider.handle(makeMonitor({ url: "example.com:53/some/path" }));
|
||||||
|
|
||||||
|
expect(mockResolverInstance.resolve4).toHaveBeenCalledWith("example.com");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("passes a bare hostname through unchanged", async () => {
|
||||||
|
const provider = new DNSProvider(createResolver);
|
||||||
|
await provider.handle(makeMonitor({ url: "example.com" }));
|
||||||
|
|
||||||
|
expect(mockResolverInstance.resolve4).toHaveBeenCalledWith("example.com");
|
||||||
|
});
|
||||||
|
|
||||||
it("stringifies non-Error throws from outside timeRequest", async () => {
|
it("stringifies non-Error throws from outside timeRequest", async () => {
|
||||||
mockResolverInstance.setServers.mockImplementationOnce(() => {
|
mockResolverInstance.setServers.mockImplementationOnce(() => {
|
||||||
throw 42;
|
throw 42;
|
||||||
|
|||||||
@@ -51,6 +51,39 @@ describe("monitorValidation — DNS fields", () => {
|
|||||||
expect(parsed.dnsServer).toBeUndefined();
|
expect(parsed.dnsServer).toBeUndefined();
|
||||||
expect(parsed.dnsRecordType).toBeUndefined();
|
expect(parsed.dnsRecordType).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("rejects a DNS monitor whose url has a scheme or path", () => {
|
||||||
|
for (const badUrl of ["https://example.com", "example.com/path", "example.com:53", " example.com"]) {
|
||||||
|
expect(() =>
|
||||||
|
createMonitorBodyValidation.parse({
|
||||||
|
...baseDnsBody,
|
||||||
|
url: badUrl,
|
||||||
|
dnsServer: "8.8.8.8",
|
||||||
|
dnsRecordType: "A",
|
||||||
|
})
|
||||||
|
).toThrow();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects a DNS monitor with an invalid dnsServer", () => {
|
||||||
|
expect(() =>
|
||||||
|
createMonitorBodyValidation.parse({
|
||||||
|
...baseDnsBody,
|
||||||
|
dnsServer: "not-an-ip",
|
||||||
|
dnsRecordType: "A",
|
||||||
|
})
|
||||||
|
).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not enforce the hostname rule for non-DNS monitors", () => {
|
||||||
|
const parsed = createMonitorBodyValidation.parse({
|
||||||
|
name: "HTTP check",
|
||||||
|
type: "http",
|
||||||
|
url: "https://example.com/some/path",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(parsed.url).toBe("https://example.com/some/path");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("editMonitorBodyValidation", () => {
|
describe("editMonitorBodyValidation", () => {
|
||||||
|
|||||||
Reference in new issue
Block a user