HTML - HTML Subresource Integrity (SRI) for Secure External Resources
1. Introduction to Subresource Integrity (SRI)
Subresource Integrity (SRI) is a security feature in HTML that helps protect websites from malicious modifications to external resources such as JavaScript files and CSS stylesheets. Websites often depend on external resources hosted on Content Delivery Networks (CDNs) or third-party servers. These resources are loaded into a webpage to provide additional functionality, improve performance, and maintain consistent styling.
However, if an external server is compromised or an attacker modifies a JavaScript or CSS file, the website may unknowingly load harmful code. This can lead to security problems such as data theft, unauthorized access, or malicious activities on the website.
Subresource Integrity helps prevent this problem by allowing developers to specify a cryptographic hash value for an external resource. The browser downloads the resource, calculates its hash, and compares it with the hash provided in the HTML code. If both values match, the browser allows the resource to load. If they do not match, the browser blocks the resource.
2. Why Subresource Integrity Is Important
External resources are commonly used in modern websites. Developers frequently include JavaScript libraries, CSS frameworks, fonts, and other files from third-party servers. Although using these resources can improve development efficiency, it also introduces potential security risks.
For example, a website may use a JavaScript library hosted on a CDN. If an attacker gains access to the CDN and replaces the original library with a malicious version, visitors may unknowingly execute harmful code when opening the website.
SRI provides an additional layer of protection by ensuring that the downloaded file matches the expected original content.
The major benefits of SRI include:
-
Protection Against File Modification: It prevents browsers from executing external JavaScript or applying external CSS when the downloaded file does not match the expected hash.
-
Improved Website Security: It reduces the risk of malicious code being introduced through compromised third-party resources.
-
Protection Against CDN Attacks: It helps protect websites when external content delivery networks experience security breaches.
-
Content Verification: It allows browsers to verify that external files have not been unexpectedly modified.
-
Increased User Trust: It supports safer browsing by reducing the possibility of unauthorized changes to website resources.
3. How Subresource Integrity Works
Subresource Integrity works through cryptographic hashing. A cryptographic hash is a fixed-length value generated from the contents of a file. Even a small modification to the original file generally produces a different hash value.
When a developer adds an SRI hash to an HTML element that loads an external resource, the browser follows a verification process.
The process works as follows:
-
The developer obtains the original JavaScript or CSS file from a trusted source.
-
A cryptographic hash is generated for the file using an algorithm such as SHA-256, SHA-384, or SHA-512.
-
The generated hash is added to the HTML element using the
integrityattribute. -
When a visitor opens the webpage, the browser downloads the external resource.
-
The browser calculates the hash of the downloaded file.
-
The calculated hash is compared with the expected hash specified in the HTML document.
-
If the hashes match, the browser allows the resource to load. If they do not match, the browser blocks the resource.
This verification process happens automatically in the browser without requiring the website visitor to perform any additional action.
4. Understanding the Integrity Attribute
The integrity attribute is the main HTML attribute used to implement Subresource Integrity. It specifies the expected cryptographic hash of an external resource.
It is commonly used with the HTML <script> and <link> elements.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Subresource Integrity Example</title>
<link
rel="stylesheet"
href="https://cdn.example.com/styles.css"
integrity="sha384-EXAMPLE_HASH_VALUE"
crossorigin="anonymous">
</head>
<body>
<h1>Welcome to My Website</h1>
<script
src="https://cdn.example.com/library.js"
integrity="sha384-EXAMPLE_HASH_VALUE"
crossorigin="anonymous">
</script>
</body>
</html>
In this example:
-
<link>loads an external CSS stylesheet. -
<script>loads an external JavaScript file. -
hrefspecifies the location of the CSS resource. -
srcspecifies the location of the JavaScript resource. -
integritycontains the expected cryptographic hash. -
crossorigin="anonymous"enables anonymous cross-origin fetching, which is generally required when using SRI with resources hosted on another origin.
The hash values shown in this example are placeholders. They must be replaced with actual hashes generated from the exact files being loaded.
5. Cryptographic Hash Algorithms Used in SRI
Subresource Integrity supports three main cryptographic hash algorithms. These algorithms generate unique hash values based on the contents of a resource.
SHA-256: SHA-256 generates a 256-bit hash value. It is widely used in security applications and provides strong protection against accidental or malicious content modification.
SHA-384: SHA-384 generates a 384-bit hash value. It provides a longer hash than SHA-256 and is commonly used in SRI implementations.
SHA-512: SHA-512 generates a 512-bit hash value. It produces a longer hash and offers a strong cryptographic verification mechanism.
The hash algorithm is specified before the hash value inside the integrity attribute.
For example:
<script
src="https://cdn.example.com/app.js"
integrity="sha384-generated_hash_value"
crossorigin="anonymous">
</script>
In this example, sha384 identifies the hashing algorithm used to verify the external JavaScript file.
6. Generating an SRI Hash
Developers must generate the correct hash value before using the integrity attribute. The hash must correspond exactly to the contents of the external resource.
One common method is to use OpenSSL from the command line.
For example, the following command generates a SHA-384 hash for a JavaScript file:
openssl dgst -sha384 -binary app.js | openssl base64 -A
The generated output can then be added to the HTML integrity attribute with the sha384- prefix.
The complete value follows this format:
integrity="sha384-GENERATED_HASH_VALUE"
Developers should regenerate the hash whenever the external file is updated. Otherwise, the browser may reject the updated resource because its contents no longer match the previously specified hash.
7. Cross-Origin Resource Sharing and SRI
Subresource Integrity is frequently used with resources hosted on external servers. Since these resources may belong to a different domain, browsers apply cross-origin security rules.
The crossorigin attribute helps control how browsers request external resources.
For SRI verification of cross-origin resources, developers generally use:
crossorigin="anonymous"
This setting requests the resource without including user credentials such as cookies or HTTP authentication information.
The external server must also provide suitable Cross-Origin Resource Sharing (CORS) response headers, such as:
Access-Control-Allow-Origin: *
This example allows cross-origin requests from any origin, although a server may instead specify an individual trusted origin.
If the external server does not provide the required CORS permission, the browser may refuse to use the resource.
Therefore, developers should ensure that both SRI and CORS are configured correctly when loading resources from third-party domains.
8. Common Mistakes When Implementing SRI
Although SRI is relatively easy to implement, developers may encounter problems when configuring it incorrectly.
Some common mistakes include:
-
Using an Incorrect Hash: If the hash does not match the downloaded file, the browser blocks the resource.
-
Failing to Update Hashes: When an external JavaScript or CSS file changes, its corresponding hash must also be updated.
-
Ignoring CORS Requirements: Cross-origin resources must support the necessary CORS behaviour for SRI verification.
-
Using Untrusted Hash Sources: Developers should generate hashes from trusted copies of the original resources rather than blindly copying unknown values.
-
Applying SRI to Unsupported Resource Types: SRI is primarily used with external scripts and stylesheets through the appropriate HTML elements. It does not provide universal integrity checking for every type of downloaded content.
-
Assuming SRI Prevents Every Attack: SRI verifies resource integrity but does not protect against malicious code that was already present in the original file or malicious changes to the HTML page itself.
Understanding these mistakes helps developers implement SRI more effectively.
9. Best Practices for Using Subresource Integrity
Developers should follow appropriate security practices when using SRI to protect websites.
-
Always obtain external resources from trusted providers.
-
Generate cryptographic hashes from verified versions of JavaScript and CSS files.
-
Use SHA-384 or another supported strong hashing algorithm.
-
Add the
integrityattribute to appropriate external script and stylesheet elements. -
Include
crossorigin="anonymous"when required for cross-origin resources. -
Update integrity hashes whenever external files are modified.
-
Avoid depending on unnecessary third-party scripts and stylesheets.
-
Test webpages in different browsers to identify SRI and CORS-related errors.
-
Monitor third-party dependencies and replace outdated or compromised resources.
-
Combine SRI with other website security measures, such as Content Security Policy (CSP).
Following these practices helps maintain the integrity of external resources and reduces security risks associated with third-party dependencies.
10. Conclusion
Subresource Integrity is an important HTML security feature that helps developers verify the authenticity of external JavaScript and CSS resources. By using the integrity attribute and cryptographic hash algorithms, browsers can detect unexpected changes to externally hosted files and prevent modified resources from being loaded.
SRI is particularly useful for websites that depend on third-party libraries, frameworks, and content delivery networks. Although it does not replace other website security measures, it provides an additional layer of protection against certain supply-chain attacks and unauthorized resource modifications.
By implementing SRI correctly and maintaining updated hash values, developers can build more secure, reliable, and trustworthy websites.