Loading...
<script
src="https://cdn.example.com/lib@1.2.0/lib.min.js"
></script><script
src="https://cdn.example.com/lib@1.2.0/lib.min.js"
integrity="sha384-BASE64_HASH_OF_FILE"
crossorigin="anonymous"
></script>The browser hashes the file and refuses to run it if it differs, so a compromised CDN cannot inject code.
integrity attribute with a 'sha384' or stronger hash.integrity also have crossorigin='anonymous'.Run openssl dgst -sha384 -binary lib.js | openssl base64 -A and prefix the result with 'sha384-'.
fetch()?Yes. Pass an integrity option to fetch(), and the promise rejects if the response does not match the hash.
Cross-origin files must be fetched with CORS for the browser to check them, so add crossorigin='anonymous' and make sure the CDN sends Access-Control-Allow-Origin.