Bindings · Exercise 04
Hand out capabilities
Run a piece of untrusted code in a Dynamic Worker that gets exactly one binding you choose, and no network.
Paste this into your agent
Copied! Now paste it into your agent. No agent yet? Here's how to get one.
Hand out capabilities
So far, every binding has come from your config. In this exercise your Worker creates one at runtime and hands it to code you don’t trust, which gets exactly that binding and nothing else: no other bindings, and no network.
What you’ll build
A route that runs a small piece of code, maybe code your agent writes, in a Dynamic Worker: a sandbox your Worker starts on demand. The sandbox gets one binding, a notebook that can only read and write its own keys in your KV namespace. It can’t see your other bindings or secrets, and it can’t reach the internet.
Dynamic Workers need the Workers Paid plan once deployed, so you’ll run this one locally. Deploying it is optional.
Prerequisites
- You’ve completed Swap resources and still have that Worker on disk.
A binding you design yourself
You could hand the sandbox your whole KV namespace, but you don’t have to. Instead, you write a small class that wraps it and exposes only what you choose: two methods, and only keys with a prefix the caller can’t change.
src/index.tsexport class Notebook extends WorkerEntrypoint<Env, { prefix: string }> {
async write(key: string, value: string) {
await this.env.NOTES.put(`${this.ctx.props.prefix}:${key}`, value);
}
async read(key: string) {
return this.env.NOTES.get(`${this.ctx.props.prefix}:${key}`);
}
}
The untrusted code is an ordinary Worker, held in a string. All it can see is what’s on its env:
export default {
async fetch(request, env) {
await env.NOTEBOOK.write("idea", "hello");
return new Response(await env.NOTEBOOK.read("idea"));
},
};
A Worker Loader binding, env.LOADER, starts that code in its own sandbox. You decide exactly what goes on its env:
src/index.tsconst worker = env.LOADER.load({
compatibilityDate: "2026-10-01",
mainModule: "main.js",
modules: { "main.js": untrustedCode },
env: { NOTEBOOK: ctx.exports.Notebook({ props: { prefix: "sandbox" } }) },
globalOutbound: null,
});
ctx.exports.Notebook(...) creates a binding to your own Notebook class on the spot, with props that only your Worker sets. globalOutbound: null takes away the network.
Why it matters
- This is capability-based security. The sandboxed code can only do what it was handed, and it was handed one object. It has no URL to reach, no key to steal, and no network.
- Your wrapper decides what’s exposed. It could also log calls, rate-limit them, or refuse them, and the sandbox can’t change its own prefix.
- This is how Code Mode works: instead of calling tools one at a time, an agent writes code against an API, and that code runs in a Dynamic Worker like this one, with outbound
fetch()blocked. - It’s where Kenton Varda’s 2024 post said bindings were headed: handing a binding to other code, scoped to one request. Workers RPC now goes further: functions and objects can be passed between Workers, and a stub forwarded from another Worker stops working when the request ends (docs).