---
title: 'Secure Studio: tokens, loopback and origins'
description: 'What guards Studio''s API in each setup: the loopback Host and cross-origin write rules without a token, the stable dev token of weft studio, studio.Token and the separate IngestToken, panel tokens scoped to one conversation, the live stream''s 60-second grant, and a UI that makes no external request.'
url: https://weftgo.dev/docs/production/security/
type: how-to
sources:
  - 'weft/CHANGELOG.md:47 (tag-scoped panel tokens), 64 (handoff for the server token), 194 (0.18.1: TestAuthMatrix reads every route), 226 (0.18.0: thread.WithRunMetadata); weft/studio/tag_token_test.go:420-495 — v0.20.2'
  - weft/studio/auth.go:26-53 (the three setups; a token rides the Authorization header only), 69-218 (panel claims, weft_pt. tokens, HMAC-SHA256 keyed by the Token, constant-time check, expiry), 230-235 (bearerToken), 246-303 (auth, ?token= refused 401, the loopback Host, the cross-origin write, server or panel token), 324-338 (tokenEqual), 347-350 (forbidden), 468-477 (mayAct), 574-617 (scopeLive, the all selector), 622-633 (serverOnly), 642-735 (servePanelTokens, ttl default 1h, scope read|playground), 741-772 (cors, Vary Origin, methods incl. PUT and DELETE, X-Request-Id exposed), 774-795 (originAllowed defaults), 802-824 (crossOriginWrite), 832-869 (hostAllowed, isLoopbackName), 871-879 (DevToken)
  - weft/studio/livegrant.go:22-50 (the grant, 60 s window, reusable, no-store, never logged, panel token stream ends at its expiry), 53 (liveGrantTTL), 363 (Cache-Control no-store)
  - 'weft/studio/studio.go:81-110 (IngestToken, Token, AllowOrigins godoc), 293-301 (Handler: does not bind a port; bind loopback or configure Token), 349-391 (ingestAuthorized: bearer, else loopback peers without proxy headers or a foreign Origin)'
  - weft/studio/handoff.go:20-45 (read-scoped tokens cannot hand off, handoffTTL 5 min, the to path rule), 50-60 (handoff only with a Token)
  - weft/studio/meta.go:299-308 (mayReadDBPath)
  - weft/studio/delete.go:47-61 (mayDelete, never a panel token)
  - weft/studio/routes.go:260-278 (/api/panel-tokens only with a Token)
  - 'weft/studio/assets.go:57-62 (cspFor, self only); studio/studio.go:397-401 (CSP, nosniff, no-referrer on every response); studio/README.md:269-270 (pricing: nothing fetched)'
  - weft/studio/README.md:29-55 (setup A without a Token, the cross-origin write), 82-91 (the dev token), 155-242 (setup C, panel tokens, children scoping, read vs playground scope, hand-off)
  - 'weft/cmd/weft/token.go:17-30, 126-130 (<db>.token, 32 random bytes base64url, 0600); cmd/weft/main.go:875 (newServer: Base and Token, no ingest-token flag)'
  - weft/AGENTS.md:350 (/api/panel-tokens, /api/panel-handoff), 402-411 (Token walls /api, ?token= refused, the live grant, the cross-origin write, IngestToken), 415-430 (the dev token)
  - weft/docs/adr/0007-approval-boundary.md:63-69 (approval is not a security boundary)
  - 'weft/CHANGELOG.md:2304 (the live grant, 0.12.0), 2387 and 2299 (the stable dev token, 0.11.0), 3136 (loopback ingest refuses forwarded requests, 0.8.0), 3163 (the UI''s token prompt and #token= hand-over), 3187 (the loopback Host rule, 0.8.0), 3902 (PUT in CORS methods), 4381 (Token walls the API, 0.7.0), 5233 (offline by construction)'
  - 'weft/studio/requests.go:187-211, 362 (readsPrompts: requests); studio/logs.go:121-127; studio/export.go:106-107, 131; studio/fixtures.go:528; studio/manifests.go:98; studio/steps.go:556; studio/diff.go:214; studio/preview.go:176'
  - weft/cmd/weft/api.go:36-49, 378-404 (weft open --with-token prints the fragment); weft/cmd/weft/resolve.go:67-78 (--token, else WEFT_STUDIO_TOKEN, else the discovery file of the Studio found); weft/cmd/weft/main.go:125-131 (the id is optional) — v0.20.2
  - weft/studio/auth.go:261-279 (setup A admits /api/live without a sig)
  - weft/CHANGELOG.md:1289-1297 (the cross-origin write rule, 0.14.0)
  - weft/studio/playground.go:160-197 (/api/runtimes empties instructions, tool-choice name and stop for a read token)
  - docs-site/examples/production/main_test.go TestTokens, TestSetupAHostWall; main.go hostedStudio (panel token scoping)
  - docs-site/examples/studio/main_test.go TestSetupARefusesCrossOriginWrite, TestTokenWallsAPI
  - 'weft/studio/auth.go:774-792 (originAllowed: with a Token and no AllowOrigins list, http or https origins on localhost or 127.0.0.1, any port); weft/studio/studio.go:412-420 (cors wraps every route, /panel.js included); weft/cmd/weft/main.go:475-494 (the extras weft studio passes: Manifest, Playground, Retain, Pricing), 875-907 (newServer: Base, Token, those extras, Open or DB; no AllowOrigins flag or env var)'
  - 'weft/studio/auth.go:775-784 (originAllowed: a configured list is the whole rule, the localhost default applies only with none)'
  - 'observed: a scratch weft studio v0.17.0 and curl preflights to /api/meta: http://localhost:5173, https://localhost:3000, http://localhost and http://127.0.0.1:8080 got Access-Control-Allow-Origin; http://app.localhost:5173, http://[::1]:5173, http://127.0.0.2:5173, http://192.168.1.5:5173 and http://myapp.test:5173 got none; GET /panel.js with Origin http://app.localhost:5173 had none either. In Chromium 151 a page on http://app.localhost:7382 could not load that Studio''s /panel.js, and a same-origin copy had /api/meta blocked'
---

# Secure Studio: tokens, loopback and origins

Studio's JSON API carries transcripts and, with the playground on, verbs that start runs and decide approvals. What guards it depends on the [setup](https://weftgo.dev/docs/reference/glossary/#setup-a-b-c): without a token (setup A) only a loopback `Host` reaches it and cross-origin writes are refused; `weft studio` (setup B) adds a stable [dev token](https://weftgo.dev/docs/reference/glossary/#dev-token); a hosted Studio (setup C) sets [`studio.Token`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Token) and hands pages [panel tokens](https://weftgo.dev/docs/reference/glossary/#panel-token) scoped to one conversation. OTLP ingest has its own guard, [`studio.IngestToken`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#IngestToken).

| Setup | `/api` answers | OTLP ingest answers | CORS |
|---|---|---|---|
| A · embedded, no `Token` | a loopback `Host` or an `AllowOrigins` host; no cross-origin unsafe method | loopback peers without proxy headers or a foreign `Origin` | none, unless `AllowOrigins` lists origins |
| B · `weft studio` | the dev token as a bearer | loopback peers, as A (the binary has no ingest-token option) | `http` and `https` origins on `localhost` and `127.0.0.1`, any port; fixed (the binary has no `AllowOrigins` option) |
| C · hosted, `Token(tok)` | the server token or a valid panel token as a bearer; the `Host` does not matter | `IngestToken`'s bearer when set, else as A | `AllowOrigins`, else `localhost` and `127.0.0.1` |

Studio's handler does not bind a port: you choose the address. Bind loopback, or set a `Token`, before you expose it wider. Studio does not terminate TLS; serve a hosted Studio behind your HTTPS listener or proxy.

## Setup A without a token

With no `Token`, the whole `/api` tree (reads, the live stream, the playground and debugger verbs, the runtime link) answers only a request whose `Host` is a loopback name: `localhost`, `*.localhost`, `127.0.0.0/8` or `[::1]`, any port. A DNS-rebinding page (`http://evil.example:7331` resolving to 127.0.0.1) is same-origin to the browser, but its `Host` is `evil.example:7331`, so it gets a 403 that names what to configure. Proxy headers (`X-Forwarded-Host`, `Forwarded`) are never read; behind a proxy, the `Host` it forwards is the one checked.

The loopback `Host` stops a rebound page, not a cross-site one: any site's page can POST to `localhost` without a preflight. So setup A also refuses a cross-origin write. An unsafe method (anything but GET, HEAD or OPTIONS) is refused when its `Origin`'s `host:port` is not the request's `Host` and not an [`AllowOrigins`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#AllowOrigins) entry, or when it sends no `Origin` but `Sec-Fetch-Site: cross-site`. The 403 starts `cross-origin write refused (setup A)`. Same-origin pages and clients that send neither header (`curl`, the runtime link) pass. Reads need no such rule: setup A sends no CORS headers, so the browser hides the answer from a foreign page.

To serve setup A on a real hostname, list that origin, `studio.AllowOrigins("http://myapp.internal:8080")`; its `host:port` must equal the request's `Host` ([Run Studio](https://weftgo.dev/docs/observe/studio/setup/#who-setup-a-answers) shows it in a program). `"*"` admits every `Host` and every origin. The UI shell and `/panel.js` carry no data and are served to any `Host`. The loopback `Host` rule arrived in weft 0.8.0; before it, a rebound page could start playground runs, approve parked calls and read transcripts. The cross-origin write rule arrived in 0.14.0.

## The dev token

`weft studio` and `weft dev` protect the API with a token that is stable per database: `<db>.token` beside the SQLite file (`.weft/weft.db.token`), 32 random bytes in base64url, mode 0600, created on the first start and reused by every later one. It is never printed. `--open` hands it to the browser in the URL fragment (`#token=`), which never reaches a server; the UI reads it, and any other browser asks for it once ("This Studio needs its API token"). [`weft open --with-token`](https://weftgo.dev/docs/reference/cli/) prints a Studio link with the token in its fragment; the id is optional. The token is `--token`, else `WEFT_STUDIO_TOKEN`, else the token in the discovery file of the Studio it found. It never reads `<db>.token`. `weft dev` passes the dev token to your app as `WEFT_STUDIO_TOKEN`.

```sh
weft studio --rotate-token
```

`--rotate-token` writes a new one before serving. `--token` or `WEFT_STUDIO_TOKEN` override the file without touching it. A database with no file (`:memory:`, ClickHouse) gets a per-process token, generated by [`studio.DevToken`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#DevToken) and printed at start. The dev token is a server token like any `Token` value, so it is also the signing key of the panel tokens minted against it. Since weft 0.11.0; before it, every start printed a fresh token.

## Tokens and the wall

`studio.Token(tok)` walls the `/api` tree. Every request needs `Authorization: Bearer <tok>` or a panel token, compared in constant time; then the `Host` no longer matters. `/panel.js` and the UI shell stay static and unchecked.

A token is accepted from the `Authorization` header only. A `?token=` query parameter is refused on every `/api` route, in every setup, whatever it holds: a 401 that starts `a token in the URL is never accepted`. A URL lands in logs, history and `Referer` headers. In weft 0.7.0 `?token=` was the live stream's way in, since `EventSource` cannot set headers; [the live grant](#live-grant) replaced it.

OTLP ingest (`/v1/traces`, `/v1/logs`) is checked against a different value:

- With `studio.IngestToken(tok)`, every ingest request needs `Authorization: Bearer <tok>`, loopback included. Each route compares the bearer with its own token, so when the two differ the API token is not accepted there and the ingest token is not accepted on `/api`. Set them to the same value and each works on both.
- Without one, ingest answers loopback peers only, and refuses even those when the request carries `Forwarded`, `X-Forwarded-For` or `X-Real-IP` (behind a proxy on the same host every client is a loopback peer) or a browser `Origin` that is not allowed. A proxied Studio sets `IngestToken`.

`otel.Studio(url, token)` sends one token as the ingest bearer, so an app exporting to a hosted Studio passes the ingest token there ([Deploy](https://weftgo.dev/docs/production/deploying/#export-to-the-hosted-studio)).

`AllowOrigins(origins...)` is the CORS list for the API and ingest. With a `Token` and no list, `localhost` and `127.0.0.1` on any port (http or https) are allowed; setup A without a list sends no CORS headers. Allowed origins get `GET, POST, PUT, DELETE, OPTIONS`, the `Authorization`, `Content-Type` and `Last-Event-ID` headers, and `X-Request-Id` exposed; answers carry `Vary: Origin` whenever CORS is in play.

Check the wall from a shell:

```sh
curl -s -o /dev/null -w '%{http_code}\n' https://studio.example/studio/api/runs
curl -s -H "Authorization: Bearer $WEFT_STUDIO_TOKEN" https://studio.example/studio/api/meta
```

The first prints `401`. The example's `TestTokens` checks the same rules offline: ingest without its bearer refused from loopback, the API token refused on ingest, `?token=` a 401.

weft's own `TestAuthMatrix` reads every route Studio registers and fails on a route with no row in its table of identities and answers, so a new route cannot ship unchecked. The OTLP receiver's two routes are rows too: the ingest token opens them, and every API identity gets 401.

## Panel tokens and handoff

The devtools panel runs in your app's page, where the server token must never go. Your backend mints a panel token for the conversation the page shows, with the server token:

```sh
curl -s -X POST https://studio.example/studio/api/panel-tokens \
  -H "Authorization: Bearer $WEFT_STUDIO_TOKEN" -H 'Content-Type: application/json' \
  -d '{"public_id":"pub_jane","ttl":"15m"}'
```

```text
{"token":"weft_pt.…","public_id":"pub_jane","scope":"read","exp":"…"}
```

`POST /api/panel-tokens` exists only when a `Token` is set, and only the server token may call it. A panel token is `weft_pt.<payload>.<signature>`: the claims `{public_id, tags, scope, exp}` signed with HMAC-SHA256 under the `Token` value, so changing the `Token` invalidates every panel token minted under it. `ttl` defaults to `1h` and must be a positive duration; there is no maximum, so choose one. The public id is the one your runs carry as `weft.public_id` (`thread.PublicID` sets it on a session's turns).

What a panel token may do:

| Access | read (default) | `"playground": true` |
|---|---|---|
| Runs, sessions, traces, the live stream of its public id | ✓ | ✓ |
| Anything outside its public id | 403 `this token is scoped to public id …` | 403 |
| Start runs, decide parked calls, steer | 403 `this token is read-only` | ✓ |
| What carries system prompts, catalogs or app logs: `/api/manifest`, `manifests/{hash}`, a run's requests, its app logs, exports with requests, fixtures | 403, with a `hidden` badge | ✓ |
| The same inside a step, a diff's system row, a playground preview; in `/api/runtimes` (200) each agent's instructions, default tool-choice name and stop sequences | hidden or emptied | ✓ |
| Experiments | server token only | server token only |
| The runtime link, breakpoints, `DELETE`, `/api/live?all=1` | 403 | 403 |
| `/api/meta`'s database path and size | omitted | omitted |

Since 0.20.0 a token can name a batch of runs by their run metadata instead of, or beside, a public id: `{"tags": {"arena.batch": "cli-20261012-115753"}}`. Such a token is read-only and held to its tags on every read, as a public id token is to its public id. Run metadata is written by your code, `weft.Metadata` on a run or `thread.WithRunMetadata` on every turn of a session, so a tag token is only as tight as that tagging; minting stays the server token's ([A tag-scoped token](https://weftgo.dev/docs/observe/devtools/security/#tokens)).

A run the token may read can have child runs with another public id, or none. Every route that reaches runs through another keeps the token's own children and reports the rest as one `hidden` hole with a count, never by id, public id or prompt. An unknown run id answers 404 as for any identity, so scoping does not reveal whether another public id's run exists. The example's `hostedStudio` mints a token for `pub_jane` and gets 200 for its runs and 403 for another public id.

`GET /api/panel-handoff?to=<path>` opens Studio from the panel without pasting a token. Under a playground-scoped panel token it answers 303 to `<base><to>#token=` with a fresh read token for the same public id, valid 5 minutes and never past the caller's own expiry; with `Accept: application/json` it answers `{location, exp}` instead. A read-scoped token gets 403: a scope never widens. Since 0.20.0 the server token hands off too: the location carries it in the fragment, as `weft open --with-token` does, so the panel's `⤢` lands signed in. `to` must be a path under Studio's base (no scheme, host, fragment, backslash or `.` segment), else 400, and every answer is `Cache-Control: no-store` with `Referrer-Policy: no-referrer`. The route exists only with a `Token`. [Secure the devtools panel](https://weftgo.dev/docs/observe/devtools/security/) covers the page side.

## Live grant

`EventSource` cannot send headers, and a token never goes in a URL, so the live stream opens with a grant. The page asks with its bearer, or, in setup A, with no credential:

```http
POST /api/live-grant
Content-Type: application/json

{"run":"r_1","kinds":"event,run"}
```

The answer is `{sig, exp}`, `Cache-Control: no-store`. The page then opens `GET /api/live?run=r_1&kinds=event,run&sig=<sig>`. The signature binds the identity that asked, the selector, the kinds set and the expiry: 60 seconds from the grant, never past a panel token's own expiry. Another selector, another kinds set, a tampered or expired sig is a 401.

A grant is a window, not a ticket: any number of streams may open with it while it lasts, because an `EventSource` reconnects with the same URL. A leaked grant URL is therefore live for 60 seconds; the token is never in it and Studio never logs the URL. Each stream opened in time runs until it is closed. A panel token's stream ends at the token's expiry with one `event: expired` frame; a server token's does not. A panel token is granted only streams inside its public id, and the `all` selector is the server token's or setup A's loopback alone. Without a `Token` the signing key is random per process, so setup A can take the same path; it also opens a stream without a sig, as its open identity. The grant arrived in weft 0.12.0.

## No external requests

Studio's UI loads nothing from another origin. Every response carries `Referrer-Policy: no-referrer`, `X-Content-Type-Options: nosniff` and `Content-Security-Policy: default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self'` plus one hash per inline script, never `'unsafe-inline'` for scripts. Pricing is a local table and nothing is fetched. A Studio on an isolated network works the same as one on the internet.

## What these tokens do not protect

Studio's tokens guard Studio's API. They do not make an approval a security control:

- **Approval is a policy seam, not a security boundary.** A per-call prompt does not contain a compromised prompt or a malicious tool; an OS-level sandbox around the tool does (ADR 0007). See [Require approval before a tool runs](https://weftgo.dev/docs/guides/approvals/).
- **A decision that crosses a process boundary should be signed.** [Sign approval decisions](https://weftgo.dev/docs/sessions/signed-approvals/#security-properties) lists what signing guarantees and what it does not.
- **Grants pre-approve.** `s.Grant` is an in-process, unsigned call, and `thread.ApproveAlways` mints a grant for the call's exact arguments, so the next such call never parks ([Grants](https://weftgo.dev/docs/sessions/approvals/#grants)).
- **The panel's playground scope acts.** A playground-scoped panel token can start runs and decide that conversation's parked calls; mint it only for a page whose user may.

## Best practices & gotchas

- **Set a `Token` before Studio listens beyond loopback.** Without one the API's only guard is the loopback `Host`, widened by `AllowOrigins`. (Source: `studio.Handler` godoc, `studio/studio.go:293-301`; `studio/auth.go:246-303`)
- **Use two different secrets for `Token` and `IngestToken`.** Each route checks its bearer by plain comparison with its own token, so the separation holds only when the values differ; then apps hold the ingest token and only your backend holds the server token, which also signs panel tokens. (Source: `studio/studio.go:368-371`; `studio/auth.go:290`, `87-99`)
- **Behind a reverse proxy, set `IngestToken`.** Loopback ingest refuses forwarded requests, so a proxied Studio without one ingests nothing remote. (Source: `studio/studio.go:349-362`; CHANGELOG 0.8.0 L3136)
- **Behind a proxy that rewrites `Host` (Vite's `changeOrigin: true`), list the front origin in `AllowOrigins`.** Setup A compares the `Origin`'s `host:port` with the `Host`. (Source: `studio/README.md:38-45`)
- **`weft studio`'s CORS list cannot be widened.** It has no flag or environment variable for `AllowOrigins`, so a dev page on any other host (`*.localhost`, `[::1]`, a LAN IP) gets no CORS headers, not even on `/panel.js`, and the panel does not appear. Use a same-origin proxy, or embed Studio with `studio.AllowOrigins`, whose list replaces the `localhost` default. [When the panel does not connect](https://weftgo.dev/docs/observe/devtools/install/#when-the-panel-does-not-connect) has the cases. (Source: `studio/auth.go:774-792`; `cmd/weft/main.go:475-494`, `768-800`; observed with curl and Chromium)
- **Mint short panel tokens.** The 1 h default has no ceiling and a token cannot be revoked short of rotating the `Token`. (Source: `studio/auth.go:642-735`)
- **Never put a token in a URL query.** It is refused anyway; send the bearer, and use a live grant for `EventSource`. (Source: `studio/auth.go:250-258`)

## See also

- [Deploy an agent service with a hosted Studio](https://weftgo.dev/docs/production/deploying/)
- [Run Studio: embedded, local binary or hosted](https://weftgo.dev/docs/observe/studio/setup/)
- [Watch runs live](https://weftgo.dev/docs/observe/studio/live/)
- [Secure the devtools panel](https://weftgo.dev/docs/observe/devtools/security/)
- [Control what content leaves the process](https://weftgo.dev/docs/production/content-and-privacy/)

Symbols used: [`studio.Token`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Token) · [`studio.IngestToken`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#IngestToken) · [`studio.AllowOrigins`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#AllowOrigins) · [`studio.DevToken`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#DevToken) · [`studio.Handler`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/studio#Handler) · [`otel.Studio`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/otel#Studio) · [`thread.PublicID`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#PublicID) · [`thread.ApproveAlways`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#ApproveAlways) · [`Session.Grant`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#Session.Grant)
