• Content Security Policy (CSP)


    Content Security Policy (CSP)

    Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and data injection attacks. These attacks are used for everything from data theft, to site defacement, to malware distribution.

    CSP is designed to be fully backward compatible (except CSP version 2 where there are some explicitly-mentioned inconsistencies in backward compatibility; more details here section 1.1). Browsers that don't support it still work with servers that implement it, and vice versa: browsers that don't support CSP ignore it, functioning as usual, defaulting to the standard same-origin policy for web content. If the site doesn't offer the CSP header, browsers likewise use the standard same-origin policy.

    To enable CSP, you need to configure your web server to return the Content-Security-Policy HTTP header. (Sometimes you may see mentions of the X-Content-Security-Policy header, but that's an older version and you don't need to specify it anymore.)

    Alternatively, the <meta> element can be used to configure a policy, for example:

    <  code ><  span class="token tag"><  span class="token punctuation"><  meta
      <  span class="token attr-name">http-equiv  <  span class="token attr-value"><  span class="token punctuation attr-equals">=  <  span class="token punctuation">"  Content-Security-Policy<  span class="token punctuation">"  
      <  span class="token attr-name">content  <  span class="token attr-value"><  span class="token punctuation attr-equals">=  <  span class="token punctuation">"  default-src 'self'; img-src https://*; child-src 'none';<  span class="token punctuation">"   <  span class="token punctuation">/>  
    

    Threats

     

    Mitigating cross-site scripting

    A primary goal of CSP is to mitigate and report XSS attacks. XSS attacks exploit the browser's trust in the content received from the server. Malicious scripts are executed by the victim's browser because the browser trusts the source of the content, even when it's not coming from where it seems to be coming from.

    CSP makes it possible for server administrators to reduce or eliminate the vectors by which XSS can occur by specifying the domains that the browser should consider to be valid sources of executable scripts. A CSP compatible browser will then only execute scripts loaded in source files received from those allowed domains, ignoring all other scripts (including inline scripts and event-handling HTML attributes).

    As an ultimate form of protection, sites that want to never allow scripts to be executed can opt to globally disallow script execution.

    Mitigating packet sniffing attacks

    In addition to restricting the domains from which content can be loaded, the server can specify which protocols are allowed to be used; for example (and ideally, from a security standpoint), a server can specify that all content must be loaded using HTTPS. A complete data transmission security strategy includes not only enforcing HTTPS for data transfer, but also marking all cookies with the secure attribute and providing automatic redirects from HTTP pages to their HTTPS counterparts. Sites may also use the Strict-Transport-Security HTTP header to ensure that browsers connect to them only over an encrypted channel.

    Using CSP

    Configuring Content Security Policy involves adding the Content-Security-Policy HTTP header to a web page and giving it values to control what resources the user agent is allowed to load for that page. For example, a page that uploads and displays images could allow images from anywhere, but restrict a form action to a specific endpoint. A properly designed Content Security Policy helps protect a page against a cross-site scripting attack. This article explains how to construct such headers properly, and provides examples.

    Specifying your policy

    You can use the Content-Security-Policy HTTP header to specify your policy, like this:

    <code><span class="token header"><span class="token header-name keyword">Content-Security-Policy</span><span class="token punctuation">:</span> <span class="token header-value csp languages-csp">policy</span></span>
    </code>

    The policy is a string containing the policy directives describing your Content Security Policy.

    Writing a policy

    A policy is described using a series of policy directives, each of which describes the policy for a certain resource type or policy area. Your policy should include a default-src policy directive, which is a fallback for other resource types when they don't have policies of their own (for a complete list, see the description of the default-src directive). A policy needs to include a default-src or script-src directive to prevent inline scripts from running, as well as blocking the use of eval(). A policy needs to include a default-src or style-src directive to restrict inline styles from being applied from a <style> element or a style attribute. There are specific directives for a wide variety of types of items, so that each type can have its own policy, including fonts, frames, images, audio and video media, scripts, and workers.

    For a complete list of policy directives, see the reference page for the Content-Security-Policy header.

    Examples: Common use cases

    This section provides examples of some common security policy scenarios.

    Example 1

    A web site administrator wants all content to come from the site's own origin (this excludes subdomains.)

    Content-Security-Policy: default-src 'self'

    Example 2

    A web site administrator wants to allow content from a trusted domain and all its subdomains (it doesn't have to be the same domain that the CSP is set on.)

    Content-Security-Policy: default-src 'self' example.com *.example.com
     

    Example 3

    A web site administrator wants to allow users of a web application to include images from any origin in their own content, but to restrict audio or video media to trusted providers, and all scripts only to a specific server that hosts trusted code.

    Content-Security-Policy: default-src 'self'; img-src *; media-src example.org example.net; script-src userscripts.example.com

    Here, by default, content is only permitted from the document's origin, with the following exceptions:

    • Images may load from anywhere (note the "*" wildcard).
    • Media is only allowed from example.org and example.net (and not from subdomains of those sites).
    • Executable script is only allowed from userscripts.example.com.

    Example 4

    A web site administrator for an online banking site wants to ensure that all its content is loaded using TLS, in order to prevent attackers from eavesdropping on requests.

    Content-Security-Policy: default-src https://onlinebanking.example.com

    The server permits access only to documents being loaded specifically over HTTPS through the single origin onlinebanking.example.com.

    Example 5

    A web site administrator of a web mail site wants to allow HTML in email, as well as images loaded from anywhere, but not JavaScript or other potentially dangerous content.

    Content-Security-Policy: default-src 'self' *.example.com; img-src *

    Note that this example doesn't specify a script-src; with the example CSP, this site uses the setting specified by the default-src directive, which means that scripts can be loaded only from the originating server.

    Testing your policy

    To ease deployment, CSP can be deployed in report-only mode. The policy is not enforced, but any violations are reported to a provided URI. Additionally, a report-only header can be used to test a future revision to a policy without actually deploying it.

    You can use the Content-Security-Policy-Report-Only HTTP header to specify your policy, like this:

    <code><span class="token header"><span class="token header-name keyword">Content-Security-Policy-Report-Only</span><span class="token punctuation">:</span> <span class="token header-value">policy</span></span>
    </code>

    If both a Content-Security-Policy-Report-Only header and a Content-Security-Policy header are present in the same response, both policies are honored. The policy specified in Content-Security-Policy headers is enforced while the Content-Security-Policy-Report-Only policy generates reports but is not enforced.

    Enabling reporting

    By default, violation reports aren't sent. To enable violation reporting, you need to specify the report-uri policy directive, providing at least one URI to which to deliver the reports:

    <code><span class="token header"><span class="token header-name keyword">Content-Security-Policy</span><span class="token punctuation">:</span> <span class="token header-value csp languages-csp">default-src 'self'; report-uri http://reportcollector.example.com/collector.cgi</span></span>
    </code>

    Then you need to set up your server to receive the reports; it can store or process them in whatever manner you determine is appropriate.

    Violation report syntax

    The report JSON object contains the following data:

    blocked-uri

    The URI of the resource that was blocked from loading by the Content Security Policy. If the blocked URI is from a different origin than the document-uri, then the blocked URI is truncated to contain just the scheme, host, and port.

    disposition

    Either "enforce" or "report" depending on whether the Content-Security-Policy-Report-Only header or the Content-Security-Policy header is used.

    document-uri

    The URI of the document in which the violation occurred.

    effective-directive

    The directive whose enforcement caused the violation. Some browsers may provide different values, such as Chrome providing style-src-elem/style-src-attr, even when the actually enforced directive was style-src.

    original-policy

    The original policy as specified by the Content-Security-Policy HTTP header.

    referrer Deprecated Non-standard

    The referrer of the document in which the violation occurred.

    script-sample

    The first 40 characters of the inline script, event handler, or style that caused the violation. Only applicable to script-src* and style-src* violations, when they contain the 'report-sample'

    status-code

    The HTTP status code of the resource on which the global object was instantiated.

    violated-directive

    The name of the policy section that was violated.

    Sample violation report

    Let's consider a page located at http://example.com/signup.html. It uses the following policy, disallowing everything but stylesheets from cdn.example.com.

    <code><span class="token header"><span class="token header-name keyword">Content-Security-Policy</span><span class="token punctuation">:</span> <span class="token header-value csp languages-csp">default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports</span></span>
    </code>

    The HTML of signup.html looks like this:

    <code><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token punctuation">&lt;</span>html <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en-US<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token punctuation">&lt;</span>head<span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token punctuation">&lt;</span>meta <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
        <span class="token tag"><span class="token punctuation">&lt;</span>title<span class="token punctuation">&gt;</span></span>Sign Up<span class="token tag"><span class="token punctuation">&lt;/</span>title<span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token punctuation">&lt;</span>link <span class="token attr-name">rel</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>stylesheet<span class="token punctuation">"</span></span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>css/style.css<span class="token punctuation">"</span></span> <span class="token punctuation">/&gt;</span></span>
      <span class="token tag"><span class="token punctuation">&lt;/</span>head<span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token punctuation">&lt;</span>body<span class="token punctuation">&gt;</span></span>
        Here be content.
      <span class="token tag"><span class="token punctuation">&lt;/</span>body<span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token punctuation">&lt;/</span>html<span class="token punctuation">&gt;</span></span>
    </code>

    Can you spot the mistake? Stylesheets are allowed to be loaded only from cdn.example.com, yet the website tries to load one from its own origin (http://example.com). A browser capable of enforcing CSP would send the following violation report as a POST request to http://example.com/_/csp-reports, when the document is visited:

    <code><span class="token punctuation">{</span>
      <span class="token property">"csp-report"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"document-uri"</span><span class="token operator">:</span> <span class="token string">"http://example.com/signup.html"</span><span class="token punctuation">,</span>
        <span class="token property">"referrer"</span><span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">,</span>
        <span class="token property">"blocked-uri"</span><span class="token operator">:</span> <span class="token string">"http://example.com/css/style.css"</span><span class="token punctuation">,</span>
        <span class="token property">"violated-directive"</span><span class="token operator">:</span> <span class="token string">"style-src cdn.example.com"</span><span class="token punctuation">,</span>
        <span class="token property">"original-policy"</span><span class="token operator">:</span> <span class="token string">"default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports"</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    </code>

    As you can see, the report includes the full path to the violating resource in blocked-uri. This is not always the case. For example, if the signup.html attempted to load CSS from http://anothercdn.example.com/stylesheet.css, the browser would not include the full path, but only the origin (http://anothercdn.example.com). The CSP specification gives an explanation of this odd behavior. In summary, this is done to prevent leaking sensitive information about cross-origin resources.

  • 相关阅读:
    css的em是根据什么来写的
    向一个对象数组里面添加新的属性 + 将一个对象数组数据拿出来变成另一个对象
    微信里iphone后退不刷新问题解决方案
    进到页面后input输入框自动获取焦点
    jquery checkbox反复调用attr('checked', true/false)只有第一次生效
    js promise中如何取到[[PromiseValue]]
    js 取得当天0点 / 23:59:59 时间
    jQuery获取包括当前元素的HTML
    C++ 实现 STL 标准库和算法(二)template 编程和迭代器粗解 实验楼笔记
    我现在怎么写博客笔记?
  • 原文地址:https://www.cnblogs.com/chucklu/p/16785801.html
Copyright © 2020-2023  润新知