Check Subresource Integrity for scripts your page loads
When a page loads a script from another site, that site supplies the code visitors run. Subresource Integrity gives the browser an expected hash so it can reject a file whose contents don't match.
Add Subresource Integrity to an outside script
- 1
Choose a file with a fixed version
Use an address whose contents are meant to stay fixed. If the file at a versionless address changes and the hash doesn't, the browser will reject the new file.
- 2
Generate the hash from the file you intend to load
Use a supported SHA-256, SHA-384 or SHA-512 digest in the integrity attribute. Check that the digest comes from the intended file before publishing it.
- 3
Make sure the outside host permits the request
Cross-origin integrity checks require CORS. Use the crossorigin attribute and confirm that the resource host returns the needed access-control header.
- 4
Update the file reference and hash together
When upgrading a script, replace its expected hash as part of the same change. Load the page and check for rejected resources before releasing it.
Use Subresource Integrity alongside your content policy
A Content Security Policy can allow a script host without checking each file's contents. An integrity hash specifies the contents you expect. Neither proves the library has no known security flaws.
The integrity guide describes how browsers compare a downloaded file with the supplied digest.
Frequently asked questions
Does a Subresource Integrity hash need to be kept secret?
No. The integrity value is part of the page markup. It records the contents you expect; it isn't a secret that grants access.
Review script policies and vulnerable libraries
Website security
Catch leaked API keys and exposed files before someone uses them against you.
Content Security Policy
Read your content policy and investigate blocked scripts before tightening it.
Vulnerable JavaScript libraries
Identify affected front-end library versions and plan an update around the code that uses them.