HTTP headers carry the metadata a browser and server exchange in both directions, and they let the browser know how to treat the content it receives. Response headers in particular are used to describe returned data, support security mechanisms, and convey operational and debugging information. Cloudflare's "HTTP Response Header Modification" action, now generally available within Transform Rules for all plan tiers, lets users set or remove response headers as traffic returns from Cloudflare to the client, replacing what previously required a Cloudflare Worker with a no-code configuration.

Where response headers matter

A common example is Content-Type, which tells the browser what content is being returned, such as text/html or image/png. The Server header carries information about the software that handled the request.

Beyond basic traffic handling, several security mechanisms are implemented as response headers: the Content Security Policy (CSP), Cross Origin Resource Sharing (CORS), and HTTP Strict Transport Security (HSTS). The goal of CSP is to mitigate and report cross-site scripting (XSS) attacks, where an injected malicious script runs inside a trusted page and can compromise a user's interactions, including the theft of sensitive data such as passwords. A CSP header names the domains the browser may treat as valid sources of executable scripts, and a compliant browser executes only scripts loaded from those domains.

On NGINX, the administrator sets the policy in the server configuration:

add_header Content-Security-Policy "default-src 'self';" always;

With Cloudflare Workers, the equivalent would be:

response.headers.set("Content-Security-Policy": "default-src 'self' example.com *.example.com",)

The browser detects the Content-Security-Policy header in the response and acts on the policy.

Why an in-path proxy

Ensuring these headers are present is usually the job of the reverse proxy sitting between client and origin, which enriches the response returned to the visitor. "HTTP Response Header Modification" moves that capability into Cloudflare, which is especially important when the origin is a SaaS provider or other third party over which the administrator has no full control. A single rule can modify up to ten response headers, using one of three options.

BLOG-795 Embedded Image - cmor6v

The three options

Set dynamic

Choose "Set dynamic" when the header value must be computed per response, for instance to add a Cloudflare Bot Management score or the visitor's country to each response.

BLOG-507 Embedded Image - Cp9kzR

These values derive from the corresponding HTTP request: the bot score in the response header is computed from the request, and ip.src.country is the visitor's country, not that of the origin that produced the response.

Set static

"Set static" fills a header with a literal string, which suits straightforward headers such as CORS or CSP policies.

BLOG-795 Embedded Image - ZFpVPJ

With either set option, an existing header of the same name is replaced: its prior value is removed and the new value is written in.

Remove

The remove option strips all response headers bearing the specified name. To ensure the Link response header never reaches the client, the rule looks like this:

BLOG-795 Embedded Image - 5eoqiO

Set dynamic rules can call Cloudflare functions: concat(), regex_replace(), to_string(), and lower(). A frequent pattern is joining different data types with concat() and to_string(); for example, concat("score=",to_string(cf.bot_management.score)) produces a header value like score=85. Regular expression functions are only available on Business and Enterprise plans.

Finer-grained policies

Typically, technologies such as CORS and CSP are applied to an entire site, or at best a directory. The filtering that comes with putting header modification inside Cloudflare changes that: administrators can key rules on the visitor's country of origin, bot score, user agent, requested filename or file extension, request method, and other fields. That makes scenarios such as a stricter CSP for verified bots than for unverified or low-score traffic possible within the rule configuration itself.

Header modification can be used to improve operations, strip sensitive data, and strengthen security, among many other use cases.