Security Headers: How CSP, HSTS and Framing Controls Work — Security & GDPR article by Rui Codex

Security headers are HTTP response headers that tell the browser which content it may load, whether it must use HTTPS, and whether other sites may frame your pages. The three that matter most for business web applications are Content-Security-Policy, Strict-Transport-Security and frame-ancestors or X-Frame-Options. Roll them out in report-only or short-lived form first, then tighten them.

Key takeaways

  • Security headers restrict what the browser allows, but they do not replace secure code.
  • A Content Security Policy should be tested in report-only mode before it is enforced.
  • HSTS should start with a short max-age, because a mistake can lock users out until the value expires.
  • Framing should be blocked on login, admin and checkout pages unless a partner genuinely needs to embed them.

What do security headers actually do?

Security headers are instructions your server sends with each response, and the browser enforces them on the page. They do not fix insecure code. They limit what happens when a script is injected, when a connection is downgraded, or when your page is embedded somewhere it should not be.

The behaviour is defined in public standards. The Content-Security-Policy header reference on MDN explains the directives, and the CSP Level 3 specification at W3C defines how browsers evaluate them. For HSTS, the rules are in RFC 6797.

Most business applications need a small set of headers set consistently on every response, including error pages and static files. A reverse proxy or CDN is often the cleanest place to do this, which is part of the work covered by our cloud and DevOps services.

How does a Content Security Policy limit scripts?

A Content Security Policy tells the browser which origins may supply scripts, styles, images, frames and other resources, and blocks anything outside that list. The main protection is against injected script: even if an attacker places a script tag in your page, the browser refuses to run it when its source is not allowed.

A restrictive starting point for an internal application looks like this: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'. Each directive narrows one category, and object-src 'none' closes older plugin routes that rarely serve a business application today.

Where a CSP fails

  • Inline script blocks and inline event handlers stop working unless you move them into files or use nonces.
  • Tag managers, chat widgets and analytics often load further scripts at runtime, so the allowlist grows over time.
  • Nonces and hashes require the server or build step to generate fresh values, which adds work to server-rendered pages.
  • A policy copied from a template without testing can break payment forms, login flows and file uploads at once.
Tip: Inventory every third-party script before you write the policy. Most CSP breakage comes from a tool that nobody remembered was on the page.

Is HSTS safe to switch on right away?

Not usually. HSTS tells the browser to use HTTPS for your domain for the duration of the max-age value, so a certificate problem or a forgotten subdomain can block access until that period ends. The header is defined in RFC 6797, and browser behaviour is summarised in the MDN Strict-Transport-Security reference.

The includeSubDomains option applies the rule to every subdomain, including old test servers or internal tools that may not have valid certificates. The preload option asks for the domain to be built into browser lists, which is hard to reverse, so it should only follow a long period of clean operation.

  • Confirm that HTTPS works on every hostname under the domain before you set the header.
  • Start with a short max-age such as 300 seconds and check logs for errors.
  • Raise the value in steps, and only add includeSubDomains once each subdomain has been verified.
  • Keep a note of the date you set each value, so you know when a rollback would take effect.

When should you block framing of your application?

Block framing when no legitimate site needs to embed your pages, which is true for most login, admin and checkout screens. Framing is the mechanism behind clickjacking, where a hidden frame sits over a decoy page so that a user clicks a control they did not intend to press.

Two headers control this. The modern option is the frame-ancestors directive in the Content-Security-Policy header, described in the MDN X-Frame-Options reference, which also notes that frame-ancestors takes precedence over the older header. The older option is X-Frame-Options, specified in RFC 7034, which accepts DENY or SAMEORIGIN.

Setting both is a reasonable approach for older browsers. The main failure is a legitimate embed: partner portals, payment provider iframes, and in-app previews all stop working when framing is blocked for the wrong pages. Choose the value per route rather than site-wide where the application mixes public and internal pages.

How do you roll out headers without breaking the app?

Deploy each header in report-only or low max-age form, collect violations for a full release cycle, and then tighten the policy in steps. This sequence turns a risky change into a series of small, reversible ones. The OWASP HTTP headers cheat sheet is a practical reference for the recommended values.

  1. List every external domain your pages load for scripts, styles, fonts, images and frames.
  2. Deploy Content-Security-Policy-Report-Only with the policy you intend to enforce, and route reports to a log you read.
  3. Fix or allowlist each violation, and move inline scripts into files where practical.
  4. Switch the policy to enforcement on a staging copy, then production, keeping the previous configuration ready.
  5. Set HSTS with a short max-age, verify HTTPS on all hostnames, and raise the value gradually.
  6. Add frame-ancestors to each route according to whether it may be embedded.
  7. Test the headers on every response type, including redirects, errors and downloads.

Where do these headers fail?

Each header protects against one class of problem and creates a different maintenance cost. The table compares them on qualitative criteria, since the exact effect depends on your application and the browsers your users run.

HeaderProtects againstMain costTypical failure
Content-Security-PolicyInjected and unauthorised scriptsMaintaining the allowlist as third-party tools changeBlocked widgets and broken inline code
Strict-Transport-SecurityDowngrade to plain HTTPHard to undo once a long max-age is cachedLockout on a host with a broken certificate
frame-ancestors or X-Frame-OptionsClickjacking through embedded framesDeciding which partners may embed which routesBlocked partner iframes or payment flows

No header replaces input validation, output encoding, authentication or patching. Treat them as a second line of defence that limits damage when another control has failed. Our baseline for securing cloud infrastructure describes the wider layers they sit within.

Hypothetical examples of a header rollout

Here are two hypothetical scenarios:

In the first, an internal operations portal loads all scripts from its own origin, but a team adds a chat widget that loads from a third-party host. The team deploys the policy in report-only mode, sees the widget flagged in the reports, and decides whether to allowlist that host or remove the widget before enforcement.

In the second, a customer-facing shop embeds a payment provider's iframe on its checkout page. Setting frame-ancestors to none across the whole site would break that embed, so the team sets it to none on admin pages and allows the provider on checkout only after confirming the provider's domain.

Frequently Asked Questions

Does a Content Security Policy stop all cross-site scripting?

No. A Content Security Policy reduces what injected script can do, but it does not remove the underlying flaw. Input handling and output encoding still need to be correct, and the policy works best as an additional layer on top of them.

Should I enable HSTS preload for my business domain?

Only after HSTS has run for a long period without errors on every hostname under the domain. Preload is difficult to reverse, so it suits domains where all subdomains are permanently served over HTTPS and the team can keep that promise over time.

Is X-Frame-Options still needed if I use frame-ancestors?

It is useful for older browsers that do not support frame-ancestors. Sending both headers with matching intent is a common approach, and modern browsers apply frame-ancestors when both are present.

Do security headers replace secure coding practices?

They do not. Headers limit the impact of a flaw but cannot find or fix it, so code review, testing, dependency updates and access control remain essential. Treat headers as one control in a layered setup.

Where should these headers be configured?

Set them at the layer that serves every response, such as a reverse proxy, load balancer or CDN, or in the application framework if no earlier layer is available. Check that errors, redirects and static files carry the same headers, because gaps often appear there.

Conclusion

Security headers are a practical way to limit script injection, protocol downgrade and clickjacking without rewriting an application. The safe path is to measure first, enforce in steps, and document each value so that a future change does not undo the work. If you want help designing a rollout for an existing application, request a project consultation with Rui Codex, or email info@ruicodex.com to discuss your project. Our web development services cover the application side of this work.

Sources

Tags: security headers Content Security Policy HSTS clickjacking X-Frame-Options web security web application security HTTP headers

Need Help Implementing This?

Our team can help you put these insights into practice. From AI automation to custom software development, we build solutions that deliver real results.

Send a project request