大网数据广告活动
Web 标准与前端开发

Content-Security-Policy: style-src directive

· 52CE 我爱测

The HTTP Content-Security-Policy (CSP) **`style-src`** directive specifies valid sources for stylesheets.

The HTTP Content-Security-Policy (CSP) style-src directive specifies valid sources for stylesheets.

<table class="properties"> <tbody> <tr> <th scope="row">CSP version</th> <td>1</td> </tr> <tr> <th scope="row">Directive type</th> <td>Fetch directive</td> </tr> <tr> <th scope="row">default-src fallback</th> <td> Yes. If this directive is absent, the user agent will look for the <code>default-src</code> directive. </td> </tr> </tbody> </table>

Syntax

Content-Security-Policy: style-src 'none';
Content-Security-Policy: style-src <source-expression-list>;

This directive may have one of the following values:

  • 'none'
    • : No resources of this type may be loaded. The single quotes are mandatory.
  • <source-expression-list>
    • : A space-separated list of source expression values. Resources of this type may be loaded if they match any of the given source expressions. For this directive, the following source expression values are applicable:

      Note that the specification also includes 'unsafe-eval' as a valid source expression value, in order to permit the CSSOM methods that parse and insert CSS strings, including the insertRule() methods and cssText setters on various interfaces, such as CSSStyleSheet.insertRule() and CSSStyleDeclaration.cssText. However no browser currently blocks these methods, so there is no need to apply unsafe-eval.

Examples

Violation cases

Given this CSP header:

Content-Security-Policy: style-src https://example.com/

the following stylesheets are blocked and won't load:

<link href="https://not-example.com/styles/main.css" rel="stylesheet" />

<style>
  #inline-style {
    background: red;
  }
</style>

<style>
  @import "https://not-example.com/styles/print.css" print;
</style>

as well as styles loaded using the Link header:

Link: <https://not-example.com/styles/stylesheet.css>;rel=stylesheet

Inline style attributes are also blocked:

<div style="display:none">Foo</div>

As well as styles that are applied in JavaScript by setting the style attribute directly, or by setting CSSStyleDeclaration.cssText:

document.querySelector("div").setAttribute("style", "display:none;");
document.querySelector("div").style.cssText = "display:none;";

However, styles properties that are set directly on the element's HTMLElement/style property will not be blocked, allowing users to safely manipulate styles via JavaScript:

document.querySelector("div").style.display = "none";

These types of manipulations can be prevented by disallowing JavaScript via the script-src CSP directive.

Unsafe inline styles

[!NOTE] Disallowing inline styles and inline scripts is one of the biggest security wins CSP provides. However, if you absolutely have to use it, there are a few mechanisms that will allow them.

To allow inline styles, 'unsafe-inline', a nonce-source or a hash-source that matches the inline block can be specified. The following Content Security Policy will allow inline styles like the style element, and the style attribute on any element:

Content-Security-Policy: style-src 'unsafe-inline';

The following style element and style attribute will be allowed by the policy:

<style>
  #inline-style {
    background: red;
  }
</style>

<div style="display:none">Foo</div>

You can use a nonce-source to only allow specific inline style blocks. You need to generate a random Nonce value (using a cryptographically secure random token generator) and include it in the policy. It is important to note, this nonce value needs to be dynamically generated as it has to be unique for each HTTP request:

Content-Security-Policy: style-src 'nonce-2726c7f26c'

You will have to set the same nonce on the style element:

<style nonce="2726c7f26c">
  #inline-style {
    background: red;
  }
</style>

Alternatively, you can create hashes from your inline styles. CSP supports sha256, sha384 and sha512. The binary form of the hash has to be encoded with base64. You can obtain the hash of a string on the command line via the openssl program:

echo -n "#inline-style { background: red; }" | openssl dgst -sha256 -binary | openssl enc -base64

You can use a hash-source to only allow specific inline style blocks:

Content-Security-Policy: style-src 'sha256-ozBpjL6dxO8fsS4u6fwG1dFDACYvpNxYeBA6tzR+FY8='

When generating the hash, don't include the style tags and note that capitalization and whitespace matter, including leading or trailing whitespace.

<style>
  #inline-style {
    background: red;
  }
</style>

Specifications

Browser compatibility

See also

  • Content-Security-Policy
  • style-src-elem
  • style-src-attr
  • Link header
  • style, link
  • @import
  • CSSStyleSheet.insertRule()
  • CSSGroupingRule.insertRule()
  • CSSStyleDeclaration.cssText

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。