Click “Open invoice” and a document appears inside the page. Your browser—the program you use to open websites—can run a small program that makes this button work. A program is a set of instructions a computer carries out. A program used within a page is called a script. 25

The script can ask the website for the invoice you selected. The website’s server, the computer or service answering requests over the internet, sends a result back. The script then shows it inside the page, without opening a completely new page. The protection we will examine concerns what the browser may load or run; it is not a rule controlling every program on the server. 25

The main page document describes the content and structure: where headings and buttons belong, for example. This format is called HTML. A script supplies behaviour, such as the invoice button’s response to a click, and is commonly written in JavaScript. Code is the written instructions, not the invoice data they handle. 25

Scripts are useful because they can act, not merely display words. That is also why unwanted script instructions are more serious than unwanted text. Suppose the portal shows a note entered by a customer. The intended behaviour is to show the note as text. If a programming flaw lets part of that note be interpreted as executable instructions, someone might insert a script that changes what the user sees or attempts actions available through the page. The browser does not automatically know that those instructions came from an unwelcome author. This class of mistake is often called cross-site scripting: unwanted code runs in the context of a legitimate page. 1

The first repair belongs in the service’s own program, its application: it must handle customer content safely rather than turn it into executable instructions. A second protection can limit which code the browser is willing to run even if an unwanted piece reaches the page. The website sends these permission rules with the page, and the browser checks relevant activity against them. This additional protection is called Content Security Policy, usually shortened to CSP. It restricts loading and execution; it does not repair the flaw that admitted the unwanted content. 1 6

A guest list, not a character reference

Think of CSP as a guest list for a particular page. The developer specifies what is allowed, and the browser checks relevant activity against those instructions. An enforced rule can stop something that does not qualify. But being admitted does not prove that a guest behaves well. Code that the policy permits still needs to be trustworthy. 6 7

Return to the invoice viewer. It might need the portal’s own scripts and a document-viewing component from another service. An appropriate policy can allow the required code while restricting other code. The developer must understand how the viewer works before deciding what to permit. Allowing everything merely because it removes errors would discard much of the point. 6

This is why “CSP installed” is an incomplete answer. A guest list accepting everybody and a guest list accepting a carefully chosen group are both lists. Likewise, the existence of a policy does not tell you how restrictive it is.

Follow the browser’s decision in our example. The genuine viewer is permitted by the page’s policy. An inserted instruction tries to load a different program from a place the policy does not allow. Before running that program, the browser checks the applicable script permission and refuses the disallowed load. The user need not notice or click “block”: enforcement is the browser’s job. This is a possible protective outcome, not a promise that every imaginable inserted instruction is stopped by every CSP. 6 7

Permitting an address has a limit: that location might also supply unwanted code, not just our invoice viewer. Developers can approve particular scripts more precisely instead of trusting every script from a location. The technical extra at the end explains two ways of doing that. Neither way checks that an approved program is harmless. 7

The important distinction is now concrete: the browser can refuse the unexpected program because it does not meet the page’s rule. A program that does meet the rule can still have a bug. The developer must also keep the rule in step with changes to the viewer.

The page needs more than one kind of permission

A picture, a script and a connection carrying data are different activities. CSP can give them different rules. A portal may legitimately display a company logo from one service without needing to run that service’s programs. Treating every external resource as the same thing makes the decision harder to understand. 6

A script may be fetched as a separate file or written directly into the main page document. A policy that broadly permits the latter can let unwanted inserted instructions run as well. The developer therefore needs to explain how intended code is approved, not merely whether the browser has received a policy. 7

The permission rules also need to stay in step with the page. Adding a new document viewer can change what code and connections the page needs. The developer should revisit the policy with that change, rather than assuming a rule written for the old viewer will remain suitable. 6

Watch first, then enforce the rules

Suppose the developer tightens the invoice page’s rules. The document viewer unexpectedly needs another resource. Enforcing the new rules immediately could stop the viewer from working. That is a compatibility problem to investigate, not a reason to abandon the protection or permit everything. 6

CSP provides a report-only mode for this stage. Under the proposed policy, the browser can report what would be disallowed without blocking it because of that proposal. An existing enforced policy can remain in place while a stricter proposal is observed. Reporting and enforcement are therefore different states, not interchangeable descriptions of protection. 6

In our example, the developer can open a made-up invoice in an authorized test environment and see whether the intended viewing and download functions work. The reports can help identify what the page actually needs. After adjusting the proposal, the developer can check it in enforced mode.

A reassuring explanation would be: “We are checking the new restriction because the viewer needs a resource we had not accounted for.” A misleading one would be: “We have report-only CSP, so the browser already blocks everything on the proposed list.”

What CSP cannot decide for you

Even a carefully designed CSP cannot decide whether the amount on an invoice is correct or whether a particular employee should approve it. Nor does permission for a script to run prove that the script has no bugs. The application and the people operating it still have their own work to do. CSP is an additional browser defence, not a substitute for sound application design. 6 7

The page also matters. A policy seen on a supplier’s public blog does not establish the policy on its signed-in invoice screen. It tells you nothing by itself about a separate service that sends invoices between systems. If your staff use the invoice screen, that is the relevant page to discuss.

You can make the conversation concrete without asking for a block of code: “What should our invoice page be allowed to run, what should it reject, and are those rules actually enforced?” Those three questions reveal much more than the colour beside CSP in a report.

Technical extra: hashes and nonces

For a script written directly into the page, one approval method uses a hash: a fingerprint calculated from the script’s text. The policy supplies the expected fingerprint. The browser calculates the fingerprint of the script it received and compares them. A match permits that script under this rule; changed text needs a newly approved fingerprint. This identifies the text, not whether its instructions are safe. 7

Another method uses a nonce: a fresh, unpredictable value generated for each page response. The application puts the value in the policy and on the intended script elements. The browser checks for the matching value before permitting them under this rule. The application must generate and attach it correctly; it is not a password that a visitor types. 7

If a report flags broad permission for code inside the page, it still has to be interpreted with the other applicable script rules and browser support. Some more specific approval mechanisms change how such permissions are treated. A highlighted word alone cannot establish that every inserted instruction would run. 7

Sources

[1] W3C: Content Security Policy Level 3 (Working Draft)

[6] MDN: Content-Security-Policy

[7] MDN: script-src

[25] MDN: How the web works