Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions .agents/skills/agents-env/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
name: agents-env
description: >-
Use and manage secrets/env vars through the `agents-env` CLI WITHOUT ever
putting the value into your own context. Use whenever you need to pass an API
putting the value into your own context. Use when you need to pass an API
key, token, or secret to a command (curl, an SDK CLI, a test runner), build a
project .env / .env.local from stored secrets, or find which env keys exist —
without printing the value. Core verbs: run (inject into a child + mask its
output + {{KEY}} argv substitution), copy (global store → local file,
value never passes through you), get / ls (discovery; values hidden in agent
mode). The global master store is read-only for agents — never try to write it.
mode), and generate (append a new random global key without seeing its value).
Existing global credentials are never changed by agent commands.
metadata:
requires:
- tool: agents-env
check: agents-env --version
install: cargo install agents-env
---

# agents-env
Expand All @@ -32,7 +38,8 @@ agents-env config path

Use the effective path from `config show`; respect existing settings and XDG
resolution. A human can choose an existing store with `config set global_store`
or register new secrets through `edit`. Config changes are refused in agent
or register externally issued credentials through `edit`. Use `generate` for
new random keys owned by your own services. Config changes are refused in agent
mode because redirecting the global store changes the write-protection boundary.
Never remove agent markers to get around this check. Then use `agents-env doctor`
to check setup and the harness deny rules. This works the same for Grok,
Expand Down Expand Up @@ -118,6 +125,18 @@ agents-env copy GEMINI_API_KEY@personal --to .env.local
agents-env set NEXT_PUBLIC_URL http://localhost:3000 --to .env.local # non-secret literals
```

Create a random key for your own service without handling its value:
```
agents-env generate SERVICE_API_KEY
agents-env run SERVICE_API_KEY -- my-service
```

Generation appends only to the configured global store. An existing name,
including case variants or tagged duplicates, is refused rather than replaced.
Malformed input is refused before saving; do not comment out old keys or repair
real credential files to force generation through. A busy store can be retried
after the other writer finishes. Generation cannot issue external provider keys.

## Selectors and scope

- `KEY@tag` picks one account when a key has several; the tag is the inline
Expand All @@ -128,9 +147,10 @@ agents-env set NEXT_PUBLIC_URL http://localhost:3000 --to .env.local # non-sec

## Rules and limits

- **Never write the global store.** `set`/`copy` only write local `.env*` files
in the cwd; the global store is human-only (a human edits it with
`agents-env edit`). Don't fight the write guard — fix the actual target.
- **Only `generate` may append a new global key.** It never accepts or prints
a value and never changes an existing name. `set`/`copy` stay local-only;
humans use `edit` for existing or externally issued credentials. Do not
bypass the guards or read the store directly.
- **Masking is defense in depth, not a sandbox.** It catches verbatim values,
not re-encoded ones (base64, splitting). Reading `.env` files directly still
exposes secrets — that is the harness deny-rule layer's job, not this tool's.
Expand Down
2 changes: 1 addition & 1 deletion .claude-plugin/marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{
"name": "agents-env",
"source": "./",
"version": "0.2.0",
"version": "0.3.0",
"description": "Skill that teaches an agent to use the agents-env CLI: inject secrets into commands without the value reaching the agent's context, copy global secrets into a project .env value-free, and discover keys without printing them.",
"category": "security",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion .claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "agents-env",
"version": "0.2.0",
"version": "0.3.0",
"description": "Env vars for AI agents: use secrets without ever seeing their values. Teaches the agent to inject API keys/tokens into commands (run + output masking + {{KEY}} substitution), copy secrets from a global store into a project .env without the value passing through context, and discover keys without printing them. Requires the `agents-env` CLI (cargo install agents-env).",
"author": {
"name": "Aiden Ahn",
Expand Down
5 changes: 4 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ This authorization covers the local CLI and its PR workflow. Registry publishing

## Architecture (one responsibility per module)

- `main.rs` — clap CLI + command handlers (`get`/`ls`/`run`/`set`/`copy`/`edit`/`doctor`/`config`), scope resolution, mask-set assembly.
- `main.rs` — clap CLI + command handlers (`get`/`ls`/`run`/`set`/`copy`/`generate`/`edit`/`doctor`/`config`), scope resolution, mask-set assembly.
- `aimode.rs` — agent-mode detection from env markers (+ config `markers=`).
- `config.rs` — typed settings loaded once per invocation, portable path resolution, guarded config edits. The global store resolves to an absolute path and is never per-command overridable.
- `store.rs` — line-preserving `.env` parser, `KEY@tag` selector, round-trip editing.
- `guard.rs` — write-side guards, backup, atomic write, git-ignore gate, secret heuristic.
- `generate.rs` — append-only global key generation, byte-preserving candidate validation, directory-relative transactions and the lock shared with human `edit` sessions.
- `mask.rs` — child injection + leftmost-longest streaming output masking + `{{KEY}}` argv substitution.

## Security invariants — do not regress (each has a test)
Expand All @@ -44,6 +45,8 @@ These are the product. A change that weakens one is a bug even if it compiles:
4. `set`/`copy` can only write bare `.env*` files in cwd; the global store is unreachable (no flag, path-separator/symlink/hardlink/samefile/cwd-in-store-dir-or-descendant all rejected).
5. Secret-bearing `copy` refuses git-tracked or non-gitignored targets (no override).
6. Writes back up to `<file>.YYMMDD.bak` (first-of-day wins) then write atomically (`O_NOFOLLOW` temp + rename).
7. `generate` is the only agent-accessible global writer: OS-random new keys only, no caller value, replacement or tag option; ASCII case-insensitive existing names and malformed stores are refused before persistence. Existing bytes and parsed values/tags remain unchanged, and a generated key must be discoverable by the same parser/selector used by `get`/`run`.
8. Global generation and `edit` hold the same exclusive sidecar lock. Store, backup and staging protections fail closed; concurrent generation never loses an accepted key. Values never appear in generation output, including human mode.

When adding a feature near these, add the adversarial test first. Known accepted limits are documented in README's threat model — don't "fix" them silently.

Expand Down
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "agents-env"
version = "0.2.0"
version = "0.3.0"
edition = "2024"
rust-version = "1.88"
description = "Env vars for AI agents — use secrets without ever seeing them"
Expand All @@ -16,6 +16,7 @@ lto = true
[dependencies]
aho-corasick = "1.1.4"
clap = { version = "4.6.1", features = ["derive"] }
getrandom = "0.4.2"
libc = "0.2.186"
same-file = "1.0.6"
shlex = "1.3.0"
Expand Down
27 changes: 22 additions & 5 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ There is no shortage of env and secret tools, but few keep the value out of the
- **Output masking** — when an injected secret appears in the child's stdout/stderr, it is replaced with `[masked:KEY]` in real time. `doppler run` and `infisical run` inject but leave the output untouched.
- **Value-free copy** — `copy` moves a secret from the global store into a local `.env` without the value passing through the agent's context.
- **Agent-mode detection** — it auto-detects Grok, Codex, OpenCode, Claude Code, and AGY from runtime markers or parent CLI names; other assistants opt in with `AGENTS_ENV_AGENT_MODE=1` or `markers=`.
- **Asymmetric write guard** — the human-managed global master `.env` cannot be modified through this tool.
- **Asymmetric write guard** — existing global values remain human-managed. Agents may only append a new, internally generated random key with `generate`.

## How it works

Expand All @@ -44,6 +44,7 @@ Masking only touches the output stream (child → caller); it never alters the i
| `run <KEY[@tag]…> -- <cmd>` | Inject into the child env, mask its output, substitute `{{KEY}}` in argv. `--all` injects the whole scope. |
| `set <KEY> <VALUE> --to <file>` | Write a non-secret value to a local file. Warns if it looks like a credential. |
| `copy <KEY[@tag]…> --to <file>` | Copy secrets from the global store into a local file; values are not printed. `--as NEWKEY` renames. |
| `generate <KEY>` | Add a new random key to the configured global store; refuse existing names and never print the value, even in human mode. |
| `edit` | Open the global store in `$EDITOR`. Human only; refused in agent mode and on non-TTY. |
| `doctor` | Check file permissions, gitignore coverage, stale backups, untagged duplicate keys, Claude Code deny rules. |
| `config show` / `config path` | Show effective settings or the config path without reading secret values. |
Expand Down Expand Up @@ -81,7 +82,8 @@ agents-env config reset editor
```

Changing settings does not create or move the secret store. Humans register
secrets with `agents-env edit`. Config writes preserve comments and other
externally issued credentials or change existing values with `agents-env edit`.
Use `generate` for new random keys owned by your own services. Config writes preserve comments and other
settings, back up once per day, and use atomic 0600 writes. Unknown keys,
duplicate settings, and read failures produce errors rather than silently
falling back to defaults. Use `config reset global_store` instead of an empty
Expand Down Expand Up @@ -109,6 +111,21 @@ agents-env -f .env.production get DATABASE
agents-env copy NOTION_API_KEY@demodev --to .env.local
```

## Generate without seeing the value

```sh
agents-env generate SERVICE_API_KEY
agents-env run SERVICE_API_KEY -- my-service
```

`generate` encodes 32 OS-random bytes as 64 lowercase hexadecimal characters. It accepts only a key name, not a value, tag, overwrite or rotation option. `--local`/`--file` are refused: generation uses the same configured global path as `get`/`run`. It does not issue credentials for external providers.

- Existing names are refused, including empty values, ASCII case variants and tagged duplicate accounts. No existing entry is replaced or commented out.
- Original bytes, comments, CRLF and order remain unchanged; only a separate entry is appended. A missing trailing newline is added as a separator. Before saving, the candidate is parsed again to verify unchanged existing values/tags and a unique, usable new key.
- Malformed lines, invalid UTF-8, NUL and any input whose meaning would change cause refusal before touching the store or backup. Errors name lines/reasons, never values. Comments are annotations and account selectors, not a duplicate-resolution mechanism.
- `generate` and `edit` share a stable exclusive lock. A busy store exits 2: retry after the other operation finishes. Retrying an existing name never rotates its key.
- Stores and backups use 0600, new parent directories use 0700. Links, special files and missing Git protection are refused; staging files must also be ignored. Generation backs up, publishes atomically and verifies the persisted key.

## Write guard

`set`/`copy` only write `.env*` files in the current directory. The global store is unreachable by construction.
Expand All @@ -121,7 +138,7 @@ agents-env copy NOTION_API_KEY@demodev --to .env.local
- Inside a git repo, a secret-bearing `copy` target must be both untracked and gitignored. Otherwise it is a hard error (no override; fix `.gitignore`).
- Backups of existing targets must also be untracked and ignored for both `set` and `copy`. Use `.env*` rather than only `.env`; a Git verification failure refuses the copy.

Every write makes a `<file>.YYMMDD.bak` backup first. From the second write of the day onward it keeps that day's first backup, since the state before the day's work is the recovery point. It then writes to an `O_NOFOLLOW` temp file and renames it into place. Backups also start with `.env`, so one `.env*` gitignore line covers them.
Every write makes a `<file>.YYMMDD.bak` backup first. From the second write of the day onward it keeps that day's first backup, since the state before the day's work is the recovery point. It then writes to an `O_NOFOLLOW` temp file and renames it into place. Backups also start with `.env`, so one `.env*` gitignore line covers them. The exceptional global append through `generate` follows the narrower rules above.

## Limitations

Expand Down Expand Up @@ -174,8 +191,8 @@ MY_AGENT_MODE=1 agents-env get TAVILY

- **Detection is a signal, not a barrier.** Mode is decided from env markers and nearby parent CLI names, so an agent can reach human mode by hiding both. The intended threat is an honest agent that should not accidentally log a secret. Direct store access by a malicious agent is for the deny rules to stop, not this tool.
- **`{{KEY}}` is visible to a same-user `ps`.** The value lands in the child's argv, where another process of the same user can read it. On a shared machine, use env injection instead of `{{KEY}}` for sensitive values.
- **Parent-directory-swap TOCTOU.** The guard canonicalizes cwd and uses `O_NOFOLLOW` on the temp file, but a same-user attacker who renames a parent directory mid-write could still redirect it. Closing this fully needs directory-fd (`openat`/`renameat`) writes, planned for a later version. It is unreachable without local same-user write access, and with that access the secrets are already exposed.
- **Line endings normalize to LF.** Round-trips preserve comments, order, and spacing, but a CRLF file is rewritten with LF and gains a trailing newline.
- **Concurrent editing boundary.** `generate` and `edit` share a lock. External editors do not follow it, so do not edit the store directly during generation. Generation uses directory-relative `openat`/`renameat` and rechecks changes before publishing, but is not a sandbox against malicious same-user mutation. Existing `set`/`copy` retain the parent-directory-swap race limit.
- **Line endings.** `generate` preserves existing bytes. Existing `set`/`copy` round-trips preserve comments, order and spacing, but rewrite CRLF as LF and add a trailing newline.

## Install

Expand Down
Loading
Loading