Cloudflare Workers 新增 fetch redirect: error 模式
Workers - Reject redirects in Workers fetch requests
涉及 Cloudflare Workers 核心网络请求行为的默认变更,2026-12-22 后生效。所有依赖 fetch 处理重定向的存量服务必须检查兼容性日期,否则可能因静默改变重定向逻辑导致安全漏洞或业务异常。
Cloudflare Workers now supports redirect: "error" in fetch() and Request, completing support for all three redirect modes defined by the WHATWG Fetch Standard ↗︎: follow, manual, and error.
Cloudflare Workers 现在支持在 fetch() 和 Request 中使用 redirect: "error",从而完整支持 WHATWG Fetch Standard ↗︎ 定义的所有三种重定向模式:follow、manual 和 error。
When the server responds with a redirect status (301, 302, 303, 307, or 308), fetch() with redirect: "error" rejects with a TypeError without contacting the redirect destination. It rejects even if the response has no Location header. Non-redirect 3xx responses, such as 304 Not Modified, resolve normally.
当服务器响应重定向状态码(301、302、303、307 或 308)时,使用 redirect: "error" 的 fetch() 会抛出 TypeError 而不会联系重定向目标。即使响应中没有 Location 头,它也会拒绝。非重定向的 3xx 响应(如 304 Not Modified)则正常解析。
Use redirect: "error" to:
使用 redirect: "error" 可以:
- Protect credentials and secrets: Fail closed on OAuth token exchanges, signed webhooks, and API calls carrying sensitive headers (such as Cookie, X-API-Key, or Authorization) so redirects cannot forward headers to another destination.
- Prevent server-side request forgery (SSRF) redirect hops: After you validate a user-supplied URL, block redirects to unvalidated hosts. This applies to webhook targets, link previews, and feed fetchers.
- Enforce strict API contracts: Catch unexpected redirects (such as an upstream API redirecting to an HTML sign-in page) immediately instead of following them, consuming extra subrequests, and failing later during response parsing.
- Share code across runtimes: Use the same redirect: "error" option in Workers and browsers when your code needs to reject redirects.
- 保护凭据和密钥:在 OAuth 令牌交换、签名 Webhook 以及携带敏感头(如 Cookie、X-API-Key 或 Authorization)的 API 调用中失败并关闭,防止重定向将头转发到其他目的地。
- 防止服务器端请求伪造 (SSRF) 重定向跳转:在验证用户提供的 URL 后,阻止对未验证主机的重定向。这适用于 Webhook 目标、链接预览和 Feed 抓取器。
- 强制执行严格的 API 契约:立即捕获意外重定向(例如上游 API 重定向到 HTML 登录页面),而不是跟随它们、消耗额外的子请求并在后续响应解析过程中失败。
- 跨运行时共享代码:当代码需要拒绝重定向时,在 Workers 和浏览器中使用相同的 redirect: "error" 选项。
src/index.jsjs
export default {
async fetch(request, env) {
try {
const response = await fetch("https://api.example.com/v1/token", {
method: "POST",
headers: {
"X-API-Key": env.API_KEY,
},
redirect: "error",
});
return response;
} catch {
return new Response(
"Upstream request failed or returned an unexpected redirect",
{
status: 502,
},
);
}
},
};src/index.tsts
export default {
async fetch(request, env): Promise<Response> {
try {
const response = await fetch("https://api.example.com/v1/token", {
method: "POST",
headers: {
"X-API-Key": env.API_KEY,
},
redirect: "error",
});
return response;
} catch {
return new Response(
"Upstream request failed or returned an unexpected redirect",
{
status: 502,
},
);
}
},
} satisfies ExportedHandler<{ API_KEY: string }>;This mode is enabled by default for Workers with a compatibility date of 2026-12-22 or later. To opt in on an earlier compatibility date, add the fetch_redirect_error compatibility flag:
对于兼容性日期为 2026-12-22 或之后的 Workers,默认启用此模式。要在较早的兼容性日期下选择加入,请添加 fetch_redirect_error 兼容性标志:
{
"$schema": "./node_modules/wrangler/config-schema.json",
"compatibility_flags": [
"fetch_redirect_error"
]
}compatibility_flags = ["fetch_redirect_error"]For compatibility dates on or after 2026-12-22, add no_fetch_redirect_error to opt out. If you pass a URL to fetch() or new Request(), the default is follow. If you pass an existing Request, both use its redirect mode unless you override it. Incoming requests use manual.
对于 2026-12-22 或之后的兼容性日期,添加 no_fetch_redirect_error 以选择退出。如果向 fetch() 或 new Request() 传递 URL,默认值为 follow。如果传递现有的 Request 对象,两者均使用其重定向模式,除非你进行覆盖。传入的请求使用 manual。
For more information, refer to Redirect handling and the fetch_redirect_error compatibility flag.
更多信息,请参阅重定向处理和 fetch_redirect_error 兼容性标志。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力