<!-- TokenOS docs · Building apps · Markdown mirror of https://tokenos.ai/docs/preview-sandbox · HTML: https://tokenos.ai/docs/preview-sandbox · index: https://tokenos.ai/llms.txt -->

# Preview sandbox & wallet bridge

Generated code is untrusted until you have reviewed it, so every preview runs in a locked-down iframe. At the same time a Web3 app is useless without a wallet — so the IDE relays wallet calls to the preview through a bridge that only works after you click **Allow**.

### What the sandbox enforces (SEC-003)

- Every preview route is served with a Content-Security-Policy `sandbox allow-scripts allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox` — note: **no** `allow-same-origin`, so the preview has an opaque origin with no cookies, storage or access to your TokenOS session.
- `frame-ancestors` is restricted to TokenOS origins; a preview cannot be embedded elsewhere.
- A strict `Permissions-Policy` blocks camera, microphone, geolocation, payment, USB, display capture and MIDI.
- The headers are applied by middleware to every path under the preview prefixes, so new preview routes inherit them.
- Published apps at `/api/apps/{slug}` are intentionally *not* sandboxed — they are your reviewed product.

### How the wallet bridge works

1. **Injection** — Every served preview HTML receives a small inline script providing memory-backed `localStorage` / `sessionStorage` and bridged `window.solana`, `window.phantom.solana` and `window.ethereum` providers. Outside the IDE the shim is a no-op and the app talks to the browser's real wallet extension.
2. **Request** — When the app calls `connect()`, `signMessage`, `signTransaction` or `signAndSendTransaction` (Solana) or `ethereum.request` (EVM), the shim posts a `tokenos:wallet` message to the parent IDE.
3. **Consent** — The IDE shows a prompt: **Allow for this project** or **Deny**. Allow is disabled until the IDE wallet is connected. Requests queue until you answer. Permission is memory-only, per project and preview URL — it resets when you reload.
4. **Relay** — Allowed requests are executed with the wallet you connected in the IDE and the result is posted back as `tokenos:wallet:result`. Wallet events are forwarded as `tokenos:wallet:event`.

| Chain | Relayed | Blocked |
| --- | --- | --- |
| Solana | connect · disconnect · signMessage · signTransaction · signAllTransactions · signAndSendTransaction · request | — |
| EVM | ethereum.request (eth_requestAccounts, eth_chainId, eth_sendTransaction, personal_sign, wallet_switchEthereumChain, wallet_addEthereumChain …), enable, send, sendAsync | eth_sign · eth_signTransaction (raw signing is never relayed) |

### The chip

The header chip above the preview shows the bridge state — `off`, `pending`, `allowed`, `denied` — and is clickable: pre-allow before the app asks, revoke an allowed project, or reset a denial.

### Inside the generated app

**lib/walletBridge.js (shipped with every build)**

```js
import { getSolana, getEthereum } from './walletBridge';

const provider = getSolana();              // bridged in the IDE, window.solana when hosted
await provider.connect();
const sig = await provider.signAndSendTransaction(tx, { rpcUrl: CONFIG.rpcUrl });
// confirmation is polled over HTTP via the RPC proxy — no WebSocket needed
```

`hooks/useWallet.js` wraps this for React and hands pages a plain address string. RPC reads go through `/api/solana-rpc` (an allow-listed proxy) so no API key is ever present in client code.

> **Tip — Rules the pipeline enforces**
> - Never add allow-same-origin to a preview iframe.
> - Design Mode and bridge messages are accepted only from the preview iframes (origin and source checks).
> - Nothing is relayed before the builder clicks Allow; a denial is remembered for the session.
> - Protected runtime files (config.js, lib/web3.js, lib/walletBridge.js, hooks/useWallet.js) are never rewritten by the sanitizers.

---

*Building apps · TokenOS documentation. Canonical page: https://tokenos.ai/docs/preview-sandbox. All docs as Markdown: https://tokenos.ai/llms.txt. Payments on TokenOS are for metered usage only; nothing on this page is investment advice or a promise of income.*
