ArticleslgStudy

computer science

Content Security Policy

Content Security Policy is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Content Security Policy rather than just read about it. In short: Content Security Policy (CSP) is a computer security standard introduced to prevent cross-site scripting (XSS), clickjacking and other code injection attacks resulting from execution of malicious content in the trusted web page context. It is a Candidate Recommendation of the W3C working group on Web Application Security, widely supported by modern web browsers.

Content Security Policy — main illustration
Content Security Policy — illustration

Key takeaways

  • Content Security Policy belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Content Security Policy to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Content Security Policy from memory before moving on to harder problems.

Reference excerpt

Content Security Policy (CSP) is a computer security standard introduced to prevent cross-site scripting (XSS), clickjacking and other code injection attacks resulting from execution of malicious content in the trusted web page context. It is a Candidate Recommendation of the W3C working group on Web Application Security, widely supported by modern web browsers. CSP provides a standard method for website owners to declare approved origins of content that browsers should be allowed to load on that website—covered types are JavaScript, CSS, HTML frames, web workers, fonts, images, embeddable objects such as Java applets, ActiveX, audio and video files, and other HTML5 features.

Status The standard, originally named Content Restrictions, was proposed by Robert Hansen in 2004, first implemented in Firefox 4 and quickly picked up by other browsers. Version 1 of the standard was published in 2012 as W3C candidate recommendation and quickly with further versions (Level 2) published in 2014. As of 2023, the draft of Level 3 is being developed with the new features being quickly adopted by the web browsers. The following header names are in use as part of experimental CSP implementations:

Content-Security-Policy – standard header name proposed by the W3C document. Google Chrome supports this as of version 25. Firefox supports this as of version 23, released on 6 August 2013. WebKit supports this as of version 528 (nightly build). Chromium-based Microsoft Edge support is similar to Chrome's. X-WebKit-CSP – deprecated, experimental header introduced into Google Chrome, Safari and other WebKit-based web browsers in 2011. X-Content-Security-Policy – deprecated, experimental header introduced in Gecko 2 based browsers (Firefox 4 to Firefox 22, Thunderbird 3.3, SeaMonkey 2.1). A website can declare multiple CSP headers, also mixing enforcement and report-only ones. Each header will be processed separately by the browser. CSP can also be delivered within the HTML code using a meta tag, although in this case its effectiveness will be limited. Internet Explorer 10 and Internet Explorer 11 also support CSP, but only sandbox directive, using the experimental X-Content-Security-Policy header. A number of web application frameworks support CSP, for example AngularJS (natively) and Django (middleware). Instructions for Ruby on Rails have been posted by GitHub. Web framework support is however only required if the CSP contents somehow depend on the web application's state—such as usage of the nonce origin. Otherwise, the CSP is rather static and can be delivered from web application tiers above the application, for example on load balancer or web server.

Bypasses In December 2015 and December 2016, a few methods of bypassing 'nonce' allowlisting origins were published. In January 2016, another method was published, which leverages server-wide CSP allowlisting to exploit old and vulnerable versions of JavaScript libraries hosted at the same server (frequent case with CDN servers). In May 2017 one more method was published to bypass CSP using web application frameworks code.

Mode of operation

If the Content-Security-Policy header is present in the server response, a compliant client enforces the declarative allowlist policy. One example goal of a policy is a stricter execution mode for JavaScript in order to prevent certain cross-site scripting attacks. In practice this means that a number of features are disabled by default:

Inline JavaScript code <script> blocks, DOM event handlers as HTML attributes (e.g. onclick) The javascript: links Inline CSS statements <style> block style attributed to HTML elements Dynamic JavaScript code evaluation eval() string arguments for setTimeout and setInterval functions new Function() constructor Dynamic CSS statements CSSStyleSheet.insertRule() method While using CSP in a new application may be quite straightforward, especially with CSP-compatible JavaScript framework, existing applications may require some refactoring—or relaxing the policy. Recommended coding practice for CSP-compatible web applications is to load code from external source files (<script src>), parse JSON instead of evaluating it and use EventTarget.addEventListener() to set event handlers.

Notes

Reporting Any time a requested resource or script execution violates the policy, the browser will fire a POST request to the value specified in report-uri or report-to containing details of the violation. CSP reports are standard JSON structures and can be captured either by application's own API or public CSP report receivers. In 2018 security researchers showed how to send false positive reports to the designated receiver specified in report-uri. This allows potential attackers to arbitrarily trigger those alarms and might render them less useful in case of a real attack. This behaviour is intended and cannot be fixed, as the browser (client) is sending the reports.

Website administrators can test a proposed Content Security Policy using the Content-Security-Policy-Report-Only HTTP response header. This allows browsers to monitor policy violations without blocking resources that violate the proposed policy, and to send reports when a reporting endpoint is configured. Unlike an enforced Content Security Policy, a report-only policy cannot be delivered through an HTML <meta> element.

Browser add-ons and extensions exemption According to the original CSP (1.0) Processing Model (2012–2013), CSP should not interfere with the operation of browser add-ons or extensions installed by the user. This feature of CSP would have recommended the allowance of any add-on, extension, or Bookmarklet to inject script into web sites, regardless of the origin of that script, and thus be exempt from CSP policies.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Content Security Policy

Start with the simplest possible case. Write down what Content Security Policy claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Content Security Policy before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Content Security Policy ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Content Security Policy

In research
Content Security Policy appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Content Security Policy in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Content Security Policy is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer security standards, Hacking (computer security), Hypertext Transfer Protocol headers, so understanding it makes those chapters shorter.
In everyday life
Look for Content Security Policy outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Content Security Policy” →

How to study Content Security Policy in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Content Security Policy means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Content Security Policy out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Content Security Policy in simple terms?

Content Security Policy (CSP) is a computer security standard introduced to prevent cross-site scripting (XSS), clickjacking and other code injection attacks resulting from execution of malicious content in the trusted web page context. It is a Candidate Recommendation of the W3C working group on W…

Why does Content Security Policy matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Content Security Policy?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Content Security Policy.

Tags

  • Computer security standards
  • Hacking (computer security)
  • Hypertext Transfer Protocol headers
  • Web applications
  • Web security exploits

Keep exploring