myHotTake

Tag: integrity attribute

  • How Do SRI Checks Secure Your JavaScript Resources?

    Hey there! If you find this story engaging, feel free to give it a like or share with your fellow coding enthusiasts.


    I’m a student in the middle of rewriting an essay draft to perfect it. I’ve spent countless hours honing my ideas, making sure each sentence is crystal clear. But as I refine my masterpiece, there’s a critical step I must not overlook: ensuring the integrity of the sources I’ve quoted. Just as I would double-check each citation to confirm its accuracy, I must also ensure that the external resources my website relies on are exactly what they claim to be.

    Enter the world of Subresource Integrity (SRI) checks in JavaScript. This nifty security feature is like my trusty red pen, meticulously reviewing every quote for authenticity. I implement it by adding a special attribute to the script tag in my HTML. This attribute, known as the integrity attribute, contains a cryptographic hash of the file’s content. It’s akin to having a master list of all the correct quotes from my essay sources, ensuring no rogue words slip through.

    As I prepare to submit my essay, I compare each quote against my master list, just like the browser checks the hash against the fetched resource. If anything is amiss, the browser raises an alert, refusing to use the compromised resource. It’s my safeguard against plagiarism, ensuring that my essay remains true to its original vision. With SRI in place, I can confidently publish my work, knowing that every external script is as reliable as the words I’ve carefully crafted.

    So, just like perfecting an essay with precision and care, implementing SRI checks ensures my web application stands strong and secure, ready to face the world. If you enjoyed this creative journey, don’t forget to share the story!


    To implement SRI, I start by generating a cryptographic hash of the script file. This hash is like a unique fingerprint for the file, ensuring its identity. Let’s say I’m using a jQuery library hosted on a CDN. To add SRI, I include the integrity attribute within the script tag:

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"
            integrity="sha384-MG8N4+...your_hash_here..."
            crossorigin="anonymous"></script>

    The integrity attribute contains the hash value, which I generate using a tool like Subresource Integrity Hash Generator. The crossorigin attribute is also crucial, as it tells the browser to handle the request in a way that supports SRI checks.

    If the content of the jQuery file changes — even by a single byte — the hash won’t match, and the browser will refuse to load it. This is akin to rejecting a questionable quote that doesn’t match my source document, maintaining the integrity of my essay and, in this case, my web application.

    Key Takeaways:

    1. Security Assurance: SRI checks ensure that the scripts and stylesheets loaded from external sources are not altered, preventing malicious code from being executed.
    2. Implementation: Add the integrity attribute to your script or link tags, using a cryptographic hash to verify the content.
    3. Cross-Origin Resource Sharing: Use the crossorigin attribute to handle resources correctly, especially when dealing with resources from a different origin.