Fix editing and deleting DNS records from the cached list

The records endpoint returned raw cache rows, whose `id` is this app's own row number
(12, 13, ...). The DNS page sends `r.id` back when a record is edited or deleted, so the
provider was handed a row number instead of its record id. That only worked straight after
a manual Sync, whose response carries the provider's ids; after any save or delete the page
reloads from the cache and the problem came back. The ID check added for connected systems
now refuses such an id with "That isn't a valid record ID".

The list now returns records shaped like a sync's: `id` is the provider's record id. Editing
and deleting work from the cached list for every DNS provider.

The test checks the list hands out the provider's id, and that deleting with it reaches
Cloudflare at the right address; with the old shape it fails.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-06 23:45:06 +02:00
1 parent 2dac273b1d
commit f71756b119
2 files changed
+29 -1

No files matched your search

+12 -1
View File
@@ -283,10 +283,21 @@ dnsRouter.get("/providers/:id/zones/:zoneId/records", asyncHandler(async (req, r
const providerId = Number(req.params.id); const providerId = Number(req.params.id);
const zoneId = req.params.zoneId; const zoneId = req.params.zoneId;
const records = await db const rows = await db
.select() .select()
.from(dnsRecordsCache) .from(dnsRecordsCache)
.where(and(eq(dnsRecordsCache.providerId, providerId), eq(dnsRecordsCache.zoneId, zoneId))); .where(and(eq(dnsRecordsCache.providerId, providerId), eq(dnsRecordsCache.zoneId, zoneId)));
// Shaped like what the provider returns on a sync: `id` is the provider's own record id, which is what editing and deleting
// send back. A cache row's own `id` is just a row number in this app's database and means nothing to the provider.
const records = rows.map((r) => ({
id: r.recordId,
type: r.type,
name: r.name,
content: r.content,
ttl: r.ttl,
priority: r.priority,
proxied: r.proxied,
}));
const [zoneCache] = await db const [zoneCache] = await db
.select() .select()
.from(dnsZonesCache) .from(dnsZonesCache)
+17
View File
@@ -378,6 +378,23 @@ describe("routes", () => {
assert.deepEqual(calls, [{ method: "DELETE", url: `https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records/${RECORD}` }]); assert.deepEqual(calls, [{ method: "DELETE", url: `https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records/${RECORD}` }]);
}); });
test("the cached record list hands out the provider's record id, and that id works for a delete", async () => {
// A row as a sync leaves it: this app's own row number (`id`) next to the provider's record id (`recordId`).
const { db } = await import("../src/db/client.js");
const { dnsRecordsCache } = await import("../src/db/schema.js");
await db.insert(dnsRecordsCache).values({ providerId: 1, zoneId: ZONE, recordId: RECORD, type: "A", name: "a.example.com", content: "1.2.3.4", ttl: 300 });
assert.equal(await send(operator, "GET", `/api/dns/providers/1/zones/${ZONE}/records`), 200);
const { records } = JSON.parse(lastBody.text) as { records: Record<string, unknown>[] };
const shown = records.find((r) => r.name === "a.example.com")!;
assert.equal(shown.id, RECORD, "the page would send the database row number to the provider instead of its record id");
assert.deepEqual(Object.keys(shown).sort(), ["content", "id", "name", "priority", "proxied", "ttl", "type"]);
calls = [];
assert.equal(await send(operator, "DELETE", `/api/dns/providers/1/zones/${ZONE}/records/${encodeURIComponent(String(shown.id))}`), 204, lastBody.text);
assert.deepEqual(calls, [{ method: "DELETE", url: `https://api.cloudflare.com/client/v4/zones/${ZONE}/dns_records/${RECORD}` }]);
});
test("Azure DNS: crafted IDs are a 400, nothing reaches Azure's management API", async () => { test("Azure DNS: crafted IDs are a 400, nothing reaches Azure's management API", async () => {
for (const bad of ["x/../..", "..", "a?b"]) { for (const bad of ["x/../..", "..", "a?b"]) {
calls = []; calls = [];