Skip to content

Commit 91db4e6

Browse files
committed
fix(dns): match record content in upsertRecord to prevent overwriting existing records
Previously, upsertRecord matched only by type and name, silently overwriting any existing record of the same type/name with different content. This caused data loss when users had multiple records of the same type (e.g. multiple TXT records for SPF + site verification). Now upsertRecord also checks the record content/target before deciding to update. If the content differs, a new record is created alongside the existing one instead of replacing it. Affected providers: Cloudflare, Porkbun, Infomaniak, OVH. Tests updated accordingly.
1 parent 1e6ab0f commit 91db4e6

8 files changed

Lines changed: 145 additions & 17 deletions

File tree

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,11 @@ describe("cloudflareClient.upsertRecord", () => {
323323
expect(createInit.method).toBe("POST");
324324
});
325325

326-
it("updates the existing record instead of creating a duplicate", async () => {
326+
it("updates the existing record when content matches", async () => {
327327
mockFetch
328-
.mockResolvedValueOnce(cfSuccess([{ id: "existing-1" }]))
328+
.mockResolvedValueOnce(
329+
cfSuccess([{ id: "existing-1", type: "A", content: "5.6.7.8" }]),
330+
)
329331
.mockResolvedValueOnce(cfSuccess({ id: "existing-1" }));
330332

331333
const result = await cloudflareClient.upsertRecord(config, {
@@ -344,6 +346,25 @@ describe("cloudflareClient.upsertRecord", () => {
344346
expect(updateInit.method).toBe("PUT");
345347
});
346348

349+
it("creates a new record when content differs from existing", async () => {
350+
mockFetch
351+
.mockResolvedValueOnce(
352+
cfSuccess([{ id: "existing-1", type: "A", content: "1.1.1.1" }]),
353+
)
354+
.mockResolvedValueOnce(cfSuccess({ id: "new-2" }));
355+
356+
const result = await cloudflareClient.upsertRecord(config, {
357+
zoneId: "zone-1",
358+
type: "A",
359+
name: "app.example.com",
360+
content: "5.6.7.8",
361+
});
362+
363+
expect(result).toEqual({ id: "new-2" });
364+
const [, createInit] = mockFetch.mock.calls[1] as [string, RequestInit];
365+
expect(createInit.method).toBe("POST");
366+
});
367+
347368
it("defaults ttl to 1 (automatic) when not provided", async () => {
348369
mockFetch
349370
.mockResolvedValueOnce(cfSuccess([]))

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ describe("infomaniakClient.upsertRecord", () => {
203203
});
204204
});
205205

206-
it("updates the existing record instead of creating a duplicate", async () => {
206+
it("updates the existing record when content matches", async () => {
207207
mockFetch
208208
.mockResolvedValueOnce(
209209
ikSuccess([
210-
{ id: 7, type: "A", source: "app", target: "1.1.1.1", ttl: 300 },
210+
{ id: 7, type: "A", source: "app", target: "1.2.3.4", ttl: 300 },
211211
]),
212212
)
213213
.mockResolvedValueOnce(ikSuccess({ id: 7 }));
@@ -228,6 +228,28 @@ describe("infomaniakClient.upsertRecord", () => {
228228
expect(lastBody().ttl).toBe(300);
229229
});
230230

231+
it("creates a new record when content differs from existing", async () => {
232+
mockFetch
233+
.mockResolvedValueOnce(
234+
ikSuccess([
235+
{ id: 7, type: "A", source: "app", target: "1.1.1.1", ttl: 300 },
236+
]),
237+
)
238+
.mockResolvedValueOnce(ikSuccess({ id: 50 }));
239+
240+
const result = await infomaniakClient.upsertRecord(config, {
241+
zoneId: "example.com",
242+
type: "A",
243+
name: "app.example.com",
244+
content: "1.2.3.4",
245+
});
246+
247+
expect(result).toEqual({ id: "50" });
248+
const [url, init] = lastCall();
249+
expect(url).toBe("https://api.infomaniak.com/2/zones/example.com/records");
250+
expect(init.method).toBe("POST");
251+
});
252+
231253
it("writes a root dot as the source for an apex record and strips the trailing dot", async () => {
232254
mockFetch
233255
.mockResolvedValueOnce(ikSuccess([]))
@@ -249,7 +271,7 @@ describe("infomaniakClient.upsertRecord", () => {
249271
mockFetch
250272
.mockResolvedValueOnce(
251273
ikSuccess([
252-
{ id: 8, type: "A", source, target: "1.1.1.1", ttl: 3600 },
274+
{ id: 8, type: "A", source, target: "1.2.3.4", ttl: 3600 },
253275
]),
254276
)
255277
.mockResolvedValueOnce(ikSuccess({ id: 8 }));

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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,21 @@ describe("ovhClient.upsertRecord", () => {
261261
expect(calls[2]?.[1].method).toBe("POST");
262262
});
263263

264-
it("updates the existing record instead of creating a duplicate", async () => {
264+
it("updates the existing record when content matches", async () => {
265265
const cfg = freshConfig();
266-
mockApi(ovhSuccess([4]), ovhSuccess(null), ovhSuccess(null));
266+
mockApi(
267+
ovhSuccess([4]),
268+
ovhSuccess({
269+
id: 4,
270+
zone: "example.com",
271+
fieldType: "A",
272+
subDomain: "app",
273+
target: "1.2.3.4",
274+
ttl: 60,
275+
}),
276+
ovhSuccess(null),
277+
ovhSuccess(null),
278+
);
267279

268280
const result = await ovhClient.upsertRecord(cfg, {
269281
zoneId: "example.com",
@@ -274,9 +286,38 @@ describe("ovhClient.upsertRecord", () => {
274286

275287
expect(result).toEqual({ id: "4" });
276288
const calls = apiCalls();
277-
expect(calls[1]?.[0]).toContain("/domain/zone/example.com/record/4");
278-
expect(calls[1]?.[1].method).toBe("PUT");
279-
expect(calls[2]?.[0]).toContain("/refresh");
289+
expect(calls[2]?.[0]).toContain("/domain/zone/example.com/record/4");
290+
expect(calls[2]?.[1].method).toBe("PUT");
291+
expect(calls[3]?.[0]).toContain("/refresh");
292+
});
293+
294+
it("creates a new record when content differs from existing", async () => {
295+
const cfg = freshConfig();
296+
mockApi(
297+
ovhSuccess([4]),
298+
ovhSuccess({
299+
id: 4,
300+
zone: "example.com",
301+
fieldType: "A",
302+
subDomain: "app",
303+
target: "1.1.1.1",
304+
ttl: 60,
305+
}),
306+
ovhSuccess({ id: 10 }),
307+
ovhSuccess(null),
308+
);
309+
310+
const result = await ovhClient.upsertRecord(cfg, {
311+
zoneId: "example.com",
312+
type: "A",
313+
name: "app.example.com",
314+
content: "5.6.7.8",
315+
});
316+
317+
expect(result).toEqual({ id: "10" });
318+
const calls = apiCalls();
319+
expect(calls[2]?.[1].method).toBe("POST");
320+
expect(calls[3]?.[0]).toContain("/refresh");
280321
});
281322

282323
it("omits the ttl so OVH applies the zone default", async () => {

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ describe("porkbunClient.upsertRecord", () => {
121121
expect(lookupUrl).toContain("/dns/retrieveByNameType/example.com/A/");
122122
});
123123

124-
it("edits the existing record instead of creating a duplicate", async () => {
124+
it("edits the existing record when content matches", async () => {
125125
mockFetch
126-
.mockResolvedValueOnce(pbSuccess({ records: [{ id: "existing-1" }] }))
126+
.mockResolvedValueOnce(
127+
pbSuccess({ records: [{ id: "existing-1", content: "5.6.7.8" }] }),
128+
)
127129
.mockResolvedValueOnce(pbSuccess({}));
128130

129131
const result = await porkbunClient.upsertRecord(config, {
@@ -138,6 +140,30 @@ describe("porkbunClient.upsertRecord", () => {
138140
expect(editUrl).toContain("/dns/edit/example.com/existing-1");
139141
});
140142

143+
it("creates a new record when content differs from existing", async () => {
144+
mockFetch
145+
.mockResolvedValueOnce(
146+
pbSuccess({ records: [{ id: "existing-1", content: "1.1.1.1" }] }),
147+
)
148+
.mockResolvedValueOnce(pbSuccess({ id: "new-2" }));
149+
150+
const result = await porkbunClient.upsertRecord(config, {
151+
zoneId: "example.com",
152+
type: "A",
153+
name: "app.example.com",
154+
content: "5.6.7.8",
155+
});
156+
157+
expect(result).toEqual({ id: "new-2" });
158+
const [createUrl, createInit] = mockFetch.mock.calls[1] as [
159+
string,
160+
RequestInit,
161+
];
162+
expect(createUrl).toContain("/dns/create/example.com");
163+
const body = JSON.parse(createInit.body as string);
164+
expect(body).toMatchObject({ name: "app", type: "A", content: "5.6.7.8" });
165+
});
166+
141167
it("defaults ttl to 600 when not provided", async () => {
142168
mockFetch
143169
.mockResolvedValueOnce(pbSuccess({ records: [] }))

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,16 @@ export const cloudflareClient: DnsClient<CloudflareConfig> = {
168168
ttl: record.ttl ?? 1,
169169
};
170170

171-
const existing = await cfFetch<{ id: string }[]>(
171+
const existing = await cfFetch<
172+
{ id: string; type: string; content: string; priority?: number }[]
173+
>(
172174
config,
173175
`/zones/${record.zoneId}/dns_records?type=${record.type}&name=${encodeURIComponent(record.name)}`,
174176
);
175177

176-
const existingRecord = existing[0];
178+
const existingRecord = existing.find(
179+
(r) => inlinePriority(r) === record.content,
180+
);
177181
if (existingRecord) {
178182
const updated = await cfFetch<{ id: string }>(
179183
config,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ export const infomaniakClient: DnsClient<InfomaniakConfig> = {
175175
const match = existing.find(
176176
(candidate) =>
177177
candidate.type === record.type &&
178-
normalizeSource(candidate.source) === source,
178+
normalizeSource(candidate.source) === source &&
179+
unquoteTarget(candidate.target) === record.content,
179180
);
180181

181182
const body = JSON.stringify(recordPayload(record, record.zoneId));

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,18 @@ export const ovhClient: DnsClient<OvhConfig> = {
285285
)}&subDomain=${encodeURIComponent(subDomain)}`,
286286
);
287287

288-
const existingId = existing[0];
288+
let existingId: number | undefined;
289+
for (const id of existing) {
290+
const candidate = await ovhFetch<OvhRecord>(
291+
config,
292+
`/domain/zone/${zone}/record/${id}`,
293+
);
294+
if (candidate.target === record.content) {
295+
existingId = id;
296+
break;
297+
}
298+
}
299+
289300
if (existingId !== undefined) {
290301
await ovhFetch(config, `/domain/zone/${zone}/record/${existingId}`, {
291302
method: "PUT",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ export const porkbunClient: DnsClient<PorkbunConfig> = {
9696
ttl: record.ttl ?? 600,
9797
};
9898

99-
const existingRecord = existing.records[0];
99+
const existingRecord = existing.records.find(
100+
(r) => r.content === record.content,
101+
);
100102
if (existingRecord) {
101103
await pbFetch(
102104
config,

0 commit comments

Comments
 (0)