diff --git a/apps/dashboard/src/app/pay/[id]/page.tsx b/apps/dashboard/src/app/pay/[id]/page.tsx index 24694c29ace..d11e3f639c0 100644 --- a/apps/dashboard/src/app/pay/[id]/page.tsx +++ b/apps/dashboard/src/app/pay/[id]/page.tsx @@ -10,6 +10,7 @@ import { } from "@/constants/server-envs"; import { getConfiguredThirdwebClient } from "@/constants/thirdweb.server"; import { PayPageWidget } from "../components/client/PayPageWidget.client"; +import { onlyUrl } from "../url-validation"; import { PayIdPageHeader } from "./header"; const title = "thirdweb Pay"; @@ -29,10 +30,16 @@ export default async function PayPage({ searchParams, }: { params: Promise<{ id: string }>; - searchParams: Promise<{ redirectUri?: string; theme?: "light" | "dark" }>; + searchParams: Promise<{ + redirectUri?: string | string[]; + theme?: "light" | "dark"; + }>; }) { const { id } = await params; - const { redirectUri, theme } = await searchParams; + const { redirectUri: rawRedirectUri, theme } = await searchParams; + // only http(s) URLs with a hostname; anything else (javascript:, data:, relative, repeated param) is dropped + const redirectUri = + typeof rawRedirectUri === "string" ? onlyUrl(rawRedirectUri) : undefined; const paymentLink = await getPaymentLink({ paymentId: id, diff --git a/apps/dashboard/src/app/pay/components/client/PayPageWidget.client.tsx b/apps/dashboard/src/app/pay/components/client/PayPageWidget.client.tsx index c3f66c49398..e8ff1777790 100644 --- a/apps/dashboard/src/app/pay/components/client/PayPageWidget.client.tsx +++ b/apps/dashboard/src/app/pay/components/client/PayPageWidget.client.tsx @@ -76,7 +76,8 @@ export function PayPageWidget({ if (!redirectUri) return; const url = new URL(redirectUri); - return window.open(url.toString()); + if (url.protocol !== "http:" && url.protocol !== "https:") return; + return window.open(url.toString(), "_blank", "noopener,noreferrer"); }} onError={(error) => { reportPaymentLinkBuyFailed({ diff --git a/apps/dashboard/src/app/pay/page.tsx b/apps/dashboard/src/app/pay/page.tsx index 8b735ffe0b8..d683f049727 100644 --- a/apps/dashboard/src/app/pay/page.tsx +++ b/apps/dashboard/src/app/pay/page.tsx @@ -4,6 +4,7 @@ import { ThemeProvider } from "next-themes"; import { defineChain, isAddress } from "thirdweb"; import { PayLandingHeader } from "./landing/header"; import { StyledBuyWidget } from "./landing/styled-buy-widget"; +import { onlyUrl } from "./url-validation"; const title = "thirdweb Payments"; const description = "Fast, secure, and simple payments."; @@ -25,30 +26,6 @@ const onlyAddress = (v: string) => (isAddress(v) ? v : undefined); const onlyNumber = (v: string) => Number.isNaN(Number(v)) ? undefined : Number(v); -/** - * Validates and normalizes a URL string. - * Only allows http: and https: protocols with a valid hostname. - * @returns normalized URL string on success, undefined on failure - */ -const onlyUrl = (v: string): string | undefined => { - try { - const url = new URL(v); - // Only allow http or https protocols - if (url.protocol !== "http:" && url.protocol !== "https:") { - return undefined; - } - // Ensure hostname is non-empty - if (!url.hostname) { - return undefined; - } - // Return normalized URL - return url.toString(); - } catch { - // Invalid URL format - return undefined; - } -}; - export default async function PayPage(props: { searchParams: Promise; }) { diff --git a/apps/dashboard/src/app/pay/url-validation.ts b/apps/dashboard/src/app/pay/url-validation.ts new file mode 100644 index 00000000000..bdc1d033c05 --- /dev/null +++ b/apps/dashboard/src/app/pay/url-validation.ts @@ -0,0 +1,23 @@ +/** + * Validates and normalizes a URL string. + * Only allows http: and https: protocols with a valid hostname. + * @returns normalized URL string on success, undefined on failure + */ +export const onlyUrl = (v: string): string | undefined => { + try { + const url = new URL(v); + // Only allow http or https protocols + if (url.protocol !== "http:" && url.protocol !== "https:") { + return undefined; + } + // Ensure hostname is non-empty + if (!url.hostname) { + return undefined; + } + // Return normalized URL + return url.toString(); + } catch { + // Invalid URL format + return undefined; + } +}; diff --git a/apps/portal/src/app/ai/mcp/page.mdx b/apps/portal/src/app/ai/mcp/page.mdx index f2bc24f63c9..b9245b96459 100644 --- a/apps/portal/src/app/ai/mcp/page.mdx +++ b/apps/portal/src/app/ai/mcp/page.mdx @@ -9,18 +9,22 @@ You can use the thirdweb MCP server to interact with the thirdweb API from your ## Remote MCP endpoint -You can access the MCP server at the following url, with your project secret key. +You can access the MCP server at the following url. Send your project secret key in the `x-secret-key` header. ```http -https://api.thirdweb.com/mcp?secretKey= +https://api.thirdweb.com/mcp +x-secret-key: ``` Make sure to keep your secret key safe and never share it with anyone. +If your client cannot send custom headers, you can pass the key as a query parameter instead: `https://api.thirdweb.com/mcp?secretKey=`. Prefer the header where you can: a key in a URL can end up in logs, browser history and config files. + By default, all tools are available. You can filter the tools available by passing a comma-separated list of tools as a query parameter. ```http -https://api.thirdweb.com/mcp?secretKey=&tools=fetchWithPayment,getWalletBalance +https://api.thirdweb.com/mcp?tools=fetchWithPayment,getWalletBalance +x-secret-key: ``` You can find all available tools at the bottom of this page. @@ -46,7 +50,10 @@ import { MultiServerMCPClient } from "@langchain/mcp-adapters"; const mcpServers = { thirdweb: { - url: `https://api.thirdweb.com/mcp?secretKey=` + url: "https://api.thirdweb.com/mcp", + headers: { + "x-secret-key": "", + }, } }; @@ -72,7 +79,11 @@ from langgraph.prebuilt import create_react_agent client = MultiServerMCPClient( { "thirdweb": { - "url": "https://api.thirdweb.com/mcp?secretKey=", + "transport": "http", + "url": "https://api.thirdweb.com/mcp", + "headers": { + "x-secret-key": "", + }, } } ) @@ -107,7 +118,7 @@ You can also use the MCP server on your own LLM client, like cursor, claude code className="flex items-center gap-3 bg-card mb-4 justify-start w-fit shadow-md hover:shadow-lg transition-shadow border-2" variant="outline" > - + Install into Cursor @@ -119,7 +130,10 @@ Add the following to your `.cursor/mcp.json` file (replace `YOUR_SECRET_KEY_HERE { "mcpServers": { "thirdweb-api": { - "url": "https://api.thirdweb.com/mcp?secretKey=YOUR_SECRET_KEY_HERE" + "url": "https://api.thirdweb.com/mcp", + "headers": { + "x-secret-key": "YOUR_SECRET_KEY_HERE" + } } } } @@ -129,13 +143,16 @@ Add the following to your `.cursor/mcp.json` file (replace `YOUR_SECRET_KEY_HERE -Add the following to your `~/.codeium/windsurf/mcp_config.json` file: +Add the following to your `~/.codeium/windsurf/mcp_config.json` file (replace `YOUR_SECRET_KEY_HERE` with your thirdweb secret key): ```json { "mcpServers": { "thirdweb-api": { - "url": "https://api.thirdweb.com/mcp?secretKey=YOUR_SECRET_KEY_HERE" + "serverUrl": "https://api.thirdweb.com/mcp", + "headers": { + "x-secret-key": "YOUR_SECRET_KEY_HERE" + } } } } @@ -150,7 +167,7 @@ Add the following to your `~/.codeium/windsurf/mcp_config.json` file: className="flex items-center gap-3 bg-card mb-4 justify-start w-fit shadow-md hover:shadow-lg transition-shadow border-2" variant="outline" > - + Install into VS Code @@ -160,9 +177,13 @@ Add the following to your VS Code `.vscode/mcp.json` file (replace `YOUR_SECRET_ ```json { - "mcp.servers": { + "servers": { "thirdweb-api": { - "url": "https://api.thirdweb.com/mcp?secretKey=YOUR_SECRET_KEY_HERE" + "type": "http", + "url": "https://api.thirdweb.com/mcp", + "headers": { + "x-secret-key": "YOUR_SECRET_KEY_HERE" + } } } } @@ -175,7 +196,7 @@ Add the following to your VS Code `.vscode/mcp.json` file (replace `YOUR_SECRET_ Run the following command to add the MCP server to your Claude Code configuration (replace `YOUR_SECRET_KEY_HERE` with your thirdweb secret key): ```bash -claude mcp add --transport http "thirdweb-api" "https://api.thirdweb.com/mcp?secretKey=YOUR_SECRET_KEY_HERE" +claude mcp add --transport http "thirdweb-api" "https://api.thirdweb.com/mcp" --header "x-secret-key: YOUR_SECRET_KEY_HERE" ``` diff --git a/apps/portal/src/app/x402/agents/page.mdx b/apps/portal/src/app/x402/agents/page.mdx index cc9453098c1..45f67daf972 100644 --- a/apps/portal/src/app/x402/agents/page.mdx +++ b/apps/portal/src/app/x402/agents/page.mdx @@ -22,6 +22,8 @@ The MCP server comes with all the tools by default, but you can filter the tools In this example, we create a ReAct agent using LangChain and filter the MCP tools to `fetchWithPayment` and `listPayableServices` as the only 2 tools we give our agent. You can view the full list of available tools [here](/ai/mcp#available-tools). +Your project secret key goes in the `x-secret-key` header. Only if your client cannot send custom headers, pass it as a `secretKey` query parameter instead; a key in a URL can end up in logs and config files. + @@ -42,7 +44,10 @@ const model = new ChatOpenAI({ const mcpServers = { thirdweb: { - url: `https://api.thirdweb.com/mcp?secretKey=&tools=fetchWithPayment,listPayableServices` + url: "https://api.thirdweb.com/mcp?tools=fetchWithPayment,listPayableServices", + headers: { + "x-secret-key": "", + }, } }; @@ -68,7 +73,11 @@ from langgraph.prebuilt import create_react_agent client = MultiServerMCPClient( { "thirdweb": { - "url": "https://api.thirdweb.com/mcp?secretKey=&tools=fetchWithPayment,listPayableServices", + "transport": "http", + "url": "https://api.thirdweb.com/mcp?tools=fetchWithPayment,listPayableServices", + "headers": { + "x-secret-key": "", + }, } } )