Skip to content

Commit db2eb4f

Browse files
committed
fix(dns): send SRV and CAA values to Cloudflare as structured data
Cloudflare treats content as read-only for SRV and CAA and expects a data object instead, so both types were rejected on write. The client now parses the inline value into the fields Cloudflare wants, and builds the payload before the lookup request so a malformed value fails without spending an API call. The form rejects a malformed SRV or CAA value up front and shows the expected shape, so the error lands on the field instead of coming back from the provider.
1 parent 9c4ad14 commit db2eb4f

3 files changed

Lines changed: 144 additions & 21 deletions

File tree

‎apps/dokploy/__test__/dns/cloudflare.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,76 @@ describe("cloudflareClient MX priority", () => {
164164
});
165165
});
166166

167+
describe("cloudflareClient structured records", () => {
168+
const stubCreate = () =>
169+
mockFetch
170+
.mockResolvedValueOnce(cfSuccess([]))
171+
.mockResolvedValueOnce(cfSuccess({ id: "rec-1" }));
172+
173+
it("sends SRV values as structured data", async () => {
174+
stubCreate();
175+
176+
await cloudflareClient.upsertRecord(config, {
177+
zoneId: "zone-1",
178+
type: "SRV",
179+
name: "_sip._tcp.example.com",
180+
content: "1 10 5269 talk.example.com",
181+
});
182+
183+
const [, init] = mockFetch.mock.calls[1] as [string, RequestInit];
184+
const body = JSON.parse(init.body as string);
185+
expect(body.data).toEqual({
186+
priority: 1,
187+
weight: 10,
188+
port: 5269,
189+
target: "talk.example.com",
190+
});
191+
expect(body.content).toBeUndefined();
192+
});
193+
194+
it("sends CAA values as structured data and drops the quotes", async () => {
195+
stubCreate();
196+
197+
await cloudflareClient.upsertRecord(config, {
198+
zoneId: "zone-1",
199+
type: "CAA",
200+
name: "example.com",
201+
content: '0 issue "letsencrypt.org"',
202+
});
203+
204+
const [, init] = mockFetch.mock.calls[1] as [string, RequestInit];
205+
const body = JSON.parse(init.body as string);
206+
expect(body.data).toEqual({
207+
flags: 0,
208+
tag: "issue",
209+
value: "letsencrypt.org",
210+
});
211+
expect(body.content).toBeUndefined();
212+
});
213+
214+
it("rejects an SRV value that is missing a part", async () => {
215+
await expect(
216+
cloudflareClient.upsertRecord(config, {
217+
zoneId: "zone-1",
218+
type: "SRV",
219+
name: "_sip._tcp.example.com",
220+
content: "1 10 5269",
221+
}),
222+
).rejects.toThrow(/SRV value/);
223+
});
224+
225+
it("rejects a CAA value that has no tag", async () => {
226+
await expect(
227+
cloudflareClient.upsertRecord(config, {
228+
zoneId: "zone-1",
229+
type: "CAA",
230+
name: "example.com",
231+
content: "0",
232+
}),
233+
).rejects.toThrow(/CAA value/);
234+
});
235+
});
236+
167237
describe("cloudflareClient proxy status", () => {
168238
it("sends the proxy status for proxiable types", async () => {
169239
mockFetch

‎apps/dokploy/components/dashboard/settings/dns/dns-record-panel.tsx‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,29 @@ const valueFields: Record<
6868
PTR: { label: "Target", placeholder: "host.example.com" },
6969
};
7070

71-
const DnsRecordSchema = z.object({
72-
type: z.enum(DNS_RECORD_TYPES),
73-
name: z.string().min(1, { message: "Name is required" }),
74-
content: z.string().min(1, { message: "Content is required" }),
75-
ttl: z.string(),
76-
proxied: z.boolean(),
77-
});
71+
const structuredValuePatterns: Partial<Record<RecordType, RegExp>> = {
72+
SRV: /^\d+\s+\d+\s+\d+\s+\S+$/,
73+
CAA: /^\d+\s+\S+\s+.+$/,
74+
};
75+
76+
const DnsRecordSchema = z
77+
.object({
78+
type: z.enum(DNS_RECORD_TYPES),
79+
name: z.string().min(1, { message: "Name is required" }),
80+
content: z.string().min(1, { message: "Content is required" }),
81+
ttl: z.string(),
82+
proxied: z.boolean(),
83+
})
84+
.superRefine((data, ctx) => {
85+
const pattern = structuredValuePatterns[data.type];
86+
if (pattern && !pattern.test(data.content.trim())) {
87+
ctx.addIssue({
88+
code: z.ZodIssueCode.custom,
89+
path: ["content"],
90+
message: `Expected ${valueFields[data.type].placeholder}`,
91+
});
92+
}
93+
});
7894

7995
type DnsRecordForm = z.infer<typeof DnsRecordSchema>;
8096

‎packages/server/src/utils/dns/cloudflare.ts‎

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,51 @@ const proxySettings = (record: { type: string; proxied?: boolean }) =>
3131
? { proxied: record.proxied }
3232
: {};
3333

34-
const splitPriority = (record: { type: string; content: string }) => {
35-
if (record.type !== "MX") {
36-
return { content: record.content };
34+
const buildValue = (record: { type: string; content: string }) => {
35+
const value = record.content.trim();
36+
37+
if (record.type === "MX") {
38+
const match = /^(\d+)\s+(\S.*)$/.exec(value);
39+
return match
40+
? { content: match[2] as string, priority: Number(match[1]) }
41+
: { content: value, priority: 10 };
3742
}
38-
const match = /^\s*(\d+)\s+(\S.*)$/.exec(record.content);
39-
return match
40-
? { content: match[2] as string, priority: Number(match[1]) }
41-
: { content: record.content.trim(), priority: 10 };
43+
44+
if (record.type === "SRV") {
45+
const parts = value.split(/\s+/);
46+
const [priority, weight, port, target] = parts;
47+
if (parts.length !== 4 || !target) {
48+
throw new Error(
49+
`Cloudflare: an SRV value must be "priority weight port target", got "${value}"`,
50+
);
51+
}
52+
return {
53+
data: {
54+
priority: Number(priority),
55+
weight: Number(weight),
56+
port: Number(port),
57+
target,
58+
},
59+
};
60+
}
61+
62+
if (record.type === "CAA") {
63+
const match = /^(\d+)\s+(\S+)\s+"?([^"]+)"?$/.exec(value);
64+
if (!match) {
65+
throw new Error(
66+
`Cloudflare: a CAA value must be \`flags tag "value"\`, got "${value}"`,
67+
);
68+
}
69+
return {
70+
data: {
71+
flags: Number(match[1]),
72+
tag: match[2] as string,
73+
value: match[3] as string,
74+
},
75+
};
76+
}
77+
78+
return { content: value };
4279
};
4380

4481
const cfFetch = async <T>(
@@ -123,19 +160,19 @@ export const cloudflareClient: DnsClient<CloudflareConfig> = {
123160
},
124161

125162
async upsertRecord(config, record) {
126-
const existing = await cfFetch<{ id: string }[]>(
127-
config,
128-
`/zones/${record.zoneId}/dns_records?type=${record.type}&name=${encodeURIComponent(record.name)}`,
129-
);
130-
131163
const payload = {
132164
type: record.type,
133165
name: record.name,
134-
...splitPriority(record),
166+
...buildValue(record),
135167
...proxySettings(record),
136168
ttl: record.ttl ?? 1,
137169
};
138170

171+
const existing = await cfFetch<{ id: string }[]>(
172+
config,
173+
`/zones/${record.zoneId}/dns_records?type=${record.type}&name=${encodeURIComponent(record.name)}`,
174+
);
175+
139176
const existingRecord = existing[0];
140177
if (existingRecord) {
141178
const updated = await cfFetch<{ id: string }>(
@@ -163,7 +200,7 @@ export const cloudflareClient: DnsClient<CloudflareConfig> = {
163200
body: JSON.stringify({
164201
type: record.type,
165202
name: record.name,
166-
...splitPriority(record),
203+
...buildValue(record),
167204
...proxySettings(record),
168205
ttl: record.ttl ?? 1,
169206
}),

0 commit comments

Comments
 (0)