跳到正文
DEV Community· Vagebond·· 3 小時前AI 評分58

AI Agent 可讀取 .env;Kluis 讓密鑰只交給指定命令

Your agent reads .env. It can't read a vault.

AI 導讀

文章指出,具備專案 shell 權限的 AI Agent 可透過命令讀取 .env,也可繼承啟動環境中的密鑰,因此忽略檔案與拒絕規則仍留有缺口。Kluis 將密鑰保存在密碼庫,透過 kluis run 交給單一命令使用,並以 Touch ID 確認讀取和記錄審計日誌;Agent 只會取得命令輸出。

正文

Vagebond

You give an AI agent a shell in your project. The project has a .env file. The agent can read it.

Most advice offers two fixes: an ignore file and deny rules. Both help. Both leave gaps. Here is where, with commands you can try.

The ignore file

An ignore file only works for the tools that read it. It can hide a file from search. It does not stop a shell command.

cat .env

If the agent can run commands, it can print the file.

Deny rules

Claude Code lets you deny tools in .claude/settings.json:

{
  "permissions": {
    "deny": ["Read(./.env)", "Read(./.env.*)", "Bash(cat:*)"]
  }
}

Now try these:

head .env
grep KEY .env
sed -n p .env
python3 -c "print(open('.env').read())"

Each rule names one path or one command. The list of other ways to read a file is long.

The environment

Many keys never sit in the repo. You export them in your shell profile:

export OPENAI_API_KEY=sk-...

The agent starts from that shell. Every child process inherits the variable.

printenv
echo $OPENAI_API_KEY

No ignore file or deny rule covers that.

Settings that help

  • Keep .env out of the folder you open the agent in.
  • Deny Read on secret paths, as above.
  • Keep Bash on ask, not allow.
  • Don't export keys in ~/.zshrc. Start the agent from a clean shell.

These shrink the gap. They don't close it. As long as the secret is in a file or in the environment, the agent can reach it.

One fix: give the key to the command

Kluis is a password vault for the Mac, for the terminal and the desktop. Your secrets live in the vault, not in plain files.

kluis run gives a secret to one command. The secret is gone when that command ends.

  1. The agent asks kluis run to start a command with a secret.
  2. Touch ID confirms the read.
  3. The command gets the secret. The agent gets the output.

The key goes to the command, never back to the agent. The agent can use a secret without reading it. A policy limits what it can use. Every read goes into an audit log.

To move what is already lying around, an import scan finds keys and passwords in your files.

What Kluis is not

Kluis does not sandbox your agent. A secret you leave in a plain file stays readable. Kluis helps once the secret is in the vault.

It requires macOS 14 or later on Apple silicon. It is free. No account, no server.

The docs and the download are on the Kluis site:

https://baken.vagebond.nl/r/TbrWtFJg

來源:DEV Community · dev.to