Content Security Policy
Extensions developed with WebExtension APIs have a Content Security Policy (CSP) applied to them by default. This restricts the sources from which they can load code such as and disallows potentially unsafe practices such as using eval()
. This article briefly explains what a CSP is, what the default policy is and what it means for an extension, and how an extension can change the default CSP.
Content Security Policy (CSP) is a mechanism to help prevent websites from inadvertently executing malicious content. A website specifies a CSP using an HTTP header sent from the server. The CSP is mostly concerned with specifying legitimate sources of various types of content, such as scripts or embedded plugins. For example, a website can use it to specify that the browser should only execute JavaScript served from the website itself, and not from any other sources. A CSP can also instruct the browser to disallow potentially unsafe practices, such as the use of eval()
.
Like websites, extensions can load content from different sources. For example, a browser action's popup is specified as an HTML document, and it can include JavaScript and CSS from different sources, just like a normal web page:
Compared to a website, extensions have access to additional privileged APIs, so if they are compromised by malicious code, the risks are greater. For this reason:
- a fairly strict content security policy is applied to extensions by default. See default content security policy.
- the extension's author can change the default policy using the
content_security_policy
manifest.json key, but there are restrictions on the policies that are allowed. Seecontent_security_policy
.
Default content security policy
The default content security policy for extensions using Manifest V2 is:
"script-src 'self'; object-src 'self';"
While for extensions using Manifest V3, the default content security policy is:
"script-src 'self'; upgrade-insecure-requests;"
These policies are applied to any extension that has not explicitly set its own content security policy using the content_security_policy
manifest.json key. It has the following consequences:
This doesn't load the requested resource: it fails silently, and any object that you expect to be present from the resource is not found. There are two main solutions to this:
- download the resource, package it in your extension, and refer to this version of the resource.
- allow the remote origin you need using the
content_security_policy
key or, in Manifest V3, thecontent_scripts
property.
Note: If your modified CSP allows remote script injection, your extension will get rejected from addons.mozilla.org (AMO) during the review. For more information, see details about security best practices.