import assert from "node:assert/strict" import { fileURLToPath } from "node:url" import { assertWireguardCreateAllowed, wireguardCreatePrefix, wireguardCreateStages, wireguardInterfaceCreateSchema, wireguardPublicKey, wireguardCreatePreserved } from "./wireguard-interface-create.js" import { wireguardInterfaceDeleteProof, wireguardInterfaceDependencyPaths, type WireguardInterfaceDeleteState } from "./wireguard-interface-delete.js" import { wireguardCorroboratedPrivateScalar } from "./wireguard-key-scalar.js" export async function acceptWireguardInterfaceCreateModel() { const key = Buffer.alloc(32, 41).toString("base64"), publicKey = wireguardPublicKey(key) const input = wireguardInterfaceCreateSchema.parse({ serverId: 111, name: "wg-owned", privateKey: key, address: "2001:db8::1/64", peer: { publicKey, allowedAddresses: ["192.0.2.2/32"], endpointAddress: "example.org", endpointPort: 54321, persistentKeepalive: 60, name: "owned", comment: "owned peer", privateKey: "none", clientAddress: "10.0.0.2", clientDns: "1.1.1.1", clientEndpoint: "vpn.example.org" } }) const state: WireguardInterfaceDeleteState = { base: { interfaces: [], peers: [], bindings: [], flow: { hostPublicKey: "", registered: false }, serverProof: "source", proof: "base" }, ipv4: [], ipv6: [], dependencies: Object.fromEntries(wireguardInterfaceDependencyPaths.map(path => [path, []])), filters: [], proof: "" } state.proof = await wireguardInterfaceDeleteProof(state) assertWireguardCreateAllowed(state, input) assert.deepEqual(wireguardCreateStages(input), ["interface", "address", "peer", "peer-enable", "address-enable", "interface-enable"]) assert.equal(await wireguardCreatePrefix(state, input, state.proof, 0), true) const after = structuredClone(state) after.base.interfaces.push({ ".id": "*a", name: input.name, disabled: "true", "listen-port": "13231", mtu: "1420", "public-key": publicKey, "private-key": key }) after.dependencies["/interface"].push({ ".id": "*a", name: input.name, type: "wg" }) assert.equal(await wireguardCreatePrefix(after, input, state.proof, 1), true) after.ipv6.push({ ".id": "*b", interface: "*a", address: "2001:0db8::1/64", dynamic: "false", disabled: "true", advertise: "no" }) assert.equal(await wireguardCreatePrefix(after, input, state.proof, 2), true) after.base.peers.push({ ".id": "*c", interface: "*a", "public-key": publicKey, "allowed-address": "192.0.2.2/32", "endpoint-address": "example.org", "endpoint-port": "54321", "persistent-keepalive": "1m", name: "owned", comment: "owned peer", "private-key": "none", "client-address": "10.0.0.2", "client-dns": "1.1.1.1", "client-endpoint": "vpn.example.org", disabled: "true" }) assert.equal(await wireguardCreatePrefix(after, input, state.proof, 3), true) after.base.peers[0].disabled = "no" assert.equal(await wireguardCreatePrefix(after, input, state.proof, 4), true) after.ipv6[0].disabled = "no" assert.equal(await wireguardCreatePrefix(after, input, state.proof, 5), true) after.base.interfaces[0].disabled = "no" assert.equal(await wireguardCreatePrefix(after, input, state.proof, 6), true) const beforeActivation = structuredClone(after); beforeActivation.base.interfaces[0].disabled = "true" const clamped = structuredClone(after); clamped.base.interfaces[0]["private-key"] = wireguardCorroboratedPrivateScalar(key, publicKey)! const untouched = structuredClone(clamped) assert.equal(wireguardCreatePreserved(beforeActivation, clamped, input, "interface-enable"), true) assert.equal(await wireguardCreatePrefix(clamped, input, state.proof, 6), true) assert.deepEqual(clamped, untouched); assert.equal(input.privateKey, key); assert.equal(beforeActivation.base.interfaces[0]["private-key"], key) const noActivation = structuredClone(beforeActivation); noActivation.base.interfaces[0]["private-key"] = clamped.base.interfaces[0]["private-key"] assert.equal(wireguardCreatePreserved(beforeActivation, noActivation, input, "peer-enable"), false, "Only the owned interface-enable stage authorizes scalar reserialization") for (let ignored = 0; ignored < 32; ignored++) { const bytes = Buffer.from(key, "base64"); bytes[0] = (bytes[0] & 248) | (ignored & 7); bytes[31] = (bytes[31] & 63) | ((ignored >> 3) << 6) const represented = structuredClone(after); represented.base.interfaces[0]["private-key"] = bytes.toString("base64") assert.equal(await wireguardCreatePrefix(represented, input, state.proof, 6), true) assert.equal(wireguardCreatePreserved(beforeActivation, represented, input, "interface-enable"), true) } const rotated = structuredClone(clamped), changedBytes = Buffer.from(key, "base64"); changedBytes[1] ^= 1 rotated.base.interfaces[0]["private-key"] = changedBytes.toString("base64"); rotated.base.interfaces[0]["public-key"] = wireguardPublicKey(rotated.base.interfaces[0]["private-key"]) assert.equal(wireguardCreatePreserved(beforeActivation, rotated, input, "interface-enable"), false) assert.equal(await wireguardCreatePrefix(rotated, input, state.proof, 6), false) const wrongPublic = structuredClone(clamped); wrongPublic.base.interfaces[0]["public-key"] = rotated.base.interfaces[0]["public-key"] assert.equal(wireguardCreatePreserved(beforeActivation, wrongPublic, input, "interface-enable"), false) for (const mutate of [(value: typeof clamped) => { value.base.interfaces[0].opaque = "NEVER_CHANGED" }, (value: typeof clamped) => { value.base.peers[0]["private-key"] = key }, (value: typeof clamped) => { value.base.interfaces[0]["private-key"] = "(hidden)" }]) { const changed = structuredClone(clamped); mutate(changed); assert.equal(wireguardCreatePreserved(beforeActivation, changed, input, "interface-enable"), false) } const foreignBaseline = structuredClone(state) foreignBaseline.base.interfaces.push({ ".id": "*f", name: "wg-foreign", "private-key": key, "public-key": publicKey, disabled: "true" }); foreignBaseline.dependencies["/interface"].push({ ".id": "*f", name: "wg-foreign", type: "wg" }) foreignBaseline.proof = await wireguardInterfaceDeleteProof(foreignBaseline) const withForeign = structuredClone(clamped); withForeign.base.interfaces.push(structuredClone(foreignBaseline.base.interfaces[0])); withForeign.dependencies["/interface"].push(structuredClone(foreignBaseline.dependencies["/interface"][0])) assert.equal(await wireguardCreatePrefix(withForeign, input, foreignBaseline.proof, 6), true) withForeign.base.interfaces[1]["private-key"] = wireguardCorroboratedPrivateScalar(key, publicKey)! assert.equal(await wireguardCreatePrefix(withForeign, input, foreignBaseline.proof, 6), false, "Create prefix must not normalize foreign baseline private bytes") for (const change of [ (value: typeof after) => { value.base.interfaces[0]["public-key"] = Buffer.alloc(32, 45).toString("base64") }, (value: typeof after) => { value.base.interfaces[0]["private-key"] = Buffer.alloc(32, 44).toString("base64") }, (value: typeof after) => { delete value.base.interfaces[0].disabled }, (value: typeof after) => { value.dependencies["/ip/firewall/filter"].push({ ".id": "*d", comment: "NEVER_OPAQUE" }) }, (value: typeof after) => { value.base.peers[0]["persistent-keepalive"] = "garbage" }, (value: typeof after) => { value.ipv6[0].dynamic = "unknown" }, (value: typeof after) => { value.dependencies["/interface"][0][".id"] = "*f" }, (value: typeof after) => { value.ipv4.push({ ".id": "*g", interface: input.name, address: "192.0.2.3/32", disabled: "true", dynamic: "false" }) }, ]) { const changed = structuredClone(after); change(changed); assert.equal(await wireguardCreatePrefix(changed, input, state.proof, 6), false) } for (const change of [ (value: typeof state) => { value.dependencies["/interface"].push({ ".id": "*a", name: input.name }) }, (value: typeof state) => { value.dependencies["/interface/vlan"].push({ ".id": "*a", interface: input.name }) }, (value: typeof state) => { value.dependencies["/ip/vrf"].push({ ".id": "*a", name: "other", interfaces: "all" }) }, (value: typeof state) => { value.dependencies["/routing/ospf/interface-template"].push({ ".id": "*a", networks: "2001:db8::/64" }) }, (value: typeof state) => { value.ipv6.push({ ".id": "*a", interface: "ether1", address: input.address! }) }, ]) { const changed = structuredClone(state); change(changed); assert.throws(() => assertWireguardCreateAllowed(changed, input)) } const quoted = wireguardInterfaceCreateSchema.parse({ serverId: 111, name: 'WG ั‚ะตัั‚ "edge" [x]', privateKey: key }) assertWireguardCreateAllowed(state, quoted) const named = structuredClone(state) named.dependencies["/interface/vlan"].push({ ".id": "*named", interface: quoted.name }) assert.throws(() => assertWireguardCreateAllowed(named, quoted)) for (const bad of [{ name: "\u0000bad" }, { name: " " }, { listenPort: 65536 }, { mtu: 65536 }, { privateKey: "bad" }, { peer: { publicKey, allowedAddresses: ["bad"] } }]) assert.equal(wireguardInterfaceCreateSchema.safeParse({ serverId: 111, name: "wg", ...bad }).success, false) console.log("WireGuard create model: all six disabled/activation prefixes, optional peer fields and literal keys, IPv6 normalization, unknown/static/native identity, whole dependency preservation, reserved ownership and selector conflicts checked") } if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) await acceptWireguardInterfaceCreateModel()