GUIDE · INCIDENT 10 min ·

An agent read the whole .env file to find one tokenTen sessions, three weeks, one confession

NordLayer reported in November 2026 that a coding agent asked to review merge requests needed one GitLab token, and loaded the entire .env file holding it in 10 sessions over three weeks. Each load sent production, HR and BI secrets to the model provider. When the file was missing, the agent printed the whole shell environment instead.

TL;DR· the answer, in twenty seconds

What happened: NordLayer says a QA engineer's coding agent, told by project instructions where a GitLab token lived, loaded the whole .env file in 10 sessions over three weeks. The file also held a production admin password, an HR API token and a BI client secret. Every load went to a third-party model provider. In one session the file was missing and the agent printed the full shell environment.

The minimum fix: Stop pointing agents at files that hold more than the one credential the task needs. Give the review task its own project-scoped, short-lived token, deny reads of .env, and strip secrets from the environment the agent's commands inherit. Rotate anything that sat in a file or variable an agent read.

The lesson: The agent did what it was told, and the instruction was the leak. Deny rules and name filters cover the paths someone anticipated. A credential the agent never holds is the only one it cannot print.

NordLayer published two cases from its AI Scout product in late November 2026, and the first one is the most ordinary credential leak I have read about this year: a coding agent needed one GitLab token, the project's instructions said the token was in .env, and the agent read the whole file, in ten separate sessions, for three weeks.

Nothing about it required an attacker, a prompt injection or a malicious package. A QA engineer asked for merge request reviews. The agent followed the project's own notes. The secrets went to a model provider because that is where an agent's tool output goes. If your instruction files tell an agent where a token lives, you run the same setup.

What NordLayer says happened

The case appears in three places. TechRadar covered it on November 21. NordLayer put out a press release on November 26 and a longer blog post with paraphrased transcripts on November 30. All three come from NordLayer, which sells the monitoring product that caught the behavior. No second party has confirmed the details, and NordLayer does not name the customer. Read it as a vendor case study with a plausible mechanism, which is what it is.

The sequence, from the blog post and the release:

  1. A QA engineer asked an AI coding agent to review merge requests.
  2. The project instructions pointed the agent to a GitLab token stored in a .env file.
  3. The same file held a production password for an internal admin tool, an API token for an HR system and a client secret for a BI service.
  4. To get the token, the agent loaded the entire file. Per the release, it sent the full contents "to a third-party AI service each time."
  5. This repeated across 10 sessions over three weeks.
  6. In one session, the .env file was missing from a new worktree. The command returned the entire shell environment instead. The release describes that output as "every password and key stored on the system."
  7. The agent then flagged its own exposure: "I leaked secrets into this session's tool output… I'd rotate them."
  8. The blog post adds a later event. The agent read the company's production admin API to answer a test question, and its reply contained the company's upstream AI provider API keys in plain text.

NordLayer's second case is shorter. A product manager could not reach the company's Confluence wiki through an MCP connection, which the company may have blocked on purpose. The manager generated a personal API token and asked the agent to use it, and the agent called Confluence's REST API with it. The block covered one route to the data, and the token opened another.

What the write-ups leave out matters for anyone trying to reproduce this:

  • Which agent and which model provider. NordLayer says "an AI coding agent" and "a third-party AI service."
  • The exact command the agent ran. "Loaded the entire file" fits cat .env, a file-read tool or source .env && echo. The missing-file fallback fits env or printenv, or a script that dumped os.environ.
  • Whether anyone rotated the secrets, and when.
  • What the provider's retention terms were for that account.

None of those gaps change the fixes below. They do mean you should not quote the "every password and key" line as a measured fact. It is NordLayer's description of a shell environment nobody else has seen.

One instruction, ten exposures

Start with step 2, because that is where the leak was designed in. Somebody wrote project instructions that told the agent where a token lived. Plenty of us have that habit. You write "the GitLab token is in .env as GITLAB_TOKEN" in AGENTS.md or CLAUDE.md so the agent stops asking. NordLayer's Vakaris Noreika calls it "a common habit among engineers" and names it as the core issue. I agree with him on that point, and the reason is mechanical.

An agent has no way to read one line of a file without the file's contents passing through its tool output. Even a careful command such as grep GITLAB_TOKEN .env puts the token itself into the transcript. A careless one puts every line there. Whatever lands in tool output becomes part of the context, and the context goes to the model provider on the next turn. It also lands in the local session transcript, and it stays in the conversation the agent may compact and resend for the rest of the session. One read of a five-line .env file sends five secrets, not one.

Then the repetition. Project instructions load at the start of every session. Each new session gets the same pointer to .env, makes the same read, and ships the same file. Ten sessions means ten copies of the admin password in ten requests, plus ten local transcripts. Nobody notices, because nothing failed. The reviews got done.

The fallback in step 6 deserves the most attention. A fresh worktree did not have the .env file, because git ignores .env and worktrees start from what git tracks. The agent did not stop and ask. It went looking for the token somewhere else, and the next place a token might live is the process environment. Printing that environment answers the question and leaks everything the shell had exported: cloud keys, database URLs, whatever direnv or a dotfile loaded that morning. This agent treated a missing file as a detour, and the detour it picked was the widest read available.

The production admin API event in step 8 has the same shape. The agent needed an answer, the admin API had it, and the response carried upstream provider keys. The agent did not set out to find them. They were in the payload, so they went into the context.

Why the usual controls miss this

The usual response is a deny rule on .env. That rule helps. It does not cover this incident, and Anthropic's own documentation tells you why.

Claude Code's permissions docs say a Read(./.env) deny rule applies to the built-in file tools and to Bash commands Claude Code can parse, such as cat, head, tail, sed and tee. The same page then lists what it does not cover: "a command that reads files without naming them, such as grep -r pattern .", and "arbitrary subprocesses that read or write files indirectly, like a Python or Node script that opens files itself." It points to the sandbox for OS-level enforcement. A deny rule also does nothing about env, because env reads no file.

So there are three layers to configure, and each covers a different path. For Claude Code, in the project's .claude/settings.json:

{
  "permissions": {
    "deny": ["Read(**/.env)", "Read(**/.env.*)"]
  },
  "sandbox": {
    "enabled": true,
    "filesystem": {
      "denyRead": ["./.env", "./.env.*"]
    },
    "credentials": {
      "envVars": [
        { "name": "GITLAB_TOKEN", "mode": "deny" },
        { "name": "DATABASE_URL", "mode": "deny" },
        { "name": "AWS_SECRET_ACCESS_KEY", "mode": "deny" }
      ]
    }
  }
}

The deny rule covers the file tools and the commands Claude Code recognizes. The sandbox denyRead covers subprocesses such as a Python script that opens .env itself. The credentials.envVars entries unset those variables before each sandboxed command runs, per the sandboxing docs, so env has nothing to print for them. The same docs say there is no built-in credential deny list, so only the variables you name get removed. They also say sandbox.credentials applies to sandboxed Bash commands only, and point to CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1 for stripping credentials from every subprocess, which recognizes credentials by name or value.

Codex has the equivalent in config.toml. The advanced configuration docs say ignore_default_excludes defaults to true, which means Codex does not strip variables named with KEY, SECRET or TOKEN unless you change it:

[shell_environment_policy]
inherit = "core"
ignore_default_excludes = false

[shell_environment_policy.filters]
"*_URL" = "exclude"
"*PASSWORD*" = "exclude"

Do all of that. Then be honest about what it buys you. Every one of these controls works from a list somebody wrote in advance: file paths, variable names, name patterns. NordLayer's Noreika makes the same point: "Security controls only ever cover the cases someone foresaw." The .env deny rule does not know about config/local.env. The name filter passes DATABASE_URL if you forgot the *_URL pattern, and passes REVIEW_CREDS whatever you do. The production admin API response in step 8 was neither a file nor an environment variable, so none of these layers would have touched it.

The Confluence case is the same lesson from the other side. The company blocked a route. The data was still reachable through a credential the user could mint in a few clicks. Blocking routes works until somebody hands the agent a token for an unblocked one.

Give the review task one token

The fix that holds whatever path the agent takes is to make sure the only credential in reach is the one the task needs, scoped to the task, and gone after it.

For this exact job, GitLab has the primitive. A project access token covers one project. GitLab's docs say it cannot reach resources in other projects or create other tokens. It is tied to a bot user, takes a role you choose, and must have an expiry date. Rotating it makes the old value inactive at once. Create one for the review agent with the lowest role and the narrowest scope that can read merge requests, and set the expiry in days, not the 365-day default.

Then keep it out of the file that holds everything else. NordLayer's own advice is to keep task-specific tokens in separate files from production secrets. Treat that as the floor. A separate file the agent can read is still a file the agent can print, and a token in a file is still valid after the session ends.

The stronger version is to stop the agent from holding the token at all. The review script needs it. The agent does not. Wrap the script so the token reaches the script's process at launch and nowhere else:

#!/usr/bin/env bash
# review-mr.sh: the agent runs this script; the token never enters its context
set -euo pipefail
: "${GITLAB_TOKEN:?run through the credential wrapper, not directly}"
MR_IID="${1:?usage: review-mr.sh <merge-request-iid>}"
PROJECT_ID="1234"   # the one project this token covers
curl -sf --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
  "https://gitlab.example.com/api/v4/projects/${PROJECT_ID}/merge_requests/${MR_IID}/diffs" \
  | jq '.[] | {old_path, new_path, diff}'

The agent sees the diff and nothing else. If the token is missing, the script fails with a message instead of letting the agent go searching. Rewrite the project instructions to match: "run ./review-mr.sh <iid> to fetch a merge request" instead of "the token is in .env." The instruction that started this incident becomes an instruction that cannot leak.

What supplies GITLAB_TOKEN to that script is the design choice. A secrets manager CLI that resolves references at launch works. So does a credential broker on the machine that injects the value into the child process. Antonio's November 17 post on running agents without handing them credentials describes one such design, and the Hacker News thread on it disagrees. One commenter argues that keeping keys from the agent while letting it trigger what the keys enable adds a layer of ceremony and little else. For a review task that objection is weaker than it sounds. A read-only, project-scoped token used by a script that returns diffs lets the agent read diffs. The production admin password and the HR token never come within reach. That narrower reach is the gain. In NordLayer's case it is the difference between one exposed review token and four exposed secrets plus a shell environment.

Count what left before you rotate

If you run agents with project instructions that point at .env, assume you have a smaller version of this incident already. Work out what left the machine before you start rotating, so you rotate the right things.

List every secret the agent could have printed. Start with every key in every .env your instructions mention, plus every credential-shaped variable in the shell you launch agents from. Then check the local transcripts for each one. The search below prints file paths and counts, never the values:

# Names of credential-shaped variables in this shell (names only)
env | cut -d= -f1 | grep -Ei 'key|secret|token|pass|_url|cred' | sort

# For each secret in .env, count transcript files that contain its value
while IFS='=' read -r name value; do
  [[ -z "$name" || "$name" == \#* ]] && continue
  value="${value%\"}"; value="${value#\"}"
  (( ${#value} < 8 )) && continue   # short values match too much
  hits=$(grep -rlF -- "$value" ~/.claude/projects ~/.codex/sessions 2>/dev/null | wc -l)
  printf '%-32s %s transcript files\n' "$name" "$hits"
done < .env

A non-zero count means the value passed through the agent's context, and the context is what goes to the model provider. Rotate those. Rotate the production and HR-class secrets first, since a model provider's retention policy is not a control you own. Then delete or purge the transcripts that hold them. A transcript with a live production password in it is one more copy of the leak, sitting in your home directory.

Then check how long the pattern ran. NordLayer's agent did it for three weeks before anyone knew, and the only alarm was the agent's own message. Look for the earliest transcript that contains a hit. Its date starts your exposure window, and it may be earlier than you expect.

What this means for your stack

Do three things this week. Search your AGENTS.md, CLAUDE.md and other instruction files for any line that tells an agent where a credential lives, and replace each one with a script the agent runs. Give each agent task its own scoped, short-lived token instead of a shared .env. Apply the deny, sandbox and environment-filter settings above, knowing they cover only the paths you listed.

The pattern that closes this category is runtime injection with a narrow scope. The agent asks for an action. A broker hands one credential to the one process that performs it, for a limited time. The value never appears in the agent's context, its transcript or its shell environment, so there is nothing for a missing-file fallback to find.

hasp is one working implementation for a developer machine. After hasp import --project-root . --bind .env moves the values into its local vault, hasp run --project-root . --env GITLAB_TOKEN=@GITLAB_TOKEN --grant-secret window --grant-window 15m -- ./review-mr.sh 42 gives the token to that script for 15 minutes, and the agent sees only the reference. It does not stop a production API from returning keys in a response body, and it does not cover a malicious process running as your user. Source-available (FCL-1.0), local-first, macOS and Linux, no account.

NordLayer's agent caught its own leak and told the user to rotate. That came after three weeks and ten sessions, and it was luck. Design your setup so that an obedient agent following your own instructions has nothing worth confessing.

Sources· cited above, in one place

NEXT STEP~90 seconds

Stop handing the agent your real keys.

hasp keeps secrets in one local encrypted vault, brokers them into the child process at exec, and never lets the agent read the value.

  • Local, encrypted vault — no account, no cloud, no telemetry by default.
  • Brokered run — agent gets a reference, the child process gets the value.
  • Pre-commit + pre-push hooks catch managed values before they ship.
  • Append-only HMAC audit log answers "did the agent touch the prod token?" in seconds.
→ okvault unlocked · binding ./api
→ okgrant once · pid 88421
→ okagent never read

macOS & Linux. Source-available (FCL-1.0, converts to Apache 2.0). No account.

Browse all clusters· eight threads, one index