--- url: "https://web.dev/articles/security-headers" titulek: "Security headers quick reference | Articles | web.dev" h1: "Security headers quick reference Stay organized with collections Save and categorize content based on your preferences." popis: "This article lists the most important security headers you can use to protect your website. Use it to understand web-based security features, learn how to implement them on your website, and as a reference for when you need a reminder." jazyk: "en" kanonicka: "https://web.dev/articles/security-headers" drobecky: ["Articles", "Security headers quick reference"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 01:49:46" znaku: 29831 slov: 4094 kodovani: "iso-8859-2" sha256: "005cc4d4991e956fad83c6ebe08be02228e9e61b43e033b31ed0ec2561640cbc" --- Security headers quick reference Stay organized with collections Save and categorize content based on your preferences. Learn more about headers that can keep your site safe and quickly look up the most important details. Eiji Kitamura Artur Janc Maud Nalpas This article lists the most important security headers you can use to protect your website. Use it to understand web-based security features, learn how to implement them on your website, and as a reference for when you need a reminder. Security headers recommended for websites that handle sensitive user data: Content Security Policy (CSP) Trusted Types Security headers recommended for all websites: X-Content-Type-Options X-Frame-Options Cross-Origin Resource Policy (CORP) Cross-Origin Opener Policy (COOP) HTTP Strict Transport Security (HSTS) Security headers for websites with advanced capabilities: Cross-Origin Resource Sharing (CORS) Cross-Origin Embedder Policy (COEP) Known threats on the web Before diving into security headers, learn about known threats on the web and why you'd want to use these security headers. Before diving into security headers, learn about known threats on the web and why you'd want to use these security headers. Protect your site from injection vulnerabilities Injection vulnerabilities arise when untrusted data processed by your application can affect its behavior and, commonly, lead to the execution of attacker-controlled scripts. The most common vulnerability caused by injection bugs is [cross-site scripting](https://portswigger.net/web-security/cross-site-scripting) (XSS) in its various forms, including [reflected XSS](https://portswigger.net/web-security/cross-site-scripting/reflected), [stored XSS](https://portswigger.net/web-security/cross-site-scripting/stored), [DOM-based XSS](https://portswigger.net/web-security/cross-site-scripting/dom-based), and other variants. An XSS vulnerability can typically give an attacker complete access to user data processed by the application and any other information hosted in the same [web origin](/articles/same-site-same-origin#origin). Traditional defenses against injections include consistent use of autoescaping HTML template systems, avoiding the use of [dangerous JavaScript APIs](https://domgo.at/cxss/sinks), and properly processing user data by hosting file uploads in a separate domain and sanitizing user-controlled HTML. Use Content Security Policy (CSP) to control which scripts can be executed by your application to mitigate the risk of injections. Use Trusted Types to enforce sanitization of data passed into dangerous JavaScript APIs. Use X-Content-Type-Options to prevent the browser from misinterpreting the MIME types of your website's resources, which can lead to script execution. Isolate your site from other websites The openness of the web allows websites to interact with each other in ways that can violate an application's security expectations. This includes unexpectedly making authenticated requests or embedding data from another application in the attacker's document, allowing the attacker to modify or read application data. Common vulnerabilities that undermine web isolation include [clickjacking](https://portswigger.net/web-security/clickjacking), [cross-site request forgery](https://portswigger.net/web-security/csrf) (CSRF), [cross-site script inclusion](https://www.scip.ch/en/?labs.20160414) (XSSI), and various [cross-site leaks](https://xsleaks.dev). Use X-Frame-Options to prevent your documents from being embedded by a malicious website. Use Cross-Origin Resource Policy (CORP) to prevent your website's resources from being included by a cross-origin website. Use Cross-Origin Opener Policy (COOP) to protect your website's windows from interactions by malicious websites. Use Cross-Origin Resource Sharing (CORS) to control access to your website's resources from cross-origin documents. [Post-Spectre Web Development](https://www.w3.org/TR/post-spectre-webdev/) is a great read if you are interested in these headers. Build a powerful website securely [Spectre](https://ieeexplore.ieee.org/document/8835233) puts any data loaded into the same [browsing context group](/articles/why-coop-coep) potentially readable despite [same-origin policy](/articles/same-origin-policy). Browsers restrict features that may possibly exploit the vulnerability behind a special environment called "[cross-origin isolation](/articles/coop-coep)". With cross-origin isolation, you can use powerful features such as `SharedArrayBuffer`. Use Cross-Origin Embedder Policy (COEP) along with COOP to enable cross-origin isolation. Encrypt traffic to your site Encryption issues appear when an application does not fully encrypt data in transit, allowing eavesdropping attackers to learn about the user's interactions with the application. Insufficient encryption can arise in the following cases: not using HTTPS, [mixed content](/articles/what-is-mixed-content), setting cookies without the [`Secure` attribute](https://developer.mozilla.org/docs/Web/HTTP/Cookies#restrict_access_to_cookies) (or [`__Secure` prefix](https://developer.mozilla.org/docs/Web/HTTP/Cookies#Cookie_prefixes)), or [lax CORS validation logic](https://blog.detectify.com/2018/04/26/cors-misconfigurations-explained/). Use HTTP Strict Transport Security (HSTS) to consisitently serve your contents through HTTPS. Content Security Policy (CSP) [Cross-Site Scripting (XSS)](https://www.google.com/about/appsecurity/learning/xss/) is an attack where a vulnerability on a website allows a malicious script to be injected and executed. `Content-Security-Policy` provides an added layer to mitigate XSS attacks by restricting which scripts can be executed by the page. It's recommended that you enable strict CSP using one of the following approaches: If you render your HTML pages on the server, use **a nonce-based strict CSP**. If your HTML has to be served statically or cached, for example if it's a single-page application, use **a hash-based strict CSP**. Example usage: A nonce-based CSP `Content-Security-Policy: script-src 'nonce-{RANDOM1}' 'strict-dynamic' https: 'unsafe-inline'; object-src 'none'; base-uri 'none';` How to use CSP Recommended usages 1. Use a nonce-based strict CSP {: #nonce-based-csp} If you render your HTML pages on the server, use **a nonce-based strict CSP**. Generate a new script nonce value for every request on the server side and set the following header: server configuration file Content-Security-Policy: script-src 'nonce-{RANDOM1}' 'strict-dynamic' https: 'unsafe-inline'; object-src 'none'; base-uri 'none'; In HTML, in order to load the scripts, set the `nonce` attribute of all ` [Google Photos](https://photos.google.com/) is a good nonce-based strict CSP example. Use DevTools to see how it's used. 2. Use a hash-based strict CSP {: #hash-based-csp} If your HTML has to be served statically or cached, for example if you're building a single-page application, use **a hash-based strict CSP**. server configuration file Content-Security-Policy: script-src 'sha256-{HASH1}' 'sha256-{HASH2}' 'strict-dynamic' https: 'unsafe-inline'; object-src 'none'; base-uri 'none'; In HTML, you'll need to inline your scripts in order to apply a hash-based policy, because [most browsers don't support hashing external scripts](https://wpt.fyi/results/content-security-policy/script-src/script-src-sri_hash.sub.html?label=master&label=experimental&aligned). index.html To load external scripts, read "Load sourced scripts dynamically" under [Option B: Hash-based CSP Response Header](/articles/strict-csp#step_1_decide_if_you_need_a_nonce-_or_hash-based_csp) section. [CSP Evaluator](https://csp-evaluator.withgoogle.com/) is a good tool to evaluate your CSP, but at the same time a good nonce-based strict CSP example. Use DevTools to see how it's used. Supported browsers Other things to note about CSP [`frame-ancestors`](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors) directive protects your site from clickjackingâa risk that arises if you allow untrusted sites to embed yours. If you prefer simpler solution, you can use `X-Frame-Options` to block being loaded, but `frame-ancestors` gives you an advanced configuration to only allow specific origins as embedders. You may have used [a CSP to ensure that all of your site's resources are loaded over HTTPS](/articles/fixing-mixed-content#content_security_policy). This has become less relevant: nowadays, most browsers block [mixed-content](/articles/what-is-mixed-content). You can also set a CSP in [report-only mode](/articles/strict-csp#step_2_set_a_strict_csp_and_prepare_your_scripts). If you can't set a CSP as a header server-side, you can also set it as a meta tag. Note that you can't use **report-only** mode for meta tags (though [this may change](https://github.com/w3c/webappsec-csp/issues/277)). Learn more [Mitigate XSS with a Strict Content Security Policy (CSP)](/articles/strict-csp) [Content Security Policy Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html) Trusted Types [DOM-based XSS](https://portswigger.net/web-security/cross-site-scripting/dom-based) is an attack where a malicious data is passed into a sink that supports dynamic code execution such as `eval()` or `.innerHTML`. Trusted Types provide the tools to write, security review, and maintain applications free of DOM XSS. They can be enabled via CSP and make JavaScript code secure by default by limiting dangerous web APIs to only accept a special objectâa Trusted Type. To create these objects you can define security policies in which you can ensure that security rules (such as escaping or sanitization) are consistently applied before the data is written to the DOM. These policies are then the only places in code that could potentially introduce DOM XSS. Example usages `Content-Security-Policy: require-trusted-types-for 'script'``// Feature detection if (window.trustedTypes && trustedTypes.createPolicy) { // Name and create a policy const policy = trustedTypes.createPolicy('escapePolicy', { createHTML: str => { return str.replace(/\/g, '>'); } }); }``// Assignment of raw strings is blocked by Trusted Types. el.innerHTML = 'some string'; // This throws an exception. // Assignment of Trusted Types is accepted safely. const escaped = policy.createHTML('<img src=x onerror=alert(1)>'); el.innerHTML = escaped; // '<img src=x onerror=alert(1)>'` How to use Trusted Types Recommended usages Enforce Trusted Types for dangerous DOM sinks CSP and Trusted Types header: Content-Security-Policy: require-trusted-types-for 'script' Currently `'script'` is the only acceptable value for `require-trusted-types-for` directive. Of course, you can combine Trusted Types with other CSP directives: Merging a nonce-based CSP from above with Trusted Types: `Content-Security-Policy: script-src 'nonce-{RANDOM1}' 'strict-dynamic' https: 'unsafe-inline'; object-src 'none'; base-uri 'none'; require-trusted-types-for 'script';` Define a policy Policy: // Feature detection if (window.trustedTypes && trustedTypes.createPolicy) { // Name and create a policy const policy = trustedTypes.createPolicy('escapePolicy', { createHTML: str => { return str.replace(/\/g, '>'); } }); } Apply the policy Use the policy when writing data to the DOM: // Assignment of raw strings are blocked by Trusted Types. el.innerHTML = 'some string'; // This throws an exception.
// Assignment of Trusted Types is accepted safely. const escaped = policy.createHTML(''); el.innerHTML = escaped; // '<img src=x onerror=alert(1)>' With `require-trusted-types-for 'script'`, using a trusted type is a
requirement. Using any dangerous DOM API with a string will result in an
error. Supported browsers Learn more [Prevent DOM-based cross-site scripting vulnerabilities with Trusted
Types](/articles/trusted-types) [CSP: require-trusted-types-for - HTTP |
MDN](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/require-trusted-types-for) [CSP: trusted-types - HTTP |
MDN](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types) [Trusted Types demo](https://www.compass-demo.com/trusted-types/)âopen DevTools Inspector and see
what is happening X-Content-Type-Options When a malicious HTML document is served from your domain (for example, if an
image uploaded to a photo service contains valid HTML markup), some browsers
will treat it as an active document and allow it to execute scripts in the
context of the application, leading to a [cross-site scripting
bug](https://www.google.com/about/appsecurity/learning/xss/). `X-Content-Type-Options: nosniff` prevents it by instructing the browser that
the [MIME type](https://mimesniff.spec.whatwg.org/#introduction) set in the
`Content-Type` header for a given response is correct. This header is
recommended for **all of your resources**. Example usage `X-Content-Type-Options: nosniff` How to use X-Content-Type-Options Recommended usages `X-Content-Type-Options: nosniff` is recommended for all resources served from
your server along with the correct `Content-Type` header. Example headers sent with a document HTML X-Content-Type-Options: nosniff Content-Type: text/html; charset=utf-8 Supported browsers Browser Support
64
12
50
11
[Source](https://developer.mozilla.org/docs/Web/HTTP/Reference/Headers/X-Content-Type-Options) Learn more [X-Content-Type-Options - HTTP MDN](https://developer.mozilla.org/docs/Web/HTTP/Headers/X-Content-Type-Options) X-Frame-Options If a malicious website can embed your site as an iframe, this may allow
attackers to invoke unintended actions by the user with
[clickjacking](https://portswigger.net/web-security/clickjacking). Also, in some
cases [Spectre-type
attacks](https://en.wikipedia.org/wiki/Spectre_(security_vulnerability)) give
malicious websites a chance to learn about the contents of an embedded document. `X-Frame-Options` indicates whether or not a browser should be allowed to render
a page in a ``, `