4.5
Some things you never hand over有些東西根本不給有些东西根本不给
This chapter assumes you have read Hooks and Enforcement design, or know that a hook can refuse a tool call but can itself fail.
這一章假設你讀過Hooks:每次呼叫都會經過的執行點和把執行層當軟體來設計,或者知道 hook 能拒絕工具呼叫,但它自己也會出錯。
读这一章之前,你要读过Hook:每次调用都会经过的执行点和把执行层当软件来设计,或者至少知道:hook 能拒绝一次工具调用,但它自己也会出故障。
The Knowledge經驗分享经验分享
In one sentence
A hook only blocks what it can see. For things that must never be touched (production, credentials, other people's repositories), the best defence is never giving them to the agent.
一句話
hook 只擋得住它看得到的。對絕對不能碰的東西(正式環境、憑證、別人的倉庫),最好的防禦是根本不交給 agent。
一句话
hook 只能拦下它看得见的东西。对绝不能碰的东西(生产环境、凭证、别人的仓库),最好的防御是压根不交给 agent。
For leaders
- The decision: for production systems, credentials and other teams' repositories, decide that the agent is never given access at all, instead of guarding the access with a hook.
- The risk: a hook sees the text of a call, not its effect, and it can fail. A guard that holds most of the time is the wrong tool for something that cannot be undone or is not yours. I have no count of incidents prevented.
- The cost of doing it right: people time. A person runs the query and pastes the answer back, which is slower than a connection. A real sandbox is a bigger step, and I have not built one. Use this where harm is large or cannot be undone, not everywhere.
- Ask the team: "Make a list of everything this agent can open, run or connect to. Which of those must never be touched, and why can the agent reach them?"
給主管
- 要做的決定:對正式環境、憑證和其他團隊的倉庫,決定 agent 完全不給存取,而不是用 hook 去守著存取。
- 風險:hook 看到的是呼叫的文字,不是它的效果,而且它會出錯。大多數時候守得住的守衛,用在無法撤銷、或不屬於你的東西上,是錯的工具。我沒有擋掉了幾次事故的數字。
- 做對的成本:人的時間。由人去執行查詢,再把答案貼回來,比直接連線慢。真正的沙箱是更大的一步,我沒有做。用在傷害大、或無法撤銷的地方,不是到處都用。
- 可以問團隊:「列出這個 agent 能打開、能執行、能連上的所有東西。其中哪些絕對不能碰,為什麼 agent 碰得到它們?」
写给管理者
- 要拍板的事:对生产系统、凭证和其他团队的仓库,决定根本不给 agent 访问权限,而不是用 hook 去守着这份访问权限。
- 风险:hook 看到的是一次调用的文本,看不到它的效果,而且它会出故障。“大多数时候守得住”的守卫,用来管无法撤销、或者不属于你的东西,是选错了工具。我没有统计过防住了多少起事故。
- 做对的代价:人的时间。由人来跑查询、把答案贴回来,比直接连接慢。真正的沙箱是更大的一步,我还没有搭过。用在危害大或者无法撤销的地方,不是到处都用。
- 可以问团队:“把这个 agent 能打开、能运行、能连接的东西列一张清单。其中哪些是绝不能碰的?为什么 agent 够得着它们?”
The problem
A good guard on a door is still a guard. It can be tricked, it can be asleep, and it can be given the wrong list. A wall with no door has none of these problems.
Everything so far in this part is a guard. A hook reads the text of a call and cannot see its effect (Hooks), and the hook itself is software that can fail (Enforcement design). Hooks and instructions are, in the words of the VS Code docs, "not substitutes for enforced permissions, sandboxing, or enterprise controls".
For most of what an agent does, this is fine. A missed stop on a file edit in a code project can be seen in the diff and undone. But some things cannot be undone, and some things are not yours:
- a production system, where one wrong command changes real data;
- credentials, where one leak cannot be taken back;
- another team's repository, where you have no right to make a mistake.
For these, a guard that holds "most of the time" is the wrong tool. The idea of this chapter is older than agents: least privilege. Give a program only what it needs for the job, and nothing else. The security guidance on prompt injection lists the same idea first among its defences: "Enforce privilege control and least privilege access" (OWASP). If the agent cannot reach a thing, it cannot be talked, tricked or confused into touching it, and no hook has to catch anything. For high-risk work this is the first choice, and the hooks of the earlier chapters come second. (A one-minute test is at the end of the chapter.)
問題
門口有個好守衛,也還是個守衛。他會被騙,會打瞌睡,也可能拿到錯的名單。沒有門的牆,沒有這些問題。
這一部分到目前為止講的都是守衛。hook 讀呼叫的文字,看不到它的效果(Hooks:每次呼叫都會經過的執行點),而 hook 本身是會出錯的軟體(把執行層當軟體來設計)。用 VS Code 文件的話說,hooks 和指令「不能代替強制的權限、沙箱或企業管控」。
agent 做的大部分事,這樣就夠了。程式碼專案裡編輯檔案時漏掉的一次攔截,可以在 diff 裡看到,也撤得回。但有些事無法撤銷,有些事不是你的:
- 正式環境,一個錯的指令就會改到真實的資料;
- 憑證,一次洩漏收不回來;
- 另一個團隊的倉庫,你沒有權利在那裡犯錯。
對這些,「大多數時候」守得住的守衛,是錯的工具。這一章的想法比 agent 還老:最小權限。只給程式做這份工作所需要的,別的都不給。關於 prompt injection 的安全指引,把同一個想法列在防禦的第一條:「落實權限控制和最小權限存取」(OWASP)。agent 碰不到的東西,它就不會被說服、被騙、或被搞糊塗而去碰它,也不需要任何 hook 去攔。對高風險的工作,這是第一選擇,前面幾章的 hook 排第二。(章末有一個一分鐘的測試。)
问题
门口再好的守卫,也还是守卫。他可能被骗,可能睡着,也可能拿到一份错的名单。一堵没有门的墙,就没有这些问题。
这一部分到目前为止讲的都是守卫。hook 读的是一次调用的文本,看不到它的效果(见Hook:每次调用都会经过的执行点),而 hook 本身也是会出故障的软件(见把执行层当软件来设计)。用 VS Code 文档的话说,hook 和指令“不能替代强制执行的权限、沙箱或企业管控”。
agent 做的大部分事,这样就够了。在代码项目里,编辑文件时漏掉的一次拦截,能在 diff 里看到,也能撤销。但有些东西撤销不了,有些东西不是你的:
- 生产系统,一条错的命令就会改动真实数据;
- 凭证,一次泄露就收不回来;
- 别的团队的仓库,你没有资格在那里犯错。
对这些,“大多数时候守得住”的守卫是选错了工具。这一章的想法比 agent 还老:最小权限。只给程序完成工作所需要的,别的一概不给。关于 prompt 注入的安全指南,把同一个想法列在防御措施的第一条:“Enforce privilege control and least privilege access”(OWASP)。agent 够不着的东西,就没法被说服、被骗、被搞糊涂去碰它,也不需要 hook 去拦什么。对高风险的工作,这是首选,前面各章讲的 hook 排第二。(本章末尾有一个一分钟就能做的测试。)
How it works
The method is a question you ask once for each dangerous thing: can I arrange the work so the agent never needs it? Often you can. Four ways.
1. Give it a copy of the files to change, not the whole workspace
Suppose the agent works on code that lives in a large shared repository. The easy way is to open that repository and let the agent roam. Every file is then in reach: the ones it should change, and a thousand it should not.
The other way is to decide first what the work will touch, and hand over only that. In my setup, for a target repository, the agent works from a plan that names the files to change. Only those files are fetched, as copies, into the task's own folder (see Manage tasks, not chats). The agent edits the copies. Your working tree, your current branch and your existing commits are not changed.
The write-back to the real repository is a separate step that a person approves (the second gate in Two gates). It writes the files on a new branch and does not commit. If anything is wrong, you delete the branch. So the agent's mistakes are limited to a folder that exists only for this task. A hook still guards that folder, but if the hook fails, the worst case is a bad copy, not a bad repository.
Also here: commands that push, open a pull request, reset hard or clean are never run by the agent. A person runs them. A guard that is wrong about a push is a leak you cannot fetch back. A command that is not available cannot be wrong.
2. Never connect to the environment; ask a person to run the query
What does the agent need from production? Usually not a connection. It needs a fact: "what is in this table", "which version is deployed", "does this job exist". A fact can be fetched by a person.
My setup gives the agent no credentials and no tools to connect to an environment. This is a rule of use, not a sandbox guarantee (see the last section). When it needs something only an environment can tell it, it writes the question in one fixed place. Each question comes with a command or a query that only reads, which the person can run as it is. The person runs it and pastes the answer back. The agent asks at its next stop, not in the middle of work.
- The agent writes down what it needs to know, with a query that only reads.
- A person reads the query and runs it against the environment.
- The person pastes the answer back. The agent goes on, and the answer is on record.
This costs time, and I will not pretend otherwise: a person is slower than a connection. In return, three things become true. The agent has no path to production, so no injected text, no bad guess and no failing hook can reach it. (The answer a person pastes back is itself untrusted text; see Text the agent reads can give it orders.) Every fact that came from an environment is written down, with who ran it. And the person sees the query before it runs, which is the review you wanted anyway.
3. Keep credentials and keys out of the context
Anything in the agent's context can be sent somewhere else: written to a file, put in a command, repeated in a summary. So a secret that the agent has seen is a secret that can leak, and the agent does not have to be malicious. A model that copies a token into a log line while debugging has done the damage.
I describe this as a design, not as something I measured:
- Do not put secrets in files the agent reads. Rules, notes and knowledge files hold no values, only where a value lives and who owns it.
- Let the program that needs the secret get it, at the moment of use, from the place meant for it, instead of passing it through the agent. The agent writes "run the deploy script"; the script holds the key.
- For the agent's own access, use a limited key: read-only, one project, and expiring soon. Then a leak is small.
- Keep the secrets out of what the agent can read by accident: environment variables of the shell it runs in, config files in the home folder. These are easy to forget.
4. Close the way out
Reading is not the only danger. A secret that cannot be sent anywhere is much less dangerous. If the agent's commands can reach any address on the internet, then anything it can read can be sent out. If it can reach only a short list, the damage is limited. This is the third circle in Untrusted input, and I treat it there. The point here is that network access is a privilege like any other: ask what the agent needs, and give that.
When it is time for a real sandbox
The measures above give the agent less. A sandbox or container is different: it is a boundary set by the operating system, around everything the agent runs. Inside it, the agent can do what it likes, and the walls hold even if the command is cleverly written. A hook cannot offer that, because it only reads the text.
My harness has none. Terminal commands can write any path the user can, and the hook stops only the common spellings. The design notes say plainly that for strong isolation you use the operating system or a container. Look at one sooner than I did if:
- the agent runs commands without a person watching (Use subagents sparingly and the later chapters on unattended work);
- the project handles anything the three lists above name;
- the agent can run code that came from somewhere you do not control.
Anthropic's write-up on sandboxing Claude Code makes a point worth copying: effective sandboxing needs both file system and network isolation. Without network isolation, a hijacked agent can send out your files; without file system isolation, it can escape the sandbox to the network (Claude Code sandboxing). One without the other is not enough. In practice that means a container or a virtual machine with its own network rules, not just a folder.
Note what the VS Code docs say about sandboxing: it is "an added layer", and "not a virtual machine or user-account boundary". A sandbox is not magic either. Read what yours does and does not cover. I have not tested VS Code's sandbox.
Test your own setup
Three questions for each thing that must never be touched:
- Is it in the agent's reach at all? List what the agent can open, run and connect to, and cross out what it does not need.
- If your hook and your instructions both failed today, what is the worst thing the agent could do with it?
- Is there a person-run step that could replace a connection or a key?
A test you can run today. In a terminal opened the way your agent opens it, run env and look at the names of the variables. Then list the files in your home folder that hold keys or tokens. Ask: could the agent read these? If you do not like the answer, that is your first fix, and it does not need a hook.
原理
方法是對每一樣危險的東西問一次的一個問題:我能不能安排這份工作,讓 agent 根本不需要它?常常可以。有四種辦法。
1. 給它要改的檔案的副本,不給整個工作區
假設 agent 做的程式碼放在一個大的共用倉庫裡。最省事的辦法是打開那個倉庫,讓 agent 隨便逛。那樣每個檔案都碰得到:它該改的,和它不該碰的一千個。
另一種辦法是先決定這份工作會碰什麼,只交出那些。在我的設定裡,對目標倉庫,agent 照一份點名要改哪些檔案的計劃工作。只有那些檔案會被取出,作為副本,放進任務自己的資料夾(見管任務,不管對話)。agent 編輯副本。你的工作樹、目前的分支和已有的提交,都不會被改。
寫回真正的倉庫,是另一個要由人批准的步驟(兩道門:人在哪裡批准的第二道門)。它把檔案寫在一個新分支上,而且不提交。有哪裡不對,你就刪掉那個分支。所以 agent 的錯誤,只限於一個只為這個任務存在的資料夾。hook 仍然守著那個資料夾,但 hook 失靈的話,最壞也只是一份壞掉的副本,不是一個壞掉的倉庫。
還有:push、開 pull request、reset --hard、clean 這類指令,agent 永遠不執行。由人執行。守衛在 push 上判斷錯了,是一次收不回來的洩漏。一個不存在的指令不可能出錯。
2. 絕不連到環境;請人去跑查詢
agent 要從正式環境得到什麼?通常不是一條連線。它要的是一個事實:「這張表裡有什麼」、「部署的是哪個版本」、「這個作業存不存在」。事實可以由人去取。
我的設定不給 agent 憑證,也不給它連到環境的工具。這是使用規則,不是沙箱的保證(見最後一節)。它需要只有環境才答得出來的東西時,就把問題寫在一個固定的地方。每個問題都附一條只讀的指令或查詢,人可以原樣執行。人執行後,把答案貼回來。agent 在它下一次停下來時提問,不在工作做到一半時。
- agent 寫下它想知道的事,附上一個只讀的查詢。
- 人看過查詢,再對環境執行。
- 人把答案貼回來。agent 繼續做,答案也留下了紀錄。
這要花時間,我不假裝沒有:人比連線慢。換來的是三件事成真。agent 沒有通往正式環境的路,所以被注入的文字、錯誤的猜測、失靈的 hook 都碰不到它。(人貼回來的答案本身是不可信的文字;見agent 讀到的文字可能在指揮它。)每一個來自環境的事實都寫下來了,也寫了是誰執行的。而且人在查詢執行之前就看到它,這本來就是你想要的審查。
3. 讓憑證和金鑰不進上下文
agent 上下文裡的任何東西,都可能被送到別的地方:寫進檔案、放進指令、在摘要裡重複。所以 agent 看過的祕密,就是可能洩漏的祕密,而且 agent 不必是惡意的。模型在除錯時把一個 token 複製進一行日誌,傷害就造成了。
我把這當作一種設計來講,不是我量過的東西:
- 不要把祕密放在 agent 會讀的檔案裡。規則、筆記和知識檔裡不放值,只寫值放在哪裡、誰負責。
- 讓需要祕密的程式自己去拿,在用的那一刻,從專門放它的地方拿,而不是經過 agent 傳遞。agent 寫「執行部署腳本」;金鑰在腳本手上。
- agent 自己的存取,用受限的金鑰:唯讀、一個專案、很快過期。這樣洩漏也很小。
- 讓祕密不出現在 agent 可能意外讀到的地方:它所在的 shell 的環境變數、家目錄裡的設定檔。這些很容易忘。
4. 把出路關上
讀取不是唯一的危險。送不出去的祕密,危險小得多。如果 agent 的指令能連到網路上的任何位址,它讀得到的任何東西就都能送出去。如果它只能連一張短清單,傷害就有限。這是agent 讀到的文字可能在指揮它裡的第三個圈,我在那裡處理。這裡的重點是:網路存取和其他權限一樣,是一種特權:問 agent 需要什麼,就給什麼。
什麼時候該用真正的沙箱
上面的辦法,是讓 agent 得到的更少。沙箱或容器不一樣:它是作業系統設下的邊界,圍住 agent 執行的一切。在裡面,agent 想做什麼都行,而且即使指令寫得很巧妙,牆還是守得住。hook 做不到,因為它只讀文字。
我的 harness 沒有。終端機指令可以寫使用者能寫的任何路徑,而 hook 只攔得住常見的寫法。設計筆記直接說:要強的隔離,就用作業系統或容器。出現下面這些情況時,比我更早去看一個:
- agent 在沒有人看著時執行指令(慎用子 agent,以及後面講無人值守工作的幾章);
- 專案處理上面三份清單裡的任何東西;
- agent 能執行來源你控制不了的程式碼。
Anthropic 那篇講 Claude Code 沙箱的文章,有一點值得照抄:有效的沙箱需要同時有檔案系統和網路的隔離。沒有網路隔離,被劫持的 agent 能把你的檔案送出去;沒有檔案系統隔離,它能逃出沙箱連到網路(Claude Code sandboxing)。只有一邊是不夠的。實際上這意味著有自己網路規則的容器或虛擬機,不只是一個資料夾。
注意 VS Code 文件對沙箱怎麼說:它是「額外的一層」,而且「不是虛擬機或使用者帳號的邊界」。沙箱也不是魔法。讀清楚你的沙箱涵蓋什麼、不涵蓋什麼。VS Code 的沙箱我沒有測過。
測一下你自己的設定
對每一樣絕對不能碰的東西,三個問題:
- 它到底在不在 agent 碰得到的範圍內?列出 agent 能打開、能執行、能連上的東西,把它不需要的劃掉。
- 如果你的 hook 和你的指令今天都失靈,agent 拿它最糟能做什麼?
- 有沒有一個由人執行的步驟,可以取代一條連線或一把金鑰?
今天就能跑的測試。在以你的 agent 打開終端機的方式打開的終端機裡,執行 env,看變數的名稱。然後列出你家目錄裡存放金鑰或 token 的檔案。問:agent 讀得到這些嗎?不喜歡答案的話,那就是你的第一個修正,而且不需要 hook。
原理
方法是对每样危险的东西问一次:我能不能安排好工作,让 agent 根本用不着它?常常是可以的。有四种办法。
1. 给它要改的文件的副本,不给整个工作区
假设 agent 要改的代码放在一个很大的共享仓库里。省事的做法是打开那个仓库,让 agent 随便逛。这样每个文件都够得着:它该改的,和它不该碰的上千个。
另一种做法是先决定这项工作会碰哪些文件,只把那些交出去。在我的配置里,对一个目标仓库,agent 照一份计划干活,计划里写明了要改的文件。只有这些文件被取出来,作为副本放进任务自己的文件夹(见管任务,不管对话)。agent 改的是副本。你的工作树、你当前的分支和已有的提交都不会被改动。
写回真实仓库是单独的一步,由人审批(见两道门:人在哪里审批里的第二道门)。它把文件写到一个新分支上,而且不提交。如果哪里不对,把分支删掉就行。所以 agent 的错误,被限制在一个只为这个任务存在的文件夹里。这个文件夹仍然有 hook 守着,但就算 hook 出了故障,最坏也不过是一份坏的副本,而不是一个坏的仓库。
另外:推送、开 pull request、硬重置(reset hard)、清理(clean)这类命令,agent 从不运行,由人来运行。一个对推送判断错了的守卫,造成的是收不回来的泄露。一条根本用不了的命令,就不会出错。
2. 从不连接环境,让人去跑查询
agent 要从生产环境得到什么?通常不是一个连接,而是一条事实:“这张表里有什么”“部署的是哪个版本”“这个作业存不存在”。事实可以由人去取。
我的配置不给 agent 凭证,也不给它连接任何环境的工具。这是使用上的规则,不是沙箱的保证(见最后一节)。当它需要只有环境才能告诉它的信息时,就把问题写在一个固定的位置。每个问题都附一条只读的命令或查询,人可以原样运行。人跑完,把答案贴回来。agent 在下一次停下来时提问,不会在干活干到一半时问。
- agent 写下它想知道的事,附上一条只读的查询。
- 人看过查询,再对环境运行。
- 人把答案贴回来。agent 继续干,答案也留了记录。
这要花时间,我不假装不是:人比连接慢。作为交换,有三件事成了真的。agent 没有通往生产环境的路,所以注入的文本、一次错误的猜测、一个出故障的 hook,都够不着生产环境。(人贴回来的答案本身也是不可信的文本,见agent 读到的文字可能在指挥它。)每一条来自环境的事实都被记了下来,连同是谁跑的。而且人在查询运行之前就看到了它,这正是你本来就想做的审查。
3. 凭证和密钥不进上下文
agent 上下文里的任何东西,都可能被送到别处:写进文件、放进命令、在摘要里重复一遍。所以 agent 见过的秘密,就是可能泄露的秘密,而 agent 不必有恶意。一个模型在调试时把 token 抄进一行日志,损害就已经造成了。
下面是一种设计,不是我测量过的东西:
- 不要把秘密放进 agent 会读的文件。规则、笔记和知识库文件里不放值,只写值存在哪里、归谁管。
- 让需要秘密的程序自己去拿,在使用的那一刻、从专门存放它的地方拿,而不是经过 agent 转手。agent 写“运行部署脚本”,密钥由脚本持有。
- 给 agent 自己用的访问,用受限的密钥:只读、限定一个项目、很快过期。这样泄露了也影响不大。
- 管住 agent 可能不经意读到的地方:它所在 shell 的环境变量、home 文件夹里的配置文件。这些很容易忘。
4. 堵上出路
读不是唯一的危险。一个送不到任何地方的秘密,危险小得多。如果 agent 的命令能连到互联网上的任何地址,那么它能读到的任何东西都能被送出去。如果它只能连一份简短的清单,损害就有限。这是agent 读到的文字可能在指挥它里的第三个圆,我在那里讲。这里要说的是:网络访问和别的权限一样,是一种权限:问问 agent 需要什么,就给什么。
什么时候该上真正的沙箱
上面的措施,都是给 agent 更少。沙箱或容器不一样:它是由操作系统设定的边界,围住 agent 运行的一切。在里面,agent 想怎么干都行,而且就算命令写得再巧妙,围墙也不倒。hook 做不到,因为它只读文本。
我的 harness 没有沙箱。终端命令能写用户能写的任何路径,hook 只拦常见的写法。设计笔记明说了:要强隔离,就用操作系统或者容器。有下面这些情况时,比我更早去看看沙箱:
- agent 在没人盯着的情况下运行命令(见慎用子 agent和后面讲无人值守的各章);
- 项目涉及上面三份清单里提到的任何东西;
- agent 能运行来路你控制不了的代码。
Anthropic 那篇讲给 Claude Code 做沙箱的文章,有一点值得照抄:有效的沙箱需要文件系统隔离和网络隔离两者兼备。没有网络隔离,被劫持的 agent 能把你的文件送出去;没有文件系统隔离,它能逃出沙箱连上网络(Claude Code sandboxing)。只有一样不够。实际做起来,就是一个有自己网络规则的容器或虚拟机,而不只是一个文件夹。
注意 VS Code 文档对沙箱的说法:它是“an added layer”,“不是虚拟机或用户账户的边界”。沙箱也不是魔法。读一读你用的那个管什么、不管什么。我没有测过 VS Code 的沙箱。
测试你自己的配置
对每样绝不能碰的东西,问三个问题:
- 它在 agent 够得着的范围内吗?列出 agent 能打开、能运行、能连接的东西,把用不着的划掉。
- 如果今天你的 hook 和指令都失灵了,agent 拿它能做的最坏的事是什么?
- 有没有一个由人来做的步骤,可以代替一个连接或一把密钥?
今天就能做的一个测试。在一个用你的 agent 打开终端的方式打开的终端里,运行 env,看变量的名字。再列出你的 home 文件夹里存着密钥或 token 的文件。问一句:agent 能读到这些吗?如果你不喜欢答案,这就是你要修的第一件事,而且不需要 hook。
Costs and traps
Less access can also mean a task that fails. An agent that cannot reach something it needs will stop or do a worse job. I have no numbers on how many tasks that costs; watch for it when you remove access.
Less access means more work for people. The ask-a-person step is slower than a connection. It is worth it for production. It is not worth it for a read of a public file. Use the method where the harm is large or cannot be undone, not everywhere.
A copy can drift from the original. The agent works on copies of the files. If the real repository moves while the work is going on, the write-back has to cope with that. Keep tasks short, and check the diff before the write-back.
Least privilege is only as good as your list. If you forgot a key in a config file, the agent has it. The test above is for finding such things.
The hook still matters. Not giving the thing is the best defence for what must never be touched. For everything else, the hook is the cheap, always-on check, and the next-best defence. I use both. The chapter's claim is about order: first remove, then guard.
I have not measured any of this. I have no count of incidents prevented. I know the design is simpler to reason about: a thing that is not there cannot be misused.
代價和陷阱
少給存取,也可能讓任務失敗。碰不到所需東西的 agent,會停下來或做得更差。這樣會讓多少任務失敗,我沒有數字;去掉存取時要留意。
少給存取,意味著人要多做事。請人去問的步驟比連線慢。對正式環境值得。對讀一個公開檔案就不值得。用在傷害大、或無法撤銷的地方,不是到處都用。
副本可能和原件漂開。agent 在檔案的副本上工作。工作進行時真正的倉庫動了,寫回就得應付這件事。任務要短,寫回之前檢查 diff。
最小權限只和你的清單一樣好。你要是忘了設定檔裡的一把金鑰,agent 就拿到了。上面的測試就是用來找這種東西的。
hook 仍然重要。不給,是對絕對不能碰的東西最好的防禦。對其他所有東西,hook 是便宜的、一直開著的檢查,是次好的防禦。我兩個都用。這一章的主張是關於順序:先拿掉,再守著。
這些我都沒有量過。我沒有擋掉了幾次事故的數字。我只知道這個設計比較好推理:不在那裡的東西,不可能被誤用。
代价和坑
权限少了,也可能意味着任务失败。agent 够不着它需要的东西,就会停下来,或者做得更差。这会让多少任务失败,我没有数字;撤掉权限时要留意。
权限少了,人要多干活。让人去问,比连接慢。对生产环境,这值得。对读一个公开文件,这不值得。用在危害大或者无法撤销的地方,不是到处都用。
副本可能和原件漂开。agent 改的是文件的副本。如果工作进行时真实仓库动了,写回时就得应付这种情况。让任务保持简短,写回之前检查 diff。
最小权限只和你的清单一样好。如果你忘了某个配置文件里的一把密钥,agent 就拿到了它。上面的测试就是用来找出这类东西的。
hook 仍然重要。不给,是对绝不能碰的东西的最好防御。对其他一切,hook 是便宜的、一直开着的检查,是次好的防御。我两个都用。这一章的主张是关于顺序的:先拿掉,再守卫。
这些我都没有测量过。我没有防住多少起事故的统计。我知道的是,这样的设计更容易推理:不存在的东西没法被滥用。
What the industry says Industry practice
The OWASP list of defences against prompt injection includes "Enforce privilege control and least privilege access" and "Require human approval for high-risk actions". (OWASP LLM01) The VS Code docs say hooks and instructions "are not substitutes for enforced permissions, sandboxing, or enterprise controls", and describe their sandbox as "an added layer", not "a virtual machine or user-account boundary". (Hooks, Security) Neither says that a hook is useless; both say it is not enough alone.
業界怎麼說 業界做法
OWASP 對 prompt injection 的防禦清單包括「落實權限控制和最小權限存取」和「高風險動作要求人批准」。(OWASP LLM01)VS Code 文件說 hooks 和指令「不能代替強制的權限、沙箱或企業管控」,並把它們的沙箱描述成「額外的一層」,不是「虛擬機或使用者帳號的邊界」。(Hooks、Security)兩邊都沒有說 hook 沒用;都說只靠它不夠。
业界怎么说 业界做法
OWASP 的 prompt 注入防御清单里有“Enforce privilege control and least privilege access”和“Require human approval for high-risk actions”。(OWASP LLM01)VS Code 文档说 hook 和指令“不能替代强制执行的权限、沙箱或企业管控”,并把它们的沙箱描述为“an added layer”,不是“虚拟机或用户账户的边界”。(Hooks、Security)两边都没有说 hook 没用;都说的是光靠它不够。
When to delete it Industry practice
A step in which a person runs a query exists because I do not trust an agent with a live connection. If one day a platform offers a connection that is enforced read-only, scoped to one table, logged, and cannot be changed by the agent, the ask-a-person step can go for that data. The other part, not handing over what is not needed, does not retire. It is the principle, not a workaround.
什麼時候可以刪掉 業界做法
由人執行查詢的那個步驟,存在是因為我不信任讓 agent 拿著即時的連線。如果有一天平台提供一種連線,強制唯讀、限定在一張表、有紀錄、agent 又改不了它,那麼這份資料的「請人去問」就可以去掉。另一部分,不交出不需要的東西,不會退場。它是原則,不是繞路的辦法。
什么时候可以删掉 业界做法
让人去跑查询这一步,之所以存在,是因为我不信任一个拿着实时连接的 agent。如果有一天平台提供了这样的连接:强制只读、限定在一张表、有日志、agent 改不了,那么对那份数据,让人去问这一步就可以去掉。另一部分,不交出用不着的东西,不会退役。它是原则,不是权宜之计。
Try it on your own setup
No harness needed. Open your agent in your own repository and paste:
List everything this agent could reach from the way it runs in this project: folders and files it can read or write, shell commands it can run, tools and servers it is connected to, accounts or credentials visible in its environment or config files, and network addresses it can call. Do this by reading the configuration and project files, not by connecting to anything. Mark each item as needed for the work, or not needed, and mark anything that is production, a credential, or another team's repository. Do not change any files and do not print secret values, only where they live.在你自己的環境試試
不用裝 harness。在你自己的倉庫裡打開 agent,貼上:
列出這個 agent 在這個專案裡以它目前的執行方式能碰到的一切:它能讀或寫的資料夾和檔案、它能執行的 shell 指令、它連著的工具和 server、在它的環境或設定檔裡看得到的帳號或憑證,以及它能呼叫的網路位址。靠讀設定檔和專案檔來做,不要連到任何東西。把每一項標成工作需要或不需要,並把任何屬於正式環境、憑證或其他團隊倉庫的標出來。不要改任何檔案,也不要印出祕密的值,只說它們放在哪裡。在你自己的环境里试试
不用装 harness。在你自己的仓库里打开 agent,粘贴:
列出这个 agent 在这个项目里按现在的运行方式能够到的一切:它能读或写的文件夹和文件、能运行的 shell 命令、连接的工具和 server、在环境或配置文件里看得到的账号或凭证,以及它能调用的网络地址。通过读配置和项目文件来做,不要连接任何东西。把每一项标成工作需要或者不需要,并把生产环境、凭证、别的团队的仓库标出来。不要修改任何文件,也不要打印秘密的值,只说它们放在哪里。Go deeper
- OWASP: LLM01 Prompt Injection
- VS Code docs: Security
- Untrusted input: the chapter on what to do when the agent reads text that gives it orders
延伸閱讀
- OWASP:LLM01 Prompt Injection
- VS Code 文件:Security
- agent 讀到的文字可能在指揮它:agent 讀到會對它下命令的文字時怎麼辦
延伸阅读
- OWASP:LLM01 Prompt Injection
- VS Code 文档:Security
- agent 读到的文字可能在指挥它:agent 读到的文字在给它下命令时该怎么办
You can stop here: that is the whole idea. The next part shows where it runs, so you can look inside.讀到這裡就夠了,這就是整個想法。下一部分告訴你它在哪裡運行,可以進去看看。读到这里就够了,核心思路就是这些。下一部分告诉你它在哪里跑,可以进去看看。
In my practice (Copilot Harness)實作实战
Copilot Harness is my open-source harness for VS Code Copilot.
Repository: https://github.com/Smars-Bin-Hu/copilot-agentic-harness
Get started: clone it, open it in VS Code, and type /harness-rookie-get-me-ready in Copilot Chat. You need Python 3.9 or later and git.
Copilot Harness 是我開源的 VS Code Copilot harness。
倉庫: https://github.com/Smars-Bin-Hu/copilot-agentic-harness
開始:clone 下來,用 VS Code 打開,在 Copilot Chat 裡輸入 /harness-rookie-get-me-ready。需要 Python 3.9 以上和 git。
Copilot Harness 是我开源的 VS Code Copilot harness。
仓库: https://github.com/Smars-Bin-Hu/copilot-agentic-harness
上手:把仓库 clone 下来,用 VS Code 打开,在 Copilot Chat 里输入 /harness-rookie-get-me-ready。需要 Python 3.9 及以上版本和 git。
Explore it in the harness
The harness has the pieces for the first two ways: the copies in the task folder, the write-back to a new branch, the commands that only a person runs, and the place where the agent asks a person for facts from an environment. After the setup above, type:
/harness-rookie-wondering I have read the chapter "Some things you never hand over". Teach me how this harness keeps the agent away from the real repository and from any environment (topic 8, especially 8.5 and 8.7). Open the real files with me, and show me what the gate can see and what it cannot.It teaches one small piece at a time, stops to ask if you are following, and opens the files with you.
在 harness 裡探索
harness 有前兩種辦法的零件:任務資料夾裡的副本、寫回新分支、只有人才執行的指令,以及 agent 向人要環境裡的事實的地方。照上面的說明準備好之後,輸入:
/harness-rookie-wondering 我讀完了「有些東西根本不給」這一章。請教我這個 harness 怎麼讓 agent 遠離真正的倉庫和任何環境(主題 8,特別是 8.5 和 8.7)。和我一起打開真實的檔案,讓我看門能看到什麼、看不到什麼。它一次教一小塊,停下來確認你跟得上,然後和你一起打開檔案。
在 harness 里探索
前两种办法所需的部件,harness 里都有:任务文件夹里的副本、写回新分支、只有人才运行的命令,以及 agent 向人询问环境里的事实的那个位置。按上面的说明准备好之后,输入:
/harness-rookie-wondering 我读完了“有些东西根本不给”这一章。请教我这个 harness 是怎么让 agent 远离真实仓库和任何环境的(主题 8,尤其是 8.5 和 8.7)。带我打开真实的文件,让我看看关卡能看到什么、看不到什么。它一次只教一小块,停下来确认你跟上了,再和你一起打开文件。