Skip to content
News

AI insights

Cloudflare Workers Adds fetch() redirect: "error" for SSRF Defense

Cloudflare Workers now supports redirect: "error" in fetch() and Request as of Nov 28, 2026. Learn how it blocks 301-308 redirects and how to configure fetch_redirect_error.

Cloudflare Workers Adds fetch() redirect: "error" for SSRF Defense

Q. Can Cloudflare Workers block fetch() from following redirects and fail immediately? Yes. With redirect: "error" announced on November 28, 2026, fetch() immediately rejects with a TypeError upon receiving a 301, 302, 303, 307, or 308 response without contacting the redirect target. It is enabled by default starting with the 2026-12-22 compatibility date, and earlier Workers can opt in via the fetch_redirect_error compatibility flag.

⚠️ Accuracy guardrail: This article reflects the official Cloudflare Workers Changelog (November 28, 2026) and the Request and Compatibility Flags documentation reviewed on December 1, 2026. Compatibility date defaults or runtime details may change in future documentation updates, and latency or CPU impact benchmarks in production are not covered because Cloudflare did not publish figures.

30-second summary

  • Cloudflare Workers now supports redirect: "error" in fetch() and Request, completing all three redirect modes in the WHATWG Fetch Standard (follow, manual, and error).
  • Whenever an origin returns 301, 302, 303, 307, or 308, fetch() rejects with a TypeError without connecting to the redirect destination—even if there is no Location header. Non-redirect 3xx codes such as 304 Not Modified and 300 still resolve normally.
  • This prevents credential leaks on OAuth token exchanges, signed webhooks, and Authorization or X-API-Key requests, while blocking SSRF redirect hops.
  • Workers with a compatibility date of 2026-12-22 or later enable redirect: "error" by default; earlier dates require adding fetch_redirect_error to wrangler.jsonc or wrangler.toml.

Why redirect: "error" matters

In serverless functions that call third-party APIs or user-supplied URLs, a single unexpected redirect can turn into a security incident or silent failure. Previously, passing a URL string to fetch() defaulted to follow, while manual forced developers to inspect every 3xx response by hand.

Cloudflare official Changelog announcement

According to the November 28, 2026 Cloudflare Changelog, redirect: "error" is built for four practical scenarios:

  • Protect credentials and secrets: Fail closed on OAuth token exchanges, signed webhooks, and API calls carrying Cookie, X-API-Key, or Authorization so redirects never forward sensitive headers to another destination.
  • Prevent SSRF redirect hops: Even after validating a user-supplied URL, block redirects to unvalidated internal or external hosts across 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, wasting subrequests, and failing later during response parsing.
  • Share code across runtimes: Use the exact same redirect: "error" option in both browser code and Workers without maintaining separate wrappers.

Advertisement

Comparing follow, manual, and error modes

Workers fetch() and the Request constructor now implement all three modes defined by the WHATWG Fetch Standard. Passing a URL string defaults to follow, while incoming Request objects default to manual. Passing an existing Request preserves its redirect mode unless explicitly overridden.

Comparison of the three Cloudflare Workers fetch redirect modes

Mode Redirects (301·302·303·307·308) Non-redirect 3xx (304 Not Modified, etc.) Primary use case
follow Automatically follows Location (default for URLs) Resolves normally Trusted public web pages and CDN assets
manual Returns the raw 3xx response (default for incoming Request) Resolves normally Proxies and custom Location inspection
error Immediately rejects with TypeError without connecting (even without Location) Resolves normally OAuth token exchange, webhooks, SSRF defense, strict API calls

Notably, even a malformed redirect response lacking a Location header triggers a TypeError under redirect: "error" if the status is 301, 302, 303, 307, or 308. Meanwhile, cache validation responses like 304 Not Modified or 300 are not treated as redirects and resolve normally.

Implementation and the 2026-12-22 compatibility date

Before adding redirect: "error", check your Worker's compatibility_date. Projects on 2026-12-22 or later have it enabled by default, whereas earlier projects must add the fetch_redirect_error compatibility flag.

export default {
  async fetch(request: Request, env: { API_KEY: string }): 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 },
      );
    }
  },
};

If your Worker uses a compatibility date prior to 2026-12-22, add fetch_redirect_error to wrangler.jsonc or wrangler.toml. Conversely, if you use 2026-12-22 or later and need to opt out, set no_fetch_redirect_error.

// wrangler.jsonc
{
  "$schema": "./node_modules/wrangler/config-schema.json",
  "compatibility_flags": [
    "fetch_redirect_error"
  ]
}

Edge cases and RPC caveats before rollout

If redirect: "error" is used without the compatibility flag enabled, it fails immediately before any network call is made. Per the official Request docs, new Request(url, { redirect: "error" }) throws a TypeError on construction, and fetch(url, { redirect: "error" }) rejects before sending a request.

Solo developer desk working on Workers API security at night

In multi-Worker architectures using Service Bindings (RPC), mismatched compatibility settings can also break calls: passing a Request with redirect: "error" over RPC to a Worker that does not have fetch_redirect_error enabled throws a TypeError during deserialization.

If you are also automating Cloudflare workflows or agent discovery, check out our recent coverage on activating Cloudflare subscriptions via CLI and MCP and Cloudflare Search for Agents.

Toris's take

If you run feed fetchers, link-preview generators, webhook dispatchers, or backend Workers that attach API keys and OAuth tokens, enable fetch_redirect_error now and default sensitive outbound calls to redirect: "error". One option line and a try/catch block cleanly shut down redirect-hop SSRF and accidental credential forwarding.

For crawlers or short-URL resolvers that intentionally follow 301/302 responses, keep follow or switch to manual with explicit host allowlist checks. And if you pass Request objects across Workers via RPC, make sure both caller and callee share the fetch_redirect_error flag or a 2026-12-22+ compatibility date.

Sources

Advertisement