T09 · Insecure Skill Coding Practices
- Location
scripts/run.js:264- Finding
Bearer Token Can Be Redirected to an Arbitrary Server
- Content
View full analysis
Vulnerability Details
File Location:
scripts/run.js:49,scripts/run.js:69,scripts/run.js:264-273, andscripts/run.js:532
Vulnerability Type: Unvalidated authentication endpoint override
Risk Level: MediumVulnerable Code
js const SERVER_BASE = process.env.KEYAPI_SERVER_URL ?? "https://mcp.keyapi.ai"; async function connect(serverUrl) { let token = process.env.KEYAPI_TOKEN; if (!token) token = await promptToken(); const client = new Client({ name: "keyapi-runner", version: "1.0.0" }); const transport = new StreamableHTTPClientTransport( new URL(serverUrl), { requestInit: { headers: { Authorization: `Bearer ${token}` } } } ); await client.connect(transport); return client; } const serverUrl = `${SERVER_BASE}/${values.platform}/mcp`; const client = await connect(serverUrl);Technical Analysis
The runner permits
KEYAPI_SERVER_URLto override the documented KeyAPI endpoint without validating its protocol or hostname. The same connection logic unconditionally attachesKEYAPI_TOKENas a bearer credential.Consequently, a process environment modified by another program, a malicious wrapper script, a compromised CI configuration, or an untrusted shell profile can redirect requests and credentials to an attacker-controlled endpoint. Plain HTTP URLs are not explicitly rejected, so the credential could also be transmitted without transport encryption where the MCP transport accepts such a URL.
Sending the token to the default documented endpoint is necessary for the Skill's functionality. Allowing that credential to be sent to an unrestricted override exceeds the minimum privileges required to access
https://mcp.keyapi.ai.Attack Path
- An attacker gains the ability to influence the environment used to launch the Skill, such as through a wrapper script, CI variable, shell configuration, or deployment settings.
- The attacker sets `KEYAPI_ ...[truncated 980 chars]
- Remediation
View remediation
Remediation Suggestions
- Pin authenticated requests to the documented origin,
https://mcp.keyapi.ai, unless endpoint overrides are strictly required. - Parse the final URL before connecting and require the
https:protocol. - Compare the normalized hostname against an explicit allowlist before attaching
KEYAPI_TOKEN. - If custom MCP endpoints are supported, require a separate credential variable for them rather than forwarding the production KeyAPI token.
- Require an explicit, conspicuous opt-in for custom endpoints and display the destination hostname before authentication.
- Reject URLs containing embedded credentials, unexpected ports, or hostnames that only superficially resemble the trusted domain.
- Add automated tests confirming that bearer credentials are never attached to unapproved origins.
Example hardening:
js const TRUSTED_ORIGIN = "https://mcp.keyapi.ai"; function validateServerUrl(serverUrl) { const url = new URL(serverUrl); if (url.protocol !== "https:" || url.origin !== TRUSTED_ORIGIN) { throw new Error(`Refusing to send KEYAPI_TOKEN to untrusted origin: ${url.origin}`); } return url; } async function connect(serverUrl) { const url = validateServerUrl(serverUrl); const token = process.env.KEYAPI_TOKEN ?? await promptToken(); const client = new Client({ name: "keyapi-runner", version: "1.0.0" }); const transport = new StreamableHTTPClientTransport(url, { requestInit: { headers: { Authorization: `Bearer ${token}` }, }, }); await client.connect(transport); return client; }- Pin authenticated requests to the documented origin,
