From e52a9b76f0da3d989124c805bacf635b5e05730c Mon Sep 17 00:00:00 2001 From: Copilot <223556219+Copilot@users.noreply.github.com> Date: Wed, 25 Nov 2026 14:38:05 +0100 Subject: [PATCH] fix(web): restrict production CSP connect origins Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- apps/web/.env.example | 14 ++++++ apps/web/src/lib/config.test.ts | 31 ++++++++++++- apps/web/src/lib/csp.ts | 47 +++++++++++++++++++ apps/web/src/vite-env.d.ts | 4 ++ apps/web/vite.config.ts | 82 ++++++++++++++++++++++----------- docs/DEPLOY.md | 15 +++++- 6 files changed, 165 insertions(+), 28 deletions(-) create mode 100644 apps/web/src/lib/csp.ts diff --git a/apps/web/.env.example b/apps/web/.env.example index 032bbd43..97ee0fa4 100644 --- a/apps/web/.env.example +++ b/apps/web/.env.example @@ -13,6 +13,20 @@ VITE_NETWORK_PASSPHRASE=Test SDF Network ; September 2015 VITE_CONTRACT_ID=CC2QMOXZERI6UOR67YKSORT7QTUHQ5QUGMHQBYVP23YM3NMUNNOEOGZY VITE_ESCROW_TOKEN_LABEL=XLM +# --- Production CSP connect-src endpoints (all public; origins are allowlisted) --- +# VITE_KEEPER_URL=https://keeper.example.com +# VITE_APPRAISAL_URL=https://appraisal.example.com +# Defaults to https://api.drand.sh when unset. +# VITE_DRAND_URL=https://api.drand.sh +# VITE_DASHBOARD_ENDPOINT=https://keeper.example.com/dashboard +# +# For local development, these may point to local services. Do not use wildcard +# hosts (for example https://*.example.com) in a production build. +# VITE_KEEPER_URL=http://localhost:3001 +# VITE_APPRAISAL_URL=http://localhost:4021 +# VITE_DRAND_URL=http://localhost:8080 +# VITE_DASHBOARD_ENDPOINT=http://localhost:3001/dashboard + # --- Testnet USDC lifecycle proof contract (agents:e2e recorded trace) --- # VITE_CONTRACT_ID=CAPTODBCDEVIK23ALBJBS2TXRTIK47ZA5MBTHYF4XLHG2BK7JPYUCU2Y # VITE_ROUND_ID=1 diff --git a/apps/web/src/lib/config.test.ts b/apps/web/src/lib/config.test.ts index e4339a42..a5bc631c 100644 --- a/apps/web/src/lib/config.test.ts +++ b/apps/web/src/lib/config.test.ts @@ -2,6 +2,7 @@ import assert from "node:assert/strict"; import { test } from "node:test"; +import { productionConnectSrc } from "./csp"; import { normalizeUrl, validatePublicConfig } from "./config"; const VALID_ENV: Record = { @@ -12,6 +13,35 @@ const VALID_ENV: Record = { VITE_ROUND_ID: "42", }; +test("production connect-src allowlists only configured endpoint origins", () => { + const connectSrc = productionConnectSrc({ + VITE_RPC_URL: "https://rpc.example.com/soroban", + VITE_KEEPER_URL: "https://keeper.example.com/api", + VITE_APPRAISAL_URL: "https://appraisal.example.com/v1", + VITE_DRAND_URL: "https://drand.example.com/", + VITE_DASHBOARD_ENDPOINT: "https://keeper.example.com/dashboard", + }); + + assert.equal( + connectSrc, + "'self' https://rpc.example.com https://keeper.example.com https://appraisal.example.com https://drand.example.com", + ); +}); + +test("production connect-src includes the configured RPC and Drand defaults", () => { + assert.equal( + productionConnectSrc({}), + "'self' https://soroban-testnet.stellar.org https://api.drand.sh", + ); +}); + +test("production connect-src rejects wildcard hosts", () => { + assert.throws( + () => productionConnectSrc({ VITE_KEEPER_URL: "https://*.example.com" }), + /VITE_KEEPER_URL must not use a wildcard origin/, + ); +}); + test("valid config returns no issues", () => { const issues = validatePublicConfig(VALID_ENV); assert.equal(issues.length, 0); @@ -272,4 +302,3 @@ test("VITE_RPC_URL with embedded credentials is rejected without echoing credent assert.doesNotMatch(credIssue.message, /webuser|websecret/); } }); - diff --git a/apps/web/src/lib/csp.ts b/apps/web/src/lib/csp.ts new file mode 100644 index 00000000..65968d36 --- /dev/null +++ b/apps/web/src/lib/csp.ts @@ -0,0 +1,47 @@ +// Copyright (c) 2026 Sub Rosa contributors + +const CONNECT_ORIGIN_KEYS = [ + "VITE_RPC_URL", + "VITE_KEEPER_URL", + "VITE_APPRAISAL_URL", + "VITE_DRAND_URL", + "VITE_DASHBOARD_ENDPOINT", +] as const; + +const DEFAULT_CONNECT_ORIGINS = { + VITE_RPC_URL: "https://soroban-testnet.stellar.org", + VITE_DRAND_URL: "https://api.drand.sh", +} as const; + +export function productionConnectSrc( + env: Record, +): string { + const origins = new Set(); + + for (const key of CONNECT_ORIGIN_KEYS) { + const configured = env[key]?.trim(); + const value = configured || DEFAULT_CONNECT_ORIGINS[key as keyof typeof DEFAULT_CONNECT_ORIGINS]; + if (!value) continue; + + let url: URL; + try { + url = new URL(value); + } catch { + throw new Error(`${key} must be a valid absolute HTTP(S) URL for production CSP.`); + } + + if (url.protocol !== "http:" && url.protocol !== "https:") { + throw new Error(`${key} must use http:// or https:// for production CSP.`); + } + if (url.username || url.password) { + throw new Error(`${key} must not contain credentials for production CSP.`); + } + if (url.hostname.includes("*")) { + throw new Error(`${key} must not use a wildcard origin in production CSP.`); + } + + origins.add(url.origin); + } + + return ["'self'", ...origins].join(" "); +} diff --git a/apps/web/src/vite-env.d.ts b/apps/web/src/vite-env.d.ts index 23420198..1ff02b4b 100644 --- a/apps/web/src/vite-env.d.ts +++ b/apps/web/src/vite-env.d.ts @@ -5,6 +5,10 @@ declare module "process/browser"; interface ImportMetaEnv { readonly VITE_RPC_URL?: string; + readonly VITE_KEEPER_URL?: string; + readonly VITE_APPRAISAL_URL?: string; + readonly VITE_DRAND_URL?: string; + readonly VITE_DASHBOARD_ENDPOINT?: string; readonly VITE_NETWORK_PASSPHRASE?: string; readonly VITE_CONTRACT_ID?: string; readonly VITE_ROUND_ID?: string; diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index acea4eb5..6ec18726 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -2,12 +2,15 @@ import react from "@vitejs/plugin-react"; import inject from "@rollup/plugin-inject"; import { createRequire } from "node:module"; -import { defineConfig } from "vite"; +import { defineConfig, loadEnv, type Plugin } from "vite"; +import { productionConnectSrc } from "./src/lib/csp"; const require = createRequire(import.meta.url); -export default defineConfig({ - plugins: [ +export default defineConfig(({ command, mode }) => { + const isProductionBuild = command === "build"; + const env = loadEnv(mode, process.cwd(), ""); + const plugins: Plugin[] = [ react(), { ...inject({ @@ -16,29 +19,56 @@ export default defineConfig({ }), enforce: "post", }, - ], - resolve: { - alias: { - buffer: require.resolve("buffer/"), - crypto: require.resolve("crypto-browserify"), - stream: require.resolve("stream-browserify"), - events: require.resolve("events/"), - "node:crypto": require.resolve("crypto-browserify"), - "node:buffer": require.resolve("buffer/"), + ]; + + if (isProductionBuild) { + const connectSrc = productionConnectSrc(env); + plugins.push({ + name: "production-connect-src-csp", + transformIndexHtml: { + order: "pre", + handler(html) { + return { + html, + tags: [{ + tag: "meta", + attrs: { + "http-equiv": "Content-Security-Policy", + content: `connect-src ${connectSrc};`, + }, + injectTo: "head", + }], + }; + }, + }, + }); + } + + return { + plugins, + resolve: { + alias: { + buffer: require.resolve("buffer/"), + crypto: require.resolve("crypto-browserify"), + stream: require.resolve("stream-browserify"), + events: require.resolve("events/"), + "node:crypto": require.resolve("crypto-browserify"), + "node:buffer": require.resolve("buffer/"), + }, + }, + define: { + global: "globalThis", + "process.env": "{}", + "process.version": JSON.stringify("v22.0.0"), + }, + optimizeDeps: { + include: ["buffer", "crypto-browserify", "stream-browserify", "process/browser", "events"], }, - }, - define: { - global: "globalThis", - "process.env": "{}", - "process.version": JSON.stringify("v22.0.0"), - }, - optimizeDeps: { - include: ["buffer", "crypto-browserify", "stream-browserify", "process/browser", "events"], - }, - server: { - port: 5173, - fs: { - allow: ["..", "../..", "../../..", "../../../.."], + server: { + port: 5173, + fs: { + allow: ["..", "../..", "../../..", "../../../.."], + }, }, - }, + }; }); diff --git a/docs/DEPLOY.md b/docs/DEPLOY.md index 9e195689..cabd7086 100644 --- a/docs/DEPLOY.md +++ b/docs/DEPLOY.md @@ -63,6 +63,20 @@ VITE_CONTRACT_ID=CA7KSDEYJEPGZEB2ZROTLUWKQQ6GIRIQNGG6Z745MZ34QHP4UJPWODEX VITE_ROUND_ID=1 ``` +Production builds add a `connect-src` CSP to the generated HTML. It allows +`'self'`, the origin of `VITE_RPC_URL` (testnet RPC by default), +`VITE_DRAND_URL` (`https://api.drand.sh` by default), and any configured +`VITE_KEEPER_URL`, `VITE_APPRAISAL_URL`, or `VITE_DASHBOARD_ENDPOINT` origins. +Only origins are included; paths and query strings do not broaden the policy. +Malformed URLs, URL credentials, and wildcard hosts fail the production build. +These settings are public and must never contain secrets. + +For local development, the web `.env.example` documents local keeper, +appraisal, Drand, and dashboard endpoints (for example +`http://localhost:3001`, `http://localhost:4021`, and +`http://localhost:8080`). The development server does not inject the production +CSP; do not use wildcard hosts in production configuration. + Important: Vite only exposes vars prefixed with `VITE_`. They are **public** in the browser bundle — never put secret keys here. ### Local dev (optional) @@ -272,4 +286,3 @@ lines, unhandled crash dumps, and DevTools HAR files can still include them: in `localStorage` or `sessionStorage`, `Cookie` and `Authorization` headers in Network HAR exports, wallet seed phrases or `S…` secret keys, and local unrevealed bid values/nonces before the round enters `Revealing`. -