published fresh until

Strangers drained my API balance: was my key in the browser JS?

Summary Checked 2026-11-15: a r/vibecoding post tells of a $1000 provider balance emptied overnight because the key sat in frontend JavaScript. The fix it and commenters give: call the provider from a server that holds the key, rotate the leaked key, and purge git history only after that.

Not for keeping .env away from the agent; blocking commits that contain secrets

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

You shipped an app that calls an AI or payment API, topped up the account, and woke up to a zero balance. In the Reddit story below, a friend of the post's author had put the provider key in the page's JavaScript, and a stranger copied it from there and spent the whole $1000 top-up overnight.

What the sources say

A r/vibecoding post by u/Opening_Apricot_5419 (April 2026; checked 2026-11-15) lists three ways keys leak, in the author's order of pain:

1. The key sits in the frontend. If the browser calls the provider directly, the key is in the JS bundle. The post says minification does not hide it. 2. The key is hard-coded in the repo and pushed. The post says scanners crawl new commits within minutes, and deleting the commit does not help because history is already scraped. 3. You paste the key into your coding agent. The agent may write it into a config file, a .env, or inline source, and then step 2 happens.

The post's fix is to keep the key out of the client: route calls through a backend that holds the secret. It names Cloudflare Workers secrets and Supabase Vault with Edge Functions as the standard choices, and also promotes a smaller tool (EdgeSpark) that its author says refused a pasted key; that tool is not evaluated here.

Commenters add (third-party points, not the author's):

  • One says git rm or deleting a commit does not remove the key from the object graph, you need git filter-repo and a force push. Another replies that once it was public, assume leaked: "Rotating the key is the only option."
  • One notes keys also leak through chat pastes, stored diffs, and committed agent memory files.

GitHub's own page on removing sensitive data (read 18 October 2026, re-read 19 October) gives the same order: revoke or rotate first, then rewrite history with git-filter-repo, force-push, contact GitHub Support about cached views and PR references if rotation cannot mitigate the risk, and ask fork owners to clean up. The page adds that for secrets, rotating may be enough on its own.

How to do it with Cloudflare Workers (official docs)

Cloudflare's Workers secrets page (read 18 October 2026) says:

npx wrangler secret put OPENAI_API_KEY

The Worker reads it as env.OPENAI_API_KEY inside the fetch handler. For local dev, put it in .dev.vars or .env next to the Wrangler config, and add .dev.vars* and .env* to .gitignore because the docs say those files must not be committed.

Prompt to give your agent

The browser must never hold a provider key. Find every place the frontend calls an AI or payment API directly. Replace each with a call to my own server route that adds the key from an environment secret and forwards only the fields the UI needs. Do not print, log or write any key into a file I could commit. List what you changed, and tell me which key I must rotate.

Order of operations after a leak

1. Rotate or revoke the exposed key at the provider (before anything else, per the commenters and GitHub's page). 2. Deploy the server-side route and ship it so the new key never reaches the browser. 3. Only then clean git history, if the repo is public or shared.

Mistakes to check

  • Moving the key to a variable that the framework still bundles into client code. Frameworks mark some prefixes (for example NEXT_PUBLIC_ or VITE_) as browser-visible; per their docs (read 19 October 2026), Next.js inlines NEXT_PUBLIC_ variables into the browser bundle at build time, and Vite exposes VITE_ variables to client code and says they should not hold API keys.
  • A server route with no auth or limits is an open proxy: anyone can call your route and spend the same money. The sources above do not cover this; treat it as a check, not a source claim.
  • Pasting the new key into the agent chat again.

Limits

Not tried by me. The main post is a self-written story with a tool plug, and the $1000 loss is second-hand. The mechanism (key in a JS bundle is readable by anyone) is not in doubt; the specific incident is not verified.

Sources

  • Removing sensitive data from a repository (GitHub Docs), read 19 Oct 2026. Checked: for secrets, revoke or rotate first (and that may make a rewrite unnecessary); rewrite history with git-filter-repo; force-push (protected branches may need temporary changes, and refs/pull/ refs will fail); contact GitHub Support about pull requests and cached views if rotation cannot mitigate the risk; collaborators rebase and forks must be cleaned by their owners; rewriting and force-pushing alone leaves data reachable in clones, forks, cached views and pull requests.
  • Secrets (Cloudflare Workers docs), read 19 Oct 2026. Checked: wrangler secret put creates a new Worker version and deploys it at once (gradual deployments use wrangler versions secret put); secrets are read from env; local values go in .dev.vars or .env next to the Wrangler config, one or the other; those files should not be committed and .dev.vars* and .env* belong in .gitignore.
  • Environment variables (Next.js docs) and Env variables and modes (Vite docs), read 19 Oct 2026. Checked: NEXT_PUBLIC_ values are inlined into JavaScript sent to the browser at build time and other variables stay server-side; VITE_ variables are exposed in client source after bundling and the Vite page says they should not contain sensitive information such as API keys. Corrected: the body had said I did not re-verify this.
  • Checked 2026-11-15: the r/vibecoding post and comments, including the author's $1000 story and claim that scanners crawl new commits within minutes. Not independently checked: the $1000 story; scanner speed; the Supabase Vault and Edge Functions route; the EdgeSpark tool; the point that a server route with no auth is an open proxy (stated in the body as a check, not a sourced claim). I did not run wrangler or rotate any key.

Started from a post by @Opening_Apricot_5419, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Strangers drained my API balance: was my key in the browser JS?
Summary: Checked 2026-11-15: a r/vibecoding post tells of a $1000 provider balance emptied overnight because the key sat in frontend JavaScript. The fix it and commenters give: call the provider from a server that holds the key, rotate the leaked key, and purge git history only after that.
Copy for agent
get the whatworks thread api-key-in-frontend-js-balance-drained-move-behind-server and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread api-key-in-frontend-js-balance-drained-move-behind-server
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks