Using redirect: "error" in Cloudflare Workers fetch to prevent connecting to the redirect destination. Checked the fetch_redirect_error flag and compatibility date

Using redirect: "error" in Cloudflare Workers fetch to prevent connecting to the redirect destination. Checked the fetch_redirect_error flag and compatibility date

Cloudflare Workers' fetch() and Request now support redirect: "error". On the Free plan, I checked which statuses are rejected, how it differs from follow and manual, the flag and 2026-12-22 compatibility date, and local dev status.
2026.12.02

This page has been translated by machine translation. View original

Hello! I'm Yuji Nishimura from the Operations Department!

When you call an external API with an API key from Cloudflare Workers, if the other side returns a redirect, fetch() resends the request to the Location destination unless you specify otherwise. According to the Cloudflare Changelog of November 28, 2026, Workers' fetch() and Request now support redirect: "error", which lets you make the request fail as soon as a redirect is received.

I deployed a Worker to a Free plan account and checked which statuses are rejected, how this differs from follow and manual, the relationship between compatibility date 2026-12-22 and the flag, and the current state of local development.

What Changed

  • Date: November 28, 2026 (Cloudflare Changelog "Reject redirects in Workers fetch requests")
  • Key points of the change:
    • fetch() and Request can now use all three redirect modes of the Fetch standard (follow, manual, error)
    • With redirect: "error", receiving 301, 302, 303, 307, or 308 rejects with a TypeError and does not connect to the redirect destination. It rejects even when there is no Location header
    • Non-redirect 3xx statuses such as 300 and 304 are returned as normal responses
  • How to enable it: It is enabled by default if the compatibility date (compatibility_date) is 2026-12-22 or later. For earlier dates, add the compatibility flag fetch_redirect_error to compatibility_flags. If you want to disable it with a date of 2026-12-22 or later, use no_fetch_redirect_error
  • Implementation: PR #8973 "Support redirect: "error" behind a Fetch compatibility flag" in workerd (the Workers runtime, which wrangler dev also uses to run locally) (merged 2026-11-29 UTC)

The Changelog lists use cases such as preventing headers from being sent to a different destination in calls that carry things like API keys, stopping redirects after validating a user-specified URL (SSRF protection), and detecting unexpected redirects immediately.

Points Worth Noting

  • Scope of what becomes enabled by default: A fetch() without redirect stayed as follow even with the flag enabled (Verification 3). The official documentation also states that the default when passing a URL is follow. What the compatibility date changes is whether redirect: "error" is accepted; existing fetch() calls do not start failing on redirects
  • Compatibility date 2026-12-22: As of December 2, 2026 this is a future date, so deployment was rejected with Can't set compatibility date in the future (Verification 5). If you want to use it now, add the flag
  • Local development (wrangler dev): With wrangler 4.169.0, it failed to start with No such compatibility flag: fetch_redirect_error (Verification 5)
  • What arrives with follow: When redirects were followed, X-API-Key reached the redirect destination on a different host, and when POSTing with 307, the body arrived as well. Authorization was stripped by a separate flag that is enabled by default from compatibility date 2025-09-01 (Verification 3)

Trying It Out

Environment

  • Cloudflare Workers (Free plan). Deployed to workers.dev
  • wrangler 4.169.0 (Node.js v24.21.0, run from a Linux container)
  • compatibility_date: 2026-11-30
  • Verification date: December 2, 2026 (JST)

Verification Setup

The diagram is a conceptual figure I made myself, not an official architecture diagram. The caller Worker sends a request with dummy X-API-Key and Authorization headers to another Worker (the redirect source). The redirect source returns a redirect with the URL of yet another Worker (the redirect destination) in Location. For each redirect mode, I checked the result on the caller side and whether the request reached the redirect source and the redirect destination.

A conceptual diagram of the verification setup I made myself. The caller Worker sends a request to the redirect source Worker and receives a 3xx and Location. error rejects with a TypeError and does not connect to the redirect destination, manual returns the 302 response as is, and follow delivers X-API-Key to the redirect destination Worker given by Location (with 307, the POST body also arrives, and Authorization is stripped).

The three Workers were each deployed to a separate hostname (<name>.<subdomain>.workers.dev, such as blog-verify-redir-app). The redirect source and the redirect destination record the number of requests they received in Workers KV.

Here is the configuration of the caller Worker. fetch_redirect_error enables the error mode. global_fetch_strictly_public was added so that other Workers on the same workers.dev subdomain are called via the same route as requests from the internet. This is a setting tailored to this verification setup and is not needed if you just want to use redirect: "error".

app/wrangler.jsonc
{
  "name": "blog-verify-redir-app",
  "main": "src/index.js",
  "compatibility_date": "2026-11-30",
  "compatibility_flags": ["fetch_redirect_error", "global_fetch_strictly_public"],
  "workers_dev": true,
  "vars": { "ORIGIN": "https://blog-verify-redir-origin.<subdomain>.workers.dev" }
}

The caller Worker puts the mode received via the query into redirect, calls fetch(), and returns whether it resolved or rejected as JSON. This is a cleaned-up excerpt of just the /try handling.

app/src/index.js
const SENSITIVE_HEADERS = {
  "X-API-Key": "dummy-api-key",
  Authorization: "Bearer dummy-token",
};

async function attempt(input, init) {
  try {
    const res = await fetch(input, init);
    return { result: "resolved", status: res.status, location: res.headers.get("location"), body: (await res.text()).slice(0, 300) };
  } catch (e) {
    return { result: "rejected", name: e.name, message: e.message };
  }
}

export default {
  async fetch(request, env) {
    // Call it like /try?mode=error&status=302. When mode=default, redirect is not specified
    const url = new URL(request.url);
    const id = url.searchParams.get("id") ?? "x";
    const status = url.searchParams.get("status") ?? "302";
    const noloc = url.searchParams.has("noloc") ? "&noloc=1" : ""; // Make it return a redirect without Location
    const method = url.searchParams.get("method") ?? "GET";
    const mode = url.searchParams.get("mode");

    const init = { method, headers: SENSITIVE_HEADERS };
    if (method === "POST") init.body = "grant_type=client_credentials";
    if (mode !== "default") init.redirect = mode;

    const target = `${env.ORIGIN}/redirect/${status}?id=${id}${noloc}`;
    return Response.json({ id, mode, status, method, ...(await attempt(target, init)) });
  },
};

The redirect source Worker just returns the status specified by the path and a Location (main part).

origin/src/index.js
const m = url.pathname.match(/^\/redirect\/(\d{3})$/);
const headers = {};
if (!url.searchParams.has("noloc")) headers.Location = `${env.DEST}/landing?id=${id}`;
return new Response(null, { status: Number(m[1]), headers });

1. Without the flag, redirect: "error" throws an exception

First I deployed without fetch_redirect_error and specified redirect: "error". new Request(url, { redirect: "error" }) threw an exception, and fetch(url, { redirect: "error" }) rejected. Both had the same message.

{"id":"N302","mode":"error","status":"302","method":"GET","result":"rejected","name":"TypeError","message":"Invalid redirect value. Use \"follow\" or \"manual\". To use \"error\", enable the fetch_redirect_error compatibility flag."}

At this point, the number of requests that reached the redirect source was 0 for both fetch() and new Request(), so it failed before sending the request.

Until now, the runtime did not accept error. Running the same code in the local development environment of wrangler 4.169.0 (workerd 1.20261127.1) produced the following message. The runtime on the deployment side has changed to a message that tells you to add the flag.

Invalid redirect value, must be one of "follow" or "manual" ("error" won't be implemented since it does not make sense at the edge; use "manual" and check the response status code).

2. With the flag, it rejects based on the redirect status

I redeployed with fetch_redirect_error and called with redirect: "error" while changing the status returned by the redirect source.

Redirect source response Result
301 / 302 / 303 / 307 / 308 (with Location) reject (TypeError)
302 (without Location) reject (TypeError)
307, POST with body reject (TypeError)
300 (with Location) resolve (status 300)
304 resolve (status 304)

The message on rejection was the same for every status.

{"id":"E302","mode":"error","status":"302","method":"GET","result":"rejected","name":"TypeError","message":"Received a redirect response with redirect mode set to \"error\"."}

In the cases where I counted arrivals with Workers KV (302, 308, 302 without Location, and POST 307), in every case the request reached the redirect source once and did not reach the redirect destination (0 times). The first request is sent, and it stops at the point the redirect response is received.

300 and 304 were not rejected even when returned with Location, and I could read the status and Location. This matches the compatibility flag documentation: "Non-redirect 3xx status codes, such as 300 and 304, resolve normally".

3. With follow, X-API-Key reaches the redirect destination, and manual returns the 302

I called the redirect source that returns 302 while changing the mode (all with fetch_redirect_error enabled). The image shows the caller Worker trying error, manual, and follow in that order, with the responses summarized using curl and jq (the subdomain is hidden).

The result of having the caller Worker try the same 302 in the order error, manual, follow. error shows rejected and the TypeError message, manual shows 302 and Location, and follow returned 200 from the redirect destination, with X-API-Key arriving and Authorization being null.

Here is a summary together with the number of arrivals at the redirect source and destination (values counted with Workers KV).

Mode Result on caller side Redirect source Redirect destination Headers received by redirect destination
error reject (TypeError) 1 time 0 times -
manual resolve (status 302, Location readable) 1 time 0 times -
follow resolve (200 from redirect destination) 1 time 1 time X-API-Key: dummy-api-key, no Authorization
Not specified resolve (200 from redirect destination) 1 time 1 time Same as follow

With follow, X-API-Key: dummy-api-key reached the redirect destination (a Worker on a different hostname) as is. In the case where I POSTed with a body and had 307 returned, the body grant_type=client_credentials also arrived. In the Fetch standard, a POST with 301/302 and 303 changes the method to GET and drops the body, so I verified body handling only with 307.

Authorization was null and did not reach the redirect destination. This is the behavior of the Workers compatibility flag strip_authorization_on_cross_origin_redirect, which is enabled by default from compatibility date 2025-09-01 (this time 2026-11-30). According to the Request documentation, for Workers with an earlier compatibility date, Workers with retain_authorization_on_cross_origin_redirect, and redirects to the same origin, Authorization is also forwarded. It states that custom headers such as X-API-Key and Cookie are forwarded even when the origin changes. If you let redirects be followed with an API that takes its key in a header, the key is sent to a different host.

With manual, the 302 response was returned as is and I could read Location. The Redirect handling section says that while browsers return opaqueredirect (status 0, no headers) for manual, Workers returns the actual status and headers.

4. An error held by a Request is carried over to copies, and can be overridden by fetch()'s second argument

Passing a Request created with new Request(url, { headers, redirect: "error" }) to fetch(req) also rejected with the same message.

What I checked Result
req.redirect "error"
redirect of a Request copied with new Request(req) "error" (carried over)
fetch(req) reject (TypeError)
fetch(req, { redirect: "follow" }) resolve (200 from redirect destination)
request.redirect received by the fetch() handler "manual"
new Request("https://example.com/").redirect "follow"

The redirect specified in fetch()'s second argument took precedence over the error on the Request. Also, the request received by the fetch() handler is manual. Redirect handling states that if you pass the received request as is to fetch(request), redirects returned by the origin are not followed and are returned as is (I did not try the proxy behavior).

5. Compatibility date 2026-12-22 cannot be written yet, and wrangler dev does not support the flag

When I set compatibility_date to 2026-12-22 instead of using the flag and deployed, the API rejected it.

[ERROR] A request to the Cloudflare API (/accounts/<account-id>/workers/scripts/blog-verify-redir-app) failed.
  Can't set compatibility date in the future: 2026-12-22
   [code: 10021]

Until 2026-12-22 arrives, you have to enable it with fetch_redirect_error rather than the compatibility date.

With the local development wrangler dev (wrangler 4.169.0, workerd 1.20261127.1), it failed to start when the flag was added.

[ERROR] service core:user:local-check: No such compatibility flag: fetch_redirect_error
[ERROR] The Workers runtime failed to start. There was likely a problem with the workerd binary or your configuration.

Removing the flag let it start, but redirect: "error" threw an exception with the old message from Verification 1. Setting the compatibility date to 2026-12-22 also failed to start with Compatibility date "2026-12-22" is in the future and unsupported.

PR #8973 was merged at 22:41 (UTC) on 2026-11-29. The latest workerd published on npm as of December 2, 2026, 1.20261129.1, also does not include this change (confirmed by comparing the tag and the merge commit on GitHub). The latest wrangler 4.170.0 also uses workerd 1.20261129.1, so the same situation is likely (I did not try starting 4.170.0). Since it already works on the deployed runtime, trying it locally will probably have to wait until a workerd and wrangler containing this change are published. Since I verified on the deployed side this time, I did not try the approach of using --remote locally.

Discussion of the Verification Results

Choosing between error and manual

Scenario Suitable mode Reason
Calls carrying API keys or credentials, OAuth token exchange error It fails without connecting to the redirect destination, so headers and body are not sent to a different host
Validating a user-specified URL before fetching it (webhook destinations, link previews, etc.) error Redirects to unvalidated hosts are not followed
Following redirects while checking the destination against an allowlist / returning redirects to the client as is manual You can read Location and the status and decide yourself
Calls that assume redirects, such as fetching public web pages follow (not specified) Same as before

Even with manual, it does not connect to the redirect destination unless you follow Location in your code. However, with manual the redirect response comes back as a normal return value, so if you forget to check the status you will treat it as a success. With error, an exception occurs the moment a redirect is received, so this kind of oversight cannot happen. The official documentation also recommends error when you want to reject redirects with the same code in browsers and Workers.

Operational notes

  • By my own reading, the impact on existing code depends on whether you already write redirect: "error". An error that used to throw an exception will start working once you raise the compatibility date to 2026-12-22 or later. If a shared library written for browsers specifies error, its behavior will change in that part. Conversely, for calls where you want to reject redirects, you need to write redirect: "error" yourself
  • The exception when rejecting with error was a TypeError, with the message Received a redirect response with redirect mode set to "error".. If you want to handle redirect failures separately from other failures, it looks like you would distinguish them by this message (I did not check what type a network error takes on the deployed side)

What I did not verify

  • The behavior where error is enabled without the flag when the compatibility date is 2026-12-22 or later, and the behavior of disabling it with no_fetch_redirect_error (because the date has not arrived and deployment is not possible). When you raise the compatibility date, you will need to remove the flag and check again
  • How the Cookie header is handled with follow. The official documentation says it is forwarded even to a different origin, so if you call APIs that authenticate with cookies, error is worth considering as well
  • How Authorization is handled on redirects to the same origin (this time only redirects to a different host). The official documentation says it is forwarded
  • How the body is handled when POSTing with 301, 302, and 303 (I confirmed that the body arrives only with 307)
  • The behavior when passing a Request with redirect: "error" over RPC (such as a Service binding) to a Worker without the flag. The official documentation says it becomes a TypeError on the receiving side
  • Behavior with a Worker that has a custom domain attached, or on a Paid plan. The results this time are from a Free plan workers.dev setup with global_fetch_strictly_public added

Summary

redirect: "error" is now usable in Cloudflare Workers' fetch() and Request. Any of 301, 302, 303, 307, and 308 results in a TypeError, and in the cases where I counted arrivals, such as 302 and 308, I confirmed that the request does not reach the redirect destination. With follow left as is, X-API-Key reached the redirect destination on a different host, and with 307 the POST body arrived as well. If you specify error in processing that calls external services with an API key, it fails the moment a redirect is received, and the key and body are not sent to the redirect destination. If you want to judge the redirect destination yourself before following it, use manual, which returns the 302 and Location as is.

What the compatibility date changes is whether error can be specified; a fetch() without redirect stays as follow. As of December 2, 2026, 2026-12-22 cannot be written as the compatibility date, so if you want to use it now, add fetch_redirect_error to compatibility_flags. The local wrangler dev does not yet know this flag, so verification has to be done on the deployed side.

I hope this is helpful to someone.


Reference links:


セキュリティ対策もお任せください

セキュリティ相談会では、御社の環境に合わせて、どこを確認し、どの対策から進めるかを一緒に考えます。「今の設定で大丈夫か、少し気になる」くらいの段階から、喫緊の課題までオンラインで無料でご相談いただけます。ぜひご活用ください。

セキュリティ相談会

セキュリティ相談会の詳細を見る

Share this article