Skip to content
Zerdalu Notes
Index
5 min
Chapter 10

Protecting Secret Environment Variables from AI Agents

How I moved local Cloudflare Worker secrets outside the Gib template on Debian and added Zed guardrails so AI agents cannot read API keys.

Contents100%

When I start a new web project, I clone my own starter template called Gib. Gib is a full-stack edge application template built with React, Hono, and Cloudflare Workers.

In a normal Cloudflare Workers project, local API keys live in a .dev.vars file at the root of the repository. That setup works fine when only a human opens the folder. It becomes risky as soon as AI coding agents work inside the project.

I run Debian and write code in Zed. I wanted a simple setup that keeps secret keys completely outside the repository. At the same time, I still wanted pnpm dev to load those secrets automatically without extra manual steps.

Why .gitignore Is Not Enough

Adding .dev.vars to .gitignore only stops Git from tracking the file. The file still sits inside the project folder on disk.

Any AI agent, editor plugin, or shell command running in the workspace can still read it. Even if you block the file in one editor, another tool or terminal command might still open it.

The safer path is defense in depth. First, move the secret files outside the repository. Second, lock down Linux file permissions on Debian. Third, add editor guardrails in Zed as a backup layer.

What I Changed in the Gib Template

I updated Gib with three changes so every new clone starts with this protection out of the box.

1. Remove .dev.vars from the repository root

The template no longer keeps a .dev.vars file in the project folder. I kept .dev.vars* in .gitignore as a fallback safety net. If someone creates that file by mistake, Git will still ignore it.

2. Point Vite envDir to an external folder

In vite.config.ts, the config reads the "name" field from package.json. It then points Vite’s envDir to an external directory under the XDG Base Directory config path:

${XDG_CONFIG_HOME:-~/.config}/gib-secrets/<project-name>

On a standard Debian machine, XDG_CONFIG_HOME is usually unset. The path resolves to ~/.config/gib-secrets/<project-name>.

Both Vite and @cloudflare/vite-plugin load .dev.vars and .env* files from envDir automatically. They also watch that directory during pnpm dev. When a secret changes, the dev server restarts on its own.

Because the path uses package.json, each cloned project gets its own folder:

  • "my-app" maps to ~/.config/gib-secrets/my-app
  • "@scope/my-app" maps to ~/.config/gib-secrets/@scope/my-app

3. Add fallback protection in .zed/settings.json

Inside the template repository, I added **/.dev.vars* to Zed’s private_files list alongside Zed’s default sensitive patterns:

{
  "private_files": ["**/.env*", "**/.dev.vars*", "**/*.pem", "**/*.key", "**/*.cert", "**/*.crt", "**/secrets.yml"]
}

If a .dev.vars file ever ends up back inside the workspace, Zed’s built-in agent is still blocked from reading it.

Project Setup Workflow After Cloning Gib

Whenever I clone Gib to start a new app, I follow five short steps.

Step 1: Rename the project

First, I update "name" in package.json and worker.name in cloudflare.config.ts. For example, I might name the project my-app:

{
  "name": "my-app"
}

Vite reads this name automatically to find the right external secrets folder.

Step 2: Create the secrets folder on Debian

Next, I create the external folder and .dev.vars file from my terminal:

PROJECT_NAME="my-app"
SECRETS_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/gib-secrets/$PROJECT_NAME"

mkdir -p "$SECRETS_DIR"
chmod 700 "${XDG_CONFIG_HOME:-$HOME/.config}/gib-secrets" "$SECRETS_DIR"
touch "$SECRETS_DIR/.dev.vars"
chmod 600 "$SECRETS_DIR/.dev.vars"

Permission 700 on the directories means only my Linux user can enter or list them. Permission 600 on .dev.vars means only my user can read or write the file.

Step 3: Declare secrets in cloudflare.config.ts

I declare every Worker secret explicitly with bindings.secret() inside cloudflare.config.ts:

import { bindings, defineConfig } from "cf/config";

export default defineConfig({
  worker: {
    // ...
    env: {
      ASSETS: bindings.assets(),
      OPENAI_API_KEY: bindings.secret(),
      STRIPE_SECRET_KEY: bindings.secret(),
    },
  },
});

Then I regenerate TypeScript types:

pnpm run cf-typegen

Declaring secrets explicitly gives two clear benefits:

  1. It generates typed properties on CloudflareBindings in .cloudflare/types/index.d.ts without reading .dev.vars.
  2. It turns on startup validation so cf dev warns right away if a required secret is missing.

Step 4: Add values to .dev.vars

I open ~/.config/gib-secrets/my-app/.dev.vars in a terminal editor outside Zed and outside the repository. Then I add the local key/value pairs:

OPENAI_API_KEY=sk-...
STRIPE_SECRET_KEY=sk_test_...

Mode-specific files also work in the same directory. For example, .dev.vars.staging overrides .dev.vars when running with --mode staging. Editing the file triggers an automatic Vite dev server restart.

Step 5: Read secrets in the Worker

Inside worker/, Hono handlers are typed with { Bindings: CloudflareBindings }. I read secrets directly from c.env:

app.post("/api/example", async (c) => {
  const apiKey = c.env.OPENAI_API_KEY;
  // ...
});

Finally, I start the local server and check that everything loads with no missing-secret warnings:

pnpm dev

One-Time Global Guardrails in Zed

Project settings only cover files inside one workspace. To protect the external folder across all projects, I added global rules in ~/.config/zed/settings.json:

{
  "private_files": ["**/.env*", "**/.dev.vars*", "**/*.pem", "**/*.key", "**/*.cert", "**/*.crt", "**/secrets.yml"],
  "agent": {
    "tool_permissions": {
      "tools": {
        "read_file": {
          "always_deny": [{ "pattern": "gib-secrets" }, { "pattern": "\\.dev\\.vars" }]
        },
        "edit_file": {
          "always_deny": [{ "pattern": "gib-secrets" }, { "pattern": "\\.dev\\.vars" }]
        },
        "terminal": {
          "always_deny": [{ "pattern": "gib-secrets" }, { "pattern": "\\.dev\\.vars" }]
        }
      }
    }
  }
}

These rules block Zed’s agent from reading, editing, or running terminal commands that match gib-secrets or .dev.vars.

Pattern rules in an editor are a strong guardrail, not a strict OS sandbox. Always read shell commands before you approve them.

Important Rules and Edge Cases

This setup handles most daily cases, but a few rules still matter.

Never put secrets in VITE_* variables

Vite embeds variables prefixed with VITE_ into the client-side browser bundle. Anyone who loads the site can read them. Keep every API key as a Worker secret and call third-party APIs from worker/.

Also keep one side effect in mind. Because envDir is set globally in vite.config.ts, Vite also loads local .env and .env.local files for non-secret VITE_* values from ~/.config/gib-secrets/<project-name>/.

Keep local and production secrets separate

The file at ~/.config/gib-secrets/<project-name>/.dev.vars is only for local development (pnpm dev and pnpm preview). Production secrets must be set directly on Cloudflare through the dashboard or the CLI:

pnpm exec cf workers secrets update OPENAI_API_KEY \
  --type secret_text \
  --text "..."

If you need to update several keys at once, check pnpm exec cf workers secrets bulk-edit --help.

Optional process.env fallback

If .dev.vars does not exist in envDir, @cloudflare/vite-plugin falls back to process.env for declared bindings.secret() entries. This lets you inject secrets only for the lifetime of a command using tools like sops exec-env or op run.

Review Worker code before running pnpm dev

When pnpm dev starts, your real local keys are loaded into the local Worker runtime. Before you start or refresh the dev server, review agent edits in worker/ and vite.config.ts. Make sure no code logs c.env.<SECRET> to the terminal or returns it in an HTTP response.

Summary

Moving .dev.vars to ~/.config/gib-secrets/<project-name> keeps secret keys completely out of the workspace on Debian. Explicit bindings.secret() entries keep TypeScript types and startup checks working without touching the secret file. Zed’s private_files and tool_permissions add a second safety layer against accidental reads.

The whole setup takes one minute when cloning Gib, and pnpm dev still feels effortless.