# HTTP TRACE 方法

HTTP TRACE 方法是一种请求方法，定义于 1999 年 6 月的 HTTP/1.1 原始规范中，它要求请求的最终接收者将收到的报文回显给客户端，作为 200（OK）响应的主体，Content-Type 为 "message/http"<sup>[1](https://www.rfc-editor.org/info/rfc2616/)</sup><sup> • </sup><sup>[2](https://wg.org/specs/rfc7231.html)</sup>。其预期用途是诊断：通过比较发送的内容与返回的内容，客户端可以了解路径上的代理和网关如何修改了请求<sup>[3](https://.dev/trace)</sup>。正是这种回显特性使 TRACE 成为 2003 年公布的跨站追踪（Cross-Site Tracing，XST）攻击的载体，而在生产服务器上禁用 TRACE 如今已成为标准的安全加固建议<sup>[4](https://www.cgisecurity.com/whitehat-mirror/WhitePaper_screen.pdf)</sup><sup> • </sup><sup>[3](https://.dev/trace)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 请求对请求报文进行远程的、应用层级的环回；最终接收者将其反射为 200（OK）响应的主体，Content-Type 为 "message/http"<sup>[2](https://wg.org/specs/rfc7231.html)</sup> |
| 起源 | RFC 2616（HTTP/1.1，1999 年 6 月）中定义的八种方法之一<sup>[1](https://www.rfc-editor.org/info/rfc2616/)</sup> |
| 注册表属性 | 在 IANA HTTP 方法注册表中列为安全且幂等<sup>[2](https://wg.org/specs/rfc7231.html)</sup> |
| 转发控制 | Max-Forwards 头限制 TRACE 请求可被转发的次数；收到值为零的中间节点必须作为最终接收者进行响应<sup>[5](https://datatracker.ietf.org/doc/rfc9110/)</sup> |
| 安全相关性 | 启用 TRACE 会允许跨站追踪（XST），Jeremiah Grossman 于 2003 年 1 月描述了该方法，称其即使设置了 HttpOnly 标志也能读取 Cookie<sup>[4](https://www.cgisecurity.com/whitehat-mirror/WhitePaper_screen.pdf)</sup> |
| 加固 | Apache 通过 `TraceEnable off` 禁用 TRACE（返回 405）；IIS 在请求筛选中从允许的方法中移除 TRACE 和 TRACK；nginx 根本不实现 TRACE<sup>[6](https://d.apache.org/docs/2.4/mod/core.html)</sup><sup> • </sup><sup>[3](https://.dev/trace)</sup> |

## 方法的工作原理

TRACE 请求不携带报文主体；RFC 2616 不允许任何请求主体随附其上<sup>[6](https://d.apache.org/docs/2.4/mod/core.html)</sup>。最终接收者——无论是源服务器还是第一个收到 Max-Forwards 值为零的服务器——将收到的报文（排除某些字段）反射回客户端，作为 200 响应的主体<sup>[2](https://wg.org/specs/rfc7231.html)</sup>。

Max-Forwards 头专为 TRACE 和 OPTIONS 而设。其值为一个十进制整数，表示请求还可以经过多少跳。每个收到包含 Max-Forwards 的 TRACE 请求的中间节点必须在转发前检查并更新该值：值为零时必须自行响应而非转发，大于零时必须将该值递减（若自身最大值更低则替换为自身最大值）<sup>[5](https://datatracker.ietf.org/doc/rfc9110/)</sup>。这使客户端能够追踪一个看似在代理链某处失败或循环的请求<sup>[5](https://datatracker.ietf.org/doc/rfc9110/)</sup>。

IANA HTTP 方法注册表将 TRACE 归类为既安全又幂等，意味着它不改变服务器状态，重复执行不会产生额外效果<sup>[2](https://wg.org/specs/rfc7231.html)</sup>。规范还对客户端施加了一项义务：发起 TRACE 请求时，禁止包含响应可能泄露的敏感信息头字段。<sup>[2](https://wg.org/specs/rfc7231.html)</sup>

## 跨站追踪

**攻击方式。** 2003 年 1 月，WhiteHat Security 创始人 Jeremiah Grossman（Jeremiah Grossman）在 Bugtraq 帖子及白皮书中公布了跨站追踪技术<sup>[4](https://www.cgisecurity.com/whitehat-mirror/WhitePaper_screen.pdf)</sup><sup> • </sup><sup>[7](https://groups.google.com/g/muc.lists.bugtraq/c/VcGZuARyZbA)</sup>。该机制利用了回显特性：当浏览器向某域的服务器发出 TRACE 请求时，会自动将该域的 Cookie 附加到请求头中，服务器则将其回显在响应主体中。页面中的脚本随后可以直接读取响应主体，从而获取 Cookie 字符串——尽管 HttpOnly 属性（微软引入的、旨在对 [JavaScript](https://www.edgepedia.org/zh/javascript) 隐藏 Cookie 的标志）阻止了通过 `document.cookie` 的访问<sup>[8](https://owasp.org/www-project-web-security-testing-guide/assets/archive/OWASP_Testing_Guide_v4.pdf)</sup>。Grossman 在 Black Hat 的演讲中还列出，XST 能够触及基本认证和 NTLM 凭据，而普通 XSS 则无法做到<sup>[9](https://blackhat.com/presentations/win-usa-03/bh-win-03-grossman/bh-win-03-grossman.pdf)</sup>。

**攻击的局限。** 浏览器通常只允许脚本连接到脚本所在的域，因此攻击者需要设法让受害者的浏览器向目标站点发送 TRACE。已记录的途径包括将 TRACE 与目标应用程序中的跨站脚本漏洞结合，或利用单独的浏览器跨域漏洞<sup>[8](https://owasp.org/www-project-web-security-testing-guide/assets/archive/OWASP_Testing_Guide_v4.pdf)</sup>。当时 Bugtraq 讨论串中的讨论还指出，所公布的技术依赖于通过 [Internet Explorer](https://www.edgepedia.org/zh/internet-explorer) 中的 XMLHTTP 控件读取 TRACE 响应，批评者将其定性为一种在 IE6 SP1 中绕过 HttpOnly 的手段，而非 TRACE 本身的缺陷<sup>[7](https://groups.google.com/g/muc.lists.bugtraq/c/VcGZuARyZbA)</sup>。

**当前暴露面。** 现代浏览器根据 Fetch 标准的限制拒绝发送 TRACE 请求，这阻断了基于浏览器的攻击路径<sup>[3](https://.dev/trace)</sup>。残余风险在于不受这些限制约束的非浏览器客户端和脚本，以及 TRACE 响应会回显整个请求——包括反向代理添加的认证头等敏感头<sup>[3](https://.dev/trace)</sup>。OWASP 指出，服务器上启用 TRACE 表明其未得到适当加固，即使在浏览器永远不会发送 TRACE 的情况下，自动化扫描工具仍可能将其标记出来<sup>[10](https://github.com/OWASP/wstg/blob/master/document/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management/06-HTTP_Methods.md)</sup>。

## 禁用 TRACE

OWASP 建议在所有生产服务器上禁用 TRACE，服务器级别的禁用仍然是对抗 XST 的主要防御手段<sup>[3](https://.dev/trace)</sup>。具体机制因服务器而异：

- **Apache httpd** 默认启用 TRACE。`TraceEnable` 指令是控制开关：`TraceEnable off` 使核心服务器和 mod_proxy 返回 405（方法不允许）错误，且 Apache 文档说明 TRACE 不受 `<Limit>` 和 `<LimitExcept>` 块的约束，因此该指令是关闭它的唯一方式<sup>[6](https://d.apache.org/docs/2.4/mod/core.html)</sup><sup> • </sup><sup>[3](https://.dev/trace)</sup>。不合规的 `TraceEnable extended` 模式仅用于测试和诊断，允许请求主体，当 Apache 作为源服务器时上限为 64 KB 加上 8 KB 的分块头<sup>[6](https://d.apache.org/docs/2.4/mod/core.html)</sup>。
- **IIS** 需要在请求筛选中从允许的方法里移除 TRACE 及相关联的 TRACK 方法<sup>[3](https://.dev/trace)</sup>。微软的 IIS 支持博客指出，安全扫描器可能在实际上测试 TRACE 时报告 TRACK 方法的使用，而在 IIS 7、8.5 和 10 上的测试表明这些方法的默认状态各不相同<sup>[11](https://techcommunity.microsoft.com/blog/iis-support-blog/http-track-and-trace-verbs/784482)</sup>。
- **nginx** 不实现 TRACE，因此无需任何配置操作<sup>[3](https://.dev/trace)</sup>。

Apache 的文档指出，启用 TRACE 并不会在 httpd 本身暴露安全漏洞；加固的价值在于切断 XST 攻击路径，而非修补服务器缺陷<sup>[3](https://.dev/trace)</sup>。

## 相关标准中的 TRACE

TRACE 被归类为安全方法，其副作用超出了方法本身。OWASP CSRF 防护速查表指出，默认的 `SameSite=Lax` Cookie 行为仍允许 Cookie 在使用安全方法的顶层导航中发送，并将 TRACE 与 GET、HEAD 和 OPTIONS 并列<sup>[12](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html)</sup>。这也是为何即使在浏览器限制已阻断大部分 TRACE 流量的情况下，方法级别的加固仍然重要。

## References

1. [RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1, RFC Editor](https://www.rfc-editor.org/info/rfc2616/)
2. [RFC 7231: HTTP/1.1 Semantics and Content, httpwg.org](https://wg.org/specs/rfc7231.html)
3. [TRACE - Expert Guide to HTTP methods, http.dev](https://.dev/trace)
4. [Jeremiah Grossman (2003). Cross-Site Tracing (XST) white paper, cgisecurity.com mirror](https://www.cgisecurity.com/whitehat-mirror/WhitePaper_screen.pdf)
5. [RFC 9110: HTTP Semantics, IETF Datatracker](https://datatracker.ietf.org/doc/rfc9110/)
6. [core - Apache HTTP Server Version 2.4 documentation](https://d.apache.org/docs/2.4/mod/core.html)
7. [TRACE used to increase the dangerous of XSS, Bugtraq thread, January 2003](https://groups.google.com/g/muc.lists.bugtraq/c/VcGZuARyZbA)
8. [OWASP Testing Guide v4, HTTP Methods testing (PDF)](https://owasp.org/www-project-web-security-testing-guide/assets/archive/OWASP_Testing_Guide_v4.pdf)
9. [Jeremiah Grossman, Cross-Site Tracing, Black Hat Windows USA 2003](https://blackhat.com/presentations/win-usa-03/bh-win-03-grossman/bh-win-03-grossman.pdf)
10. [OWASP Web Security Testing Guide, Test HTTP Methods (wstg repository)](https://github.com/OWASP/wstg/blob/master/document/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management/06-HTTP_Methods.md)
11. [HTTP TRACK and TRACE verbs, Microsoft IIS Support Blog](https://techcommunity.microsoft.com/blog/iis-support-blog/http-track-and-trace-verbs/784482)
12. [Cross-Site Request Forgery Prevention Cheat Sheet, OWASP Cheat Sheet Series](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › HTTP and web communication protocols

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/http-trace-method

English original: https://www.edgepedia.org/http-trace-method
