Security/CSP/Specification: Difference between revisions

no edit summary
No edit summary
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This specification document is DEPRECATED (old and crusty). The W3C has undertaken standardization of CSP and you can find the [http://www.w3.org/TR/CSP/ W3C spec here].
=Specification=
=Specification=


Line 7: Line 9:
=Definitions=
=Definitions=


A <b>policy</b> is composed of <b>directives</b>, such as "<tt>allow none</tt>".  Each directive is composed of a <b>directive name</b> and a <b>directive value</b>, which is either a list of <b>host items</b> or a <b>URI</b>, for certain types of directives.
A <b>policy</b> is composed of <b>directives</b>, such as "<tt>allow 'none'</tt>".  Each directive is composed of a <b>directive name</b> and a <b>directive value</b>, which is either a list of <b>host items</b> or a <b>URI</b>, for certain types of directives.


When CSP is activated for a site, a few <b>[[Security/CSP#Content_Restrictions|base restrictions]]</b> in the browser environment are enforced <i>by default</i> to help provide proper enforcement of any policy defined.  These base restrictions provide general security enhancements by limiting the types of dynamic content that is allowed: generally any script on a site that converts text into code (through the use of <tt>eval()</tt> or similar functions) is disallowed.  Details of the refinements can be found in the [[Security/CSP/Specification#Base_Restrictions|Base Restrictions]] section.
When CSP is activated for a site, a few <b>[[Security/CSP#Content_Restrictions|base restrictions]]</b> in the browser environment are enforced <i>by default</i> to help provide proper enforcement of any policy defined.  These base restrictions provide general security enhancements by limiting the types of dynamic content that is allowed: generally any script on a site that converts text into code (through the use of <tt>eval()</tt> or similar functions) is disallowed.  Details of the refinements can be found in the [[Security/CSP/Specification#Base_Restrictions|Base Restrictions]] section.
Line 13: Line 15:
==Policy Language and Syntax==
==Policy Language and Syntax==


A policy is composed of directives with their corresponding values.  Any number of directives can be defined, but the <b><tt>allow</tt> directive must always be present</b>.  Each directive is followed with a list of host expressions except for <tt>policy-uri</tt> and <tt>report-uri</tt> which contain a single URI value. Some [[Security/CSP/Spec#Sample_Policy_Definitions|example policy sets]] are provided below.
A policy is composed of directives with their corresponding values.  Any number of directives can be defined, but the <b><tt>allow</tt> directive must always be present</b>, or CSP will revert to enforcing the policy <tt>allow 'none'</tt> for the protected content.  Each directive is followed with a list of host expressions except for <tt>policy-uri</tt> and <tt>report-uri</tt> which contain a single URI value. Some [[Security/CSP/Spec#Sample_Policy_Definitions|example policy sets]] are provided below.


Note: In the case of policy refinements as described above, it is possible to have two report-uri values; in this situation, a copy of the report is sent to each of the two URIs.
Note: In the case of policy refinements as described above, it is possible to have two report-uri values; in this situation, a copy of the report is sent to each of the two URIs.
Line 39: Line 41:


;allow:
;allow:
* The catch-all section that defines the security policy for all types of content which are not called out in any of the other directives.  Defines the default policy for un-specified content types.
* The catch-all section that defines the security policy for all types of content which are not called out in any of the other directives.  Defines the default policy for un-specified content types, except for <tt>frame-ancestors</tt>, which MUST be enforced as "*" when not explicitly stated.
* User Agents MUST not load content from any source if the allow directive is not explicitly specified. This can be considered equivalent to the policy "allow 'none'".
* User Agents MUST NOT load content from any source if the allow directive is not explicitly specified. This can be considered equivalent to the policy "allow 'none'".
* User Agents MUST enforce this directive for all HTTP requests not subject to one of the more specific directives.
* User Agents MUST enforce this directive for all HTTP requests not subject to one of the more specific directives.


Line 48: Line 50:
** <tt>inline-script</tt> enables inline scripts and <tt>javascript:</tt> URIs
** <tt>inline-script</tt> enables inline scripts and <tt>javascript:</tt> URIs
** <tt>eval-script</tt> enables the <tt>eval()</tt> functionality of scripts interpreted by the browser, and allows code to be created from strings in uses of the <tt>new Function()</tt> constructor, <tt>setTimeout</tt> and <tt>setInterval</tt>
** <tt>eval-script</tt> enables the <tt>eval()</tt> functionality of scripts interpreted by the browser, and allows code to be created from strings in uses of the <tt>new Function()</tt> constructor, <tt>setTimeout</tt> and <tt>setInterval</tt>
* User Agents must ignore any tokens not recognized by CSP, and SHOULD post a non-fatal warning to the error console.
* User Agents MUST ignore any tokens not recognized by CSP, and SHOULD post a non-fatal warning to the error console.


;img-src:
;img-src:
* Indicates which sources are valid for images and favicons.
* Indicates which sources are valid for images and favicons.
* User Agents MUST not request images from non-approved sources.
* User Agents MUST NOT request images from non-approved sources.
* User Agents MUST subject image requests to the allow directive if img-src is not explicitly specified.  
* User Agents MUST subject image requests to the allow directive if img-src is not explicitly specified.  


;media-src:
;media-src:
* Indicates which sources are valid for <tt>audio</tt> and <tt>video</tt> elements.
* Indicates which sources are valid for <tt>audio</tt> and <tt>video</tt> elements.
* User Agents MUST not request <tt>audio</tt> and <tt>video</tt> elements from non-approved sources.
* User Agents MUST NOT request <tt>audio</tt> and <tt>video</tt> elements from non-approved sources.
* User Agents MUST subject audio and video requests to the allow directive if media-src is not explicitly specified.
* User Agents MUST subject audio and video requests to the allow directive if media-src is not explicitly specified.


Line 63: Line 65:
* Indicates which sources are valid for scripts.
* Indicates which sources are valid for scripts.
* Regulates which scripts can be loaded via the <tt>src=</tt> attribute.
* Regulates which scripts can be loaded via the <tt>src=</tt> attribute.
* User Agents MUST not request scripts from non-approved sources.
* User Agents MUST NOT request scripts from non-approved sources.
* User Agents MUST subject script requests to the allow directive if script-src is not explicitly specified.
* User Agents MUST subject script requests to the allow directive if script-src is not explicitly specified.


;object-src:
;object-src:
* Indicates which sources are valid for <tt>object</tt>, <tt>embed</tt>, and <tt>applet</tt> elements.
* Indicates which sources are valid for <tt>object</tt>, <tt>embed</tt>, and <tt>applet</tt> elements.
* User Agents MUST not request objects from non-approved sources.
* User Agents MUST NOT request objects from non-approved sources.
* User Agents MUST subject object, embed, and applet requests to the allow directive if object-src is not explicitly specified.
* User Agents MUST subject object, embed, and applet requests to the allow directive if object-src is not explicitly specified.


;frame-src:
;frame-src:
* Indicates which sources are valid for <tt>frame</tt> and <tt>iframe</tt> elements.
* Indicates which sources are valid for <tt>frame</tt> and <tt>iframe</tt> elements.
* User Agents MUST not request frame content from non-approved sources.
* User Agents MUST NOT request frame content from non-approved sources.
* User Agents MUST subject frame requests to the allow directive if frame-src is not explicitly specified.
* User Agents MUST subject frame requests to the allow directive if frame-src is not explicitly specified.


;font-src:
;font-src:
* Indicates which sources are valid for <tt>@font-src</tt> CSS loads.
* Indicates which sources are valid for <tt>@font-src</tt> CSS loads.
* User Agents MUST not request fonts served from non-approved sources when intended for use as a font in CSS.
* User Agents MUST NOT request fonts served from non-approved sources when intended for use as a font in CSS.
* User Agents MUST subject requests caused by <tt>@font-src</tt> to the allow directive if font-src is not explicitly specified.
* User Agents MUST subject requests caused by <tt>@font-src</tt> to the allow directive if font-src is not explicitly specified.


;xhr-src:
;xhr-src:
* Indicates which sources are valid for <tt>XMLHttpRequest</tt> connections.
* Indicates which sources are valid for <tt>XMLHttpRequest</tt> connections.
* User Agents MUST not cause XMLHttpRequests to open requests to sources not permitted by this directive.
* User Agents MUST NOT cause XMLHttpRequests to open requests to sources not permitted by this directive.
* User Agents MUST subject requests caused by <tt>XMLHttpRequest</tt> to the allow directive if xhr-src is not explicitly specified.
* User Agents MUST subject requests caused by <tt>XMLHttpRequest</tt> to the allow directive if xhr-src is not explicitly specified.


;frame-ancestors:
;frame-ancestors:
* Indicates which sources are valid <b>ancestors</b> for embedding the protected resource via <tt>object</tt>, <tt>frame</tt> and <tt>iframe</tt> tags.  An ancestor is any HTML document between the protected resource and the top of the window frame tree; for example, if A embeds B which embeds C, both A and B are <b>ancestors</b> of C.  If A embeds both B and C, B is <i>not</i> an ancestor of C, but A still <i>is</i>.
* Indicates which sources are valid <b>ancestors</b> for embedding the protected resource via <tt>object</tt>, <tt>frame</tt> and <tt>iframe</tt> tags.  An ancestor is any HTML document between the protected resource and the top of the window frame tree; for example, if A embeds B which embeds C, both A and B are <b>ancestors</b> of C.  If A embeds both B and C, B is <i>not</i> an ancestor of C, but A still <i>is</i>.
* If this directive is not explicitly stated in the policy, it is assumed to be "*".  This is different than the other source directives that inherit the value of <tt>allow</tt>.
* All web pages that are ancestors of the protected content must be indicated by the value of this directive.  For example, if A embeds B which embeds C, and C defines a <tt>frame-ancestors</tt> as "B,C" then C is not rendered as a subframe.
* All web pages that are ancestors of the protected content must be indicated by the value of this directive.  For example, if A embeds B which embeds C, and C defines a <tt>frame-ancestors</tt> as "B,C" then C is not rendered as a subframe.
* Answers the question: "Which sites may embed this resource?"
* Answers the question: "Which sites may embed this resource?"
* User Agents MUST not render the protected document when any of its frame ancestors are not allowed by this directive.
* User Agents MUST NOT render the protected document when any of its frame ancestors are not allowed by this directive.
* User Agents MUST always render the protected document if frame-ancestors is not explicitly specified.
* User Agents MUST always render the protected document if frame-ancestors is not explicitly specified.
* Note that this directive addresses the [http://jeremiahgrossman.blogspot.com/2008/10/clickjacking-web-pages-can-see-and-hear.html clickjacking] threat, but not [http://www.cgisecurity.com/articles/csrf-faq.shtml CSRF]
* Note that this directive addresses the [http://jeremiahgrossman.blogspot.com/2008/10/clickjacking-web-pages-can-see-and-hear.html clickjacking] threat, but not [http://www.cgisecurity.com/articles/csrf-faq.shtml CSRF]
Line 99: Line 102:
* Indicates which sources are valid for externally linked stylesheets.
* Indicates which sources are valid for externally linked stylesheets.
* User Agents MUST always allow inline stylesheets and style attributes of HTML tags.
* User Agents MUST always allow inline stylesheets and style attributes of HTML tags.
* User Agents MUST not request stylesheets from sources not allowed by the style-src directive.
* User Agents MUST NOT request stylesheets from sources not allowed by the style-src directive.
* User Agents MUST subject stylesheet requests to the allow directive if style-src is not explicitly specified.
* User Agents MUST subject stylesheet requests to the allow directive if style-src is not explicitly specified.


;report-uri:
;report-uri:
* Instructs the browser where to send a report when CSP is violated.
* Instructs the browser where to send a report when CSP is violated.
* The report will be an XML document with MIME type application/xml sent via POST to the specified URI contained in the value of this directive.
* The report will be an JSON object with MIME type application/json sent via POST to the specified URI contained in the value of this directive.
* Acceptable report URIs MUST use the scheme and port as the protected content, and the [http://publicsuffix.org public suffix] and most general DNS label of the protected content and the report URI must match.  For example www.foo.co.uk and reports.foo.co.uk, but not reports.bar.co.uk.  Relative URIs are acceptable, and are resolved within the same scheme, host and port as the document served with the CSP.   
* Acceptable report URIs MUST use the scheme and port as the protected content, and the [http://publicsuffix.org public suffix] and most general DNS label of the protected content and the report URI must match.  For example www.foo.co.uk and reports.foo.co.uk, but not reports.bar.co.uk.  Relative URIs are acceptable, and are resolved within the same scheme, host and port as the document served with the CSP.   
* User Agents MUST send violation reports to any acceptable URIs in this directive.  Details about the information provided in violation reports are found in the [[#Violation Report Syntax|Violation Report Syntax]] section.
* User Agents MUST send violation reports to any acceptable URIs in this directive.  Details about the information provided in violation reports are found in the [[#Violation Report Syntax|Violation Report Syntax]] section.
* User Agents MUST ignore report URIs that don't match the public suffix and base host match requirements.  User Agents SHOULD log one error to an error console.  User Agents MUST then continue CSP enforcement as if the report URI were not specified.  
* User Agents MUST ignore report URIs that don't match the public suffix and base host match requirements.  User Agents SHOULD log one error to an error console.  User Agents MUST then continue CSP enforcement as if the report URI were not specified.  
* User Agents MUST not honor HTTP 3xx response codes to prevent HTTP header leakage across domains.
* User Agents MUST NOT honor HTTP 3xx response codes to prevent HTTP header leakage across domains.


;policy-uri:
;policy-uri:
Line 124: Line 127:
If a port is not specified as the source expression, a User Agent MUST use the default port for the source's scheme (whether it is inherited or explicitly specified in the source expression).
If a port is not specified as the source expression, a User Agent MUST use the default port for the source's scheme (whether it is inherited or explicitly specified in the source expression).


When a scheme alone is the entire source expression (e.g., <tt>javascript:</tt>) a User Agent MUST not enforce host and port restrictions.  This is because for some schemes, host and port are irrelevant (e.g., <tt>data:</tt>).
When a scheme alone is the entire source expression (e.g., <tt>javascript:</tt>) a User Agent MUST NOT enforce host and port restrictions.  This is because for some schemes, host and port are irrelevant (e.g., <tt>data:</tt>).


====Host-less Schemes====
====Host-less Schemes====
Line 157: Line 160:
  <policy>            ::= <allow-directive>";"<directive-list>
  <policy>            ::= <allow-directive>";"<directive-list>
   
   
  <allow-directive>  ::= allow <source-list>
  <allow-directive>  ::= allow <src-dir-value>
   
   
  <directive-list>    ::= <empty> | <directive>";"<directive-list>
  <directive-list>    ::= <empty> | <directive>";"<directive-list>
Line 220: Line 223:
   
   
  <ldh-str>          ::= <let-dig-hyp>
  <ldh-str>          ::= <let-dig-hyp>
                       | <ldh-symbol><let-dig-hyp>
                       | <ldh-str><let-dig-hyp>
   
   
  <let-dig-hyp>      ::= <letter> | <digit> | "-"
  <let-dig-hyp>      ::= <letter> | <digit> | "-"
Line 248: Line 251:
==Violation Report Syntax==
==Violation Report Syntax==
User Agents MUST notify any provided report-uri when its containing policy is violated. These reports contain information about the protected resource and the violating content, and MUST be transmitted to any specified <tt>report-uri</tt>s via HTTP POST if available in the employed scheme, otherwise User Agents MUST choose an appropriate "submit" method.   
User Agents MUST notify any provided report-uri when its containing policy is violated. These reports contain information about the protected resource and the violating content, and MUST be transmitted to any specified <tt>report-uri</tt>s via HTTP POST if available in the employed scheme, otherwise User Agents MUST choose an appropriate "submit" method.   
User Agents MUST not honor redirection responses.   
User Agents MUST NOT honor redirection responses.   
Reports MUST be an XML document containing the following fields:
The report body MUST be a JSON object having the following properties:


; <tt>request</tt> : HTTP request line of the resource whose policy is violated (including method, resource, path, HTTP version)
; <tt>request</tt> : HTTP request line of the resource whose policy is violated (including method, resource, path, HTTP version)
Line 257: Line 260:
; <tt>original-policy</tt> : The original policy as served in the X-Content-Security-Policy HTTP header (or if there were multiple headers, a comma separated list of the policies)
; <tt>original-policy</tt> : The original policy as served in the X-Content-Security-Policy HTTP header (or if there were multiple headers, a comma separated list of the policies)
   
   
NOTE: in the case where a protected resource is not rendered because the <tt>frame-ancestors</tt> directive was violated, User Agents MUST not send <tt>blocked-uri</tt> (it is assumed to be the same as the request URI).
NOTE: in the case where a protected resource is not rendered because the <tt>frame-ancestors</tt> directive was violated, User Agents MUST NOT send <tt>blocked-uri</tt> (it is assumed to be the same as the request URI).


Violation Report XML Schema:
Violation Report JSON Format:


  <?xml version="1.0" encoding="ISO-8859-1" ?>
  {
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   csp-report: {
   <xs:element name="csp-report">
     request: "GET /index.html HTTP/1.1",
     <xs:complexType>
    request-headers: "Host: example.com
      <xs:sequence>
                      User-Agent: ...
        <xs:element name="request" type="string" use="required" />
                      ...",
        <xs:element name="request-headers" type="string" />
    blocked-uri: "...",
        <xs:element name="blocked-uri" type="string" />
    violated-directive: "..."
        <xs:element name="violated-directive" type="string" use="required" />
   }
        <xs:element name="original-policy" type="string" use="required" />
  }
      </xs:sequence>
    </xs:complexType>
   </xs:element>
  </xs:schema>


 
The MIME type of the transmitted report will be set to <tt>application/json</tt>.
The MIME type of the transmitted report will be set to <tt>application/xml</tt>.


===Violation Report Sample===
===Violation Report Sample===


In this example, a page located at <tt>http://example.com/index.html</tt> was requested using HTTP 1.1 via the GET method.  It provided a policy that included the directive "<tt>img-src self</tt>", which was violated by a request for <tt><nowiki>http://evil.com/some_image.png</nowiki></tt>.  The sample XML data sent to the policy-specified <tt>report-uri</tt> follows.
In this example, a page located at <tt>http://example.com/index.html</tt> was requested using HTTP 1.1 via the GET method.  It provided a policy that included the directive "<tt>img-src 'self'</tt>", which was violated by a request for <tt><nowiki>http://evil.com/some_image.png</nowiki></tt>.  The sample JSON object sent to the policy-specified <tt>report-uri</tt> follows.


  <csp-report>
  {
  <request>GET /index.html HTTP/1.1</request>
  "csp-report":
  <request-headers><![CDATA[
    {
            Host: example.com
      "request": "GET http://index.html HTTP/1.1",
            User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0
      "request-headers": "Host: example.com                                                      
            Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
                          User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.3a5pre) Gecko/20100601 Minefield/3.7a5pre                                                       
            Accept-Language: en-us,en;q=0.5
                          Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
            Accept-Encoding: gzip,deflate
                          Accept-Language: en-us,en;q=0.5                                        
            Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
                          Accept-Encoding: gzip,deflate                                          
            Keep-Alive: 300
                          Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7                          
            Connection: keep-alive
                          Keep-Alive: 115                                                         
  ]]></request-headers>
                          Connection: keep-alive",
  <blocked-uri><nowiki>http://evil.com/some_image.png</nowiki></blocked-uri>
      "blocked-uri": "<nowiki>http://evil.com/some_image.png</nowiki>",
  <violated-directive>img-src self</violated-directive>
      "violated-directive": "img-src 'self'",
  <original-policy>allow none; img-src *, allow self; img-src self</original-policy>
      "original-policy": "allow 'none'; img-src *; allow 'self'; img-src 'self'"
  </csp-report>
    }
  }


=User Agent Behavior=
=User Agent Behavior=
Line 313: Line 312:
</font>
</font>
<font color="#060">
<font color="#060">
* User Agents MUST not block:
* User Agents MUST NOT block:
** Scripts imported from external files whose sources are allowed by the protected document's policy AND are served with a Content-Type of <tt>application/javascript</tt> or <tt>application/json</tt>.
** Scripts imported from external files whose sources are allowed by the protected document's policy AND are served with a Content-Type of <tt>application/javascript</tt> or <tt>application/json</tt>.
</font>
</font>
Line 328: Line 327:
</font>
</font>
<font color="#060">
<font color="#060">
* User Agents MUST not block:
* User Agents MUST NOT block:
** Functions declared using the function operator, e.g. function f() { some_code }, or var f = function() { some_code }
** Functions declared using the function operator, e.g. function f() { some_code }, or var f = function() { some_code }
** calls to setTimeout using a Function argument, e.g. setTimeout(myFunc, 1000)
** calls to setTimeout using a Function argument, e.g. setTimeout(myFunc, 1000)
Line 342: Line 341:
</font>
</font>
<font color="#060">
<font color="#060">
* User Agents MUST not block:
* User Agents MUST NOT block:
** data: URIs when used as a source for inline content explicitly allowed by the protected document's policy.  
** data: URIs when used as a source for inline content explicitly allowed by the protected document's policy.  
</font>
</font>
Line 356: Line 355:
</font>
</font>
<font color="#060">
<font color="#060">
* User Agents MUST not block:
* User Agents MUST NOT block:
** XBL bindings loaded via the chrome: or resource: protocols
** XBL bindings loaded via the chrome: or resource: protocols
</font>
</font>
Line 380: Line 379:


==Activation and Enforcement==
==Activation and Enforcement==
CSP is activated by a client's browser when the <tt>X-Content-Security-Policy</tt> HTTP header is provided in a HTTP response.
User Agents MUST activate CSP and enforce it for a document when the <tt>X-Content-Security-Policy</tt> HTTP header is provided in its HTTP response.  User Agents MUST parse AND begin enforcing the policy before any of the protected content is parsed.


The Content Security Policy to be enforced can be delivered to the browser in one of two ways: directly as the value in the <tt>X-Content-Security-Policy</tt> HTTP header or a file served from the same host as the resource to be secured. The <tt>X-Content-Security-Policy</tt> header must either contain a policy definition <i>or</i> a <tt>policy-uri</tt> field; if both are present, the browser will raise a [[Security/CSP/Spec#Error_Handling|CSP console error]] and enforce the most restrictive ("allow none") policy.
==Policy Refinements with Multiple Headers==
       
When multiple instances of the <tt>X-Content-Security-Policy</tt> HTTP header are present in an HTTP response, the User Agent MUST enforce the intersection of the policies; essentially, the User Agent SHOULD enforce a policy that is more strict than both the policies specified in the multiple headers, but only strict enough to correspond to rules in all policies.  Any web request that satisfied ''all'' policies alone MUST be accepted by the new policy, but any request rejected by ''any of'' of the two policies MUST be rejected by the new policy.
The syntax is identical between file-based and header-based policy. The contents of a policy file are equivalent to the value of the X-Content-Security-Policy header.


==Policy Refinements with Multiple Headers==
User Agents MUST calculate the intersection on a directive-by-directive basis (i.e., the intersection of the <tt>allow</tt> directive is taken and enforced as the <tt>allow</tt> part of the effective policy). Explicitly, for two policies:
When multiple instances of the <tt>X-Content-Security-Policy</tt> HTTP header are present in an HTTP response, the intersection of the policies is enforced; essentially, the browser enforces a policy that is more strict than both the policies specified in the multiple headers, but only strict enough to correspond to rules in all policies.  Any web request that satisfied ''all'' policies alone will be accepted by the new policy, but any request rejected by ''any of'' of the two policies will be rejected.  The intersection is calculated on a directive-by-directive basis (i.e., the intersection of the <tt>allow</tt> directive is taken and enforced as the <tt>allow</tt> part of the effective policy). Explicitly, for two policies:


<blockquote>
<blockquote>
Line 400: Line 397:
</blockquote>
</blockquote>


If more than two instances of the <tt>X-Content-Security-Policy</tt> header are present in the response, the intersection is done digest-style: the first two policies are removed from the set of headers to digest, intersected, and the result is placed back in the set.  This continues until only one policy remains.  e.g.,
If more than two instances of the <tt>X-Content-Security-Policy</tt> header are present in the response, the User Agent MUST perform intersection digest-style: the first two policies are removed from the set of headers to digest, intersected, and the result is placed back in the set.  This continues until only one policy remains.  e.g.,
  intersect(A, B, C, D) = intersect(A, intersect(B, intersect(C,D)))
  intersect(A, B, C, D) = intersect(A, intersect(B, intersect(C,D)))


If two policy headers are present, one (P<sub>1</sub>) may allow scripts from domains A, B and C.  The policy in the other header (P<sub>2</sub>) may allow scripts from domains B, C and D.  The policy enforced (P<sub>enforced</sub>) by the browser will allow scripts from domains B and C only (P<sub>enforced</sub> = P<sub>1</sub> &cap; P<sub>2</sub>).
''Example: If two policy headers are present, one (P<sub>1</sub>) may allow scripts from domains A, B and C.  The policy in the other header (P<sub>2</sub>) may allow scripts from domains B, C and D.  The policy enforced (P<sub>enforced</sub>) by the browser will allow scripts from domains B and C only (P<sub>enforced</sub> = P<sub>1</sub> &cap; P<sub>2</sub>).''
 
===Why Intersect Policies?===
 
Because the <tt>X-Content-Security-Policy</tt> header may appear multiple times in the response, it is possible they're crafted by different entities and may conflict.  A decision must be made about which policy to use, or whether to combine them or not.  Assuming there are two different policies present, there are five obvious ways to address this conflict:
 
#<b>Ignore both.  Raise error in the console.  Enforce "allow none" (most secure).</b><br/>Simplest and safest way to lock down when the policies conflict.
#<b>Ignore both. Raise error in the console.  Enforce "allow *" (most relaxed).</b><br/>This is a fail-open policy and will keep the site from breaking if two policies conflict.
#<b>Use the first header's policy.</b>
#<b>Use the second header's policy.</b>
#<b>Enforce the intersection of the policies.</b><br/><i>This is the technique used by CSP</i>.  For each URI that is accepted by <i>both</i> policies, the new intersected policy will also accept the URI.  Any request that is not accepted by <i>both</i> policies will be rejected by the new policy. This provides the ability for interaction between multiple policy specifications, allowing infrastructure admins to add a global policy to all sites on their network that may additionally want more restrictions.  This technique allows the use of multiple headers to make a widely used policy more restrictive, but never more relaxed.
 
The fifth option, enforcing the intersection of the policies, is the best balance between safety and flexibility so CSP implements this conflict resolution technique.  Furthermore, it is likely that those controlling the network are not always the same people who maintain or create the web application itself -- the ability to "refine" or further restrict enforced policies allows the web programmers to tighten the belt on a CSP-protected page without violating the policy set out by the network (possibly set by the sysadmins).


===Conflicting <tt>report-uri</tt> values===
===Conflicting <tt>report-uri</tt> values===
If multiple headers define policies with <tt>report-uri</tt> values, a single report is sent to each of the provided URIs.  If any <tt>report-uri</tt> values are the same, only one report for each violation is sent to that URI; essentially one report is sent to each distinct URI upon policy violation.
User Agents MUST send a single report to each URI when multiple headers define policies with different <tt>report-uri</tt> values


Report-duplication (or multiple reporting) is useful in the case where two different groups want to receive reports, but may not share access to the reports archive.  Take for instance a large web company that has a separate sysadmin staff (who are also in charge of security at some level) and project teams.  One project team may be interested in receiving reports about violations of their CSP, but are not interested in violations on other parts of the web site.  The sysadmin team wants to record all violations from all parts of the site into a massive archive.  The multiple reporting technique allows both entities to receive the reports they want without causing extra data-mining work on the part of the sysadmin team to isolate the reports that each project team may want.
''Example: Report-duplication (or multiple reporting) is useful in the case where two different groups want to receive reports, but may not share access to the reports archive.  Take for instance a large web company that has a separate sysadmin staff (who are also in charge of security at some level) and project teams.  One project team may be interested in receiving reports about violations of their CSP, but are not interested in violations on other parts of the web site.  The sysadmin team wants to record all violations from all parts of the site into a massive archive.  The multiple reporting technique allows both entities to receive the reports they want without causing extra data-mining work on the part of the sysadmin team to isolate the reports that each project team may want.''


====Data Leak Vectors====
====Data Leak Vectors====
Since HTTP headers and the entire request string are sent in the report, it is possible that, in case of compromise, a violation report could leak private information to an arbitrary URI.  To avoid any possible cross-domain cookie or authentication token transfer, <b>all reports must be sent to the same origin (scheme/host/port) that served the protected content</b>.
Since HTTP headers and the entire request string are sent in the report, it is possible that, in case of compromise, a violation report could leak private information to an arbitrary URI.   
 
To avoid any possible cross-domain cookie or authentication token transfer, User Agents MUST only transmit reports to ''the same origin (scheme, host, and port)'' that served the protected content.


===Policy Refinement Procedure===
===Policy Refinement Procedure===


Two headers present conflicting policies, they are resolved through a straightforward process: first the policies are made explicit (see below), then they are intersected, and the resulting policy is enforced.  Through this process, the enforced policy will <i>never be more lenient</i> than either of the conflicting policies.
User Agents MUST resolve two headers present conflicting policies through the following process:
# The policies are made explicit (see below)
# They are intersected
# The resulting policy is enforced.   


[[Image:CSP_Policy_Refinement_Overview.png|Policy Refinement Overview]]
[[Image:CSP_Policy_Refinement_Overview.png|Policy Refinement Overview]]


This refinement procedure is only followed if there are multiple instances of the <tt>X-Content-Security-Policy</tt> HTTP header present in the HTTP response.
User Agents MUST follow this refinement procedure when there are multiple instances of the <tt>X-Content-Security-Policy</tt> HTTP header present in the HTTP response, or multiple policies are present in the HTTP header (separated by commas).


====Making a Policy Explicit====
====Making a Policy Explicit====
Line 449: Line 439:


[[Image:CSP_Directive_Intersect.png|Intersecting two directives]]
[[Image:CSP_Directive_Intersect.png|Intersecting two directives]]


==Handling Parse Errors==
==Handling Parse Errors==
A number of different problems may be encountered while parsing the CSP policy.
A number of different problems may be encountered while parsing the CSP policy.
These parse errors are not <em>policy violations</em>, and any error messages caused by parse errors are separate from the violation report sites may elect to receive when their site's <em>policies are violated</em>, via the report-uri directive.  Parse errors are only reported locally in the user agent.
These parse errors are not <em>policy violations</em>, and any error messages caused by parse errors are separate from the violation report sites may elect to receive when their site's <em>policies are violated</em>, via the report-uri directive.   


;Unrecognized Directive: If an unrecognized directive (name not recognized) is encountered by CSP, the directive and its value are skipped (up to a semicolon or end of header, whichever is first) and a warning message is logged to the Error Console stating the unrecognized directive name.
User Agents SHOULD report parse errors locally in the user agent's error console.


;Unrecognized <tt>options</tt> token: If an unrecognized token is present in the <tt>options</tt> directive value, it is ignored and a warning message is logged to the Error Console stating the unrecognized token.
;Unrecognized Directive: If an unrecognized directive (name not recognized) is encountered by a User Agent, the directive and its value are skipped (up to a semicolon or end of header, whichever is first) and the User Agent SHOULD report a warning message to the Error Console stating the unrecognized directive name.


;Missing "allow": If the "allow" directive is not present, a warning message is logged to the Error Console and "allow none" is assumed by the policy.  The rest of the policy is enforced as written assuming no other policy errors are encountered.
;Unrecognized <tt>options</tt> token: If an unrecognized token is present in the <tt>options</tt> directive value, the User Agent MUST ignore it and SHOULD report a warning message to the Error Console stating the unrecognized token.


;Directive Syntax Error: When any known directive contains a value that violates [[Security/CSP/Spec#Policy_Language_and_Syntax|CSP syntax]], Content Security Policy follows a "fail closed" security model and falls back to the most secure policy, "allow none".
;Missing "allow": If the "allow" directive is not present, the User Agent SHOULD report a warning message to the Error Console and MUST assume the directive value "allow 'none'" for the policy.  The User Agent MUST enforce the rest of the policy as written (assuming no other policy errors are encountered).


;No Recognized Directives: If no recognized directives are present in the stated policy, a warning message will be logged to the Error Console stating "invalid policy", and CSP will enforce the policy "allow none".
;Directive Syntax Error: When any known directive contains a value that violates [[Security/CSP/Spec#Policy_Language_and_Syntax|CSP syntax]], the User Agent SHOULD report a warning message stating the invalid syntax to the Error Console AND MUST "fail closed" by enforcing the most secure policy, "allow 'none'" for the protected document.


;Other Parsing Errors: Any other parsing errors not covered here may cause CSP to fail closed.  If such a case should arise, a message will be logged to the Error Console describing the violation.
;No Recognized Directives: If no recognized directives are present in the stated policy, the User Agent SHOULD report a warning message to the Error Console stating "invalid policy" AND MUST enforce the policy "allow 'none'" on the protected document.


;Other Parsing Errors: Any other parsing errors not covered here SHOULD cause the User Agent to enforce the policy "allow 'none'".  If such a case should arise, the User Agent SHOULD report a descriptive error to the Error Console describing the problem.


=Non-Normative Client-Side Considerations=
=Non-Normative Client-Side Considerations=
Line 478: Line 467:


== Report-Only mode ==
== Report-Only mode ==
To ease deployment, CSP can be deployed in "report-only" mode where a policy served is not enforced, but any violations are reported to a provided URI.  The effect is a "what if" scenario where a site can specify a policy and measure how much breaks.
To ease deployment, CSP can be deployed in "report-only" mode where a policy served is not enforced, but any violations are reported to a provided URI.  The effect is a "what if" scenario where a site can specify a policy and measure how much breaks.  Additionally, a report-only header can be used to test a future revision to a policy without actually deploying it.


Report-only mode is enabled by specifying a policy in the <tt>X-Content-Security-Policy-Report-Only</tt> header instead of the <tt>X-Content-Security-Policy</tt> header.   
Report-only mode is enabled by specifying a policy in the <tt>X-Content-Security-Policy-Report-Only</tt> header instead of the <tt>X-Content-Security-Policy</tt> header.   


If both a <tt>X-Content-Security-Policy-Report-Only</tt> header and a <tt>X-Content-Security-Policy</tt> header are present in the same response, a warning is posted to the user agent's error console and any policy specified in <tt>X-Content-Security-Policy-Report-Only</tt> is ignoredThe policy specified in <tt>X-Content-Security-Policy</tt> headers is enforced.
If both a <tt>X-Content-Security-Policy-Report-Only</tt> header and a <tt>X-Content-Security-Policy</tt> header are present in the same response, both policies are honored.  The policy specified in <tt>X-Content-Security-Policy</tt> headers is enforcedAll loads/scripts are compared the one specified in the <tt>X-Content-Security-Policy-Report-Only</tt> header, and any violations generate reports but are not enforced.
 


=HTTP Server Behavior=
=HTTP Server Behavior=


==HTTP Header Placement==
;HTTP Header Placement:
The <tt>X-Content-Security-Policy</tt> HTTP Response header should be present in the [http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 Message Headers] section of a server's HTTP response.  Specifically, it must NOT appear in the [http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.40 Trailer Headers] section of the response, so that the policy may be enforced as the rest of the page content loads.  Multiple <tt>X-Content-Security-Policy</tt> Response headers will be considered; if more than one is present, the intersection of the policies is enforced.
The <tt>X-Content-Security-Policy</tt> HTTP Response header MAY be present in the [http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 Message Headers] section of a server's HTTP response.  Specifically, it MUST NOT appear in the [http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.40 Trailer Headers] section of the response, so that the policy may be enforced as the rest of the page content loads.  Multiple <tt>X-Content-Security-Policy</tt> Response headers MAY be inserted.
canmove, Confirmed users
1,537

edits