Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(mcp-bridge): re-check resolved addresses at connect time to stop DNS rebinding

nirholase2a44bed5b4e
diff --git a/mcp-bridge/src/bazaar-discover.js b/mcp-bridge/src/bazaar-discover.js
index 087f98f37..d4f37ff3f 100644
--- a/mcp-bridge/src/bazaar-discover.js
+++ b/mcp-bridge/src/bazaar-discover.js
@@ -21,7 +21,7 @@
import axios from 'axios';
-import { assertPayableUrl, maxPriceAtomic } from './url-guard.js';
+import { assertPayableUrl, createGuardedAgents, maxPriceAtomic } from './url-guard.js';
const DEFAULT_LIMIT = 20;
const DEFAULT_BAZAAR_URL = 'https://api.cdp.coinbase.com/platform/v2/x402/discovery/resources';
@@ -125,6 +125,7 @@ export async function fetchBazaarResources({ url = bazaarUrl(), limit = discover
timeout: 15_000,
validateStatus: (s) => s >= 200 && s < 300,
maxRedirects: 0,
+ ...createGuardedAgents(),
});
const items = Array.isArray(res.data?.items) ? res.data.items : [];
return items;
diff --git a/mcp-bridge/src/url-guard.js b/mcp-bridge/src/url-guard.js
index ca113fda2..cdb69c5d3 100644
--- a/mcp-bridge/src/url-guard.js
+++ b/mcp-bridge/src/url-guard.js
@@ -18,6 +18,9 @@
// No external dependency: the CIDR checks parse octets / IPv6 hextets directly.
import { promises as dns } from 'node:dns';
+import dnsCb from 'node:dns';
+import http from 'node:http';
+import https from 'node:https';
import net from 'node:net';
const DEFAULT_MAX_PRICE_ATOMIC = 100_000n; // $0.10 USDC
@@ -265,3 +268,36 @@ export async function assertPayableUrl(rawUrl) {
function stripBrackets(host) {
return host.startsWith('[') && host.endsWith(']') ? host.slice(1, -1) : host;
}
+
+/**
+ * dns.lookup replacement for http(s) agents. assertPayableUrl resolves once to
+ * validate, but the HTTP client resolves again at connect time, so a rebinding
+ * DNS server could answer public first and an internal address second. This
+ * lookup re-checks every address at the moment the socket is opened, and the
+ * connection is made to the very address that passed the check.
+ */
+export function guardedLookup(hostname, options, callback) {
+ const opts = typeof options === 'function' ? {} : options || {};
+ const cb = typeof options === 'function' ? options : callback;
+ dnsCb.lookup(hostname, { ...opts, all: true }, (err, records) => {
+ if (err) return cb(err);
+ const bad = (records || []).find(({ address }) => isBlockedAddress(address));
+ if (!records || records.length === 0 || bad) {
+ const e = new Error(
+ `refused: "${hostname}" resolves to a private/blocked address${bad ? ` (${bad.address})` : ''}`,
+ );
+ e.code = 'ESSRF_BLOCKED';
+ return cb(e);
+ }
+ if (opts.all) return cb(null, records);
+ return cb(null, records[0].address, records[0].family);
+ });
+}
+
+// Agents whose connect-time DNS resolution is subject to the same range check.
+export function createGuardedAgents() {
+ return {
+ httpAgent: new http.Agent({ lookup: guardedLookup }),
+ httpsAgent: new https.Agent({ lookup: guardedLookup }),
+ };
+}
diff --git a/mcp-bridge/src/x402-axios-client.js b/mcp-bridge/src/x402-axios-client.js
index afde9feea..d57ecc78f 100644
--- a/mcp-bridge/src/x402-axios-client.js
+++ b/mcp-bridge/src/x402-axios-client.js
@@ -29,6 +29,7 @@
// amount per channel under `~/.x402-mcp-bridge/channels/client/`. Restart-safe.
import axios from 'axios';
+import { createGuardedAgents } from './url-guard.js';
import { x402Client, x402HTTPClient } from '@x402/core/client';
import { wrapAxiosWithPayment } from '@x402/axios';
import { ExactEvmScheme } from '@x402/evm/exact/client';
@@ -192,6 +193,8 @@ export async function buildBuyerAxios() {
// (169.254.169.254, localhost, RFC1918) that bypasses that check. With
// maxRedirects:0 a 3xx surfaces as an error instead of being chased.
maxRedirects: 0,
+ // SSRF: re-validate DNS answers at connect time (rebinding TOCTOU).
+ ...createGuardedAgents(),
}),
client,
);
diff --git a/tests/mcp-bridge-url-guard-rebinding.test.js b/tests/mcp-bridge-url-guard-rebinding.test.js
new file mode 100644
index 000000000..872982784
--- /dev/null
+++ b/tests/mcp-bridge-url-guard-rebinding.test.js
@@ -0,0 +1,25 @@
+import { describe, it, expect } from 'vitest';
+import http from 'node:http';
+import axios from 'axios';
+import { createGuardedAgents } from '../mcp-bridge/src/url-guard.js';
+
+describe('mcp-bridge url-guard: connect-time DNS pinning', () => {
+ it('refuses a hostname that resolves to loopback at connect time', async () => {
+ let hit = false;
+ const server = http.createServer((_req, res) => {
+ hit = true;
+ res.end('internal');
+ });
+ await new Promise((r) => server.listen(0, '127.0.0.1', r));
+ const { port } = server.address();
+ try {
+ // "localhost" stands in for a rebinding name whose second answer is internal.
+ await expect(
+ axios.get(`http://localhost:${port}/`, { ...createGuardedAgents(), maxRedirects: 0 }),
+ ).rejects.toThrow(/private\/blocked/);
+ expect(hit).toBe(false);
+ } finally {
+ server.close();
+ }
+ });
+});