Add Subresource Integrity to an outside script

  1. 1

    Choose a file with a fixed version

    Use an address whose contents are meant to stay fixed. If the file at a versionless address changes and the hash doesn't, the browser will reject the new file.

  2. 2

    Generate the hash from the file you intend to load

    Use a supported SHA-256, SHA-384 or SHA-512 digest in the integrity attribute. Check that the digest comes from the intended file before publishing it.

  3. 3

    Make sure the outside host permits the request

    Cross-origin integrity checks require CORS. Use the crossorigin attribute and confirm that the resource host returns the needed access-control header.

  4. 4

    Update the file reference and hash together

    When upgrading a script, replace its expected hash as part of the same change. Load the page and check for rejected resources before releasing it.

Use Subresource Integrity alongside your content policy

  • A Content Security Policy can allow a script host without checking each file's contents. An integrity hash specifies the contents you expect. Neither proves the library has no known security flaws.

  • The integrity guide describes how browsers compare a downloaded file with the supplied digest.

Frequently asked questions

Does a Subresource Integrity hash need to be kept secret?

No. The integrity value is part of the page markup. It records the contents you expect; it isn't a secret that grants access.