Skip to content
Muhammad Moiz

How to Keep .env Secrets Safe From AI Coding Agents

Cloudflare just bought Varlock. Here's how to keep .env secrets away from AI coding agents, with a practical checklist for your team.

Muhammad Moiz
Published
min read
8 min read
Red padlock resting on a black computer keyboard
On this page

The safest way to keep .env secrets away from AI coding agents is to stop giving them real secrets at all: describe your config in a schema the agent can read, keep the actual values in a secret manager, and block the agent from reading local secret files. Cloudflare's purchase of Varlock on December 2, 2026 is a sign that this approach is becoming the default. Below is what happened, why plain .env files are now a real risk, and a checklist you can apply this week.

What did Cloudflare announce on December 2?

Cloudflare published a post titled "Varlock is joining Cloudflare". Varlock is an open-source toolkit for environment variables and secrets. The key points:

  • Varlock stays open source under the MIT license and stays vendor-neutral, with plugins for popular secret vaults and frameworks.
  • The team had planned a paid hosted Varlock service. That is cancelled; the effort moves to Cloudflare Secrets Store and the wider Cloudflare developer platform.
  • They want the .env.schema file format (the "@env-spec" syntax) to become an open standard that other tools adopt, not just Varlock.
  • The roadmap mentions better support for agent workflows, languages beyond JavaScript, native encryption helpers and team vaults that don't need an external backend. No dates were given.

The reason given for the deal is the part worth paying attention to. The Cloudflare post says "coding agents turned this fragile chore into an immediate security risk", and it lists prompt injection and faster supply chain attacks as new threats. Tech press covering the deal (The Next Web) framed it the same way: a way to keep AI coding agents away from secrets.

Why are .env files risky with AI coding agents?

A .env file was never great security. It sits as plain text on a laptop, gets pasted into Slack when a new developer joins, and is copied into three different hosting dashboards. Rotating a key is so annoying that most teams only do it after a leak.

AI coding agents make this worse in three practical ways:

  1. They read your project folder. An agent that can open files can open .env. Once a key is in the model's context, it can end up in a chat log, a generated file, a commit or a debugging message.
  2. They run commands. An agent that runs printenv, prints a config object or dumps a failing request to the terminal can expose a secret without anyone asking it to.
  3. They can be tricked. Prompt injection means text inside a README, an issue or a web page the agent reads can try to make it send data somewhere. If the agent holds a live key, there is something to steal.

There is also a newer group of builders: founders using Lovable, Bolt, Replit or Cursor who meet API keys for the first time through an AI tool. Keys stored in the wrong place are one of the holes covered in vibe coding security: 8 holes in almost every AI-built app.

Laptop screen showing colourful source code in an editor
Photo: Mohammad Rahmani on Unsplash

A real example: tokens in logs and the LiteLLM compromise

In October 2026 a malicious LiteLLM release, version 1.107.0, was pushed to PyPI and Docker on October 21. According to Help Net Security's report, it was live on PyPI for under two hours and on Docker for a little longer. The payload went after cloud credentials, Git credentials, SSH keys, browser logins, and the authentication files of AI coding tools including Codex, Claude, Gemini and Cursor.

How the attacker got in is still the interesting part. LiteLLM said the attacker appeared to use stolen GitHub CLI OAuth tokens, and that it believed those tokens were lifted from the logs of an internet-exposed internal deployment. LiteLLM also said it had seen Claude Code include a user's GitHub CLI token in a response body, so any system that logs full responses could end up storing that token. At the time of the report LiteLLM had not confirmed the entry point, so treat that link as unproven.

The lesson doesn't depend on the exact root cause. Secrets now travel through more places than your .env file: agent sessions, AI gateways, request logs and tool configs. Every one of those is a place where a key can be copied. If you want the wider picture on poisoned packages, see npm supply chain attacks: how to protect your app.

How does Varlock's .env.schema approach work?

Varlock replaces the usual .env.example with a .env.schema file that you commit to Git. It uses the normal KEY=value format, plus comment "decorators" that describe each variable: whether it is required, whether it is sensitive, and what type it should be. Real secret values aren't stored in the schema. Instead, a value can call a resolver function that fetches it from a vault, such as op() for 1Password.

Here is a short example I wrote for a typical Next.js app that takes Stripe payments:

# @currentEnv=$APP_ENV
# @defaultSensitive=false
# ---

# Which environment we are running in
# @type=enum(development, staging, production)
APP_ENV=development

# Public site URL, safe to show anyone
NEXT_PUBLIC_SITE_URL=http://localhost:3000

# Stripe secret key, pulled from 1Password at runtime
# @required
# @sensitive=true
# @type=string(startsWith=sk_)
STRIPE_SECRET_KEY=op(op://my-app/stripe/secret-key)

The benefit for AI agents is simple. The agent can read this file and understand exactly which settings exist, which are secret and what format they need, without ever seeing a real key. Varlock's site describes this as giving agents full context on your config while hiding the values.

The project also lists features beyond the schema: a varlock scan command and Git hooks to catch leaked secrets before they are committed, redaction of sensitive values from logs and console output, runtime checks for secrets leaking into client bundles or server responses, and a credential proxy that runs agents and MCP servers with placeholder values and injects the real ones only at the network boundary. It supports frameworks such as Next.js, Vite, Astro and SvelteKit, and secret backends including 1Password, Infisical, AWS Secrets Manager, Azure Key Vault, Google Secret Manager and Bitwarden. If you already have a .env.example, varlock init will draft a schema from it, which you then review by hand.

Do you have to use Varlock?

No. Varlock is one good option, and Cloudflare's backing makes it more likely to stick around. The principle matters more than the tool: describe config openly, store values in a vault, and keep raw secrets out of anything an agent or a log can read. Doppler, Infisical or your cloud provider's secret manager can get you most of the way.

Checklist: keep secrets away from your AI coding agent

Work through this in order. The first three items take less than an hour.

  1. Block secret files in your agent's settings. Most AI editors and agents support deny rules or an ignore file. In Claude Code, for example, the settings docs show deny rules like these:
{
  "permissions": {
    "deny": ["Read(./.env)", "Read(./.env.*)"]
  }
}

Check your own tool's docs for the equivalent. An ignore rule is a seat belt, not a wall: an agent that can run shell commands may still reach the file, so don't stop here.

  1. Use development keys locally. Your laptop and your agent should only ever see test or sandbox keys with low limits. Production keys belong in your hosting provider's secret settings or a vault.
  2. Search your Git history. Run a secret scanner (such as varlock scan, Gitleaks or GitHub secret scanning) across the whole history, not just the latest commit. Anything found should be rotated, not just deleted.
  3. Commit a schema, not values. Replace .env.example with a .env.schema or equivalent so humans and agents both know what's needed.
  4. Move real values into a secret manager. Pull them at runtime or deploy time instead of copying them between dashboards and chat.
  5. Stop logging full request and response bodies. Gateways, proxies and error trackers should redact headers and bodies that can hold tokens.
  6. Scope and expire tokens. Give each key the smallest permissions it needs, and prefer short-lived tokens. A leaked read-only key with a 24-hour life is a much smaller incident.
  7. Practise rotation. Write down how to rotate each key and do it once now, so you can do it quickly when something does leak.

What does this mean for founders and app owners?

If your app was built quickly with an AI tool, or a small team works with coding agents every day, secret handling is probably one of the easiest risks to fix and one of the costliest to ignore you have. A leaked Stripe, OpenAI or database key can mean fraudulent charges, a surprise usage bill or a data breach you have to report.

The good news is that the fix is mostly process plus a bit of setup, not a rewrite. You don't need to stop using AI agents; you need them to work with placeholders instead of real keys.

If you'd like someone to check this for you, my production audit covers secrets, keys in client code, database access rules and logging. And if your app was built with Lovable, Bolt or Replit and is heading for real users, I also help make AI-built apps production-ready.

Sources

About the author

Muhammad Moiz · Senior Full-Stack Engineer

I'm a senior full-stack engineer with 5+ years building production web apps in React, Next.js and Node.js. I build products end to end for founders and small businesses, and take over apps started with AI tools when they need to grow up.

More about me