canmove, Confirmed users
1,537
edits
No edit summary |
|||
(15 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 | * 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 | * 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 | * 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 | * 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 | * 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 | * 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 | * 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 | * 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 | * 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 | * 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 | * 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 | * 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 | * 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 | 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 < | <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- | | <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 | User Agents MUST NOT honor redirection responses. | ||
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 | 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 | Violation Report JSON Format: | ||
{ | |||
csp-report: { | |||
request: "GET /index.html HTTP/1.1", | |||
request-headers: "Host: example.com | |||
User-Agent: ... | |||
...", | |||
blocked-uri: "...", | |||
violated-directive: "..." | |||
} | |||
} | |||
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/ | |||
===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 | 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 http://index.html HTTP/1.1", | |||
"request-headers": "Host: example.com | |||
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: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 | |||
Accept-Language: en-us,en;q=0.5 | |||
Accept-Encoding: gzip,deflate | |||
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 | |||
Keep-Alive: 115 | |||
Connection: keep-alive", | |||
"blocked-uri": "<nowiki>http://evil.com/some_image.png</nowiki>", | |||
"violated-directive": "img-src 'self'", | |||
"original-policy": "allow 'none'; img-src *; allow 'self'; img-src 'self'" | |||
} | |||
} | |||
=User Agent Behavior= | =User Agent Behavior= | ||
Line 313: | Line 312: | ||
</font> | </font> | ||
<font color="#060"> | <font color="#060"> | ||
* User Agents MUST | * 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 | * 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 | * 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 | * 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 451: | Line 450: | ||
;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. | ;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. | ||
;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). | ;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). | ||
;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. | ;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. | ||
;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. | ;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. | ;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. | ||
Line 468: | 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, | 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 enforced. All 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= |