Subresource Integrity can help a browser reject a script whose bytes differ from the integrity metadata the publisher approved. It does not establish that the approved code is safe or that a familiar asset origin is trustworthy. Start with an approved reference artifact and its exact bytes.
The CSP dependency guide concerns permitted sources and operations. This fixture concerns byte agreement for one permitted public script. A source can be allowed by policy while delivering a changed representation that fails integrity.
Use a harmless local or intercepted-response fixture. The script below only sets a synthetic DOM marker. It makes no external business request, uses no credentials and changes no application record.
Use the documented hash and CORS contract
The current MDN Subresource Integrity guide describes an integrity value containing an algorithm prefix and a base64-encoded digest. It documents supported SHA prefixes and how browsers compare fetched resource contents before applying the resource.
For cross-origin SRI requests, the documentation also requires the relevant CORS permission. A correct digest cannot override a missing cross-origin access contract. The example uses crossorigin=anonymous and an explicitly public resource response with Access-Control-Allow-Origin:*. Do not copy that public-resource choice onto a private endpoint.
Multiple digest values have additional selection rules described by MDN. This fixture deliberately uses one SHA-384 value so the expected algorithm and artifact remain unambiguous. It is not a generic integrity-attribute parser or an assessment of every browser implementation.
Keep the exact approved script and digest
The approved fixture body is the following line followed by one newline:
document.documentElement.dataset.integrityFixture = 'approved';
Compute the digest from those approved bytes, not from an unfamiliar downloaded response that you then declare approved. The complete deterministic calculation is:
import hashlib
import base64
asset = b"document.documentElement.dataset.integrityFixture = 'approved';" + bytes([10])
integrity = 'sha384-' + base64.b64encode(hashlib.sha384(asset).digest()).decode('ascii')
print(integrity)
The expected integrity value for this exact fixture is:
sha384-WrEUEGqTe9gSwFCyionLguA92Gv3nIuMGtsPtmGPZATzGrpN0NyvNepFM3YjPwX/
A missing newline or changed whitespace changes the bytes and can change the digest even when the JavaScript would perform the same harmless action. Keep the file representation and algorithm explicit.
Use the complete requesting document
The fixture document requests that one approved script. In the controlled baseline, the script response is JavaScript with the stated public CORS permission. All addresses are fictional and should be intercepted or mapped in an authorized local test rather than fetched as real dependencies.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Harmless integrity fixture</title>
<script src="https://assets.example/approved.js"
integrity="sha384-WrEUEGqTe9gSwFCyionLguA92Gv3nIuMGtsPtmGPZATzGrpN0NyvNepFM3YjPwX/"
crossorigin="anonymous"></script>
</head>
<body><p>Public explanation remains available without the diagnostic script.</p></body>
</html>
The visible explanation does not depend on the diagnostic script. Inspect document.documentElement.dataset.integrityFixture to determine whether the script executed. A successful network status is supporting evidence, not the integrity result itself.
Require the benign changed-byte case to fail
For the first negative, append a harmless comment to the script response while retaining the approved integrity attribute. Its behavior would otherwise be equivalent, but its bytes no longer match. The browser should not execute that changed resource under the approved SRI contract, and the marker should be absent.
For the second negative, restore the approved bytes but remove the required cross-origin permission from the controlled response. The digest alone should not produce a successful script marker. Record the actual browser error rather than labelling every failure a hash mismatch.
Keep the baseline and each mutation in clean contexts. A marker left by an earlier execution would make the negative appear successful. Use the same document and intended resource URL so the tested difference remains the response contract rather than an unrelated route.
Reject the digest-of-hex mistake
The base64 value must encode the binary digest bytes. Encoding the hexadecimal text returned by hexdigest produces a different value even though it looks like a long plausible integrity string. Keep a deliberate wrong-value fixture that base64-encodes that hex text and require the script marker to remain absent.
Likewise, preserve the final newline in the approved reference. The printed calculation appends the byte value ten explicitly so a copied code block does not accidentally turn an escaped newline into a different string. This representation choice is part of the deterministic fixture, not a requirement that every real JavaScript file end with a newline. A publisher can approve another byte sequence, provided the digest corresponds to that exact sequence.
Treat an updated hash as an approval decision
If the asset legitimately changes, the owner must approve the new artifact before updating its integrity metadata. Automatically hashing whatever the CDN returned after a mismatch would remove the intended comparison with an independently approved reference.
The artifact identity review makes that trust distinction explicit. A matching digest proves agreement with its reference under the chosen algorithm;it does not prove the reference was uncompromised or that the code is suitable for the task.
Also inspect caching and deployment order. A new page digest paired with old asset bytes can fail just as old metadata paired with new bytes can. The release change record should identify both versions and the actual public observation.
Preserve transformations and failure scope
Find whether the asset path minifies, reformats or otherwise transforms the approved script. A build pipeline needs a clear point at which the published representation and digest are generated together. Do not calculate one from a source file while serving another transformed file and assume semantic similarity is enough.
Keep content decoding and client collection behavior understood when comparing actual bytes. The browser applies its specified integrity process;an ad hoc text-editor save is not equivalent to its fetched resource representation. Retain the original approved artifact and exact response evidence under the authorized process.
A blocked diagnostic script does not prove every public paragraph disappeared. Report the missing dependency’s actual consequence and preserve unrelated working content. That keeps an integrity finding from becoming an unsupported whole-site incident claim.
Release the approved relationship
The receipt should contain the independent approved artifact, digest calculation, emitted attribute, CORS context, actual browser marker and negative results. Identify any untested delivery layer or client rather than certifying the entire site from one fixture.
Approve when the intended script executes only with the approved bytes and required access contract in the tested environment. A mismatch remains an actionable finding until its cause and artifact approval are resolved.
This is a precise public dependency safeguard. It does not guarantee software safety, search discovery or that an allowed resource origin will always serve the intended code.
