Server-Side Request Forgery (SSRF) in sparql_query / fetch-based request paths via unvalidated redirects and DNS rebinding
Description
The project implements a strong SSRF defense for its primary CKAN request path: the Node branch of makeCkanRequest builds an http/https Agent from createSsrfSafeLookup, which re-resolves the hostname, rejects when any resolved address is private/internal, and pins the socket to the validated IP — correctly defeating DNS rebinding.
The sparql_query tool does not use that path. querySparqlEndpoint uses the global fetch() and guards it with a single up-front check, assertHostnameResolvesSafe(url.hostname). That check:
- validates only the original hostname, while
fetchfollows HTTP redirects by default (redirect: "follow") and never re-validates redirect targets; and - resolves the name once, independently of the resolution
fetchperforms when it actually connects (a time-of-check/time-of-use gap enabling DNS rebinding).
As a result, a caller-supplied endpoint_url can be used to reach internal-only services and cloud metadata endpoints (e.g. http://169.254.169.254/), exfiltrating their responses back through the tool output.
Affected code
src/tools/sparql.ts:
export async function querySparqlEndpoint(endpointUrl, query) {
validateServerUrl(endpointUrl);
const url = new URL(endpointUrl);
if (url.protocol !== "https:") throw new Error("Only HTTPS endpoints are allowed");
// SSRF: block hostnames that resolve to private/internal addresses (DNS-name bypass)
await assertHostnameResolvesSafe(url.hostname); // <-- checks ORIGINAL host only, once
...
response = await fetch(endpointUrl, { method: "POST", ... }); // <-- follows redirects, re-resolves DNSsrc/utils/http.ts — the guard fails open on any resolution error:
export async function assertHostnameResolvesSafe(hostname) {
let addresses;
try { addresses = await dnsMod.promises.lookup(hostname, { all: true }); }
catch { return; } // <-- DNS error => no check performed
for (const a of addresses) if (isBlockedIp(a.address)) throw ...;
}The same fetch-without-pinning pattern is used by the Workers branch of makeCkanRequest and by the MQA fetch in src/tools/quality.ts.
Impact
On Node deployments an attacker who can supply tool arguments (directly, or by influencing an LLM/agent that drives the MCP server — and, in the network-exposed Express transport that advertises no authentication, any unauthenticated client) can:
- reach internal network services not otherwise reachable from outside;
- read cloud instance metadata (e.g. AWS IMDS
169.254.169.254), which can expose IAM credentials; - use the server as a request proxy against internal HTTP endpoints, with the response body returned in the tool result.
The CKAN_ALLOWED_DOMAINS allowlist does not mitigate this: it is enforced in validateServerUrl on the CKAN path, and even there only against the initial hostname — querySparqlEndpoint accepts arbitrary HTTPS endpoints by design.
Scope note (CVSS): scored with S:C because the vulnerable server is used to pivot to a different security authority (internal/metadata services). On Cloudflare Workers the platform blocks internal egress, so that deployment is not exploitable via this path.
Proof of concept
See poc/redirect-ssrf-poc.mjs and REPRODUCE.md. The PoC reproduces the exact guard sequence of querySparqlEndpoint; for a genuine public attacker host all three guards pass, and the redirect is followed to a simulated internal metadata service whose secret body is returned to the caller.
$ node poc/redirect-ssrf-poc.mjs
Response body returned to the MCP tool:
SECRET-IAM-CREDENTIALS-aws-metadata-body
RESULT: VULNERABLE — redirect followed to the internal target;
its response was exfiltrated back through the SPARQL tool.Remediation
Route the fetch-based paths through the same protection already used by the axios path. Concretely, any of:
- Disable automatic redirects on these
fetchcalls (redirect: "manual") and, for each3xx, re-runvalidateServerUrl+assertHostnameResolvesSafeon theLocationbefore following it (bounded hop count). - Pin to a validated IP. Resolve the host, verify every address with
isBlockedIp, then connect to the literal IP (customundicidispatcher /lookup) so the socket cannot be rebound — mirroringcreateSsrfSafeLookup. - For
sparql_queryspecifically, following redirects is unnecessary;redirect: "error"closes the redirect vector outright, and pinning closes the rebinding vector.
Additionally, assertHostnameResolvesSafe should fail closed (reject) when DNS resolution errors, rather than returning and letting the request proceed.