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(erc8004): encode each CAIP segment so the agent resolver route matches

nirholas857ce9464171
diff --git a/api/v1/agents/[caip].js b/api/v1/agents/[...caip].js
similarity index 73%
rename from api/v1/agents/[caip].js
rename to api/v1/agents/[...caip].js
index c8db83716..5f7511c9b 100644
--- a/api/v1/agents/[caip].js
+++ b/api/v1/agents/[...caip].js
@@ -8,7 +8,17 @@
* The :caip parameter is a CAIP-style ref:
* eip155:<chainId>:<registryAddress>/<tokenId>
*
- * URL-encode it: `eip155%3A8453%3A0x8004A169...%2F1`.
+ * Pass the ref with its `/` as a REAL path separator, which is why this is a
+ * catch-all route:
+ *
+ * GET /api/v1/agents/eip155:8453:0x8004A169.../1
+ *
+ * Percent-encoding the colons is fine (`eip155%3A8453%3A0x8004A169.../1`) —
+ * the router decodes each segment. Percent-encoding the SLASH is not: the API
+ * dispatcher rejects any segment that decodes to contain "/" or "\", because
+ * that is exactly how "%2f..%2f..%2fvite.config" would smuggle a traversal past
+ * the segment split (server/route-resolve.mjs `apiSegments`). A `%2F` ref
+ * therefore 404s before reaching this file, by design.
*
* Response (200):
* {
@@ -31,6 +41,24 @@ import { resolveOnChainAgent, resolveURI } from '../../_lib/onchain.js';
import { fetchSafePublicUrl } from '../../_lib/ssrf-guard.js';
const CAIP_RE = /^eip155:(\d+):(0x[a-fA-F0-9]{40})\/(\d+)$/;
+
+// Rebuild the ref from the catch-all param. Runtimes differ on the shape they
+// hand back (this server joins the decoded segments into a string; Vercel-style
+// catch-alls hand back the array), so accept both, and fall back to the path
+// for any runtime that populates neither.
+function caipFromReq(req) {
+ const fromQuery = req.query?.caip;
+ if (Array.isArray(fromQuery)) return fromQuery.filter(Boolean).join('/');
+ if (typeof fromQuery === 'string' && fromQuery) return fromQuery;
+ const { pathname } = new URL(req.url, 'http://internal');
+ const m = pathname.match(/\/api\/v1\/agents\/(.+)$/);
+ if (!m) return '';
+ try {
+ return m[1].split('/').map(decodeURIComponent).join('/');
+ } catch {
+ return '';
+ }
+}
const CACHE_HEADERS = {
// 5 min fresh, 1 h stale-while-revalidate at the edge.
'cache-control': 'public, s-maxage=300, stale-while-revalidate=3600',
@@ -43,7 +71,9 @@ export default wrap(async (req, res) => {
const rl = await limits.mcpIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
- const raw = decodeURIComponent(req.query?.caip || '');
+ // The dispatcher already percent-decoded each path segment; decoding again
+ // here would corrupt any ref carrying a literal "%".
+ const raw = caipFromReq(req);
const m = CAIP_RE.exec(raw);
if (!m) {
return error(res, 400, 'invalid_caip', 'expected eip155:<chainId>:<registry>/<tokenId>');
diff --git a/specs/ENS_AGENT_CLAIM.md b/specs/ENS_AGENT_CLAIM.md
index ffaaa585d..14a6169d3 100644
--- a/specs/ENS_AGENT_CLAIM.md
+++ b/specs/ENS_AGENT_CLAIM.md
@@ -56,7 +56,7 @@ Either direction alone is **unverified**. A consumer (resolver, badge, indexer)
## Resolver behavior
-The HTTP resolver at [`/api/v1/agents/:caip`](../api/v1/agents/[caip].js) currently returns the on-chain agent record plus card-level verification:
+The HTTP resolver at [`/api/v1/agents/:caip`](../api/v1/agents/[...caip].js) currently returns the on-chain agent record plus card-level verification:
```json
{
diff --git a/src/erc8004/badge.js b/src/erc8004/badge.js
index 476b99920..3642155a3 100644
--- a/src/erc8004/badge.js
+++ b/src/erc8004/badge.js
@@ -55,7 +55,11 @@ async function fetchCard({ agent, cardUrl, resolver }) {
return await r.json();
}
if (!agent) throw new Error('no agent ref or card-url provided');
- const url = `${resolver.replace(/\/$/, '')}/api/v1/agents/${encodeURIComponent(agent)}`;
+ // The resolver takes the CAIP ref's "/" as a real path separator, so encode
+ // each segment and rejoin. A whole-ref encodeURIComponent turns it into
+ // "%2F", which the API dispatcher rejects as a smuggled separator (404).
+ const ref = String(agent).split('/').map(encodeURIComponent).join('/');
+ const url = `${resolver.replace(/\/$/, '')}/api/v1/agents/${ref}`;
const r = await fetch(url, { mode: 'cors' });
if (!r.ok) throw new Error(`resolver HTTP ${r.status}`);
const body = await r.json();