cloudflare / cloudflare/sandbox-sdk
`setAllowedHosts`: blocked outbound requests return opaque `520 "Origin is disallowed"` — allow customizing the block response
- Dominant language
- TypeScript
- Stars
- 1.1k
- Forks
- 114
- Avg merge
- 22h 42m
- Merged PRs (30d)
- 14
Description
**Summary**
When `setAllowedHosts()` is used, any outbound request to a non-allowlisted host is rejected with `520` and the fixed plain-text body `Origin is disallowed` (from `@cloudflare/containers`). Sandboxed code that calls `.json()` on the response — the common case — crashes with an opaque parse error, and there's no supported way to customize the block response without giving up the allowlist gate entirely.
Version: `@cloudflare/sandbox@0.12.1`.
**Repro**
```python
import requests
resp = requests.post("https://disallowed-host.example", json={})
print(resp.json())
```
- **Node/JS** runtime: `Unexpected token 'O', "Origin is disallowed" is not valid JSON`
- **Python** runtime: `json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)` — body not echoed, indistinguishable from an empty response.
Both runtimes receive the same `520 "Origin is disallowed"`; the difference is only how each language's JSON parser formats the decode error.
**Why it's hard to handle today**
In `@cloudflare/containers`, the `allowedHosts` gate runs *before* any `outbound` / `outboundByHost` handler and hardcodes `new Response('Origin is disallowed', { status: 520 })`. So the only way to return a custom (e.g. structured-JSON) block response is to drop `setAllowedHosts` and re-implement allow/deny inside a catch-all `outbound` handler — which moves the egress security gate into user code.
**Request**
Provide a supported way to customize the blocked-host response while keeping the `allowedHosts` gate — e.g. an optional `onDisallowedHost(request) => Response` hook, or a configurable default block response. Returning structured JSON (e.g. `{ "error": "host_not_allowed", "host": "..." }`) would let calling code surface a clear, parseable error in every runtime.
Contributor guide
Research direction
Start by inspecting the @cloudflare/containers allowedHosts gate and how it precedes outbound and outboundByHost handlers. Reproduce the blocked request in the Node/JS and Python examples, then define and test a supported customization that preserves the allowlist gate and returns a parseable block response.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- networking, security
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100