Add integrity verification to external scripts, why do you still need to explicitly write cross-domain attributes in the tag?
When adding integrity checking to external scripts, the cross-domain attribute must also be explicitly written in the tag, because the check depends on the premise of cross-domain requests. The document states: Since the resources that require integrity checking must use cross-domain requests, the cross-domain attribute must be included in the tag. Without it, the browser's access method to such resources is inconsistent with the check requirements, and the check value cannot be determined, resulting in the resource being directly rejected for loading, or the check is virtually ineffective.
At what step does the check occur?
It occurs after resource download and before execution: the browser calculates the summary of the content and compares it with the value given in the tag. If they do not match, the resource is not used. It protects the section of 'reference not replaced', which is the scenario where the content distribution node or the external script source is replaced. It does not protect what the script itself does. The script that is often replaced is the entry point for cross-site scripting (XSS), and this check reduces the probability of 'loading a file tampered with passively'. What the file does after loading still depends on source restrictions.
So it is suitable for hanging on to version-fixed third-party scripts and styles. References that change with the content are not suitable to be added, because each update of the source file will cause the verification to fail, and the page will directly lack styles or interactions.
Why must the cross-domain attribute be explicitly written
The cross-domain attribute determines the mode in which the request is issued. By default, the use of scripts and styles does not carry complete cross-domain semantics, and the response received by the browser cannot safely participate in summary comparison. After explicit declaration, the request is issued in cross-domain mode, and verification has comparable input.
The most common accident is to add the check value but forget to write the cross-domain attribute: It is not noticeable during local testing due to cache hits, but resource loading fails after going online. Conversely, if only the cross-domain attribute is written without the check, there is no check effect at all. The two types should appear in pairs.
| Writing points | Common mistakes | The appearance when it occurs |
|---|---|---|
| Check value and cross-domain attribute are paired | Only give the check value | Resource is refused to load, page lacks style or no interaction |
| Multiple algorithm options | Only give a single summary | The entire site fails at the same time after changing the algorithm in the source file |
| Version fixed reference | Validate dynamic content | Failure triggered on each update |
| Review after batch replacement | Only change script, not style | Half of the references still use the old summary |
The format of the validation value matches the alternative
The value is a summary of one or more content pieces, separated by whitespace. Each summary consists of an algorithm prefix, a hyphen, and an encoded hash value. The allowed prefixes are sha256, sha384, and sha512. The value of providing multiple summaries is in the transition: the source file can be loaded normally as long as any of them matches during the next update, and the site does not need to replace all references at the same time during the same release window.
When generating an abstract, use the actual file distributed as the standard, do not use the source file before compression and merging; if there are construction steps in the middle, take the hash on the construction output, otherwise, the content online will not be consistent with the calculation basis.
Batch verification on the template side
Reference points are usually scattered in the common header and footer of the template. When making changes, both scripts and styles need to be updated. There is a station-wide replacement tool suitable for batch repairing such reference points, but after batch changes, it must be reviewed: replacements are made based on text matching, and writing differences across templates (attribute order, whether parameters are included) are easy to miss a part.
The more stable sequence is to first run the full station reference list on the test station, request each resource with verification one by one, confirm that the summary is consistent with the output, and then go to production. If the template changes are made simultaneously with the in-station visual template editing capability, after the change is completed, still check against the same list, the editing capability will not verify the online summary for you.
Common questions
Will the CDN certificate replacement fail after verification?No, verification only compares the file content summary and is unrelated to the transport layer certificate; but if the CDN makes compression and rewriting of the file, it will fail.
Do you need to add internal scripts?Resources that are the same domain and change with construction do not need to be added, the value is concentrated on external references with fixed versions.
Will there be a prompt if the validation fails?The browser console will record, and ordinary visitors will only see missing functions, so during the gray release period, it is necessary to actively compare the reference list.
Can we only provide a single algorithm for a long time?Yes, but when changing algorithms, all references must be changed at once; providing two summaries can stretch the transition period.