Marketing wants analytics (a script that records visits) added, support wants a live chat widget. Each one is a single <script src> line. Now your checkout page runs code from two other companies, and that code sees whatever your users type there.
A third-party script is JavaScript you load from someone else's server: analytics, a live chat widget, ads, or a library from a CDN. One <script src> line gives that code exactly the same access as your own code. Picture giving your house key to a cleaning service. The key opens every room, not only the kitchen. And if the service's office is broken into, the thief is holding your house key too. The house key is access to your page, the cleaning service is the vendor (the company that makes the script), and the thief is the attacker who breaks into that vendor.
A third-party script runs with full rights
The browser doesn't treat your scripts differently from scripts from another domain. Once it's loaded through <script src>, the script runs inside your page, with your page's origin. The same-origin policy (the browser rule that keeps one origin away from another) doesn't help here, because to the browser that script is part of your page. So the chat widget loaded through <script src="https://widget.example.net/chat.js"> can:
- read and change everything on the page, including the values in form inputs;
- read cookies that aren't
HttpOnly, and whatever is inlocalStorage; - send requests anywhere your page allows.
That includes the card number typed into the checkout form: as long as the script runs in your page, that number can be read.
That doesn't mean the vendor is malicious. The risk is in the supply chain (everyone who helps provide your code). If the vendor's server, CDN or account is broken into, the attacker's code runs on every site that loads the vendor's script. An attack through a supplier like this is called a supply chain attack. A real case: in 2024 the polyfill.io domain was sold to a new owner, and its script started injecting malicious code into more than 100,000 sites that loaded it.
The inventory: list and grade every tag
The first step to protect the page: make an inventory, a list of every third-party tag on the page, then grade the tags one by one. This is the inventory of https://coffeeshop.example.com/checkout, with the same four tags the Lab grades. The third column asks whether the file's contents are locked with a hash (SRI). The last column asks: does the script need to read or change the page's content?
| Tag | Over HTTPS? | Contents locked? | Needs to touch the page? |
|---|---|---|---|
/js/app.js |
yes, own origin | not needed, own file | yes, it's the shop's code |
dayjs.min.js from cdn.jsdelivr.net |
yes | not yet | yes, the shop's code calls it |
analytics.js from cdn.example.com |
no, still http:// |
can't, the vendor changes it | yes, its job is to measure the page |
chat.js from widget.example.net |
yes | can't, the vendor changes it | no, it only needs its own box |
Three fixes come out of that table:
http://becomeshttps://. A script overhttp://can be changed by anyone on the same network (public Wi-Fi, a router). This attack is called man-in-the-middle (the attacker sits in the middle of the data's path). On an HTTPS page, modern browsers even block that script right away as mixed content (http://content on an HTTPS page), so the feature breaks too.- A versioned file is locked with SRI. The URL
[email protected]names its version, so the file at that URL will never change, and its contents can be locked with a hash. - What doesn't need the page moves into an iframe. The chat widget can run in a box of its own.
SRI: lock a file's contents with a hash
SRI (Subresource Integrity) is an integrity attribute holding a hash of the file's contents. You learned it in the SRI lesson. The browser hashes the file that arrives and refuses to run it if even one byte differs. You make the hash once, from the original file. This command downloads the dayjs file with curl, then computes its SHA-384:
$ curl -s https://cdn.jsdelivr.net/npm/[email protected]/dayjs.min.js | openssl dgst -sha384 -binary | openssl base64 -A
DpVxUeeBWjUvUV1czyIHJAjh+jYUZFu2lLakbdua5vbwOrBGi1UgaKCHjTC+x3Ky
Then you write that hash in the <script> tag:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dayjs.min.js"
integrity="sha384-DpVxUeeBWjUvUV1czyIHJAjh+jYUZFu2lLakbdua5vbwOrBGi1UgaKCHjTC+x3Ky"
crossorigin="anonymous" defer></script>
integrity="sha384-...": the algorithm's name (sha384), a minus sign, then the hash from the command above.crossorigin="anonymous": requests the file through CORS without cookies. Required for a file from another origin: without this attribute the browser isn't allowed to read the file to check it, so the script is blocked.defer: the script runs after the HTML has been read, so it doesn't delay showing the page.
SRI only fits a file whose contents stay the same. The vendors of analytics.js and chat.js can change those files at any time, so the hash you write would soon stop matching.
CSP: the list of allowed hosts
CSP (Content Security Policy) is the list of sources a page may use, sent to the browser. Here the list controls where scripts and iframes may come from. Its contents come from the inventory above, written as a meta tag:
<meta http-equiv="Content-Security-Policy"
content="script-src 'self' https://cdn.jsdelivr.net/npm/[email protected]/ https://cdn.example.com; frame-src https://widget.example.net">
script-src: the hosts allowed to supply scripts.'self'means the page's own origin.frame-src: the hosts allowed to show in an iframe.- A script from a host that isn't listed is blocked.
http://cdn.example.comis blocked too, because its scheme differs from thehttps://on the list. - For a public CDN, write the path (the part of the URL after the host), not only the host.
cdn.jsdelivr.netserves files from any npm package, and anyone can publish a package on npm. So if only the host is allowed, an attacker who manages to insert a tag can load their own package from it.
The widget in a sandboxed iframe
The chat widget doesn't need to read your checkout form. Load the widget as a separate page, inside an iframe:
<iframe src="https://widget.example.net/embed"
sandbox="allow-scripts allow-forms allow-popups"
title="Chat"></iframe>
- An iframe from another origin is a separate document. The same-origin policy now applies: the widget can't read your page's DOM (the page content JavaScript can reach), cookies or
localStorage. - The
sandboxattribute limits further what the iframe's content may do. Written with no value, it forbids everything: scripts, forms, popups, even sending the parent page (the page that holds the iframe) to another URL. Then you add only the permissions it needs:allow-scripts,allow-forms,allow-popups.
⚠️ Safeguards left half done
integritywithoutcrossorigin. The CDN script is blocked right away, with an error in the console. Add both.- SRI on a file the vendor changes. As soon as the vendor releases a new version at the same URL, the hash stops matching, and the feature stops without anyone noticing. Use SRI for versioned files, and CSP for the rest.
sandbox="allow-scripts allow-same-origin"for a page from your own origin.allow-same-originlets the iframe's content keep its real origin, here your own origin. With those two permissions, the script inside the iframe can remove its own sandbox attribute. For content from your own origin, never combine them.- Adding a tag without recording it. A tag that isn't in the inventory never gets graded. CSP helps: a host that isn't listed is blocked right away.
TL;DR: A third-party script (JavaScript loaded from someone else's server) runs with the same rights as your code. So if its supplier (the vendor that makes the script) is broken into, the attacker's code runs in your page. This is called a supply chain attack. Record every tag in an inventory (a list of all third-party tags), then grade them one by one. https:// is a must, because http:// can be changed by someone in the middle of the data's path, and is blocked on an HTTPS page. A versioned file (its version is written in the URL) is locked with a hash in integrity (SRI), plus crossorigin="anonymous". The CSP script-src lists only the hosts in the inventory (for a public CDN, with the path, the part of the URL after the host). A widget that doesn't need to read or change your page moves into an iframe with the sandbox attribute, which limits the iframe's content.
⚡ Full rights, same as your code
Guess first: a script from a third-party CDN (someone else's server) runs with the same rights as your code. What is the main risk?
A supply chain attack (an attack through a supplier). If the server, CDN or account of the vendor (the company that makes the script) is broken into, the attacker's code runs on every site that loads it, yours included.
Meaning: A script loaded through <script src> runs inside your page, with your page's origin. It can read form values, cookies that aren't HttpOnly, and localStorage.
⚡ An inventory of every tag
Meaning: Record every third-party tag in an inventory (a list of all tags on the page), then grade them one by one. There are three fixes: http:// becomes https://, a versioned file (its version is written in the URL) is locked with SRI, and a script that doesn't need to touch the page (read or change it) moves into an iframe.
⚡ SRI and CSP
Write:
<meta http-equiv="Content-Security-Policy"
content="script-src 'self' https://cdn.jsdelivr.net/npm/[email protected]/ https://cdn.example.com; frame-src https://widget.example.net">
Meaning: SRI only fits a file whose contents stay the same, so CSP guards the rest. CSP lists the allowed sources: script-src for scripts, frame-src for iframes. A host that isn't listed is blocked, and so is http:// when the list says https://. For a public CDN, the path (the part of the URL after the host) is written too, so other files on that CDN stay blocked.
⚡ The widget in a sandboxed iframe
Write:
<iframe src="https://widget.example.net/embed"
sandbox="allow-scripts allow-forms allow-popups"
title="Chat"></iframe>
Meaning: This chat widget is loaded in an iframe from another origin. That iframe is a separate document, so the widget can't read your page's DOM (the page content JavaScript can reach), cookies or localStorage. sandbox limits it further: only the permissions written (here scripts, forms and popups) are given.