cloud / internal-packages / installer / worker / scopes.jsmain at aacbc74
// scopes.js — WHAT THE WIZARD ASKS CLOUDFLARE FOR at the full consent, and why. No imports: the
// Worker reads it, the page shows it, and test/scopes.mjs checks it.
/** What iterate asks Cloudflare for at the full consent, and why, in the words the page shows. */
export const PERMISSIONS = [
["user-details.read", "Your name and email."],
["account-settings.read", "The account's name and settings."],
["memberships.read", "Which accounts you belong to."],
[
"workers-scripts.write",
"Deploy iterate's Workers, and give the account its workers.dev address.",
],
["workers-r2.write", "Store iterate's files in R2, and check that R2 is on."],
["containers.write", "Run iterate's sandboxes. Also tells us Workers Paid is on."],
["zone.read", "See the domains in the account."],
["zone.write", "Add a domain you bring from another registrar."],
["registrar-domains.read", "Check which domains are free, and their prices."],
["registrar-domains.admin", "Buy the domain you pick, in your account, when you confirm."],
["aig.read", "See whether a card is on file, and which one."],
// what the stack's deploy needs (core/alchemy's README lists them)
["workers-kv-storage.write", "Create iterate's key-value stores."],
["d1.write", "Create iterate's databases."],
["artifacts.write", "Store your minds' code in Artifacts."],
["cloudchamber.write", "Run iterate's sandbox containers."],
["workers-routes.write", "Point your domain at iterate."],
["dns.write", "Add the DNS records your domain needs."],
["workers-observability.read", "Read iterate's logs when something goes wrong."],
["ai.write", "Let your agents call models through Workers AI and AI Gateway."],
["browser-rendering.write", "Let your agents use a browser."],
["email-sending.write", "Send mail from your domain: your sign-in codes, and your agents' mail."],
["email-routing-rule.write", "Receive mail on your domain."],
// a stack with mail has a queue of its own: Cloudflare puts each mail that did not arrive on it
["queues.write", "Tell your agents when a mail that they sent did not arrive."],
// Email Routing is a setting of the zone: Cloudflare takes "turn it on" (POST
// /zones/<id>/email/routing/enable) with this scope alone, and the stack's deploy makes that call
["zone-settings.write", "Turn on Email Routing for your domain, so that it can receive mail."],
["secrets-store.write", "Keep iterate's deploy state's secrets in Secrets Store."],
];
// THE REST OF THE ACCOUNT. Jonas, 11 Nov 2026: "The wizard needs to request everything that we
// need. It can, in fact, request aggressively a lot … of permissions, maybe even super admin
// permission on the account … because the account is the sandbox." So the consent also asks for
// what the stack and its owner's agents could need later, and no deploy stops again for a scope
// that nobody asked (kaletsky.dev stopped three times for zone-settings.write).
//
// Cloudflare has no scope for "all of the account" (GET /client/v4/oauth/scopes: 404 scopes, none
// of them an administrator). This list is the nearest thing. It leaves out what reaches beyond
// the account (the person's own API tokens, profile and memberships), Zero Trust's devices and
// SCIM, and the network products (Magic, load balancers, IP prefixes). A scope must be registered
// on the OAuth client before the consent may ask it: Cloudflare refuses the whole consent for one
// that is not ("invalid_scope").
//
// NOT IN THIS LIST: account-api-tokens.write (make an API token of the account), `TELEMETRY_SCOPE`
// below. Cloudflare's consent page shows it under "Sensitive scopes", not ticked, and a person who
// presses Authorize without the tick gets "This application requires a permission you have
// unchecked" and no consent at all. So the wizard asks it apart: it says first what to tick, and
// offers the consent without it.
//
// Each group: [title, what it is for, scopes]. Write covers read in Cloudflare's permissions.
export const WIDER = [
[
"Workers, storage and data",
"Pipelines, Vectorize, Hyperdrive, Pages, builds, logs, and what else you and your agents build on Workers.",
"pipelines.write pipelines.send vectorize.write query-cache.write page.write workers-ci.write workers-scripts.edit workers-scripts.admin workers-observability.write workers-observability-telemetry.write workers-tail.read workers-r2-bucket-item.write r2-catalog.write r2-catalog-sql.read cf-agents.write agent-memory.write mcp-portals.write realtime.write realtime.admin flagship.write pubsub.write messaging.edit messaging.read k2.write constellation.write cloud-connector.write cfspeed.write",
],
[
"AI",
"AI Gateway, AI Search, web search, your own models, and the gateways your agents call.",
"aig.write aig.run agw.write agw.run ai-search.write ai-search.run ai-search.index rag.write rag.run websearch.write websearch.run ai-model.write aiaudit.write firewall-for-ai.write",
],
[
"Your domain",
"DNS settings, certificates, cache, redirects, rules, the firewall, load balancers and waiting rooms of the domains in the account.",
"zone-dns-settings.write account-dns-settings.write zone-custom-asset.write registrar-sandbox-domains.read ssl-and-certificates.write account-ssl-and-certificates.write cache-settings.write cache.purge zone-disable-esc.write account-disable-esc.write account-rulesets.write account-rule-lists.write transform-rules.write zone-transform-rules.write dynamic-redirect.write mass-url-redirects.write managed-headers.write response-compression.write snippets.write origin.write config-settings.write custom-errors.write custom-pages.write account-custom-error-rules.write account-custom-pages.write zone-waf.write challenge-widgets.write page-rules.write bot-management.write sanitize.write load-balancers.write waiting-rooms.write",
],
[
"Mail",
"The addresses mail is forwarded to, and the addresses that mail is not sent to.",
"email-routing-address.write email-routing-suppression.write email-routing-account-rule.read",
],
[
"Access and tunnels",
"Put a login in front of an app, and reach a machine of yours through a tunnel.",
"access.write access-app.write access-policy.write argotunnel.write",
],
["Media", "Images, video and live calls.", "images.write stream.write calls.write moq.write"],
[
"The account itself",
"Its settings, its notifications, its logs and its analytics.",
"account-settings.write notifications.write audit-logs.read logs.write account-logs.write zone-observability.write account-analytics.read analytics.read radar.read intel.write resource-sharing.read",
],
].map(([title, why, scopes]) => [title, why, scopes.split(" ")]);
/** The scopes the wizard's own steps and the stack's deploy need: a consent without one is told. */
export const NEEDED = PERMISSIONS.map(([scope]) => scope);
export const SCOPES = [...NEEDED, ...WIDER.flatMap(([, , scopes]) => scopes)];
/** The one more scope that a stack's own telemetry takes: its deploy makes two narrow API tokens
* of the account for the telemetry Worker (core/alchemy README, "Telemetry"). The mind's own
* Cloudflare token takes the same scope (core/alchemy README, "The mind's Cloudflare token"). A
* consent asks it when its link asks for one of the two: the wizard's full consent asks for both
* (`/connect?level=full&telemetry=1&cloudflare=1`), and a deployment's card asks for one at "Turn
* on telemetry" (`telemetry=1`) or "Give this mind a Cloudflare connection" (`cloudflare=1`). */
export const TELEMETRY_SCOPE = "account-api-tokens.write";
/** Whether a consent's scope lets a deploy make the mind's own Cloudflare token. */
export const cloudflareTokenGranted = (scope) =>
String(scope || "")
.split(/\s+/)
.includes(TELEMETRY_SCOPE);
/** Whether a consent's scope lets a deploy turn telemetry on: the token scope, and the three of
* WIDER that make the warehouse and the two destinations. */
export const telemetryGranted = (scope) => {
const granted = new Set(String(scope || "").split(/\s+/));
return [
TELEMETRY_SCOPE,
"pipelines.write",
"r2-catalog.write",
"workers-observability.write",
].every((needed) => granted.has(needed));
};
/** The edition of what the mind's own Cloudflare token may do, in the stack this Cloud deploys
* (core/alchemy/src/cloudflare-token.ts). It goes up with each release that changes the token's
* permission groups (test/scopes.mjs holds it against them). A stack whose token is of an older
* edition gets this one only from an update whose access may make API tokens: its card offers
* that consent. 1: every token made before the edition was counted. */
export const CLOUDFLARE_TOKEN_EDITION = 3;
/** What a run's lines say of the mind's Cloudflare connection, over what the run before it said
* (`before`): whether the stack has it, and the edition of its token. A run that ended before
* its deploy said either keeps both (container/run.sh prints them after a deploy that worked). */
export function connectionAfter(lines, before) {
if (lines.includes("cloudflare-connection off")) return { connection: false };
if (!lines.includes("cloudflare-connection on"))
return { connection: before?.connection, connectionEdition: before?.connectionEdition };
return {
connection: true,
connectionEdition: lines.includes("cloudflare-token made by this release")
? CLOUDFLARE_TOKEN_EDITION
: before?.connectionEdition,
};
}
/** Whether a stack's mind has a Cloudflare token of this Cloud's edition: one whose record names
* no edition is from before the editions, so 1. */
export const cloudflareTokenCurrent = (run) =>
run?.connection === true && (run.connectionEdition ?? 1) >= CLOUDFLARE_TOKEN_EDITION;