diff --git a/server/src/routes/dns.ts b/server/src/routes/dns.ts index 6d8239e..f53977c 100644 --- a/server/src/routes/dns.ts +++ b/server/src/routes/dns.ts @@ -283,10 +283,21 @@ dnsRouter.get("/providers/:id/zones/:zoneId/records", asyncHandler(async (req, r const providerId = Number(req.params.id); const zoneId = req.params.zoneId; - const records = await db + const rows = await db .select() .from(dnsRecordsCache) .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 .select() .from(dnsZonesCache) diff --git a/server/test/pathInjection.test.ts b/server/test/pathInjection.test.ts index 9ffc060..f34a506 100644 --- a/server/test/pathInjection.test.ts +++ b/server/test/pathInjection.test.ts @@ -378,6 +378,23 @@ describe("routes", () => { 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[] }; + 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 () => { for (const bad of ["x/../..", "..", "a?b"]) { calls = [];