Skip to content

Commit b2692cd

Browse files
authored
fix(domain): validate hostname format to reject invalid characters (Dokploy#4729)
* fix(domain): validate hostname format to reject invalid characters Underscores and other invalid characters were accepted in domain inputs with no validation, causing Let's Encrypt to silently fail certificate issuance while Dokploy fell back to a self-signed cert. Fixes Dokploy#4716 * fix(create-server): update SSH key label for clarity in server creation form
1 parent db0cb66 commit b2692cd

8 files changed

Lines changed: 99 additions & 9 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { VALID_HOSTNAME_REGEX } from "@dokploy/server";
2+
import { describe, expect, it } from "vitest";
3+
4+
describe("VALID_HOSTNAME_REGEX", () => {
5+
it.each([
6+
"example.com",
7+
"sub.example.com",
8+
"bbn-client.example.com",
9+
"a.b.c.example.co",
10+
"xn--80ak6aa92e.com",
11+
"123.example.com",
12+
])("accepts valid hostname %s", (host) => {
13+
expect(VALID_HOSTNAME_REGEX.test(host)).toBe(true);
14+
});
15+
16+
it.each([
17+
"bbn_client.example.com",
18+
"-example.com",
19+
"example-.com",
20+
"example",
21+
"exa mple.com",
22+
"example..com",
23+
"",
24+
`a${"a".repeat(63)}.com`,
25+
])("rejects invalid hostname %s", (host) => {
26+
expect(VALID_HOSTNAME_REGEX.test(host)).toBe(false);
27+
});
28+
29+
// IDNs (Cyrillic, German umlauts, etc.) must be submitted in their
30+
// ACME/punycode form ("xn--...") — that's what Let's Encrypt issues
31+
// certificates for, so raw Unicode labels are rejected here.
32+
it.each(["пример.рф", "bücher.de", "日本語.jp"])(
33+
"rejects raw unicode IDN %s",
34+
(host) => {
35+
expect(VALID_HOSTNAME_REGEX.test(host)).toBe(false);
36+
},
37+
);
38+
39+
it.each([
40+
"xn--e1afmkfd.xn--p1ai", // punycode for пример.рф
41+
"xn--bcher-kva.de", // punycode for bücher.de
42+
"xn--wgv71a119e.jp", // punycode for 日本語.jp
43+
])("accepts punycode-encoded IDN %s", (host) => {
44+
expect(VALID_HOSTNAME_REGEX.test(host)).toBe(true);
45+
});
46+
});

‎apps/dokploy/components/dashboard/application/domains/handle-domain.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
INVALID_HOSTNAME_MESSAGE,
3+
VALID_HOSTNAME_REGEX,
4+
} from "@dokploy/server/utils/hostname-validation";
15
import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema";
26
import { DatabaseZap, Dices, RefreshCw, X } from "lucide-react";
37
import Link from "next/link";
@@ -53,7 +57,10 @@ export const domain = z
5357
.refine((val) => val === val.trim(), {
5458
message: "Domain name cannot have leading or trailing spaces",
5559
})
56-
.transform((val) => val.trim()),
60+
.transform((val) => val.trim())
61+
.refine((val) => VALID_HOSTNAME_REGEX.test(val), {
62+
message: INVALID_HOSTNAME_MESSAGE,
63+
}),
5764
path: z.string().min(1).optional(),
5865
internalPath: z.string().optional(),
5966
stripPath: z.boolean().optional(),

‎apps/dokploy/components/dashboard/settings/servers/welcome-stripe/create-server.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ export const CreateServer = ({ stepper }: Props) => {
195195
{sshKey.name}
196196
</SelectItem>
197197
))}
198-
<SelectLabel>
199-
Registries ({sshKeys?.length})
200-
</SelectLabel>
198+
<SelectLabel>SSH Keys ({sshKeys?.length})</SelectLabel>
201199
</SelectGroup>
202200
</SelectContent>
203201
</Select>

‎apps/dokploy/components/dashboard/settings/web-domain.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
INVALID_HOSTNAME_MESSAGE,
3+
VALID_HOSTNAME_REGEX,
4+
} from "@dokploy/server/utils/hostname-validation";
15
import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema";
26
import { GlobeIcon } from "lucide-react";
37
import { useEffect } from "react";
@@ -35,7 +39,13 @@ import { api } from "@/utils/api";
3539

3640
const addServerDomain = z
3741
.object({
38-
domain: z.string().trim().toLowerCase(),
42+
domain: z
43+
.string()
44+
.trim()
45+
.toLowerCase()
46+
.refine((val) => VALID_HOSTNAME_REGEX.test(val), {
47+
message: INVALID_HOSTNAME_MESSAGE,
48+
}),
3949
letsEncryptEmail: z.string(),
4050
https: z.boolean().optional(),
4151
certificateType: z.enum(["letsencrypt", "none", "custom"]),

‎apps/dokploy/server/db/validations/domain.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
INVALID_HOSTNAME_MESSAGE,
3+
VALID_HOSTNAME_REGEX,
4+
} from "@dokploy/server/utils/hostname-validation";
15
import { z } from "zod";
26

37
export const domain = z
@@ -8,7 +12,10 @@ export const domain = z
812
.refine((val) => val === val.trim(), {
913
message: "Domain name cannot have leading or trailing spaces",
1014
})
11-
.transform((val) => val.trim()),
15+
.transform((val) => val.trim())
16+
.refine((val) => VALID_HOSTNAME_REGEX.test(val), {
17+
message: INVALID_HOSTNAME_MESSAGE,
18+
}),
1219
path: z.string().min(1).optional(),
1320
port: z
1421
.number()
@@ -45,7 +52,10 @@ export const domainCompose = z
4552
.refine((val) => val === val.trim(), {
4653
message: "Domain name cannot have leading or trailing spaces",
4754
})
48-
.transform((val) => val.trim()),
55+
.transform((val) => val.trim())
56+
.refine((val) => VALID_HOSTNAME_REGEX.test(val), {
57+
message: INVALID_HOSTNAME_MESSAGE,
58+
}),
4959
path: z.string().min(1).optional(),
5060
port: z
5161
.number()

‎packages/server/src/db/validations/domain.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { z } from "zod";
2+
import {
3+
INVALID_HOSTNAME_MESSAGE,
4+
VALID_HOSTNAME_REGEX,
5+
} from "../../utils/hostname-validation";
26

37
export const domain = z
48
.object({
@@ -8,7 +12,10 @@ export const domain = z
812
.refine((val) => val === val.trim(), {
913
message: "Domain name cannot have leading or trailing spaces",
1014
})
11-
.transform((val) => val.trim()),
15+
.transform((val) => val.trim())
16+
.refine((val) => VALID_HOSTNAME_REGEX.test(val), {
17+
message: INVALID_HOSTNAME_MESSAGE,
18+
}),
1219
path: z.string().min(1).optional(),
1320
internalPath: z.string().optional(),
1421
stripPath: z.boolean().optional(),
@@ -71,7 +78,10 @@ export const domainCompose = z
7178
.refine((val) => val === val.trim(), {
7279
message: "Domain name cannot have leading or trailing spaces",
7380
})
74-
.transform((val) => val.trim()),
81+
.transform((val) => val.trim())
82+
.refine((val) => VALID_HOSTNAME_REGEX.test(val), {
83+
message: INVALID_HOSTNAME_MESSAGE,
84+
}),
7585
path: z.string().min(1).optional(),
7686
internalPath: z.string().optional(),
7787
stripPath: z.boolean().optional(),

‎packages/server/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export * from "./utils/filesystem/directory";
104104
export * from "./utils/filesystem/ssh";
105105
export * from "./utils/git-branch-validation";
106106
export * from "./utils/gpu-setup";
107+
export * from "./utils/hostname-validation";
107108
export * from "./utils/notifications/build-error";
108109
export * from "./utils/notifications/build-success";
109110
export * from "./utils/notifications/database-backup";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Valid hostname per RFC 1123: labels of letters, digits and hyphens
2+
// (no leading/trailing hyphen), separated by dots. Underscores are rejected
3+
// because Let's Encrypt refuses to issue certificates for them.
4+
export const VALID_HOSTNAME_REGEX =
5+
/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/;
6+
7+
export const INVALID_HOSTNAME_MESSAGE =
8+
"Invalid domain name. Use only letters, numbers, hyphens and dots (e.g. example.com). Underscores are not allowed.";

0 commit comments

Comments
 (0)