Skip to content

Scope Leaf's Cloudflare tokens to its own Workers, and give Tend's agent the logs - #825

Merged
max-sixty merged 6 commits into
mainfrom
resolve-agent-token-access
Sep 19, 2026
Merged

max-sixty merged 6 commits into
mainfrom
resolve-agent-token-access

Conversation

@max-sixty

@max-sixty max-sixty commented Sep 19, 2026 •

Copy link
Copy Markdown
Owner

Agents on the maintainer's machine and Tend's agent in CI can now each run the Workers Observability query in worker/README.md without a person present, and none of Leaf's Cloudflare tokens can reach another Worker, another zone's settings, DNS, or token management. The account also serves tend-website and 28 other zones; the maintainer's own login remains the only credential that can change a domain or mint a token.

The Cloudflare side is already applied. Per-Worker token scoping shipped on 2026-09-15, so each token now names the Workers it may edit instead of holding account-wide Workers Scripts Write:

  • Leaf agent administration (agents on the maintainer's machine) holds Individual Workers Editor on leaf-website and leaf-website-dev, and Observability Read rather than Write. The Cloudflare Leaf agent administration 1Password item holds it, and the service account reads it without a prompt.
  • Leaf site deploy (CI) replaces the user-owned leaf-ci-worker-deploy as the cloudflare-deploy secret, scoped to leaf-website; Cloudflare refuses a per-Worker scope on a user-owned token. publish-site run 35413097355 deployed leaf.page with it.
  • Leaf observability (Tend CI) is new: Workers Observability Read and Account Analytics Read, stored as the tend environment's CLOUDFLARE_API_TOKEN.

In the repository:

  • .config/tend.yaml passes the tend environment's token into the agent's launch environment through sandbox_env, and the regenerated tend-*.yaml workflows carry it. Only agents answering an issue or a red run on main get it (issues, issue_comment on an issue, ci-fix's workflow_run); reviews, mentions on pull requests and the scheduled polls run with it empty, since each may run a pull request's code. A red run counts as main's only when its head repository is this one, since workflow_run's branch filter matches the triggering run's head ref. An agent answering an issue that fetches a pull request itself is told to run that code with the token unset. The token is read-only; what a leak would expose is every Worker's logs on the account.
  • .claude/skills/running-tend/SKILL.md tells Tend's agent to query the logs when an issue or a red publish-site concerns the deployed site, and to drop the token before running a pull request's code.
  • worker/README.md lists the three tokens with what each reaches, says which permissions stay account-wide and that what they expose is every Worker's logs, fills in the account id in the query, and drops the claim that the query needs Observability Write.

Ref #772, where leaf-agent had no credentials to read these logs.

This was written by Claude Code on behalf of max-sixty

🤖 Generated with Claude Code

max-sixty and others added 3 commits September 18, 2026 14:03
… tokens

`worker/README.md` now lists every Cloudflare token Leaf uses, one per holder,
with the permissions read off the account rather than remembered:

- `Leaf agent administration` (account-owned) serves agents on the
  maintainer's machine. Agents read it from the `Max` 1Password vault through
  the service account, which needs no approval; the README gives that command.
  A bare `op` call waits on a desktop prompt, which is what stopped earlier
  unattended sessions.
- `leaf-ci-worker-deploy` (user-owned) is `publish-site`'s token in the
  `cloudflare-deploy` environment. The README previously described it as the
  same token as the one above; it is a separate token without the
  observability permissions.
- `Leaf observability (Tend CI)` (new, account-owned) reaches Tend's agent
  through the `tend` environment and `sandbox_env`: Account Analytics,
  Workers Tail and Workers Observability, all Read. Every Tend agent,
  including review on `pull_request_target`, reads outside contributors'
  text, so this secret must never hold a token that edits Workers.

The Observability query works with `Workers Observability: Read`; the README
said it needed Write. Measured against the account: no token is allowed DNS,
members, billing or token management, and no token can read or change a
non-Leaf zone; the two that edit Workers can edit every Worker in the account.

The query's account id is filled in, and the token recipe lists exact
permissions instead of starting from the Edit Cloudflare Workers template.
`running-tend` tells Tend's agent to query the logs when an issue or red
`publish-site` concerns the deployed site. Workflows regenerated with
tend 0.2.10.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Cloudflare shipped per-Worker token scoping on 2026-09-15, so the account-wide
`Workers Scripts Write` that let both deploy tokens edit `tend-website` is no
longer the only way to deploy `leaf-website`. Each token now names the Workers
it may edit, keyed by script tag:

- `Leaf agent administration` and the new `Leaf site deploy (CI)` hold
  `Individual Workers Editor` on Leaf's Workers and are refused on every other
  Worker in the account, including its code.
- `Leaf site deploy (CI)` replaces `leaf-ci-worker-deploy`, which was
  user-owned; Cloudflare refuses a granular scope on a user-owned token. The
  `cloudflare-deploy` environment secret holds the new one, the old token is
  revoked, and publish-site run 35413097355 deployed leaf.page with it.
- `Leaf observability (Tend CI)` drops account-wide `Workers Tail Read`; it
  never streamed a live tail.
- `Leaf agent administration` drops `Workers Observability Write`, since the
  query in this README runs on Read.

wrangler publishes a custom domain through per-Worker endpoints, so the
production deploy needs nothing account-wide beyond containers and account
settings. A `workers.dev` deploy also reads the account's subdomain, refused
under a per-Worker scope alone, so the agent token that deploys
`leaf-website-dev` adds `Workers Metadata Read-Only`, which still returns 403
on another Worker's code.

The 1Password item is renamed to `Cloudflare Leaf agent administration`, after
the token it actually holds rather than the read-only name it had.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The token now rides in every tend agent's sandbox launch environment, and two of those workflows execute contributor-controlled code with it there. I don't think that blocks the change — read-only is the right call and it's your account — but it's the first credential this repo puts inside the sandbox, against a design whose stated point is that "the bot PAT and the Anthropic credential live only in the proxy, never in the agent's environment", so it seems worth deciding deliberately rather than inheriting.

The path: sandbox_env is baked into the agent's launch env, and tend's action documents sandbox_setup as running "with the agent's launch env" inside the same lifecycle step. On tend-review the disposable workspace is refs/pull/N/merge, so it holds the fork's tree — and this repo's sandbox_setup runs uv sync --frozen there, from the PR's own uv.lock, before the agent starts. The review then runs the suite from that tree per Review test selection. Egress doesn't bound the leak: tend's mitmproxy allowlist governs credential injection, and inject_credentials.py passes every non-allowlisted host through untouched so package managers keep working.

So the holder isn't only "the agent reading outside contributors' text", which is what the .config/tend.yaml comment reasons about — it's any code a fork PR ships. Read-only bounds what a leak can do, not who ends up holding it, and both of this token's permissions are account-wide: Workers Observability Read and Account Analytics Read reach every Worker and every Analytics Engine dataset on the account that, as you note, also serves tend-website and 28 other zones. The README's safety sentence is exact about what isn't at risk — "reads another Worker's code" — and silent about what is.

Two levers if you want one, either is cheap:

  • Withhold the value from the workflows that check out a PR. sandbox_env values render verbatim into with:, so a ${{ github.workflow != 'tend-review' && ... }} guard stays a .config/tend.yaml change rather than a hand edit of a generated file. Watch the empty-string ternary — cond && '' || secret yields the secret in both branches, so the empty value has to be the || arm.
  • Or record it where the next reader meets it: the README paragraph enumerating what no token reaches is the natural place to say that a fork PR's own code runs with the Tend token, and that account-wide observability is what a leak buys.

Separately, 832797e dropped Workers Tail Read from the Tend token and the new table gives it to no one, but the same section still tells agents they may use wrangler tail — suggestion inline.

How the exposure path was established
  • sandbox_env reaches the agent env: claude/action.yaml at 0.2.11 passes inputs.sandbox_env as TEND_SANDBOX_ENV into the sandbox env file, and describes sandbox_setup as running "with the agent's launch env and the workspace as cwd".
  • The workspace holds the fork's tree: shared/steps/prepare_agent_workspace.py's checkout_review fetches refs/pull/N/merge, which tend-review.yaml selects with checkout_mode: review.
  • Egress is open: proxy/inject_credentials.py — "A request to any other host … is passed through untouched. The proxy's --allow-hosts flag is a functionality optimization … this allowlist is the credential boundary."
  • The generated workflows themselves check out clean: all nine agent jobs declare environment: tend, the rendered input matches the generator's macros.yaml.j2 position and NAME=VALUE join exactly, and every file parses.

Comment thread worker/README.md Resolved
A review, and a mention on a pull request, put that pull request's code in
the agent's workspace, and `sandbox_setup` builds it with `uv sync` before the
agent starts, all with the launch environment that carries
`CLOUDFLARE_API_TOKEN`. So a fork's own code held a token that reads every
Worker's logs and every Analytics Engine dataset on the account. The
`sandbox_env` value is now empty on those events, by the same test Tend's
workspace checkout applies; agents working from `main` keep it.

The README now says what the account-wide permissions expose, and no longer
offers `wrangler tail` to the Tend token, which lost `Workers Tail Read`. The
agent token's `Workers Metadata Read-Only` opens a live tail on any Worker,
measured on `tend-website`, and the README says so.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The guard is keyed on the event, so it covers the two workflows whose workspace arrives holding a pull request's code — but the token stays in the launch environment of the scheduled poll that also handles fork pull requests. tend-mention-relay.yaml's own comment excludes forks because "the notifications poll covers them"; tend-notifications fires on schedule, so the expression takes the secrets.CLOUDFLARE_API_TOKEN arm; and the notifications skill routes an unreviewed PR head into a normal review, which under Review test selection in running-tend runs a test selection from that PR's tree. Fork code executing under the token is what the guard removes on tend-review; on the recovery path it stays.

The expression can't see that — the notifications workspace is main at launch, and the fork tree arrives in a worktree the session makes for itself. So it's a choice between saying so and closing it. Saying so is the suggestion inline. Closing it would be a line in running-tend's Cloudflare logs section: a scheduled session drops CLOUDFLARE_API_TOKEN from the environment before it runs a pull request's code.

Where each link in that path is stated
  • Fork PRs route to the poll: .github/workflows/tend-mention-relay.yaml, in the relay job's comment — "Fork PRs are excluded to hold long-standing behaviour: the notifications poll covers them, and it applies author-association tiers that this path does not."
  • The poll is scheduled: tend-notifications.yaml is on: schedule plus workflow_dispatch, with checkout_mode: base, so none of github.event.pull_request, github.event.issue.pull_request, or github.event.client_payload is present and the expression yields the secret.
  • The poll reviews: /tend-ci-runner:notifications, step 3 — "use the normal live workflow: /tend-ci-runner:triage for an issue, /tend-ci-runner:review for an unreviewed PR head".
  • That review runs the tree: running-tend, Review test selection — "Before approving a product change, choose and run the smallest test selection that exercises the failures the diff could introduce."

Comment thread worker/README.md Resolved
max-sixty and others added 2 commits September 18, 2026 19:39
The previous guard withheld the token from events whose workspace starts on a
pull request's code, but the scheduled notifications poll starts on `main`,
kept the token, and reviews fork pull requests that the relay skips, running
their code. Listing that workflow too would be one more exception. The rule is
now the positive one that matches why the token exists: `issues`,
`issue_comment` on an issue, and `ci-fix`'s `workflow_run` on `main` get it,
which is when `running-tend` sends an agent to the logs; every other agent runs
with it empty.

An agent answering an issue can still fetch a pull request's code itself.
`running-tend` tells it to run that code with `env -u CLOUDFLARE_API_TOKEN`,
and the README says that this is an instruction, not a mechanism.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`tend-ci-fix` filters `workflow_run` on `branches: ["main"]`, which matches the
triggering run's head branch rather than its base. A fork whose default branch
is also `main` opens a pull request, `ci` fails, and the run arrives looking
like a red run on main — so the fix session would fetch the fork's code with
the token in its environment. The `workflow_run` arm now also requires
`head_repository` to be this repository.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit cbcf007 into main Sep 19, 2026
7 checks passed
@max-sixty
max-sixty deleted the resolve-agent-token-access branch September 19, 2026 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants