Skip to content

fix(traffic): keep the AI-bot Worker's secret in a Worker Secret, not its code - #1252

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/bot-worker-secret-binding
Oct 20, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/bot-worker-secret-binding

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Follow-up from #1179's review (not blocking there): "Generated Worker: add ctx.passThroughOnException(), keep the secret in a secret binding rather than in the source, and send origin plus pathname instead of the full URL."

passThroughOnException() and origin + pathname are already on main. This PR does the third item.

Server

  • bot-worker-script.js: the generated Worker no longer holds the connection secret. It reads env.ANSVISOR_BOT_SECRET, a Worker Secret (Settings › Variables and Secrets).
  • Without that Secret, the Worker reports nothing and the site is unaffected, so no rejected posts reach /bots/ingest.
  • The name is one constant, WORKER_SECRET_NAME. POST /api/bots/connections returns it as workerSecretName for a Worker, and null for the other senders.

Web

  • The connect dialog's Worker steps gain "In the Worker's Settings › Variables and Secrets, add a variable of type Secret with the name and value below."
  • The secret shows in its own copy block: "Secret value (name: ANSVISOR_BOT_SECRET)". NewBotConnection.workerSecretName carries the name.

Existing Workers, with the secret in their code, keep working: /bots/ingest still takes the Bearer secret, however it's set.

Related issue

Follow-up to #1179.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Unit tests

  • bot-worker-script.test.js:
    • the script doesn't contain the secret and reads env.ANSVISOR_BOT_SECRET;
    • with the Secret, a batch posts Bearer <secret>;
    • without it, the page is served and nothing is posted.
  • routes/bots.test.js: a Worker connection's script doesn't contain the secret, and the response names the Secret.
  • _bots-connect.test.tsx: the Worker steps and the secret's copy block.
  • ai-bots.test.ts: the name is parsed.

In the browser: with the branch's API on :8081 and web on :3003, AI bots › Connect › Cloudflare Worker › Create connection shows four steps, the Worker code without abot_, and the secret under its name. I checked it in light and dark mode, at 1440 and 1024 px, then deleted the connection.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also: web yarn test (1792) and yarn build; server npm run lint, npm test (1841).

🤖 Generated with Claude Code

… its code

The generated Cloudflare Worker held the connection secret in its
source, so anyone who could read the Worker, its versions or a
screenshot of it had the secret. The Worker now reads it from a Worker
Secret named ANSVISOR_BOT_SECRET (env.ANSVISOR_BOT_SECRET) and reports
nothing until it's added, without affecting the site. The connect
dialog adds the step and shows the secret on its own, with the name
from the server's response (workerSecretName). Workers set up before,
with the secret in their code, keep working.

Follow-up to ansvisor#1179's review; passThroughOnException and origin plus
pathname were already in place.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk the last generated-Worker note from #1179: the secret now lives in a Worker Secret (env.ANSVISOR_BOT_SECRET), the dialog adds the step and shows the secret under that name, and a Worker without it reports nothing. passThroughOnException and origin + pathname were already on main. Existing Workers with the secret in their code keep working.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. Taking the secret out of the Worker source, and skipping the report when no Secret is set, is the safe default. Existing Workers keep working because /bots/ingest still takes the Bearer secret.

@gkhngyk
gkhngyk merged commit 126928e into ansvisor:main Oct 20, 2026
6 checks passed
cihan-gyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 20, 2026
ansvisor#1252 and this branch each added a test to _bots-connect.test.tsx at the
same place: both are kept, and this branch's failed-read test takes
ansvisor#1252's connection shape (workerSecretName, a Worker without the secret).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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