CKTIS / HTML
integrity attribute — resource integrity
The integrity attribute supplies the browser with an expected digest for an external resource. Subresource Integrity (SRI) allows a script or stylesheet to be rejected when its contents do not match the declared metadata.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
integrity="sha256-<base64-digest>"integrity is a textual attribute for Subresource Integrity (SRI), not a global or boolean attribute. The form sha256-<base64-digest> combines an algorithm name with a Base64-encoded digest. The digest describes the resource contents, not its URL; changing those contents requires updating the digest.
On script, the attribute sets integrity metadata for the initial fetch of an external script. It does not check code written directly inside the element or automatically cover imported modules. On link, it is allowed only when rel contains stylesheet, preload, or modulepreload.
When a fetched resource fails its integrity check, the browser does not use it as a successfully loaded script or stylesheet. SRI does not replace HTTPS or trust in the document containing the digest: an attacker who can change both the resource and its integrity declaration can replace both. Cross-origin resources need CORS support; typically use crossorigin="anonymous", and the server must allow that request.
The example tries twice to load the same Blob as a script. The Blob contains exactly three bytes: abc, with no newline. The first attempt uses a matching SHA-256 digest and the second uses a mismatched one. The example displays Matching digest: load and Mismatched digest: error on separate lines. In other situations, an error event can also indicate a network error rather than a digest mismatch. Set integrity before fetching begins; changing the attribute later does not recheck an already loaded script.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Script integrity check</title>
</head>
<body>
<pre id="result" aria-live="polite"></pre>
<script>
// The fixture is exactly three bytes: abc, with no newline.
// Define abc so the fixture can also execute as a classic script.
window.abc = 1;
const fixture = new Blob(['abc'], { type: 'text/javascript' });
function loadWithIntegrity(integrity) {
return new Promise(resolve => {
const url = URL.createObjectURL(fixture);
const script = document.createElement('script');
function finish(event) {
script.remove();
URL.revokeObjectURL(url);
resolve(event.type);
}
script.addEventListener('load', finish, { once: true });
script.addEventListener('error', finish, { once: true });
script.integrity = integrity;
script.src = url;
document.head.append(script);
});
}
Promise.all([
loadWithIntegrity('sha256-ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0='),
loadWithIntegrity('sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=')
]).then(([match, mismatch]) => {
document.getElementById('result').textContent =
`Matching digest: ${match}\nMismatched digest: ${mismatch}`;
});
</script>
</body>
</html>Result
Matching digest: load Mismatched digest: error
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.