From 9d75af2c28f603afacc874073e6fa22ae1a1a7d1 Mon Sep 17 00:00:00 2001 From: yinbowen-dc <65101280+yinbowen-dc@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:46:08 +0800 Subject: [PATCH] fix(dashboard): restrict checkout redirects and isolate opener --- apps/dashboard/package.json | 7 +- .../client/PayPageWidget.client.test.ts | 99 +++++++++++++++++++ .../client/PayPageWidget.client.tsx | 10 +- apps/dashboard/vitest.config.mts | 16 +++ pnpm-lock.yaml | 34 +++++++ turbo.json | 4 + 6 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 apps/dashboard/src/app/pay/components/client/PayPageWidget.client.test.ts create mode 100644 apps/dashboard/vitest.config.mts diff --git a/apps/dashboard/package.json b/apps/dashboard/package.json index 2296758562f..87b7c00a257 100644 --- a/apps/dashboard/package.json +++ b/apps/dashboard/package.json @@ -108,7 +108,8 @@ "postcss": "8.5.23", "storybook": "9.1.21", "tailwindcss": "3.4.17", - "typescript": "5.8.3" + "typescript": "5.8.3", + "vitest": "4.1.11" }, "name": "thirdweb-dashboard", "private": true, @@ -125,7 +126,9 @@ "preinstall": "npx only-allow pnpm", "start": "next start", "storybook": "storybook dev -p 6006", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "test:unit": "turbo run test --filter=thirdweb-dashboard", + "test": "vitest run" }, "version": "3.0.0" } diff --git a/apps/dashboard/src/app/pay/components/client/PayPageWidget.client.test.ts b/apps/dashboard/src/app/pay/components/client/PayPageWidget.client.test.ts new file mode 100644 index 00000000000..46ee4d31e79 --- /dev/null +++ b/apps/dashboard/src/app/pay/components/client/PayPageWidget.client.test.ts @@ -0,0 +1,99 @@ +import { type ComponentProps, createElement } from "react"; +import { renderToStaticMarkup } from "react-dom/server"; +import { CheckoutWidget } from "thirdweb/react"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { reportPaymentLinkBuySuccessful } from "@/analytics/report"; +import { PayPageWidget } from "./PayPageWidget.client"; + +vi.mock("next-themes", () => ({ useTheme: () => ({ theme: "light" }) })); +vi.mock("thirdweb/react", async (importOriginal) => ({ + ...(await importOriginal()), + AutoConnect: () => null, + CheckoutWidget: vi.fn(() => null), +})); +vi.mock("@/analytics/report", () => ({ + reportPaymentLinkBuyFailed: vi.fn(), + reportPaymentLinkBuySuccessful: vi.fn(), +})); +vi.mock("@/hooks/chains/v5-adapter", () => ({ + useV5DashboardChain: () => ({ id: 1 }), +})); + +const open = vi.fn(); + +beforeEach(() => { + vi.stubGlobal("window", { open }); +}); + +afterEach(() => { + vi.clearAllMocks(); + vi.unstubAllGlobals(); +}); + +function completeCheckout(redirectUri: string | undefined): void { + renderToStaticMarkup( + createElement(PayPageWidget, { + chainId: 1, + clientId: "test-client-id", + purchaseData: undefined, + recipientAddress: "0x0000000000000000000000000000000000000001", + redirectUri, + token: { + address: "0x0000000000000000000000000000000000000002", + decimals: 18, + name: "Test token", + symbol: "TEST", + }, + }), + ); + const props = vi.mocked(CheckoutWidget).mock.calls[0]?.[0] as + | ComponentProps + | undefined; + expect(props?.onSuccess).toBeTypeOf("function"); + // This handler does not consume the SDK's payment result. + const onSuccess = props?.onSuccess as (() => void) | undefined; + onSuccess?.(); +} + +describe("payment success redirect", () => { + it.each([ + [ + "https://merchant.example/complete?order=1#receipt", + "https://merchant.example/complete?order=1#receipt", + ], + ["http://localhost:3000/done", "http://localhost:3000/done"], + ["HTTPS://MERCHANT.EXAMPLE", "https://merchant.example/"], + ])( + "opens %s without giving the destination an opener", + (input, normalized) => { + completeCheckout(input); + expect(open.mock.calls).toEqual([ + [normalized, "_blank", "noopener,noreferrer"], + ]); + expect(reportPaymentLinkBuySuccessful).toHaveBeenCalledOnce(); + }, + ); + + it.each([ + undefined, + "", + "not a URL", + "/relative/path", + "//merchant.example/done", + "https://", + "javascript:alert(1)", + " JaVaScRiPt:alert(1)", + "data:text/html,hello", + "file:///tmp/receipt.html", + "ftp://merchant.example/receipt", + "blob:https://merchant.example/id", + "mailto:merchant@example.com", + ])( + "ignores invalid or non-HTTP destination %s without breaking checkout", + (input) => { + expect(() => completeCheckout(input)).not.toThrow(); + expect(open).not.toHaveBeenCalled(); + expect(reportPaymentLinkBuySuccessful).toHaveBeenCalledOnce(); + }, + ); +}); 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..e9b852b9af8 100644 --- a/apps/dashboard/src/app/pay/components/client/PayPageWidget.client.tsx +++ b/apps/dashboard/src/app/pay/components/client/PayPageWidget.client.tsx @@ -75,8 +75,14 @@ export function PayPageWidget({ reportPaymentLinkBuySuccessful(); if (!redirectUri) return; - const url = new URL(redirectUri); - return window.open(url.toString()); + let url: URL; + try { + url = new URL(redirectUri); + } catch { + return; + } + if (url.protocol !== "http:" && url.protocol !== "https:") return; + return window.open(url.toString(), "_blank", "noopener,noreferrer"); }} onError={(error) => { reportPaymentLinkBuyFailed({ diff --git a/apps/dashboard/vitest.config.mts b/apps/dashboard/vitest.config.mts new file mode 100644 index 00000000000..3ecbdd9f491 --- /dev/null +++ b/apps/dashboard/vitest.config.mts @@ -0,0 +1,16 @@ +import { fileURLToPath } from "node:url"; +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + esbuild: { jsx: "automatic" }, + resolve: { + alias: { + "@": fileURLToPath(new URL("./src/@", import.meta.url)), + app: fileURLToPath(new URL("./src/app", import.meta.url)), + }, + }, + test: { + environment: "node", + include: ["src/**/*.test.ts"], + }, +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d39867ac255..e6b6044b9f3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -471,6 +471,9 @@ importers: typescript: specifier: 5.8.3 version: 5.8.3 + vitest: + specifier: 4.1.11 + version: 4.1.11(@opentelemetry/api@1.9.1)(@types/node@22.14.1)(@vitest/coverage-v8@4.1.11)(@vitest/ui@4.1.11)(happy-dom@20.14.3(bufferutil@4.0.9)(utf-8-validate@5.0.10))(msw@2.10.2(@types/node@22.14.1)(typescript@5.8.3))(vite@7.3.7(@types/node@22.14.1)(jiti@2.7.0)(lightningcss@1.30.1)(terser@5.43.1)(tsx@4.20.3)(yaml@2.8.4)) apps/nebula: dependencies: @@ -40280,6 +40283,37 @@ snapshots: tsx: 4.20.3 yaml: 2.8.4 + vitest@4.1.11(@opentelemetry/api@1.9.1)(@types/node@22.14.1)(@vitest/coverage-v8@4.1.11)(@vitest/ui@4.1.11)(happy-dom@20.14.3(bufferutil@4.0.9)(utf-8-validate@5.0.10))(msw@2.10.2(@types/node@22.14.1)(typescript@5.8.3))(vite@7.3.7(@types/node@22.14.1)(jiti@2.7.0)(lightningcss@1.30.1)(terser@5.43.1)(tsx@4.20.3)(yaml@2.8.4)): + dependencies: + '@vitest/expect': 4.1.11 + '@vitest/mocker': 4.1.11(msw@2.10.2(@types/node@22.14.1)(typescript@5.8.3))(vite@7.3.7(@types/node@22.14.1)(jiti@2.7.0)(lightningcss@1.30.1)(terser@5.43.1)(tsx@4.20.3)(yaml@2.8.4)) + '@vitest/pretty-format': 4.1.11 + '@vitest/runner': 4.1.11 + '@vitest/snapshot': 4.1.11 + '@vitest/spy': 4.1.11 + '@vitest/utils': 4.1.11 + es-module-lexer: 2.3.2 + expect-type: 1.4.0 + magic-string: 0.30.21 + obug: 2.2.1 + pathe: 2.0.3 + picomatch: 4.0.5 + std-env: 4.2.0 + tinybench: 2.9.0 + tinyexec: 1.3.1 + tinyglobby: 0.2.15 + tinyrainbow: 3.1.1 + vite: 7.3.7(@types/node@22.14.1)(jiti@2.7.0)(lightningcss@1.30.1)(terser@5.43.1)(tsx@4.20.3)(yaml@2.8.4) + why-is-node-running: 2.3.0 + optionalDependencies: + '@opentelemetry/api': 1.9.1 + '@types/node': 22.14.1 + '@vitest/coverage-v8': 4.1.11(vitest@4.1.11) + '@vitest/ui': 4.1.11(vitest@4.1.11) + happy-dom: 20.14.3(bufferutil@4.0.9)(utf-8-validate@5.0.10) + transitivePeerDependencies: + - msw + vitest@4.1.11(@opentelemetry/api@1.9.1)(@types/node@22.14.1)(@vitest/coverage-v8@4.1.11)(@vitest/ui@4.1.11)(happy-dom@20.14.3(bufferutil@4.0.9)(utf-8-validate@6.0.5))(msw@2.10.2(@types/node@22.14.1)(typescript@5.8.3))(vite@7.3.7(@types/node@22.14.1)(jiti@2.7.0)(lightningcss@1.30.1)(terser@5.43.1)(tsx@4.20.3)(yaml@2.8.4)): dependencies: '@vitest/expect': 4.1.11 diff --git a/turbo.json b/turbo.json index aeeca3aace2..c34c1131dcc 100644 --- a/turbo.json +++ b/turbo.json @@ -2,6 +2,10 @@ "$schema": "https://turborepo.org/schema.json", "globalEnv": ["CI", "TW_SECRET_KEY", "TW_CLIENT_ID"], "tasks": { + "thirdweb-dashboard#test": { + "dependsOn": ["^build"], + "outputs": [] + }, "bench": { "cache": false, "dependsOn": []