Cybersecurity

Content Security Policy for an Online Shop: Keep Injected Scripts Out of Your Checkout

12 November 2026 · Matteo Arellano
← All cybersecurity articles

Cybersecurity · Work Journal

What a Content Security Policy is, why inline scripts are its weak spot, how to list the tools an online shop really uses, and why the checkout page needs the strictest rules. With the video, a worked example and a checklist.

cybersecurityContent Security PolicyCSPeCommercecheckoutpaymentsvideo
Watch: One Line of Text That Stops Website Attacks (Content Security Policy, Explained Simply)

A Content Security Policy (CSP) is a list of rules that tells the browser which scripts may run on your page. The video above explains the basics with one example: Sofia’s online furniture shop, and a small program hidden inside a review.

This article stays with Sofia’s shop and goes one step further. It shows how to build a strict policy for an online shop, from the product pages to the checkout.

The short version:

  • Put your own scripts in files, and stop scripts written inside the HTML from running.
  • List every outside tool your shop really uses, and allow only those hosts.
  • Give the checkout page its own, stricter list. It is the page thieves want most.
  • Test in report-only mode before you switch the policy on.

At the end there is a section on card payments and a checklist.

What a Content Security Policy is

Think of your shop as a private event and the browser as the person at the door. The Content Security Policy is the guest list. Your server sends it with every page, and the browser refuses anything that is not on it.

The list travels as a response header. A small one for Sofia’s shop looks like this:

Content-Security-Policy: default-src 'self'; script-src 'self' https://reviews.example

In words: load things only from my own site ('self'), and run scripts only from my own site or from reviews.example, the company that runs the review widget. Each rule in the list covers one kind of content.

Rule What it controls
script-src Which scripts may run
style-src Which styles may apply
img-src Where images may come from, such as product photos on a separate image host
frame-src Which sites you may embed in a frame, such as a video or a payment form
connect-src Where scripts may send or fetch data
frame-ancestors Which sites may embed your page
default-src The fallback for kinds of content you did not list

When the browser blocks something, it writes a red line in the developer console that names the rule. That line is your main tool while you set a policy up.

A policy can also block things you want. I wrote about one such case in Why Your Security Policy Can Block Google Ads Conversion Tracking.

The weak spot: inline scripts

A policy that allows inline scripts stops very little. To see why, you need the two ways a script can reach a page.

  • As a file. The page points to it: <script src="/js/basket.js"></script>. The browser knows which host the file came from.
  • Inline. The code is written inside the HTML: <script> ...code... </script>. It has no host. It is simply part of the page.

Now go back to Sofia’s shop. A stranger writes a product review, and hides a small inline script inside the text. If the shop shows the review without cleaning it, the script becomes part of the product page. This attack is called cross-site scripting (XSS). To the browser, the stranger’s inline script and Sofia’s own inline script look the same.

That is the problem with the keyword 'unsafe-inline'. In script-src it means “run every inline script”, and that includes the stranger’s. A policy with 'unsafe-inline' can earn a green mark on a header scanner and still let injected code run.

Animation: three scripts arrive at a shop page. A script file from the shop’s own site passes the policy and runs. The payment provider’s script comes from a host on the list, so it passes and runs. An injected inline script is not on the list and is blocked.

This animation is plain CSS inside an image file. No script runs, so it works under the same strict policy this article describes.

Inline styles are a smaller risk. Many embedded widgets need 'unsafe-inline' in style-src to draw themselves, and keeping it there is a common and reasonable trade. The rule to remove is the one for scripts.

Step one: move your own scripts into files

Once all of your own scripts are files on your own domain, script-src 'self' allows them and you can drop 'unsafe-inline'. For most shops this is tidy-up work.

  1. Move inline code into files. A size picker or a “show more photos” button written between <script> tags becomes a .js file that the page loads.
  2. Remove inline event attributes. onclick="addToBasket()" in the HTML counts as inline script. Attach the event from your script file instead.
  3. Pass values through the HTML, not through generated code. If a script needs a value, such as a product ID or a price, put it in a data- attribute on an element and let the script read it.
  4. Check your theme, plugins and build tools. Some of them paste small scripts back into the HTML to save a request. Look for a setting that turns this off.
  5. Check the finished pages, not the source. Open “View page source” in the browser and search for <script> tags without a src. That is the only view the browser gets.

One kind of inline block can stay: data blocks such as <script type="application/ld+json">, used for product structured data. The browser reads them as data and never runs them, so the policy does not block them.

If your shop runs on a hosted platform, you may not control every header or every script on the page. Check what your platform allows before you plan the policy.

Step two: list the tools your shop really uses

An online shop rarely runs only its own code. Reviews, analytics, chat, ads and payments usually come from other companies, and each one needs its host on the list. Here is Sofia’s list:

Tool Where it runs Rules that need its host
Basket, menu, size picker Her own site Covered by 'self'
Review widget Product pages script-src, connect-src
Analytics All pages except checkout script-src, connect-src, often img-src
Chat bubble Product pages script-src, connect-src, frame-src
Payment provider Checkout only script-src, frame-src, connect-src

Three tips make the list easier to build:

  • Use the provider’s documentation. Most payment, chat and review companies publish the exact hosts their tools need. Copy those, not a guess.
  • Watch out for tag managers. A tag manager can add new scripts without a change to your code. Every host it loads must be on the list, and any custom code it adds counts as inline script.
  • Remove what you do not use. An old tracking pixel from a campaign that ended last year is still a door into your page. If no one reads its data, delete it and remove its host.

When a script cannot be moved: hashes and nonces

Sometimes a tool insists on an inline script, and you cannot turn it into a file. You still do not need 'unsafe-inline'. A policy can allow one specific inline script in two ways.

  • A hash. The browser calculates a fingerprint of the script and compares it with a fingerprint in your policy, such as 'sha256-...'. This works when the script is exactly the same on every page view.
  • A nonce. A nonce is a random value, short for “number used once”. The server puts it in the policy header and on the allowed script, and the browser runs the script only if the two match. This works when the script changes from one page view to the next.

MDN’s CSP implementation guide gives a simple rule: use nonces if your server builds each page, and hashes if you serve the same static content to everyone. Google’s strict CSP guide calls a policy based on nonces or hashes a “strict CSP”.

A few things to know before you choose:

  • A nonce must be new and random for every response. MDN’s guidance is a value of at least 128 bits from a secure random generator. A fixed value typed into a settings file protects nothing.
  • A nonce needs code that runs on every request, because something has to create the value. A page with a nonce must also never be stored in a shared cache and served to many shoppers, because a value that everyone has is no longer a secret.
  • A hash breaks when the script changes. If a tool updates its inline code, the fingerprint no longer matches and the script is blocked.
  • Both turn off 'unsafe-inline'. Once a nonce or a hash is in script-src, browsers ignore 'unsafe-inline' in that rule. Any inline script you forgot to move will stop working, so finish step one first.

Use these only for the scripts you really cannot move. Every inline script you turn into a file is one less thing to manage.

Worked example: a stricter list for the checkout

Sofia’s product pages need reviews, analytics and chat. Her checkout needs none of them. The checkout needs only her own scripts and the payment provider.

So she sends two policies. Her server picks the policy by page: one for the shop, and a shorter one for every page under /checkout/.

Two policies for one shop. The product page allows the shop’s own scripts, the review widget, analytics and chat. The checkout page allows only the shop’s own scripts and the payment provider. Reviews, analytics and chat are not allowed on the checkout.

The product pages:

Content-Security-Policy: default-src 'self';
  script-src 'self' https://reviews.example https://analytics.example https://chat.example;
  connect-src 'self' https://reviews.example https://analytics.example https://chat.example;
  frame-src https://chat.example;
  img-src 'self' https://images.example https://analytics.example;
  object-src 'none'; base-uri 'self'

The checkout:

Content-Security-Policy: default-src 'self';
  script-src 'self' https://pay.example;
  connect-src 'self' https://pay.example;
  frame-src https://pay.example;
  img-src 'self' https://images.example;
  object-src 'none'; base-uri 'self'; frame-ancestors 'none'

Line breaks are added here so the policies are easy to read. In the real header, each policy is one line.

Now follow the stranger from the video. The script hidden in the review is inline and has no allowed host, so the browser blocks it on the product page. And even if one of the outside tools were tampered with one day, it does not load on the checkout at all. The page where customers type their card details has the shortest guest list in the shop.

Two lines in the checkout policy are worth a closer look. object-src 'none' blocks old plugin content that has no place in a shop. frame-ancestors 'none' stops other sites from showing your checkout inside their own page, a trick used to make shoppers click things they cannot see.

If your shop takes card payments

The safest way to take card payments is to keep card details off your own pages. A script on a page can read whatever a customer types into that page, and thieves who inject one small script into a checkout can copy every card number entered there. Your goal is to give such a script nothing to read.

Three ways to take a card payment, safest first. Redirect to the provider: the card number is typed on the provider’s site. Provider’s form in a frame: it is typed inside the provider’s frame, so lock down the page around it. Your own card form: it is typed on your page, where any script can read it, so avoid this.

With a redirect, the customer leaves your site before typing anything. With a frame, the card fields belong to the provider and your scripts cannot read inside them, but a script on your page could still replace the frame with a fake one. So the page around it must be locked down.

What to do on any page that shows a payment form:

  1. Use a strict policy. No 'unsafe-inline' and no 'unsafe-eval' in script-src. Allow only your own domain and the hosts your payment provider lists in its documentation, in script-src, frame-src and connect-src.
  2. Keep the page bare. No analytics, chat, ads or testing scripts on the checkout. Every script on that page is one more thing that could be tampered with.
  3. Pin the scripts you can. The integrity attribute (Subresource Integrity) makes the browser refuse a file that has changed. It suits third-party scripts with a fixed version. Payment providers usually update their own script and ask you to load it directly, so it does not fit there.
  4. Get reports. Add a reporting address to the policy so that blocked attempts in real shoppers’ browsers reach you, instead of staying in their consoles.
  5. Do the trusted work on your server. Keep the payment provider’s secret key on the server, never in the page or in your code repository. Set the amount on the server. Treat an order as paid only when the provider’s signed server-to-server notification says so, never because the browser showed a success page.

The card industry’s standard, PCI DSS, now asks for much of this. Since 31 March 2025, requirement 6.4.3 says every script on a payment page must be authorised, checked for integrity and listed with a reason for being there. Requirement 11.6.1 says you must detect unauthorised changes to security headers and script content. A Content Security Policy, Subresource Integrity and CSP reporting are the usual tools for meeting them.

Which rules apply to you depends on the setup. For the simplest self-assessment (SAQ A), the script condition does not apply when you redirect customers to the provider. If you embed the provider’s form, you must confirm that your page is not open to script attacks. Ask your payment provider or acquiring bank which questionnaire fits your shop. This section is general guidance, not a compliance assessment.

Rolling it out without breaking the shop

A policy that is too strict can stop the “Add to basket” button or the payment form. So send it in report-only mode first. The header name changes to Content-Security-Policy-Report-Only, and the browser then logs what it would block without blocking anything.

  1. Deploy in report-only mode. Open each kind of page: home, category, product, basket and checkout. Use every widget, video and form.
  2. Place a test order. Go through the full checkout with your payment provider’s test mode, including the redirect back to your shop and the confirmation page.
  3. Read the console. Look for lines that start with “Refused to”. Each one names a rule and a host. Decide if the host belongs on the list or the script should go.
  4. Enforce. Rename the header to Content-Security-Policy once the console is clean.
  5. Check the live shop, not your settings. After each change, open the browser’s network panel and look at the response headers on a real page. A policy that is in your settings but not in the response protects nothing.
  6. Review the list when the shop changes. A new chat tool, a new review provider or a new campaign pixel needs its host added, and the old one removed.

What a CSP does not protect

A Content Security Policy is a second lock, not the front door. The front door is cleaning what customers type: a shop should show a review as text, never as code. The policy limits the damage on the day something gets past that.

It does nothing about these:

  • A trusted host that gets compromised. If you allow scripts from reviews.example and that service is tampered with, the browser still runs its script. Allow as few hosts as you can, and keep the checkout list the shortest.
  • Bad code in your own files. Anything in your shop’s code, including a plugin or a library you installed, is served from your own domain and is allowed.
  • A stolen account. Whoever gets into your shop admin, your hosting or your domain registrar can change the shop and the policy. Turn on two-factor authentication on all of them.

Checklist

  • My own scripts are files. The finished HTML has no inline script that runs, except ones allowed by a hash or a nonce.
  • script-src has no 'unsafe-inline' and lists only the hosts the shop uses.
  • I listed every outside tool, with the hosts from its documentation, and removed the ones no one uses.
  • If I use nonces, each one is random, new for every response, and never served from a shared cache.
  • The checkout has its own, shorter policy: my own scripts and the payment provider only.
  • Card details are typed on the provider’s page or in the provider’s frame, and the secret key lives on the server.
  • The policy is sent as an HTTP header, and I checked it on the live shop.
  • I tested in report-only mode, including a full test order, before enforcing.
  • Blocked attempts are reported to an address I read.
  • Two-factor authentication is on for the shop admin, hosting and domain registrar.

Sources