CSP - alexanderteplov/computer-science GitHub Wiki

Content Security Policy

Content security policy mechanism with content-security-policy header aims to restrict downloading a resource by its origin. Possible values: self, "https://some-url.com".

Example

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

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.

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.

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:
Content-Security-Policy: default-src 'self'; report-uri http://reportcollector.example.com/collector.cgi

Directives

Fetch directives

Fetch directives control the locations from which certain resource types may be loaded.

Document directives

Document directives govern the properties of a document or worker environment to which a policy applies. (default-src, font-src, frame-src, image-src, ...)

Navigation directives

Navigation directives govern to which locations a user can navigate or submit a form, for example. (base-uri, sandbox)

Reporting directives

Reporting directives control the reporting process of CSP violations. (form-action, frame-ancestors)

Links

⚠️ **GitHub.com Fallback** ⚠️