You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mint a new bearer token for the sandbox's preview-URL auth gate. The old token stops working immediately — there is no zero-downtime dual-token mode, so roll the new token out to your caller before discarding the old one.
7
+
8
+
If the sandbox was originally created without [`previewAuth`](/api-reference/sandboxes/create), this call installs a token and starts enforcing the gate from that point on.
9
+
10
+
The plaintext is returned exactly once in the response. Only the SHA-256 hash is persisted server-side; the server cannot show you the token again later.
Copy file name to clipboardExpand all lines: docs/api-reference/sandboxes/create.mdx
+15-1Lines changed: 15 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,13 +45,27 @@ If both `cpuCount` and `memoryMB` are provided, they must match a platform tier.
45
45
Name of a pre-built snapshot for instant boot
46
46
</ParamField>
47
47
48
+
<ParamFieldbody="previewAuth"type="object">
49
+
Opt in to bearer-token authentication on the sandbox's preview URLs. When set, every request to `https://sb-<id>-p<port>.<domain>` must include an `Authorization: Bearer <token>` (or `X-OC-Preview-Token: <token>`) header; missing or wrong → 401.
50
+
51
+
-`scheme`*(string)*: must be `"bearer"`. Reserved for HMAC/JWT later.
52
+
-`token`*(string)*: `"auto"` (or omitted) → server generates a 256-bit random token. An explicit string of at least 16 characters lets you bring your own.
53
+
54
+
The plaintext is returned exactly once in the response as `previewAuthToken`; only its SHA-256 hash is stored. Use [`POST /api/sandboxes/{id}/preview/rotate`](/api-reference/preview/rotate-auth) to mint a new one.
55
+
56
+
Omit this field for the legacy open behavior — preview URLs respond to anyone who can reach the hostname.
Preview URLs persist across hibernation/wake cycles — no need to re-create them.
59
59
60
+
## Bearer-Token Authentication
61
+
62
+
By default, anyone who knows the preview hostname can hit your sandbox's port. To require an `Authorization: Bearer <token>` header on every request, opt in at create time:
Mint a new bearer token for the sandbox's preview URLs. The old token stops working immediately. The new plaintext is printed once — capture it before moving on; the server will not return it again.
45
+
46
+
```bash
47
+
oc preview rotate-auth sb-abc
48
+
# New preview auth token (shown once): qx2sSi5IYX...
49
+
```
50
+
51
+
Calling this on a sandbox that was created without `--preview-auth` installs a token and starts enforcing the gate from that point on.
52
+
53
+
See [`oc sandbox create --preview-auth`](/reference/cli/sandbox#oc-sandbox-create) to enable at create time, or [`--preview-auth-token`](/reference/cli/sandbox#oc-sandbox-create) to bring your own.
Copy file name to clipboardExpand all lines: docs/reference/cli/sandbox.mdx
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,8 +29,18 @@ Create a new sandbox. **Alias:** `oc create`
29
29
Metadata `KEY=VALUE` (repeatable)
30
30
</ParamField>
31
31
32
+
<ParamFieldquery="--preview-auth"type="bool">
33
+
Require a bearer token on the sandbox's preview URLs. The server generates a 256-bit random token and prints it once. See [Preview-URL authentication](/sandboxes/preview-urls#authentication).
The `domain` and `getPreviewDomain(port)` properties construct the preview hostname directly — no API call needed. Traffic is routed through the control plane proxy to the correct sandbox.
25
25
26
+
## Authentication
27
+
28
+
Preview URLs are public by default — anyone who can guess or learn the hostname can hit your sandbox's port. To require a bearer token in front of every request, opt in at create time with `previewAuth`.
29
+
30
+
When enabled, the control plane validates an `Authorization: Bearer <token>` (or `X-OC-Preview-Token: <token>`) header on every preview-URL request. Missing or wrong → 401. The check happens before traffic ever reaches your sandbox, so a brute-force run also can't wake a hibernated sandbox.
31
+
32
+
### Enable at create time
33
+
34
+
<CodeGroup>
35
+
36
+
```typescript TypeScript
37
+
const sandbox =awaitSandbox.create({
38
+
previewAuth: { scheme: "bearer", token: "auto" },
39
+
});
40
+
41
+
// previewAuthToken is returned exactly once — store it somewhere durable.
`token: "auto"` (or omitting the field) tells the server to generate a 256-bit random token. To bring your own — useful when your gateway already has a secret it can share with both ends — pass an explicit string of at least 16 characters:
The token gates **every port** on the sandbox — there's one token per sandbox, shared across all preview URLs you create on it.
96
+
97
+
### Rotation
98
+
99
+
When you need to roll the token — exposure, scheduled rotation, employee offboarding — call `rotate`. The old token stops working immediately and the new plaintext is returned exactly once:
# sandbox.preview_auth_token is also updated in place.
111
+
```
112
+
113
+
```bash CLI
114
+
oc preview rotate-auth sb-abc123
115
+
# New preview auth token (shown once): <new token>
116
+
```
117
+
118
+
</CodeGroup>
119
+
120
+
There is no zero-downtime dual-token mode in v1 — roll out the new token to your caller before discarding the old one, or expect a brief window of 401s during the swap.
121
+
122
+
### What's stored and what's not
123
+
124
+
- Only the **SHA-256 hash** of the token is persisted. The server never sees the plaintext after the create or rotate response returns.
125
+
- There is no GET endpoint that returns the token. If you lose it, your only options are to rotate (mint a new one) or recreate the sandbox.
126
+
- A sandbox created **without**`previewAuth` has open preview URLs — exactly the same as before this feature existed. The feature is fully opt-in and backwards compatible.
127
+
128
+
### Where the check happens
129
+
130
+
Authentication is enforced by the cell's control plane immediately before the request is proxied to the worker. That means:
131
+
132
+
-**Hibernated sandboxes** stay hibernated on missing/wrong tokens — bad tokens can't burn wake capacity.
133
+
-**Self-hosted deployments** without the Cloudflare edge get the same gate for free — the check follows the sandbox, not the front door.
134
+
26
135
## Explicit Preview URLs
27
136
28
137
For tracking, custom domains, or auth configuration, use the preview URL API:
0 commit comments