Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/web/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion apps/web/src/lib/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {
Expand All @@ -12,6 +13,35 @@ const VALID_ENV: Record<string, string> = {
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);
Expand Down Expand Up @@ -272,4 +302,3 @@ test("VITE_RPC_URL with embedded credentials is rejected without echoing credent
assert.doesNotMatch(credIssue.message, /webuser|websecret/);
}
});

47 changes: 47 additions & 0 deletions apps/web/src/lib/csp.ts
Original file line number Diff line number Diff line change
@@ -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, string | undefined>,
): string {
const origins = new Set<string>();

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(" ");
}
4 changes: 4 additions & 0 deletions apps/web/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
82 changes: 56 additions & 26 deletions apps/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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: ["..", "../..", "../../..", "../../../.."],
},
},
},
};
});
15 changes: 14 additions & 1 deletion docs/DEPLOY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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`.