/** * Editing a Technitium zone: switching it off and on, and its options (who may ask it, who may copy it, who is told when it changes) — how an edit is checked and planned against the kind of zone * and what it has now, what is sent to Technitium, what is said when something else moved, who may, what is refused and what is written in the audit log. * * Run with: npm test --workspace server */ import { test, describe, before, after } from "node:test"; import assert from "node:assert/strict"; import { createServer as createHttpServer, type Server } from "node:http"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import type { AddressInfo } from "node:net"; const dir = mkdtempSync(join(tmpdir(), "hm-tn-zopts-")); process.env.DATABASE_PATH = join(dir, "test.sqlite"); process.env.SESSION_DIR = join(dir, "sessions"); process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32); const express = (await import("express")).default; const { db } = await import("../src/db/client.js"); const { users, dnsProviders, integrationCredentials, auditLog } = await import("../src/db/schema.js"); const { runMigrations } = await import("../src/db/migrate.js"); const { encryptSecret } = await import("../src/crypto.js"); const zo = await import("../src/dns/zoneOptions.js"); const { createTechnitiumAdapter } = await import("../src/dns/adapters/technitium.js"); const { dnsRouter } = await import("../src/routes/dns.js"); await runMigrations(); const opts = (over: Partial = {}): import("../src/dns/zoneOptions.js").DnsZoneOptions => ({ name: "home.lan", type: "Primary", internal: false, disabled: false, queryAccess: "Allow", queryAccessNetworkACL: [], zoneTransfer: "Deny", zoneTransferNetworkACL: [], zoneTransferTsigKeyNames: [], notify: "ZoneNameServers", notifyNameServers: [], availableTsigKeyNames: ["key.lan", "catalog"], other: {}, ...over, }); const plan = (type: string, current: ReturnType, wanted: import("../src/dns/zoneOptions.js").ZoneOptionsChange) => zo.planOptionsChange(type, current, wanted); const ok = (p: ReturnType) => { assert.ok(!("error" in p), "error" in p ? p.error : ""); return p; }; const failure = (p: ReturnType) => { assert.ok("error" in p, "expected an error"); return (p as { error: string }).error; }; describe("a network list entry", () => { test("an address or a network, with ! to refuse it, in the form it is sent", () => { assert.equal(zo.aclEntry("192.168.1.0/24"), "192.168.1.0/24"); assert.equal(zo.aclEntry(" !10.0.0.5 "), "!10.0.0.5"); assert.equal(zo.aclEntry("FD00::/8"), "fd00::/8"); assert.equal(zo.aclEntry("::1"), "::1"); }); test("anything else is not: text, a network too big for its kind, an empty refusal", () => { for (const bad of ["", "!", "lan", "192.168.1.0/33", "fd00::/129", "300.1.1.1", "10.0.0.1/", "10.0.0.1,10.0.0.2", "a b"]) assert.equal(zo.aclEntry(bad), null, JSON.stringify(bad)); }); }); describe("what an edit comes to", () => { test("what is already so is no change, and nothing is sent", () => { const p = ok(plan("Primary", opts(), { queryAccess: "Allow", zoneTransfer: "Deny", notify: "ZoneNameServers" })); assert.deepEqual([p.changed, "send" in p ? p.send : null], [[], {}]); }); test("only the group that differs is sent; a mode that does not go by a list sends none", () => { const p = ok(plan("Primary", opts({ queryAccessNetworkACL: ["10.0.0.0/8"] }), { queryAccess: "AllowOnlyPrivateNetworks", zoneTransfer: "Deny" })); assert.deepEqual("send" in p ? p.send : null, { queryAccess: "AllowOnlyPrivateNetworks" }); assert.deepEqual(p.changed, ["queryAccess"]); }); test("a mode that goes by a list needs the list, written as addresses and networks", () => { assert.match(failure(plan("Primary", opts(), { queryAccess: "UseSpecifiedNetworkACL" })), /list of networks, and the list is empty/); assert.match(failure(plan("Primary", opts(), { queryAccess: "UseSpecifiedNetworkACL", queryAccessNetworkACL: ["home"] })), /"home" is not an address or a network/); const p = ok(plan("Primary", opts(), { queryAccess: "AllowZoneNameServersAndUseSpecifiedNetworkACL", queryAccessNetworkACL: ["192.168.1.0/24", " !192.168.1.66"] })); assert.deepEqual("send" in p ? p.send : null, { queryAccess: "AllowZoneNameServersAndUseSpecifiedNetworkACL", queryAccessNetworkACL: ["192.168.1.0/24", "!192.168.1.66"] }); }); test("a list changed under the same mode is a change — in its order, which matters — and the mode is sent with it", () => { const cur = opts({ queryAccess: "UseSpecifiedNetworkACL", queryAccessNetworkACL: ["10.0.0.0/8", "!10.0.0.5"] }); assert.deepEqual(ok(plan("Primary", cur, { queryAccessNetworkACL: ["10.0.0.0/8", "!10.0.0.5"] })).changed, []); const swapped = ok(plan("Primary", cur, { queryAccessNetworkACL: ["!10.0.0.5", "10.0.0.0/8"] })); assert.deepEqual(["send" in swapped ? swapped.send : null, swapped.changed], [{ queryAccess: "UseSpecifiedNetworkACL", queryAccessNetworkACL: ["!10.0.0.5", "10.0.0.0/8"] }, ["queryAccess"]]); }); test("zone transfer: the keys it is limited to must be ones the server has; they are sent with the mode, and none is a change to none", () => { assert.match(failure(plan("Primary", opts(), { zoneTransfer: "Allow", zoneTransferTsigKeyNames: ["nope.lan"] })), /no TSIG key called "nope\.lan"/); const p = ok(plan("Primary", opts(), { zoneTransfer: "AllowOnlyZoneNameServers", zoneTransferTsigKeyNames: ["key.lan"] })); assert.deepEqual("send" in p ? p.send : null, { zoneTransfer: "AllowOnlyZoneNameServers", zoneTransferTsigKeyNames: ["key.lan"] }); const cleared = ok(plan("Primary", opts({ zoneTransfer: "Allow", zoneTransferTsigKeyNames: ["key.lan", "catalog"] }), { zoneTransferTsigKeyNames: [] })); assert.deepEqual("send" in cleared ? cleared.send : null, { zoneTransfer: "Allow", zoneTransferTsigKeyNames: [] }); assert.deepEqual(ok(plan("Primary", opts({ zoneTransfer: "Allow", zoneTransferTsigKeyNames: ["key.lan", "catalog"] }), { zoneTransferTsigKeyNames: ["catalog", "key.lan"] })).changed, [], "the order of keys is not a change"); }); test("notify: named servers need addresses; a mode that names none sends none", () => { assert.match(failure(plan("Primary", opts(), { notify: "SpecifiedNameServers" })), /none is named/); assert.match(failure(plan("Primary", opts(), { notify: "SpecifiedNameServers", notifyNameServers: ["ns1.lan"] })), /"ns1\.lan" is not an address/); const p = ok(plan("Primary", opts(), { notify: "BothZoneAndSpecifiedNameServers", notifyNameServers: ["10.0.0.2", "fd00::2"] })); assert.deepEqual("send" in p ? p.send : null, { notify: "BothZoneAndSpecifiedNameServers", notifyNameServers: ["10.0.0.2", "fd00::2"] }); const none = ok(plan("Primary", opts({ notify: "SpecifiedNameServers", notifyNameServers: ["10.0.0.2"] }), { notify: "None" })); assert.deepEqual("send" in none ? none.send : null, { notify: "None" }); }); test("what a kind of zone does not have is refused: a Forwarder neither transfers nor notifies here, a secondary catalog has no query access", () => { assert.match(failure(plan("Forwarder", opts({ type: "Forwarder" }), { zoneTransfer: "Allow" })), /Forwarder zone has no zone transfer/); assert.match(failure(plan("Forwarder", opts({ type: "Forwarder" }), { notify: "None" })), /Forwarder zone has no notify/); assert.match(failure(plan("SecondaryCatalog", opts({ type: "SecondaryCatalog" }), { queryAccess: "Deny" })), /no query access/); assert.deepEqual(ok(plan("Forwarder", opts({ type: "Forwarder" }), { queryAccess: "Deny" })).changed, ["queryAccess"]); }); test("a zone whose mode the provider did not say is not changed blindly", () => { assert.match(failure(plan("Primary", opts({ queryAccess: null }), { queryAccessNetworkACL: ["10.0.0.0/8"] })), /not known/); assert.match(failure(plan("Primary", opts({ notify: null }), { notifyNameServers: ["10.0.0.2"] })), /not known/); }); test("what else the zone says that moved is named; what did not is not", () => { const before = opts({ other: { update: '"Deny"', catalog: '"c1"', dnssecStatus: '"Unsigned"' } }); assert.deepEqual(zo.unexpectedChanges(before, opts({ other: { update: '"Deny"', catalog: '"c1"', dnssecStatus: '"Unsigned"' } })), []); assert.deepEqual(zo.unexpectedChanges(before, opts({ other: { update: '"Allow"', dnssecStatus: '"Unsigned"' } })), ["catalog", "update"]); }); }); /** A Technitium with a few zones that keeps the options it is given, and can be told to forget something it was not asked to change. */ interface ZoneState { name: string; type: string; internal: boolean; disabled: boolean; queryAccess: string; queryAccessNetworkACL: string[]; zoneTransfer: string; zoneTransferNetworkACL: string[]; zoneTransferTsigKeyNames: string[]; notify: string; notifyNameServers: string[]; update: string; updateNetworkACL: string[]; catalog: string; notifyFailed: boolean; } const fresh = (): Map => new Map( [ ["home.lan", "Primary"], ["mirror.lan", "Secondary"], ["corp.example", "Forwarder"], ["localhost", "Primary"], ].map(([name, type]) => [ name, { name, type, internal: name === "localhost", disabled: false, queryAccess: "Allow", queryAccessNetworkACL: [], zoneTransfer: "Deny", zoneTransferNetworkACL: [], zoneTransferTsigKeyNames: [], notify: "ZoneNameServers", notifyNameServers: [], update: "UseSpecifiedNetworkACL", updateNetworkACL: ["192.168.180.0/24"], catalog: "catalog1", notifyFailed: false }, ]), ); let zones = fresh(); let forgetUpdates = false; let refuse: string | null = null; const seen: { path: string; auth: string; form: Record }[] = []; let fake: Server; let fakeUrl = ""; before(async () => { fake = createHttpServer((req, res) => { let body = ""; req.on("data", (c) => (body += c)); req.on("end", () => { const u = new URL(req.url ?? "", "http://x"); const form = Object.fromEntries(new URLSearchParams(body)); const query = Object.fromEntries(u.searchParams); seen.push({ path: u.pathname, auth: String(req.headers.authorization ?? ""), form: { ...query, ...form } }); const send = (json: unknown) => { res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify(json)); }; const params = { ...query, ...form }; const zone = zones.get(params.zone ?? ""); if (u.pathname === "/api/zones/list") return send({ status: "ok", response: { zones: [...zones.values()].map((z) => ({ name: z.name, type: z.type, internal: z.internal, disabled: z.disabled })) } }); if (refuse !== null && u.pathname !== "/api/zones/options/get") return send({ status: "error", errorMessage: refuse }); if (!zone) return send({ status: "error", errorMessage: `No such zone was found: ${params.zone}` }); if (u.pathname === "/api/zones/options/get") { const { name, type, internal, disabled, queryAccess, queryAccessNetworkACL, zoneTransfer, zoneTransferNetworkACL, zoneTransferTsigKeyNames, notify, notifyNameServers, update, updateNetworkACL, catalog, notifyFailed } = zone; return send({ status: "ok", response: { name, type, internal, disabled, dnssecStatus: "Unsigned", notifyFailed, notifyFailedFor: [], catalog, queryAccess, queryAccessNetworkACL, zoneTransfer, zoneTransferNetworkACL, zoneTransferTsigKeyNames, notify, notifyNameServers, update, updateNetworkACL, availableTsigKeyNames: ["key.lan", "catalog"] } }); } if (u.pathname === "/api/zones/enable" || u.pathname === "/api/zones/disable") { zone.disabled = u.pathname.endsWith("/disable"); return send({ status: "ok" }); } if (u.pathname === "/api/zones/options/set") { const list = (v: string) => (v === "false" ? [] : v.split(",")); if (params.queryAccess !== undefined) zone.queryAccess = params.queryAccess; if (params.queryAccessNetworkACL !== undefined) zone.queryAccessNetworkACL = list(params.queryAccessNetworkACL); if (params.zoneTransfer !== undefined) zone.zoneTransfer = params.zoneTransfer; if (params.zoneTransferNetworkACL !== undefined) zone.zoneTransferNetworkACL = list(params.zoneTransferNetworkACL); if (params.zoneTransferTsigKeyNames !== undefined) zone.zoneTransferTsigKeyNames = list(params.zoneTransferTsigKeyNames); if (params.notify !== undefined) zone.notify = params.notify; if (params.notifyNameServers !== undefined) zone.notifyNameServers = list(params.notifyNameServers); // A server that sets what it is not told to back to its default. if (forgetUpdates) { zone.update = "Deny"; zone.updateNetworkACL = []; } return send({ status: "ok" }); } res.writeHead(404); res.end("{}"); }); }); await new Promise((resolve) => fake.listen(0, "127.0.0.1", resolve)); fakeUrl = `http://127.0.0.1:${(fake.address() as AddressInfo).port}`; }); after(() => { fake.close(); }); const reset = () => { zones = fresh(); forgetUpdates = false; refuse = null; seen.length = 0; }; describe("what is sent to Technitium", () => { const adapter = () => createTechnitiumAdapter({ url: `${fakeUrl}/`, token: "tok-1" }); test("the options are read with the token in the header, asking which TSIG keys there are; what the server says that is not edited is kept aside", async () => { reset(); const o = await adapter().getZoneOptions!("home.lan"); assert.deepEqual(seen[0].path, "/api/zones/options/get"); assert.deepEqual([seen[0].auth, seen[0].form], ["Bearer tok-1", { zone: "home.lan", includeAvailableTsigKeyNames: "true", includeAvailableCatalogZoneNames: "false" }]); assert.deepEqual([o.name, o.type, o.internal, o.disabled, o.queryAccess, o.zoneTransfer, o.notify, o.availableTsigKeyNames], ["home.lan", "Primary", false, false, "Allow", "Deny", "ZoneNameServers", ["key.lan", "catalog"]]); assert.deepEqual(Object.keys(o.other).sort(), ["catalog", "dnssecStatus", "update", "updateNetworkACL"]); assert.equal(o.other.updateNetworkACL, '["192.168.180.0/24"]'); }); test("a zone is switched off and on with its own two calls", async () => { reset(); await adapter().setZoneEnabled!("home.lan", false); assert.deepEqual([seen[0].path, seen[0].form], ["/api/zones/disable", { zone: "home.lan" }]); assert.equal(zones.get("home.lan")!.disabled, true); await adapter().setZoneEnabled!("home.lan", true); assert.equal(seen[1].path, "/api/zones/enable"); assert.equal(zones.get("home.lan")!.disabled, false); }); test("the options go as the parameters Technitium names, lists comma-separated; a list of none is the word false", async () => { reset(); await adapter().setZoneOptions!("home.lan", { queryAccess: "UseSpecifiedNetworkACL", queryAccessNetworkACL: ["10.0.0.0/8", "!10.0.0.5"], zoneTransfer: "Allow", zoneTransferTsigKeyNames: [], notify: "SpecifiedNameServers", notifyNameServers: ["10.0.0.2", "10.0.0.3"], }); assert.deepEqual(seen[0].form, { zone: "home.lan", queryAccess: "UseSpecifiedNetworkACL", queryAccessNetworkACL: "10.0.0.0/8,!10.0.0.5", zoneTransfer: "Allow", zoneTransferTsigKeyNames: "false", notify: "SpecifiedNameServers", notifyNameServers: "10.0.0.2,10.0.0.3", }); }); test("the zone list says which are switched off and what kind they are", async () => { reset(); zones.get("mirror.lan")!.disabled = true; const list = await adapter().listZones(); assert.deepEqual(list.map((z) => [z.id, z.type, z.disabled]), [["home.lan", "Primary", false], ["mirror.lan", "Secondary", true], ["corp.example", "Forwarder", false]]); }); test("what Technitium says when it refuses is the error", async () => { reset(); refuse = "Access was denied to use Zones: Modify"; await assert.rejects(() => adapter().setZoneOptions!("home.lan", { queryAccess: "Deny" }), /Access was denied/); await assert.rejects(() => adapter().setZoneEnabled!("home.lan", false), /Access was denied/); }); }); describe("the routes", () => { let app: Server; let base = ""; let technitium = 0; let disabled = 0; let cloudflare = 0; const as = (sub: string) => ({ "content-type": "application/json", "x-sub": sub }); const getOptions = (id: number, zone: string, sub = "admin") => fetch(`${base}/providers/${id}/zones/${encodeURIComponent(zone)}/options`, { headers: as(sub) }); const patch = (id: number, zone: string, body: unknown, sub = "admin") => fetch(`${base}/providers/${id}/zones/${encodeURIComponent(zone)}/options`, { method: "PATCH", headers: as(sub), body: JSON.stringify(body) }); const enable = (id: number, zone: string, enabled: unknown, sub = "admin") => fetch(`${base}/providers/${id}/zones/${encodeURIComponent(zone)}/enabled`, { method: "POST", headers: as(sub), body: JSON.stringify({ enabled }) }); const writes = () => seen.filter((s) => /\/(set|enable|disable)$/.test(s.path)); const audit = async (action: string) => (await db.select().from(auditLog)).filter((e) => e.action === action); before(async () => { await db.insert(users).values([ { oidcSub: "admin", name: "Alma", role: "admin" }, { oidcSub: "operator", name: "Olle", role: "operator" }, { oidcSub: "viewer", name: "Vic", role: "viewer" }, ]); const cred = (await db.insert(integrationCredentials).values({ name: "dns:technitium", encryptedSecret: encryptSecret(JSON.stringify({ token: "tok-route" })) }).returning({ id: integrationCredentials.id }))[0]; const config = JSON.stringify({ url: fakeUrl }); technitium = (await db.insert(dnsProviders).values({ providerType: "technitium", name: "Home DNS", credentialId: cred.id, config }).returning({ id: dnsProviders.id }))[0].id; disabled = (await db.insert(dnsProviders).values({ providerType: "technitium", name: "Old DNS", credentialId: cred.id, config, enabled: false }).returning({ id: dnsProviders.id }))[0].id; cloudflare = (await db.insert(dnsProviders).values({ providerType: "cloudflare", name: "Cloudflare", config: "{}" }).returning({ id: dnsProviders.id }))[0].id; const a = express(); a.use(express.json()); a.use((req, _res, next) => { (req as unknown as { session: unknown }).session = { user: { sub: String(req.headers["x-sub"] ?? "viewer") } }; next(); }); a.use("/api/dns", dnsRouter); app = await new Promise((resolve) => { const s = a.listen(0, "127.0.0.1", () => resolve(s)); }); base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/dns`; }); after(() => { app.close(); try { rmSync(dir, { recursive: true, force: true }); } catch { // Windows keeps the database file locked until the process exits. } }); test("the providers say which can edit zones", async () => { const body = (await (await fetch(`${base}/providers`, { headers: as("viewer") })).json()) as { providers: { id: number; canEditZones: boolean }[] }; const can = Object.fromEntries(body.providers.map((p) => [p.id, p.canEditZones])); assert.deepEqual([can[technitium], can[disabled], can[cloudflare]], [true, true, false]); }); test("only an administrator reads or changes a zone's options or switches it; nothing reaches Technitium for anyone else", async () => { reset(); for (const sub of ["viewer", "operator"]) { assert.equal((await getOptions(technitium, "home.lan", sub)).status, 403, sub); assert.equal((await patch(technitium, "home.lan", { queryAccess: "Deny" }, sub)).status, 403, sub); assert.equal((await enable(technitium, "home.lan", false, sub)).status, 403, sub); } assert.deepEqual(seen, []); }); test("the options a zone has now, without what is only kept aside", async () => { reset(); const res = await getOptions(technitium, "home.lan"); assert.equal(res.status, 200); const { options } = (await res.json()) as { options: Record }; assert.deepEqual([options.name, options.type, options.queryAccess, options.availableTsigKeyNames], ["home.lan", "Primary", "Allow", ["key.lan", "catalog"]]); assert.equal("other" in options, false); }); test("switching a zone off and on: Technitium is asked, the audit log says who, and what is already so asks nothing", async () => { reset(); const off = await enable(technitium, "home.lan", false); assert.deepEqual(await off.json(), { enabled: false, changed: true }); assert.equal(zones.get("home.lan")!.disabled, true); assert.deepEqual(writes().map((s) => s.path), ["/api/zones/disable"]); const again = await enable(technitium, "home.lan", false); assert.deepEqual(await again.json(), { enabled: false, changed: false }); assert.equal(writes().length, 1); assert.deepEqual(await (await enable(technitium, "home.lan", true)).json(), { enabled: true, changed: true }); const entries = await audit("disable_zone"); assert.deepEqual([entries.length, entries[0].category, entries[0].targetType, entries[0].targetId], [1, "dns", "dns_zone", "home.lan"]); assert.equal((await audit("enable_zone")).length, 1); }); test("the server's own zones, a zone that is not there and a body that does not say on or off are not switched", async () => { reset(); assert.equal((await enable(technitium, "localhost", false)).status, 409); assert.equal((await enable(technitium, "nothere.lan", false)).status, 502, "Technitium's own words for it"); assert.equal((await enable(technitium, "home.lan", "yes")).status, 400); assert.deepEqual(writes(), []); }); test("an edit sends the group that differs, whole, and answers with the zone as it is now; the audit log names what changed", async () => { reset(); const res = await patch(technitium, "home.lan", { queryAccess: "UseSpecifiedNetworkACL", queryAccessNetworkACL: ["10.0.0.0/8", " !10.0.0.5"], zoneTransfer: "Deny" }); assert.equal(res.status, 200); const body = (await res.json()) as { changed: string[]; unexpected: string[]; options: { queryAccess: string; queryAccessNetworkACL: string[] } }; assert.deepEqual([body.changed, body.unexpected, body.options.queryAccess, body.options.queryAccessNetworkACL], [["queryAccess"], [], "UseSpecifiedNetworkACL", ["10.0.0.0/8", "!10.0.0.5"]]); assert.deepEqual(writes().map((s) => s.form), [{ zone: "home.lan", queryAccess: "UseSpecifiedNetworkACL", queryAccessNetworkACL: "10.0.0.0/8,!10.0.0.5" }]); const [entry] = await audit("update_zone_options"); assert.deepEqual(JSON.parse(entry.detail ?? "{}"), { providerId: technitium, name: "home.lan", changed: ["queryAccess"] }); }); test("an edit that changes nothing sends nothing and writes nothing", async () => { reset(); const before = (await audit("update_zone_options")).length; const res = await patch(technitium, "home.lan", { queryAccess: "Allow", notify: "ZoneNameServers" }); assert.deepEqual(((await res.json()) as { changed: string[] }).changed, []); assert.deepEqual(writes(), []); assert.equal((await audit("update_zone_options")).length, before); }); test("if the server also changed something that was not asked, the answer and the audit log say so", async () => { reset(); forgetUpdates = true; const res = await patch(technitium, "home.lan", { notify: "None" }); assert.equal(res.status, 200); const body = (await res.json()) as { changed: string[]; unexpected: string[] }; assert.deepEqual([body.changed, body.unexpected], [["notify"], ["update", "updateNetworkACL"]]); const entries = await audit("update_zone_options"); assert.deepEqual(JSON.parse(entries.at(-1)!.detail ?? "{}").unexpected, ["update", "updateNetworkACL"]); }); test("what is wrong with an edit is said before anything is sent: a kind of zone, a missing list, a key or a field that does not exist, a value that is no mode", async () => { reset(); const bad: [string, unknown][] = [ ["corp.example", { zoneTransfer: "Allow" }], ["corp.example", { notify: "None" }], ["home.lan", { queryAccess: "UseSpecifiedNetworkACL" }], ["home.lan", { queryAccess: "UseSpecifiedNetworkACL", queryAccessNetworkACL: ["lan"] }], ["home.lan", { zoneTransfer: "Allow", zoneTransferTsigKeyNames: ["nope"] }], ["home.lan", { notify: "SpecifiedNameServers", notifyNameServers: [] }], ["home.lan", { queryAccess: "Everyone" }], ["home.lan", { notify: "SeparateNameServersForCatalogAndMemberZones" }], ["home.lan", { disabled: true }], ["home.lan", { queryAccessNetworkACL: "10.0.0.0/8" }], ]; for (const [zone, body] of bad) { const res = await patch(technitium, zone, body); assert.equal(res.status, 400, JSON.stringify([zone, body])); assert.equal(typeof ((await res.json()) as { message: string }).message, "string"); } assert.deepEqual(writes(), []); }); test("a Forwarder's query access can be changed; a zone name that is not written as one, and the server's own zones, are refused", async () => { reset(); assert.equal((await patch(technitium, "corp.example", { queryAccess: "Deny" })).status, 200); assert.equal(zones.get("corp.example")!.queryAccess, "Deny"); for (const bad of ["Home.LAN", "home.lan.", "a b", "a/b"]) assert.equal((await patch(technitium, bad, { queryAccess: "Deny" })).status, 400, bad); assert.equal((await patch(technitium, "localhost", { queryAccess: "Deny" })).status, 409); assert.equal(zones.get("localhost")!.queryAccess, "Allow"); }); test("what Technitium says when it refuses is passed on, and nothing is written in the audit log", async () => { reset(); const before = (await audit("update_zone_options")).length; refuse = "Access was denied to use Zones: Modify"; const res = await patch(technitium, "home.lan", { queryAccess: "Deny" }); assert.equal(res.status, 502); assert.match(((await res.json()) as { message: string }).message, /Access was denied/); assert.equal((await audit("update_zone_options")).length, before); }); test("a provider that cannot edit zones, a switched-off one, an unknown one and an id that is no id are not asked", async () => { reset(); const unsupported = await patch(cloudflare, "home.lan", { queryAccess: "Deny" }); assert.equal(((await unsupported.json()) as { error: string }).error, "not_supported"); assert.equal(((await (await patch(disabled, "home.lan", { queryAccess: "Deny" })).json()) as { error: string }).error, "provider_disabled"); assert.equal((await patch(999999, "home.lan", { queryAccess: "Deny" })).status, 404); assert.equal((await fetch(`${base}/providers/abc/zones/home.lan/options`, { headers: as("admin") })).status, 400); assert.deepEqual(seen, []); }); });