Workersのfetchでリダイレクトを拒否可能に
原題: Reject redirects in Workers fetch requests
changelog / Compute / Workers / 2026/11/28 09:00 JST
Cloudflare Workersのfetch()とRequestで、WHATWG Fetch標準のredirectモードerrorが使えるようになった。これでfollow・manualに続き3モードが揃った。redirectにerrorを指定すると、サーバーが301/302/303/307/308を返した際にリダイレクト先へ接続せずTypeErrorで失敗する。Locationヘッダーが無くても拒否され、304など非リダイレクトの3xxは通常通り処理される。OAuthのトークン交換や署名付きWebhookなど機密ヘッダーを伴う通信で、リダイレクトによる別ホストへのヘッダー流出を防げるほか、ユーザー指定URLの検証後にSSRF的なリダイレクト先への遷移を遮断したり、想定外のリダイレクトを早期に検出してAPI契約の逸脱を捕捉したりできる。認証情報を扱うWorkerでは、失敗時に閉じる設計の選択肢が増えた。
出典サイトで原文を開く → 出典サイトの説明: Support for the standard Fetch redirect "error" mode lets Workers fail closed on redirects without contacting the redirect destination.
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 . 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. Use redirect: “error” to: 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
出典: 公式ページ