The page will only fail after the third-party script is replaced, to what extent can the integrity check prevent?
After the third-party script is replaced, the page will only fail. The integrity check can prevent this kind of 'Content does not match the declaration': The browser calculates the hash of the retrieved file once according to the specified algorithm and compares it with the value written in the tag. If it is consistent, it will load; if not, it will be directly rejected and report a network error. It cannot prevent 'This host is already giving you the wrong thing'.
Which kind of attack does the check prevent?
The document clearly locates: Integrity verification is a defensive measure used to ensure that the file content retrieved by the application is exactly consistent with what is expected. A typical scenario is when an external host is taken over and content is injected into the file—the script is still the same address, the same filename, but the bytes have changed.
This type of attack is effective because most page loading logic only looks at the address and not the content. Browsers trust 'the things retrieved from that address' by default, and once the host referenced is compromised, the problem directly enters the user's session. Verifying extends the trust point from the address to the content, at the cost of having to synchronize the update of that string every time a dependency is upgraded.
How to write the integrity value, and how does the browser compare?
The syntax is to provide the Base64 hash of the algorithm and prefix while referencing external resources, the browser calculates the actual content using the same algorithm and compares it with the listed values one by one; if there is a match, it loads, and if all do not match, it refuses to load and reports a network error.
The two details are the easiest to step on:
- When accessing resources across domains, it is necessary to declare CORS at the same time, and the tag must include the crossorigin attribute. The document explicitly states that browsers do not allow no-cors requests to use integrity checks, and requests missing this item will fail directly rather than degrading to unverified.
- A resource can list multiple values. During grayscale release, listing the hashes of the old and new versions side by side can avoid instant widespread errors during replacement, but this is just a transitional measure, and long-term parallelism will make the verification lose its meaning.
The three types of situations it cannot block.
The first type is that the host itself is not trustworthy. The verification only guarantees that 'the content obtained is the one you specified', and if the specified content originally comes from an address that can be arbitrarily replaced, the attacker can bypass it by replacing the entire file and synchronously updating the reference value.
The second type is a replacement under the same origin path. The validation is written in the reference tag, and if the script is dynamically concatenated within the site or overwritten by other programs on the same server, changes in content and value updates may occur simultaneously.
The third type is the abuse of legitimate content. The signature is passed, and the content is correct, but the execution behavior of the script itself exceeds the scope of your authorization——for example, a statistics script sends user behavior to places it shouldn't go, and integrity verification is not involved in this layer at all.
The part that is controllable within the site is to localize the dependencies. After the third-party script is changed to self-hosted within the site, the reference address is no longer affected by external hosts, and the remaining verification points become 'who has the permission to modify this file', which falls under the category of permissions and backups. The built-in XSS protection handles content injection, while self-hosting handles source loss of control, and the two layers should not be substituted for each other.
A more stable approach within the site: to localize the dependencies.
It is recommended to arrange the order in this way: first list the inventory, list all scripts, styles, and fonts on the page that are not from the site's domain; then judge one by one whether external dependencies are necessary, and localize those that can be localized; keep the remaining external references while adding verification and cross-domain declarations; finally, incorporate the inventory into the release process, and force a reason to be stated when adding new external dependencies.
The inventory should follow the template, not rely on memory. The template centralizes the reference locations, changing one place affects the entire site, which is more reliable than maintaining each page separately.
The order of recovery after a problem occurs
Once the verification fails or the script is replaced, the order of processing affects the scope of loss.
First, break the entry: remove or modify the problematic reference from the template to point to a local copy, so that the page can recover its usability first. This step has a higher priority than locating the cause because the failure is usually manifested as the disappearance of local functions on the page, and users are still affected continuously.
Then collect the scene: keep a copy of the actual file and response header at the time of failure as a basis for subsequent comparison and accountability. This step can be overwritten by the cache clearing action, so it must be completed before cleaning.
Then verify the rollback version and data: confirm that the latest backup covers the template and static files, and can return to the state before replacement. When restoring, verify in the order of 'first program files, then data' to avoid bringing dirty files back into clean data.
Finally, do reinforcement: change this dependency to a local copy, supplement verification values, and write the reference list into the release checklist.
Common questions
Question: Isn't it the safest to add verification to all scripts?Answer: It only makes sense for resources that are indeed retrieved from external hosts. Adding verification to in-house resources does not increase protection but increases maintenance costs.
Ask: Will adding 'crossorigin' cause some users to be unable to access resources?Answer: When the server does not return the CORS header, the request will fail. Before the check, use the real network environment to verify the availability of the reference.
Ask: How often is the value updated?Answer: It follows the update of the dependency version, not the calendar. Regular batch refreshes may cause the check value and version to be disconnected, making it difficult to determine which content corresponds to which set of values when an error occurs.