> Agent-ready spec from Diff of the Day (2026-11-28, idea 2). Give this file to your coding agent (Claude Code, Codex, Cursor) and ask it to build the P0 requirements first.

# Spec: Secrets fence for coding agents

## Problem Statement
Coding agents read files to finish tasks, and a .env file holds many secrets at once. In a case NordLayer published in Nov 2026, an agent asked to use one GitLab token loaded the whole .env file in 10 sessions over 3 weeks, exposing production passwords and API tokens in its output. A local fence that hands the agent only the one secret it needs, and blocks whole-file reads, removes that failure mode.

## Goals
- Block an agent's attempts to read .env files and print the shell environment, in the agent you use.
- Give the agent one named secret on request without showing the rest.
- Log every blocked attempt with time, command and file.

## Non-Goals
- A full secrets manager: use the one you have; this only stands between the agent and files.
- Protection against a malicious user: the goal is to stop accidental exposure by the agent.
- Covering every agent: start with one that supports pre-tool hooks.

## User Stories
- As a developer who uses a coding agent, I want reads of .env files blocked so that one task cannot expose every secret.
- As a developer, I want the agent to ask for a named secret so that it gets only the token the task needs.
- As a developer, I want a log of blocked attempts so that I can see how often the agent tried.
- Edge case: as a developer, I want commands that dump the environment (env, printenv, set) blocked too, because a missing .env can make the agent print the whole environment instead.

## Requirements
### P0 (must have)
1. **Pre-tool hook** for your agent that inspects file reads and shell commands before they run.
   - [ ] Given a read of `.env`, `.env.*` or `*.pem`, when the hook runs, then the tool call is refused with a short reason.
   - [ ] Given the commands `env`, `printenv` or `set` with no arguments, when the hook runs, then the call is refused.
2. **Named-secret helper**: a small command the agent may run that returns one variable by name from .env, and refuses names on a deny list.
3. **Log** `fence.log`: time, tool, command or path, decision.
4. **Test suite** with at least 10 allowed and 10 blocked examples.

### P1 (nice to have)
- An allow list per repo for files that look secret but are not.
- A weekly summary of blocked attempts.

### P2 (future)
- Support for a second agent.

## Success Metrics
- Leading: all blocked and allowed examples pass; a normal day of agent work runs with no false blocks you have to override.
- Lagging: zero whole-file secret reads in a month of logs.

## Open Questions
- Which agent and hook mechanism? (you)
- Which file patterns does your stack use for secrets? (you)

## Timeline
One evening for P0.

## Agent Instructions
- **Suggested stack**: the hook language your agent supports (shell or Python), with tests in the same language.
- **Build order**: the test examples first → hook with path rules → command rules → named-secret helper → log.
- **Safety**: never print a secret value in tests or logs; use fake values in fixtures.
- **Done when**: the test suite passes and a real session shows a blocked .env read in fence.log.

## Sources
- NordLayer AI Scout investigations: https://nordlayer.com/blog/ai-agent-investigations/
