A CSP hash approves exact script bytes, not a trusted file path
A Content Security Policy can authorize an inline script with a cryptographic hash instead of enabling unsafe-inline. The browser computes the script's content, compares it with the policy source expression, and runs the script only when the bytes match. This is a useful control for static bootstrap code, but it approves exact content. It does not mark a route, vendor, or file path as trusted forever.
The Content Security Policy Level 3 specification defines nonce and hash source expressions for inline script. It also describes how a hash source can match an external script when the triggering element carries matching integrity metadata. The policy still needs an algorithm and Base64 value, and the page's response headers and script element must agree.
Exact bytes have consequences
Whitespace, attribute text, template substitutions, comments, and server-side minification can change the hash. A deployment that generates a fresh inline block on every request may be better served by a nonce. A static block whose bytes are frozen can use a hash, but the build system must update the policy when the block changes. A mismatch should break the script visibly in test and report-only monitoring.
strict-dynamic changes the trust path for scripts created or loaded by an already trusted script. The CSP specification states that host-source and scheme-source expressions, as well as 'self', are ignored for script loading when strict-dynamic is active. That behavior can be useful for dynamic application bootstrap code, but it shifts trust to the parent script and its DOM changes. It is not a blanket allowlist for every third-party origin.
Hashes are also not approval for an entire application bundle. The browser checks the element that triggers the request and its integrity metadata. A script whose bytes change after approval no longer matches. A hash cannot prove that the code's logic is safe, and it cannot stop a same-origin server compromise from serving a different approved program later.
Review the whole policy
Inventory inline scripts, event-handler attributes, third-party bundles, and dynamically inserted script elements. Prefer nonces for short-lived or generated blocks, hashes for stable static blocks, and explicit integrity metadata for external assets. Remove inline event handlers where the application can use event listeners.
Test policy enforcement with the same build output that reaches production. Include the response header, HTML, service worker behavior, and any server-side templating path. Use report-only mode to collect violations before enforcing a new hash, then remove the report-only policy or deployment step that allowed the legacy behavior.
A CSP hash is a precise authorization for specific bytes. That precision is its strength, and it is why the hash must be managed as part of the build, not copied into a policy and forgotten.
Top comments (0)