Stop IDs in a request from being turned into other calls to connected systems
The Tailscale device, Proxmox node, Cloudflare zone/record and Azure record/zone IDs
were put into the outgoing request path as typed. An operator could send a crafted ID
("x/../../tailnet/-/keys/k1") and make the app call a different endpoint than the button
meant -- delete an auth key instead of a device, stop every guest on a node, delete a whole
DNS zone -- under the integration's own credential. Gitea and Dockhand encoded their IDs but
still passed "..".
- utils/pathSegment.ts: checks an ID against what a real one looks like, then encodes it
(encodeURIComponent alone leaves ".." untouched). Used by the Tailscale, Proxmox,
Cloudflare, Azure, Gitea and Dockhand adapters.
- The routes refuse a bad ID with a 400 before anything is sent, and the DNS routes turn the
adapters' refusal into a 400. A server's Proxmox node name is validated when saved.
- Azure: a zone ID without "::" is now refused instead of being cut into two bogus pieces.
- Regression tests (npm test --workspace server, Node's built-in runner): the crafted IDs
are refused for every adapter and route, real IDs still work, viewers are still refused.
Against the old code 13 of the 22 tests fail.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fae7089448
commit
a641211484
13 files changed
+549
-26
No files matched your search
@@ -10,6 +10,7 @@
|
||||
*/
|
||||
import type { DnsAdapter, DnsRecord, DnsRecordInput, DnsZone } from "../types.js";
|
||||
import { withDiagLogging } from "../../services/diagLog.js";
|
||||
import { ID_PATTERNS, InvalidIdentifierError, pathSegment } from "../../utils/pathSegment.js";
|
||||
|
||||
const ARM_BASE = "https://management.azure.com";
|
||||
const API_VERSION = "2018-05-01";
|
||||
@@ -80,8 +81,22 @@ export function createAzureAdapter(config: AzureConfig): DnsAdapter {
|
||||
return `${resourceGroup}::${zoneName}`;
|
||||
}
|
||||
|
||||
// Resource group, zone, record name and type all come from the request (a zone ID, a record ID, a record's name), so each is
|
||||
// checked before it goes into an ARM path — see utils/pathSegment. "@" (the zone apex) is a legal path character, kept as is.
|
||||
function zonePath(resourceGroup: string, zoneName: string) {
|
||||
const group = pathSegment(resourceGroup, "resource group", ID_PATTERNS.azureResourceGroup);
|
||||
const zone = pathSegment(zoneName, "DNS zone", ID_PATTERNS.azureZone);
|
||||
return `/subscriptions/${config.subscriptionId}/resourceGroups/${group}/providers/Microsoft.Network/dnsZones/${zone}`;
|
||||
}
|
||||
function recordPath(resourceGroup: string, zoneName: string, type: string, relName: string) {
|
||||
const kind = pathSegment(type, "record type", ID_PATTERNS.azureRecordType);
|
||||
const name = pathSegment(relName, "record name", ID_PATTERNS.azureRecordSet).replace(/%40/g, "@");
|
||||
return `${zonePath(resourceGroup, zoneName)}/${kind}/${name}`;
|
||||
}
|
||||
|
||||
function decodeZoneId(zoneId: string) {
|
||||
const idx = zoneId.indexOf("::");
|
||||
if (idx < 0) throw new InvalidIdentifierError("zone ID");
|
||||
return { resourceGroup: zoneId.slice(0, idx), zoneName: zoneId.slice(idx + 2) };
|
||||
}
|
||||
|
||||
@@ -127,7 +142,7 @@ export function createAzureAdapter(config: AzureConfig): DnsAdapter {
|
||||
|
||||
async function listRecords(zoneId: string): Promise<DnsRecord[]> {
|
||||
const { resourceGroup, zoneName } = decodeZoneId(zoneId);
|
||||
const path = `/subscriptions/${config.subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.Network/dnsZones/${zoneName}/recordSets`;
|
||||
const path = `${zonePath(resourceGroup, zoneName)}/recordSets`;
|
||||
const data = await armFetch(path);
|
||||
|
||||
const records: DnsRecord[] = [];
|
||||
@@ -232,7 +247,7 @@ export function createAzureAdapter(config: AzureConfig): DnsAdapter {
|
||||
if (relName.endsWith(`.${zoneName}`)) relName = relName.slice(0, -(zoneName.length + 1));
|
||||
if (relName === zoneName) relName = "@";
|
||||
|
||||
const path = `/subscriptions/${config.subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.Network/dnsZones/${zoneName}/${record.type}/${relName}`;
|
||||
const path = recordPath(resourceGroup, zoneName, record.type, relName);
|
||||
|
||||
let existing: any = null;
|
||||
try {
|
||||
@@ -268,8 +283,8 @@ export function createAzureAdapter(config: AzureConfig): DnsAdapter {
|
||||
if (newRelName.endsWith(`.${zoneName}`)) newRelName = newRelName.slice(0, -(zoneName.length + 1));
|
||||
if (newRelName === zoneName) newRelName = "@";
|
||||
|
||||
const oldPath = `/subscriptions/${config.subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.Network/dnsZones/${zoneName}/${type}/${oldRelName}`;
|
||||
const newPath = `/subscriptions/${config.subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.Network/dnsZones/${zoneName}/${type}/${newRelName}`;
|
||||
const oldPath = recordPath(resourceGroup, zoneName, type, oldRelName);
|
||||
const newPath = recordPath(resourceGroup, zoneName, type, newRelName);
|
||||
|
||||
let existing: any = null;
|
||||
try {
|
||||
@@ -313,7 +328,7 @@ export function createAzureAdapter(config: AzureConfig): DnsAdapter {
|
||||
const type = parts[1];
|
||||
const content = parts.slice(2).join("::");
|
||||
|
||||
const path = `/subscriptions/${config.subscriptionId}/resourceGroups/${resourceGroup}/providers/Microsoft.Network/dnsZones/${zoneName}/${type}/${relName}`;
|
||||
const path = recordPath(resourceGroup, zoneName, type, relName);
|
||||
const existing = await armFetch(path);
|
||||
const props = existing.properties;
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
import type { DnsAdapter, DnsRecord, DnsRecordInput, DnsZone } from "../types.js";
|
||||
import { withDiagLogging } from "../../services/diagLog.js";
|
||||
import { ID_PATTERNS, pathSegment } from "../../utils/pathSegment.js";
|
||||
|
||||
const BASE = "https://api.cloudflare.com/client/v4";
|
||||
|
||||
@@ -19,6 +20,10 @@ export function createCloudflareAdapter(config: CloudflareConfig): DnsAdapter {
|
||||
};
|
||||
}
|
||||
|
||||
// Zone and record IDs come from the request. Cloudflare's are always 32 hex characters (see utils/pathSegment).
|
||||
const zone = (id: string) => pathSegment(id, "zone ID", ID_PATTERNS.cloudflareId);
|
||||
const rec = (id: string) => pathSegment(id, "record ID", ID_PATTERNS.cloudflareId);
|
||||
|
||||
async function cfFetch(path: string, options: RequestInit = {}) {
|
||||
const res = await fetch(`${BASE}${path}`, { ...options, headers: headers() });
|
||||
const data = await res.json();
|
||||
@@ -45,7 +50,7 @@ export function createCloudflareAdapter(config: CloudflareConfig): DnsAdapter {
|
||||
let page = 1;
|
||||
let all: any[] = [];
|
||||
while (true) {
|
||||
const result = await cfFetch(`/zones/${zoneId}/dns_records?per_page=100&page=${page}`);
|
||||
const result = await cfFetch(`/zones/${zone(zoneId)}/dns_records?per_page=100&page=${page}`);
|
||||
all = all.concat(result);
|
||||
if (result.length < 100) break;
|
||||
page++;
|
||||
@@ -75,7 +80,7 @@ export function createCloudflareAdapter(config: CloudflareConfig): DnsAdapter {
|
||||
|
||||
async function addRecord(zoneId: string, record: DnsRecordInput): Promise<DnsRecord> {
|
||||
const body = toBody(record);
|
||||
const result = await cfFetch(`/zones/${zoneId}/dns_records`, {
|
||||
const result = await cfFetch(`/zones/${zone(zoneId)}/dns_records`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
@@ -84,7 +89,7 @@ export function createCloudflareAdapter(config: CloudflareConfig): DnsAdapter {
|
||||
|
||||
async function updateRecord(zoneId: string, recordId: string, record: DnsRecordInput): Promise<DnsRecord> {
|
||||
const body = toBody(record);
|
||||
const result = await cfFetch(`/zones/${zoneId}/dns_records/${recordId}`, {
|
||||
const result = await cfFetch(`/zones/${zone(zoneId)}/dns_records/${rec(recordId)}`, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
@@ -92,7 +97,7 @@ export function createCloudflareAdapter(config: CloudflareConfig): DnsAdapter {
|
||||
}
|
||||
|
||||
async function deleteRecord(zoneId: string, recordId: string): Promise<void> {
|
||||
await cfFetch(`/zones/${zoneId}/dns_records/${recordId}`, { method: "DELETE" });
|
||||
await cfFetch(`/zones/${zone(zoneId)}/dns_records/${rec(recordId)}`, { method: "DELETE" });
|
||||
}
|
||||
|
||||
return withDiagLogging("cloudflare", { listZones, listRecords, addRecord, updateRecord, deleteRecord });
|
||||
|
||||
Reference in new issue
Block a user