{"canonical_url":"https://engineer.company/glossary/sri/","content_markdown":"# Subresource Integrity\n\n## What it is\n\nSubresource Integrity lets a page state in advance exactly which bytes a script\nor stylesheet must contain, and has the browser discard the file if what arrives\nis different. It is usually shortened to SRI. The page writes a cryptographic\ndigest of the file into an `integrity` attribute on the `script` or `link`\nelement that loads it. When the response comes in, the browser runs the same\nhash function over the bytes it received and compares the result with the digest\non the element. If the two are equal, the file runs or styles the page as usual.\nIf they differ, the browser treats the response as a network error: the script\nnever executes and the stylesheet never applies.\n\nThe value is the name of a hash function, a hyphen, and the digest in base64.\nOne command over the finished file produces it, and running it twice shows how\nlittle it takes to change the answer:\n\n```text\n$ printf \"alert('Hello, world.');\" \u003e hello.js\n$ openssl dgst -sha384 -binary hello.js | openssl base64 -A\nH8BRh8j48O9oYatfu5AZzq6A9RINhZO5H16dQZngK7T62em8MUt1FLm52t+eX6xO\n$ echo \u003e\u003e hello.js\n$ openssl dgst -sha384 -binary hello.js | openssl base64 -A\nqu8P7hCLFcBpas8BOnGmaANCkfXJIpFttDEaxOj0Pq9H6Q8NYjsMb+PVqwuJygVe\n```\n\nWith `sha384-` in front, the first output is the attribute's value for that\none-line script. One newline added at the end gives a digest with nothing in\ncommon with the first, and a browser holding the first would refuse the second\nfile.\n\nThe specification recognises three hash functions, SHA-256, SHA-384 and SHA-512,\nall from the SHA-2 family, and calls SHA-384 a good baseline at the time of\nwriting. An attribute may list several digests separated by spaces. The browser\npicks the strongest function among them and accepts the file when it matches any\ndigest made with that function. That lets a page move to a stronger algorithm,\nor accept two releases of a library during a switch-over, without a window in\nwhich the page carries only wrong digests.\n\nThe attribute exists on `script` and `link`, and the same value can ride on a\n`Link` response header that preloads a file. The specification's current text is\nits second edition, a W3C Working Draft from the Web Application Security\nWorking Group; the first edition became a W3C Recommendation in 2016.\n\n## Why it matters\n\nA page that loads a library from a content delivery network, or from any host\nits owners do not run, hands that host the power to run code on the page. HTTPS\nprotects the file on its way across the network, but it says nothing about what\nthe host chose to send in the first place. If the CDN's storage is broken into,\nan account that publishes to it is taken over, or the file behind a familiar\naddress is quietly replaced, every page that includes it runs the new code with\nthe page's full authority. The page's own code has no way to tell the\ndifference.\n\nSRI moves that decision back to the page. The digest is computed by the site's\nowner over a file they have read or built, and it travels inside the page, from\nthe site's own server. A host that serves anything else has its file refused\ninstead of run. The page loses a feature until somebody looks, a visible fault\nwith an obvious cause, instead of quietly running somebody else's code.\n\nThe digest also pins a version. An address such as `/lib/latest.js` can start\nserving a new release under a page at any moment; the digest cannot follow it.\nAn upgrade the owner never chose becomes a refusal on one specific element,\nreported through an error event.\n\nIt is worth being exact about what SRI does not do. It checks a file against a\ndigest; it does not judge whether the file is safe. A digest of a library that\nalready contains a hostile line protects that hostile line faithfully. It leaves\ninline script alone, which is the work of a Content Security Policy. And it\ntrusts the page that carries the digest, so the page itself still has to arrive\nintact.\n\n## What it looks like in practice\n\nThis site uses it on its own stylesheet. The [security page](/security/) lists\nSubresource Integrity on the stylesheet: the bundle is fingerprinted and hashed\nwhen the site is built, so the reader's browser can verify that it arrived\nunmodified. No executable JavaScript is published and there are no third-party\nrequests. A reader who counts the hosts in the browser's network panel finds\none, the site's own.\n\nThe [credits page](/credits/) describes the same file from the build's side, as\none CSS bundle concatenated and fingerprinted at build time and served with an\nSRI hash. Fingerprinting puts a hash of the file's contents into its name, so a\nchanged stylesheet gets a new address and a browser never mistakes an old cached\ncopy for the current one, while the integrity value lets the browser check the\nbytes behind that address. The build produces both from the same bytes in the\nsame step, so every release ships the stylesheet and its digest together, and\nthe commonest failure described below, a file that changes after it was hashed,\nhas very little room to happen.\n\n## Where it goes wrong\n\n**Something between the build and the browser rewrites the file.** A CDN that\nminifies scripts, a proxy that injects into response content, or a deploy step\nthat rewrites a file after its digest was taken changes the content bytes, and\nthe browser refuses the result even though nobody attacked anything.\nContent-preserving gzip or Brotli recompression does not cause that mismatch:\nthe check uses the decoded response body, not the compressed bytes sent over\nthe network. The specification puts the duty on content-modifying\nintermediaries: they must keep the digest in step with what they serve, or\ndeliver the original untouched, and the origin should send\n`Cache-Control: no-transform` to ask them to leave the file alone. The dependable\norder is to take the digest last, over exactly the decoded content bytes the\nbrowser will check.\n\n**A cross-origin file arrives without CORS.** The specification requires an\nintegrity check on a file from another origin to use CORS, and calls an attempt\nwithout it a logical error. For example, the element can use\n`crossorigin=\"anonymous\"` and the host must answer with a header that shares the\nfile. `crossorigin=\"use-credentials\"` also uses CORS, with server headers that\nexplicitly allow the requesting origin and credentials. Without a CORS request\nand matching server policy, the browser blocks the file rather than load it\nunchecked. The rule protects other sites: without it, an attacker could load a\nprivate response with a guessed digest and learn from the outcome, for example,\nwhether the reader is logged in there.\n\n**The digest arrives over plain HTTP.** SRI protects a page against a\ncompromised file host, never against someone on the network path. On a page\nserved without HTTPS, whoever can rewrite the file in transit can rewrite or\ndelete the digest in the page as well, so the specification recommends\ndelivering integrity metadata only to a secure context.\n\n**The hash function is one the browser skips.** The specification recognises\nonly SHA-256, SHA-384 and SHA-512 and recommends that browsers refuse known-weak\nfunctions such as MD5 and SHA-1. The surprise is what happens to a token outside\nthat list: the browser ignores it, and an attribute made only of ignored tokens\ncounts as carrying no metadata at all, so the file loads unchecked. A build tool\nthat can fingerprint with MD5, as Hugo's can, makes this easy to ship by\naccident.\n\n**A refused file takes the page down with it.** When the check fails, the\nbrowser fires an error event on the element and the script or stylesheet is\nsimply missing. For a library the page cannot run without, the specification\nsuggests catching the event and loading a fallback copy from a trusted, if\nslower, source such as the site's own server. Without one, the protection works\nas designed and the reader still gets a broken page.\n\n**Only the elements that carry a digest are checked.** An integrity attribute on\none script says nothing about the next one somebody adds without it, and\nreviewing the scripts that have one will never reveal the one that lacks it. The\ndraft adds a response header that covers this from the server side.\n`Integrity-Policy` with `blocked-destinations=(script)` blocks every external\nscript that arrives without integrity metadata, and\n`Integrity-Policy-Report-Only` lists them first, so they can be fixed before\nanything is blocked.\n\n## Further reading\n\n- [Subresource Integrity](https://www.w3.org/TR/SRI/) — the current draft, with\n  the verification algorithm, the `Integrity-Policy` headers and the notes on\n  proxies, plain HTTP and cross-origin leaks.\n- [Subresource Integrity, W3C Recommendation 2016](https://www.w3.org/TR/2016/REC-SRI-20160623/)\n  — the first edition, the stable Recommendation the draft grew from.\n- [Fetch Standard](https://fetch.spec.whatwg.org/) — where a request's integrity\n  metadata is checked against the response body, and the CORS protocol a\n  cross-origin check depends on.\n- [Hugo: resources.Fingerprint](https://gohugo.io/functions/resources/fingerprint/)\n  — how a static site generator fingerprints a file at build time and hands back\n  the matching integrity value.\n- [FIPS 180-4, Secure Hash Standard](https://csrc.nist.gov/pubs/fips/180-4/upd1/final)\n  — the federal standard that defines SHA-256, SHA-384 and SHA-512.\n\n\u003chttps://engineer.company/glossary/sri/\u003e\n","date_modified":"2026-11-09T21:50:25Z","description":"How Subresource Integrity lets a browser refuse a script or stylesheet that changed after it was hashed, and what breaks when a file and its digest drift apart.","formats":{"gemtext":"https://engineer.company/glossary/sri/index.gmi","gopher":"https://engineer.company/glossary/sri/gophermap","html":"https://engineer.company/glossary/sri/","json":"https://engineer.company/glossary/sri/index.json","markdown":"https://engineer.company/glossary/sri/index.md","pdf":"https://engineer.company/glossary/sri/index.pdf"},"kind":"page","language":"en","title":"What is Subresource Integrity?","url":"https://engineer.company/glossary/sri/","version":1}