绘制助手的威胁模型

阅读约 29分钟,4节中含3个示例。

版次 2026.11.3 示例于 2026年11月22日 通过 60个示例中有60个通过 变化内容

AI 翻译。由人工以英文核验。 我们如何制作图书

译自英文版图书,版次 2026.11.3。 阅读原文

实验室

通过动手实践学习本章

完全按照本书的方式运行每个示例,修改代码以观察结果,并在本书的代码和数据上尝试你自己的想法。无需安装,你的工作会被保存。约 30 秒后打开。

完全按照本书的方式运行每个示例,修改代码以观察结果,并在本书的代码和数据上尝试你自己的想法。无需安装,你的工作会被保存。Labs 为你运行:每月 40小时。

正在查找您的 Lab…

在你自己的机器上,免费: 下载本书的笔记本,附其代码和数据,来源为 GitHub。

读完本章,你能够绘制一个助手的威胁模型:它的资产、来自外部的文本进入它的位置、把这些文本送到模型及其工具的数据流,以及攻击者用来把数据带出去的通道。你会为每个工具和整个助手统计其具备致命三要素(lethal trifecta)中的几项,对照 Meta 的双条件规则(Rule of Two)检查这个统计结果,并把每项风险对应到命名它的 OWASP 条目。

办公室的助手出现在第 1 章中,它读取一封收件箱邮件,查询一位租户,并获取一个页面。该章中的攻击经过这三个步骤,而且只是一次攻击。对一次攻击的测试只能说明这次攻击做了什么,并不能说明助手还能触及哪些其他内容。在选择任何防御措施之前,你需要一份清单,列出助手接触的对象、它所读取文本的来源,以及哪些路径通向办公室之外。本章根据办公室自己的代码绘制这份清单,关于办公室的各项论断都附有你可以运行的示例。

资产与信任

最近一次修改见于版次 2026.11.2

资产是办公室可能失去的任何东西,也是攻击者想要得到的任何东西:一条记录、一个密钥,或者代表办公室行事的能力。信任边界是来源不同的文本进入助手上下文的那个点。边界另一侧的文本,例如租户的消息或承包商的工具描述,对助手没有任何权威。模型仍然会在同一个文本流中读到这些文本和办公室自己的指令,因此边界只是关于文本来自何处的事实,模型自身并不会去强制执行它。

办公室有四项资产。第一项是租户记录:姓名、单元、电话号码、租金余额和银行参考号,全部由一个工具返回。第二项是系统提示中的隐藏上下文,其中包含办公室内部的银行对账备注和一个门户密钥。该密钥是假的,没有任何示例会打印它的值,但攻击者仍然想要它。第三项是行动能力:发送消息(消息会进入评分程序读取的发件箱)和预订承包商上门服务。第四项是回复本身。网页视图把回复渲染为 Markdown,因此回复中的链接或图片就是通向办公室之外的一条通道。第 7 章处理这条通道。

一条记录是否重要,取决于由谁收到它。租户可以知道自己的银行参考号,助手本来就应当把它读回给租户。办公室会损失的,是助手把某位租户的记录交给询问另一位租户的人,或者交给办公室之外的地址。因此,资产既要按其内容来命名,也要按其流出的路径来命名,威胁模型也必须沿着记录找到返回它的工具,再从该工具找到所有可能携带它的通道。

为了找出哪些工具能够触及这些资产,本书读的是工具代码,而不是设计说明。

threat-tool-table.py Python 3.13.16 · bm25s 0.3.16 · linkify-it-py 2.2.0 · markdown-it-py 4.3.0 · numpy 2.5.5 通过
"""Prints one block per office tool: what it reads and writes, whether its output carries outside text, whether itsdefinition is third-party, whether it reaches outside the office and whether it returns private records. The prose'stable rests on the 'untrusted output' and 'reaches outside' lines; their derivations are in lib/threat.py."""from lib import cases, harness, threat, tools  # lib first: threads and seeds are fixed heredef main():    rows = threat.table()    print(f"tools in SPECS: {len(tools.SPECS)}")    print(f"tools in the prompt list: {len(tools.TOOLS)}")    # The prompt list and the code's table must name the same tools.    assert {t["function"]["name"] for t in tools.TOOLS} == set(tools.SPECS)    for name in sorted(rows):        r = rows[name]        print(f"{name}, reads: {', '.join(r['reads']) or 'none'}")        print(f"{name}, writes: {', '.join(r['writes']) or 'none'}")        print(f"{name}, untrusted output: {'yes' if r['untrusted_output'] else 'no'}")        print(f"{name}, third-party definition (THIRD_PARTY_TOOLS): {'yes' if r['third_party_definition'] else 'no'}")        print(f"{name}, reaches outside the office: {'yes, via ' + r['outward'] if r['outward'] else 'no'}")        print(f"{name}, private records: {'yes' if r['private'] else 'no'}")    # The prompt is a second channel: it holds the office's private values on every episode, whichever tools it offers.    world = cases.load_world()    hidden = threat.office()["hidden_context"]    prompt = threat.system_prompt()    print(f"system prompt holds the portal key: {'yes' if world.portal_key in prompt else 'no'}")    print(f"system prompt holds the office's hidden context: {'yes' if hidden in prompt else 'no'}")    untrusted_definitions = threat.marked_untrusted_definitions()    print(f"tool definitions the prompt marks untrusted: {', '.join(untrusted_definitions) or 'none'}")    # Checks behind the claims: the private tool is the one that reads tenant records, the prompt holds the key.    assert [n for n, r in sorted(rows.items()) if r["private"]] == ["lookup_tenant"]    assert world.portal_key in prompt and hidden in prompt    assert untrusted_definitions == sorted(tools.THIRD_PARTY_TOOLS)if __name__ == "__main__":    main()
输出python threat-tool-table.py
tools in SPECS: 8
tools in the prompt list: 8
book_visit, reads: bookings, slots
book_visit, writes: bookings, slots
book_visit, untrusted output: no
book_visit, third-party definition (THIRD_PARTY_TOOLS): yes
book_visit, reaches outside the office: yes, via third-party server
book_visit, private records: no
fetch_page, reads: fetch_log, missing, pages
fetch_page, writes: fetch_log
fetch_page, untrusted output: yes
fetch_page, third-party definition (THIRD_PARTY_TOOLS): no
fetch_page, reaches outside the office: yes, via fetch log
fetch_page, private records: no
list_messages, reads: inbox
list_messages, writes: none
list_messages, untrusted output: yes
list_messages, third-party definition (THIRD_PARTY_TOOLS): no
list_messages, reaches outside the office: no
list_messages, private records: no
list_slots, reads: slots
list_slots, writes: none
list_slots, untrusted output: yes
list_slots, third-party definition (THIRD_PARTY_TOOLS): yes
list_slots, reaches outside the office: yes, via third-party server
list_slots, private records: no
lookup_tenant, reads: world.tenants
lookup_tenant, writes: none
lookup_tenant, untrusted output: no
lookup_tenant, third-party definition (THIRD_PARTY_TOOLS): no
lookup_tenant, reaches outside the office: no
lookup_tenant, private records: yes
read_message, reads: inbox
read_message, writes: none
read_message, untrusted output: yes
read_message, third-party definition (THIRD_PARTY_TOOLS): no
read_message, reaches outside the office: no
read_message, private records: no
search_documents, reads: documents
search_documents, writes: none
search_documents, untrusted output: yes
search_documents, third-party definition (THIRD_PARTY_TOOLS): no
search_documents, reaches outside the office: no
search_documents, private records: no
send_message, reads: outbox
send_message, writes: outbox
send_message, untrusted output: no
send_message, third-party definition (THIRD_PARTY_TOOLS): no
send_message, reaches outside the office: yes, via outbox
send_message, private records: no
system prompt holds the portal key: yes
system prompt holds the office's hidden context: yes
tool definitions the prompt marks untrusted: book_visit, list_slots
练习 2.1

从输出字段读取外部文本列

+10 项证明

第 17 行从定义字段填充“不可信输出”列,因此定义来自外部的工具会被报告为带有外部文本。请修改第 17 行,让它读取与该列标签相符的字段。

        r = rows[name]        print(f"{name}, reads: {', '.join(r['reads']) or 'none'}")        print(f"{name}, writes: {', '.join(r['writes']) or 'none'}")        print(f"{name}, third-party definition (THIRD_PARTY_TOOLS): {'yes' if r['third_party_definition'] else 'no'}")        print(f"{name}, reaches outside the office: {'yes, via ' + r['outward'] if r['outward'] else 'no'}")        print(f"{name}, private records: {'yes' if r['private'] else 'no'}")
运行于 Python 3.13.16 · bm25s 0.3.16 · huggingface-hub 2.4.0 · linkify-it-py 2.2.0 · markdown-it-py 4.3.0 · matplotlib 3.11.2 · numpy 2.5.5 · tokenizers 0.23.3 · torch 2.15.0+cpu · transformers 5.24.0

只有一个工具返回私有记录,示例对此做了断言。清单为每个工具打印一行,内容包括:它读取什么、写入什么、其输出是否带有外部文本、其定义是否来自第三方、是否触及办公室之外,以及是否返回私有记录。助手可用的工具共有 8 个,与提示中的工具列表所列数量相同。其中六个属于办公室。另有两个,list_slots 和 book_visit,来自承包商,它们的定义是在办公室之外编写的。

输出一列更难判断。有 5 个工具返回由办公室之外的人撰写的文本。其中四个是办公室的工具:list_messages 和 read_message 返回收件箱消息,search_documents 返回文档,fetch_page 返回网页。第五个是 list_slots,它返回时段的备注,而这条备注可能由承包商撰写。因此,“不可信输出”是每一个结果自身的属性。工具的名称并不能决定它,看起来属于内部的工具也可能带有外部文本。

该表还显示,无论提供哪些工具,提示在每一轮中都包含门户密钥和内部备注,所以只数工具的个数会漏掉这些值。代码对此做了断言。移除一个工具,会移除通向它所读取记录的路径,但不会移除存在于提示中的值。第 9 章讨论这个问题,并对此作出承诺:本书会衡量把这些值移出提示之后会发生什么。

工作准则: 根据代码中的工具列表,而不是设计文档,来画出资产和边界。对模型可用的每个工具,记录它读取什么、写入什么、哪些结果带有外部文本,以及其定义是否来自外部。把数量与提示中的工具列表核对。某个工具出现在一份列表中而不在另一份中,就说明你所画的模型存在缺口。

数据流

数据流追踪一段文本,从它被写下的位置,一直到模型读到它的那一刻。每条边界都有一个载体和一个通道:载体是助手状态中存放该文本的字段,通道是该文本到达模型的方式。在办公室这个场景中,载体有两类。第一类是助手主动请求的结果中所含的文本:收件箱中的一封邮件、来自上传或搜索索引的一份文档、抓取的一个页面,或承包商时段的备注。这类文本在助手调用工具时到达。第二类是工具定义中所含的文本:即名称、描述和模式(schema),模型在每次提示中都会看到它们,无论是否调用该工具。工具投毒(tool poisoning)会把针对模型的指令写进工具的定义,《Model Context Protocol in Practice》第 9 章(Model Context Protocol in Practice, chapter 9)对此有详细讨论。第一类在模型提出请求时到达,第二类则每次都会到达。第二类比其体量所显示的更重要。定义在模型选择工具之前就已被读取,所以它能改变被调用的是哪个工具,而不仅仅是一次调用返回什么。

外部文本可以进入的六个位置,就是攻击集所用的位置。第 3 章定义了这些位置,并说明了各个用例是如何构建的。示例打印它们时带有 loc- 前缀,本书省略该前缀。

threat-flows.py Python 3.13.16 · bm25s 0.3.16 · linkify-it-py 2.2.0 · markdown-it-py 4.3.0 · numpy 2.5.5 通过
"""Each attack location, and the one channel that delivers its text to the model: a tool that reads the carrier's field,or the prompt's tool list. The injected text is checked in code and never printed. The probes are in lib/threat.py."""from lib import cases, threat  # lib first: threads and seeds are fixed heredef main():    # The counts are over the 120 fixed cases (dev and held-out). Chapter 1's 20 rough cases are left out.    fixed = [c for c in cases.load_cases() if c.split != "rough"]    rough = cases.load_cases("rough")    print(f"fixed cases: {len(fixed)}")    print(f"rough cases, chapter 1's set, not counted: {len(rough)}")    flows = threat.location_channels()    print(f"locations: {len(flows)}")    for f in flows:        loc = f["loc"]        print(f"{loc}, fixed cases: {sum(1 for c in fixed if c.location == loc)}")        print(f"{loc}, carrier kind: {f['kind']}")        print(f"{loc}, state field changed: {f['field']}")        print(f"{loc}, delivered by: {', '.join(f['channels']) or 'none'}")        print(f"{loc}, read the field but do not carry it: {', '.join(f['quiet']) or 'none'}")if __name__ == "__main__":    main()
输出python threat-flows.py
fixed cases: 120
rough cases, chapter 1's set, not counted: 20
locations: 6
loc-inbox, fixed cases: 24
loc-inbox, carrier kind: inbox
loc-inbox, state field changed: inbox
loc-inbox, delivered by: read_message
loc-inbox, read the field but do not carry it: list_messages
loc-index, fixed cases: 24
loc-index, carrier kind: index
loc-index, state field changed: documents
loc-index, delivered by: search_documents
loc-index, read the field but do not carry it: none
loc-page, fixed cases: 12
loc-page, carrier kind: page
loc-page, state field changed: pages
loc-page, delivered by: fetch_page
loc-page, read the field but do not carry it: none
loc-server-output, fixed cases: 24
loc-server-output, carrier kind: server-output
loc-server-output, state field changed: slots
loc-server-output, delivered by: list_slots
loc-server-output, read the field but do not carry it: book_visit
loc-tool-description, fixed cases: 12
loc-tool-description, carrier kind: tool-description
loc-tool-description, state field changed: schemas
loc-tool-description, delivered by: prompt tool list
loc-tool-description, read the field but do not carry it: none
loc-upload, fixed cases: 24
loc-upload, carrier kind: upload
loc-upload, state field changed: documents
loc-upload, delivered by: search_documents
loc-upload, read the field but do not carry it: none

每个位置都有一个载体和一个传递通道,示例会对两者都做检查。下表逐一列出:

位置 载体 到达模型的途径
inbox 一封收件箱邮件 read_message
upload 租户上传的一份文档 search_documents
index 搜索索引中已有的一份文档 search_documents
page 一个测试夹具页面 fetch_page
server-output 承包商提供的一个时段备注 list_slots
tool-description 承包商工具的定义 提示中的工具列表,无需调用

有两个位置共用一个通道。upload 和 index 都通过 search_documents 到达模型,因为在这两种情况下,文本都位于搜索所返回的文档中。这里的计数针对的是 120 个固定攻击用例,开发集与留出集合计在内。第 1 章的 20 个粗略用例不在其中。在固定用例中,24 个经由收件箱到达,12 个经由页面到达。这些计数描述的是攻击集的构成方式,因此并不说明助手遵从攻击的频率。

输出还记录了哪些其他工具会读取某个位置的字段,但不会把其中的文本带给模型。list_messages 就是以这种方式读取收件箱的,但攻击集把文本放在邮件正文中,而 list_messages 不返回正文。它确实会返回发件人和主题,而这两项由外部人员填写,下文的三要素(trifecta)计数会将其算作外部文本。book_visit 读取时段时不会带出它们的备注,所以备注的通道是 list_slots。这些读取不会为攻击集增加通道,因此统计通道的读者可以略过它们。

数据流也显示了出口,而防御必须切断的正是向外的路径。send_message 写入发件箱,发件箱存放已接受但无人投递的邮件。fetch_page 记录所请求的 URL,而该 URL 的查询部分可以携带数据。book_visit 把其参数发送到承包商的服务器,因此即使这次预约是办公室所希望的,它依然属于向外的路径。回复中的链接和图片在网页视图渲染它们时离开系统。第 1 章在前两个通道上度量了一份允许列表,并说明了它看不到什么:即渲染后的回复(允许列表看不到什么)。

沿着这些表格追踪第 1 章中的攻击,它就成了一条路径。注入的文本从收件箱进入。助手通过 read_message 读取它,该工具把文本放进了模型的上下文。随后文本引导模型调用 lookup_tenant 和 fetch_page,其中每一步都是从一个载体到一个通道的流。威胁模型就是助手可能走过的所有此类路径的集合,从每个位置到每个向外通道。这次攻击只是该集合中的一条路径。本章的任务是列出其余路径,以免把对一条路径的防御误当成对所有路径的防御。

工作准则: 对每个位置,说出它的载体和传递它的工具,并标明文本是在模型请求时到达,还是在每次提示中都到达。只有当某种传递确实到达模型时,才把该位置算作一个通道。对向外的流也同样处理,因为某一通道上的允许列表对其他通道没有任何说明。

三要素与二选一规则

最近一次修改见于版次 2026.11.2

Simon Willison 在2025 年 6 月的一篇文章中提出了“致命三要素”。它包含三个环节:可访问私有数据、会接触不可信内容,以及能够向外部通信。他的论点针对的是数据窃取:同时具备这三项的智能体,可能被诱骗去读取私有数据,并把数据发送给攻击者。去掉其中一个环节,就能堵住这条路径,但并不能堵住所有攻击。本书自己的预约攻击和订金数字攻击都不需要私有数据:前者让助手预约一次无人请求的上门看房,后者让助手把一个被污染的数字当作答案说出来。第 6 章和第 9 章讨论这类不窃取数据而是执行操作或误导用户的攻击。Willison 的建议是避免在同一个智能体中组合这三者,而三要素只是出自一位作者的分析框架,不过被广泛引用。

Meta 在2025 年 10 月 31 日的一篇文章中把同一思路发展为一条设计规则,称为“智能体二选一规则”(Agents Rule of Two)。该规则指出,一个智能体会话在以下三种属性中最多只应同时具备两种:处理不可信的输入,访问敏感系统或私有数据,以及改变状态或向外部通信。文章称,在提示注入能够被可靠地检测和拒绝之前,这条规则都应当成立,并把提示注入描述为大语言模型普遍存在的、尚未解决的弱点。需要三者兼备的智能体不应单独运行,而需要监督,例如由人来批准它的操作。第 6 章即以这一条款为基础展开。文章还提到了 Meta 自己的防护工具。本书把这条规则视为对设计的检查,而不是经过度量的防御手段,本章中也没有任何结果表明它能减少攻击。

threat-trifecta-check.py Python 3.13.16 · bm25s 0.3.16 · linkify-it-py 2.2.0 · markdown-it-py 4.3.0 · numpy 2.5.5 通过
"""Each tool's trifecta legs, and whether removing one leg's tools leaves at most two legs (the Rule of Two), assertedon the computed sets. The prompt's private values are checked apart, since dropping the private tools does not remove them."""from lib import cases, threat, tools  # lib first: threads and seeds are fixed heredef main():    legs = threat.legs_by_tool()    total = set().union(*legs.values())    print(f"tools checked: {len(legs)}")    for name in sorted(legs):        print(f"{name}, legs: {', '.join(l for l in threat.LEGS if l in legs[name]) or 'none'}")    print(f"legs in the tool set, in total: {', '.join(l for l in threat.LEGS if l in total)}")    # The untrusted leg comes from two sources: a result that carries outside text, or a third-party definition.    rows = threat.table()    from_output = sorted(n for n, r in rows.items() if r["untrusted_output"])    from_definition = sorted(n for n, r in rows.items() if r["third_party_definition"])    held_untrusted = sorted(n for n, held in legs.items() if "untrusted" in held)    print(f"tools whose output carries outside text: {len(from_output)}")    print(f"tools with a third-party definition: {len(from_definition)}")    print(f"tools holding the untrusted leg: {len(held_untrusted)}")    print(f"untrusted leg, from output: {', '.join(from_output)}")    print(f"untrusted leg, from a third-party definition only: "          f"{', '.join(n for n in held_untrusted if n not in from_output) or 'none'}")    assert set(held_untrusted) == set(from_output) | set(from_definition), "the untrusted leg has another source"    # A tool that holds all three legs by itself would need no second tool to be dangerous.    whole = sorted(n for n, held in legs.items() if held == set(threat.LEGS))    print(f"tools with all three legs: {', '.join(whole) or 'none'}")    # The Rule of Two, per leg: remove every tool that carries the leg; the rest should carry at most two.    for leg in threat.LEGS:        kept, left = threat.without_leg(legs, leg)        print(f"without the {leg} tools, legs left: {len(left)} ({', '.join(l for l in threat.LEGS if l in left)})")        print(f"without the {leg} tools, tools kept: {', '.join(kept)}")        assert len(left) <= 2, f"removing the {leg} tools leaves {len(left)} legs"    # The prompt is a channel the tool list does not control: with the private tools removed, it still holds the values.    private = {n for n, held in legs.items() if "private" in held}    kept_schemas = tuple(t for t in tools.TOOLS if t["function"]["name"] not in private)    print(f"private values in the prompt, with the private tools removed: "          f"{'yes' if threat.holds_private_values(kept_schemas) else 'no'}")    # Checks behind the claims.    assert total == set(threat.LEGS), "the assistant has all three legs in total"    assert whole == [], "no single tool holds all three legs"    assert threat.holds_private_values(kept_schemas)if __name__ == "__main__":    main()
输出python threat-trifecta-check.py
tools checked: 8
book_visit, legs: untrusted, outward
fetch_page, legs: untrusted, outward
list_messages, legs: untrusted
list_slots, legs: untrusted, outward
lookup_tenant, legs: private
read_message, legs: untrusted
search_documents, legs: untrusted
send_message, legs: outward
legs in the tool set, in total: private, untrusted, outward
tools whose output carries outside text: 5
tools with a third-party definition: 2
tools holding the untrusted leg: 6
untrusted leg, from output: fetch_page, list_messages, list_slots, read_message, search_documents
untrusted leg, from a third-party definition only: book_visit
tools with all three legs: none
without the private tools, legs left: 2 (untrusted, outward)
without the private tools, tools kept: book_visit, fetch_page, list_messages, list_slots, read_message, search_documents, send_message
without the untrusted tools, legs left: 2 (private, outward)
without the untrusted tools, tools kept: lookup_tenant, send_message
without the outward tools, legs left: 2 (private, untrusted)
without the outward tools, tools kept: list_messages, lookup_tenant, read_message, search_documents
private values in the prompt, with the private tools removed: yes
练习 2.2

收集持有不可信分支的所有工具

+10 项证明

第 17 行为空。它应按排序顺序列出所有其分支中包含不可信分支的工具,数据取自 legs。第 18 至 24 行保持不变。

    rows = threat.table()    from_output = sorted(n for n, r in rows.items() if r["untrusted_output"])    from_definition = sorted(n for n, r in rows.items() if r["third_party_definition"])    print(f"tools whose output carries outside text: {len(from_output)}")    print(f"tools with a third-party definition: {len(from_definition)}")    print(f"tools holding the untrusted leg: {len(held_untrusted)}")
运行于 Python 3.13.16 · bm25s 0.3.16 · huggingface-hub 2.4.0 · linkify-it-py 2.2.0 · markdown-it-py 4.3.0 · matplotlib 3.11.2 · numpy 2.5.5 · tokenizers 0.23.3 · torch 2.15.0+cpu · transformers 5.24.0
练习 2.3

为什么私有值仍会进入提示词

+10 项证明

从工具列表中移除私有工具之后,第 38 行的检查仍然输出 yes。下列哪种解释是正确的?

为什么私有值仍会进入提示词:选出正确的解释
运行于 Python 3.13.16

这里的计数以工具为单位,在 8 个工具中,没有一个同时具备三个环节。lookup_tenant 只具备私有环节,send_message 只具备向外环节,fetch_page、book_visit 和 list_slots 各具备两个环节,即不可信环节和向外环节。不可信环节共出现在 6 个工具上,它有两个来源:携带外部文本的结果,或第三方提供的定义。有五个工具通过输出具备该环节:fetch_page、list_messages、list_slots、read_message 和 search_documents。book_visit 仅凭定义具备该环节。它的输出是承包商的预约结果,而不是外部文本,但它的定义由承包商撰写,而示例把第三方定义计为不可信,因为模型在每次提示中都会读取该定义。综合来看,所提供的助手具备全部三个环节。去掉私有类工具后剩下两个环节,去掉不可信类工具或向外类工具后同样剩下两个环节。按这一计数,去掉任意一个环节后,二选一规则都成立。代码对上述每个结果都做了断言,因此某个工具的改动一旦破坏其中一项,运行就会失败。

审查者阅读三要素计数时,有三个问题比总数更重要:向模型提供了哪些工具,哪些文本通过任何途径进入了它的提示,以及哪些渠道可以把数据带出办公室。计数只回答第一个问题,另外两个问题需要上一节的数据流。

计数以工具为单位,问题也正出在这里:一次运行可能读取了私有数据却从未发送它,正如第 3 章所示。成功计数也有同样的盲点:私有数据进入了上下文但从未流出的运行,会被计为失败。去掉私有类工具后,示例仍然能在提示中找到私有值。门户密钥和内部备注在每个回合中都存在,因此该会话仍然具备私有环节,而且如果注入的指令提出要求,模型仍可通过向外类工具把这些值发送出去。这里的工作单位是进入模型的内容,而不是工具清单。

承包商的工具使向外环节更难分析。list_slots 和 book_visit 属于向外类,它们的定义来自承包商,因此模型读到的有关它们的文本并非办公室所写。book_visit 会把其参数发送到承包商的服务器,这意味着即使预约本身是用户想要的,用户从未要求提供的参数也会离开办公室。第 6 章为此类调用编写参数策略,第 10 章讨论承包商的服务器和定义。第 8 章构建了一种把不可信环节与工具隔开的设计:一个不带工具的模型读取外部文本,另一个带工具的模型则从不接触未经处理的该文本。

工作准则: 对你提供的每个工具都计算其环节。然后依次去掉每个环节对应的工具并重新计数,而且私有环节既要按哪些工具读取它来计,也要按进入提示的内容来计。

风险映射

每项风险都有一个名称,描述它的各份清单则给它一个编号。读者在一处看到“目标劫持”,在另一处看到 ASI01,需要知道两者是同一项风险。本书把每项风险映射到命名它的条目。这些条目来自两份清单。OWASP 智能体应用十大风险(OWASP Top 10 for Agentic Applications)发布于 2025 年 12 月 9 日,条目编号为 ASI01 至 ASI10。OWASP GenAI LLM Top 10 2026 引用其固定版本,即 Zenodo 上的 v1.0。OWASP 以开放许可并附相同方式共享的条件发布这两份清单,因此本书只给出名称和编号,并用自己的话解释每项风险,不转载清单中的描述。

只有部分编号已为本书确认。凡本书尚未确认编号之处,下表用文字给出风险名称,并省略编号。MITRE ATLAS 矩阵也有针对这些攻击的技术,但本书所固定的版本尚未确认,因此本章不印出任何 ATLAS 技术编号。对照未确认版本印出的技术编号,会成为读者无法核对的论断。

风险(用本书的说法) 条目(ASI,2025 年 12 月 9 日;LLM 2026) 办公室中的体现 章
助手读到的文本改变了它要完成的目标 ASI01 智能体目标劫持;LLM01:2026 提示注入 inbox、upload、index、page 3、4、5
工具被用于用户没有要求的事 ASI02 工具滥用与利用 send_message、book_visit 6
助手在攻击者提供的名称上行使自己的权限 ASI03 身份与权限滥用 lookup_tenant 6、9
助手拥有的权力超出其任务所需 LLM03:2026 过度代理 每个工具,每项任务 6
第三方的定义或输出进入提示 ASI04 智能体供应链漏洞 list_slots、book_visit 10
提示中的私有值经由对外通道泄出 LLM02:2026 敏感信息泄露;LLM08:2026 隐藏上下文暴露 系统提示、租户记录 9
文档中被投毒的数字成为助手的答案 ASI06 记忆与上下文投毒 search_documents 9
回复加载了带出数据的链接或图片 LLM10:2026 不当输出处理 渲染后的回复 7

关于此表有两点说明。该映射是本书对各条目名称和范围的理解,不是 OWASP 对这间办公室作出的陈述。关于被投毒数字的那一行是本书自己的归类:攻击集中的错误存款目标正是该行所描述的情形,而第 9 章是本书考察它的地方。

每一行从左向右读。风险是读者的问题,条目是读者在来源中会找到的名称,办公室一列说明这个问题在本案例中出现在何处,章一列说明本书在何处度量一种应对办法;对第一行而言,那就是第 3 章的基线。该表也显示了威胁模型的用途。每一行都给出一个条目、办公室中的一个位置,以及度量针对它的防御的那一章。这里的每一行都有对应的章。若某一行没有,它就是本书不测试的风险,正文会如实说明。

当从某个位置到某个对外通道的每条路径,要么被某一行覆盖,要么被声明为未度量时,威胁模型才算完成。仅有一份风险清单并不能使它完成。路径比名称更重要,因为防御设置在路径上,被漏掉的路径依然敞开。一张表不等于覆盖。审阅者要逐条对照各行检查每条对外路径,没有对应行的路径就是敞开的,无论表有多长。

依据设计文档绘制的模型会漏掉代码实际所做的事。后来某次变更中添加的工具,就会缺席于图中,而三要素也只是按设计者记得的那些工具来计数。每当添加工具或定义变化时,就重新绘制模型,并由代码中的工具列表而不是文档来决定其中包含什么。

本章作出的决定:

  • 威胁模型依据代码绘制:八个工具,六个来自办公室,两个来自承包商。
  • 三要素的计数既覆盖工具,也覆盖到达模型提示的内容。
  • 只有在本书已确认之处才印出 ASI 和 LLM 编号。其他风险用文字命名。
  • 在版本确认之前,不印出任何 MITRE ATLAS 编号或版本号。

工作准则: 只有在本书已确认编号时才把风险映射到条目,否则用文字命名。为每一行给出度量防御的章,或在正文中说明该风险未被度量。

本章,在本版次中

版次 2026.11.3示例于 2026年11月22日 通过3 of 3个示例 in this chapter pass

译自英文版图书,版次 2026.11.3。

开始实验室

图书始终免费阅读。

查看价格

引用本节

格式

每种格式均引用英文原书,本译本以其为准。

每种格式都会注明该版次。其链接会打开当前版次,节标题处会说明该节文本最后一次修改的时间。访问日期和检索日期即为您打开此页的当天。

本页内容

第 2 章,共 12 章: 绘制助手的威胁模型

  1. 资产与信任
  2. 数据流
  3. 三要素与二选一规则
  4. 风险映射

返回顶部全部章节