Articles

How to add security headers (HSTS, CSP) without changing your code

Add a Content-Security-Policy, X-Frame-Options or any other security header to every response with one edge rule, on top of HSTS defaults. Compared across PaaS.

Updated

Gregale already sends Strict-Transport-Security, X-Content-Type-Options: nosniff and a Referrer-Policy for every app that doesn't set its own. One edge rule adds a Content-Security-Policy, X-Frame-Options or any other header to every response, without changing your code. Your app's own values always win over the defaults. Security headers and edge rules are in beta on every plan, Free included.

What you get without doing anything

When your app sets none of these headers, Gregale adds:

HeaderOn your app's own addressOn your custom domain
Strict-Transport-Securitymax-age=31536000; includeSubDomainsmax-age=31536000
X-Content-Type-Optionsnosniffnosniff
Referrer-Policystrict-origin-when-cross-originstrict-origin-when-cross-origin
X-Frame-Optionsnonenone
Permissions-Policynonenone

When your app sets one of them itself, Gregale sends your value unchanged.

Add a Content-Security-Policy and the rest

bash
gregale edge rule create --app my-app --kind headers --match-host @app --match-path '/**' \
  --headers-response-set "Content-Security-Policy:default-src 'self'; frame-ancestors 'none'" \
  --headers-response-set 'X-Frame-Options:DENY' \
  --headers-response-set 'Permissions-Policy:camera=(), microphone=(), geolocation=()' \
  --headers-response-remove Server
  • --match-host @app covers every host the app serves, custom domains included, even ones you add later.
  • --match-path '/**' covers every path. Without it, a rule matches only the root path /.
  • set replaces the header, so the rule's value wins over one your app sends; --headers-response-add appends a value and --headers-response-remove deletes the header.

Check the result:

bash
curl -sI https://my-app.example.com/ | grep -i -E 'strict-transport|content-security|x-frame|permissions'

Roll out a CSP without breaking the site

A strict policy can block your own scripts. Send it in report-only mode first: the browser reports violations in its console and blocks nothing.

bash
gregale edge rule create --app my-app --kind headers --match-host @app --match-path '/**' \
  --headers-response-set "Content-Security-Policy-Report-Only:default-src 'self'"

When the console stays clean, switch the header name to Content-Security-Policy.

HSTS on a custom domain

On a custom domain, the default HSTS header does not include subdomains, because Gregale does not serve your other subdomains. Once every subdomain serves HTTPS, add them yourself:

bash
gregale edge rule create --app my-app --kind headers --match-host @app --match-path '/**' \
  --headers-response-set 'Strict-Transport-Security:max-age=63072000; includeSubDomains; preload'

Browsers remember HSTS for the whole max-age, so add preload only when you are sure.

How other platforms compare

PlatformSecurity headers on a backend without changing its code
GregaleHSTS, nosniff and Referrer-Policy by default; any other header with one edge rule, on every path and host
VercelA headers list in vercel.json sets response headers by path
RenderCustom header rules are a static-site feature; a web service sets its own headers
RailwayNo header setting; a Railway moderator says you would code your application to send those headers
Fly.iofly.toml can add or remove fixed response headers

Checked against each vendor's public documentation and forum answers on November 27, 2026: Vercel project configuration, Render static site headers, Railway: adding custom HTTP headers, Fly.io response header modification.

Pricing

Security headers cost nothing extra. A headers rule counts toward your plan's edge rules per app: 5 on Free, 25 on Hobby ($10 a month), 100 on Pro ($30) and 500 on Scale ($99). See Security headers on your app, Edge rules and Gregale pricing.

Questions

Which security headers does Gregale add by default?

When your app sets none of them: Strict-Transport-Security, X-Content-Type-Options: nosniff and Referrer-Policy: strict-origin-when-cross-origin. X-Frame-Options and Permissions-Policy are not added by default.

How do I add a Content-Security-Policy without changing my code?

Create a headers edge rule: gregale edge rule create --app my-app --kind headers --match-host @app --match-path /** --headers-response-set "Content-Security-Policy:default-src 'self'". It applies to every response on every host the app serves.

Why does my header rule apply only to the home page?

A rule created without --match-path matches only the root path /. Use --match-path /** to cover every path.

Does my app’s own header win over Gregale’s?

Over the defaults, yes: when your app sets one of those headers, Gregale sends your value unchanged. A headers rule that sets a header replaces it, so the rule’s value wins over your app’s.