Traducción en curso

 


el Header HTTP Content-Security-Policy de respuesta permite a los administradores de un sitio web controlar los recursos que el User-Agent puede cargar a una pagina. Con algunas (Poquísimas) excepciones, las políticas implican principalmente especificar el servidor de origen la protección de puntos finales del script. Esto ayuda a protegerse contra ataques Cross-site scripting (XSS).

 

Para mas información, ve también este articulo en Content Security Policy (CSP).

 
Header type Response header
Forbidden header name no

Sintaxe

Content-Security-Policy: <policy-directive>; <policy-directive>

Directivas

Fetch directives

"Fetch directives" controla la ubicación o ubicaciones desde la cual se pueden cargar ciertos tipos de recursos

child-src
Define los recursos validos para web workers.  y contextos de navegación anidados cargados usando elementos como <frame> and <iframe>.

En lugar de child-src, los autores quienes deseen regular los contextos de navegación anidados y "workers" deberían usar las directivas frame-src y worker-src, respectivamente.

connect-src
Restricts the URLs which can be loaded using script interfaces
default-src
Serves as a fallback for the other fetch directives.
font-src
Specifies valid sources for fonts loaded using @font-face.
frame-src
Specifies valid sources for nested browsing contexts loading using elements such as <frame> and <iframe>.
img-src
Specifies valid sources of images and favicons.
manifest-src
Specifies valid sources of application manifest files.
media-src
Specifies valid sources for loading media using the <audio> , <video> and <track> elements.
object-src
Specifies valid sources for the <object>, <embed>, and <applet> elements.
Elements controlled by object-src are perhaps coincidentally considered legacy HTML elements and aren't recieving new standardized features (such as the security attributes sandbox or allow for <iframe>). Therefore it is recommended to restrict this fetch-directive (e.g. explicitly set object-src 'none' if possible).
prefetch-src
Specifies valid sources to be prefetched or prerendered.
script-src
Specifies valid sources for JavaScript.
style-src
Specifies valid sources for stylesheets.
webrtc-src
Specifies valid sources for WebRTC connections.
worker-src
Specifies valid sources for Worker, SharedWorker, or ServiceWorker scripts.

Document directives

Document directives govern the properties of a document or worker environment to which a policy applies.

base-uri
Restricts the URLs which can be used in a document's <base> element.
plugin-types
Restricts the set of plugins that can be embedded into a document by limiting the types of resources which can be loaded.
sandbox
Enables a sandbox for the requested resource similar to the <iframe> sandbox attribute.
disown-opener
Ensures a resource will disown its opener when navigated to.

Navigation directives govern to which location a user can navigate to or submit a form to, for example.

form-action
Restricts the URLs which can be used as the target of a form submissions from a given context.
frame-ancestors
Specifies valid parents that may embed a page using <frame>, <iframe>, <object>, <embed>, or <applet>.
navigate-to
Restricts the URLs to which a document can navigate by any means (a, form, window.location, window.open, etc.)

Reporting directives

Reporting directives control the reporting process of CSP violations. See also the Content-Security-Policy-Report-Only header.

report-uri
Instructs the user agent to report attempts to violate the Content Security Policy. These violation reports consist of JSON documents sent via an HTTP POST request to the specified URI.

Though the report-to directive is intended to replace the deprecated report-uri directive, report-to isn’t supported in most browsers yet. So for compatibility with current browsers while also adding forward compatibility when browsers get report-to support, you can specify both report-uri and report-to:

Content-Security-Policy: ...; report-uri https://endpoint.example.com; report-to groupname

In browsers that support report-to, the report-uri directive will be ignored.

report-to
Fires a SecurityPolicyViolationEvent.

Other directives

block-all-mixed-content
Prevents loading any assets using HTTP when the page is loaded using HTTPS.
referrer
Used to specify information in the referer (sic) header for links away from a page. Use the Referrer-Policy header instead.
require-sri-for
Requires the use of SRI for scripts or styles on the page.
trusted-types
Used to specify a whitelist of Trusted Types policies (Trusted Types allows applications to lock down DOM XSS injection sinks to only accept non-spoofable, typed values in place of strings).
upgrade-insecure-requests
Instructs user agents to treat all of a site's insecure URLs (those served over HTTP) as though they have been replaced with secure URLs (those served over HTTPS). This directive is intended for web sites with large numbers of insecure legacy URLs that need to be rewritten.

CSP in workers

Workers are in general not governed by the content security policy of the document (or parent worker) that created them. To specify a content security policy for the worker, set a Content-Security-Policy response header for the request which requested the worker script itself.

The exception to this is if the worker script's origin is a globally unique identifier (for example, if its URL has a scheme of data or blob). In this case, the worker does inherit the content security policy of the document or worker that created it.

Multiple content security policies

CSP allows multiple policies being specified for a resource, including via the Content-Security-Policy header, the Content-Security-Policy-Report-Only header and a <meta> element.

You can use the Content-Security-Policy header more than once like in the example below. Pay special attention to the connect-src directive here. Even though the second policy would allow the connection, the first policy contains connect-src 'none'. Adding additional policies can only further restrict the capabilities of the protected resource, which means that there will be no connection allowed and, as the strictest policy, connect-src 'none' is enforced.

Content-Security-Policy: default-src 'self' http://example.com;
                         connect-src 'none';
Content-Security-Policy: connect-src http://example.com/;
                         script-src http://example.com/

Examples

Example: Disable unsafe inline/eval, only allow loading of resources (images, fonts, scripts, etc.) over https:

// header
Content-Security-Policy: default-src https:

// meta tag
<meta http-equiv="Content-Security-Policy" content="default-src https:">

Example: Pre-existing site that uses too much inline code to fix but wants to ensure resources are loaded only over https and disable plugins:

Content-Security-Policy: default-src https: 'unsafe-eval' 'unsafe-inline'; object-src 'none'

Example: Don't implement the above policy yet; instead just report violations that would have occurred:

Content-Security-Policy-Report-Only: default-src https:; report-uri /csp-violation-report-endpoint/

See Mozilla Web Security Guidelines for more examples.

Specifications

Specification Status Comment
Content Security Policy Level 3 Working Draft Adds disown-opener, manifest-src, navigate-to, report-to, strict-dynamic, worker-src. Undeprecates frame-src. Deprecates report-uri in favor if report-to.
Mixed Content Candidate Recommendation Adds block-all-mixed-content.
Subresource Integrity Recommendation Adds require-sri-for.
Upgrade Insecure Requests Candidate Recommendation Adds upgrade-insecure-requests.
Content Security Policy Level 2 Recommendation Adds base-uri, child-src, form-action, frame-ancestors, plugin-types, referrer, and report-uri. Deprecates frame-src.
Content Security Policy 1.0 Obsolete Defines connect-src, default-src, font-src, frame-src, img-src, media-src, object-src, report-uri, sandbox, script-src, and style-src.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Content-Security-PolicyChrome Soporte completo 25
Soporte completo 25
Soporte completo 14
Nombre alternativo
Nombre alternativo Usa un nombre no estandar : X-Webkit-CSP
Edge Soporte completo 14Firefox Soporte completo 23
Soporte completo 23
Soporte completo 4
Nombre alternativo
Nombre alternativo Usa un nombre no estandar : X-Content-Security-Policy
IE Soporte completo 10
Notas Nombre alternativo
Soporte completo 10
Notas Nombre alternativo
Notas Only supporting 'sandbox' directive.
Nombre alternativo Usa un nombre no estandar : X-Content-Security-Policy
Opera Soporte completo 15Safari Soporte completo 7
Soporte completo 7
Soporte completo 6
Nombre alternativo
Nombre alternativo Usa un nombre no estandar : X-Webkit-CSP
WebView Android Soporte completo SiChrome Android Soporte completo SiEdge Mobile Soporte completo SiFirefox Android Soporte completo 23Opera Android Soporte completo SiSafari iOS Soporte completo 7.1
Soporte completo 7.1
Soporte completo 5.1
Notas
Notas X-Webkit-CSP
Samsung Internet Android Soporte completo Si
<meta> element supportChrome Soporte completo SiEdge Soporte completo SiFirefox Soporte completo 45IE Sin soporte NoOpera Soporte completo SiSafari Soporte completo SiWebView Android Soporte completo SiChrome Android Soporte completo SiEdge Mobile Soporte completo SiFirefox Android Soporte completo 45Opera Android Soporte completo SiSafari iOS Soporte completo SiSamsung Internet Android Soporte completo Si
Worker supportChrome Soporte completo SiEdge ? Firefox Soporte completo 50IE Sin soporte NoOpera ? Safari Sin soporte NoWebView Android Soporte completo SiChrome Android Soporte completo SiEdge Mobile ? Firefox Android Soporte completo 50Opera Android ? Safari iOS Sin soporte NoSamsung Internet Android Soporte completo Si

Leyenda

Soporte completo  
Soporte completo
Sin soporte  
Sin soporte
Compatibility unknown  
Compatibility unknown
Ver notas de implementación.
Ver notas de implementación.
Usa un nombre no estandar.
Usa un nombre no estandar.

See also

Etiquetas y colaboradores del documento

Etiquetas: 
Colaboradores en esta página: 27z
Última actualización por: 27z,