External Resource Integrity
Summary
The External Resource Integrity defined in this document is a Content Attestation (CA) property for assuring the integrity of external resource files such as images, videos, etc. While it can assure the integrity of the resource referenced by a URL, it is limited to URLs that return the same byte sequence as a response regardless of the user agent.
We plan to receive feedback on this External Resource Integrity from the companies participating in the pilot experiments. We may add better methods in the future while monitoring the status of updates to related specifications.
Reference: Consideration of a method to enable verification when using CDN with image processing
Scope
- Dynamic resources through content negotiation are outside the scope of this document.
Terminology
For terms not explained in this document, please see Terminology.
- Content Attestation (CA)
External Resource Integrity Format
It must be a JSON object. Below is an example of an External Resource Integrity:
{
"type": "ExternalResourceTargetIntegrity",
"cssSelector": "#hero-image",
"integrity": "sha256-OYP9B9EPFBi1vs0dUqOhSbHmtP+ZSTsUv2/OjSzWK0w="
}
The following properties are defined:
| Name | Type | Description |
|---|---|---|
type | string | REQUIRED. It MUST be ExternalResourceTargetIntegrity. |
integrity | string | REQUIRED. It MUST be the sriString data type. For available hash functions, it MUST conform to Hash Algorithm. Example: sha256-4HLmAAYVRClrk+eCIrI1Rlf5/IKK0+wGoYjRs9vzl7U= |
cssSelector | string | OPTIONAL. It MUST be a CSS Selector (Selectors Level 3). When this property is present, the CSS selector is used instead of the integrity attribute to identify the target HTML element. Example: #hero-image |
CA issuers should specify cssSelector so that the elements that cssSelector matches will not change regardless of dynamic changes to the page (RECOMMENDED). For example, instead of specifying only a tag name such as img for cssSelector, specify a more specific CSS selector such as #hero-image or img.rareClassName. If there is no CSS selector that can stably and uniquely identify the target element, it is RECOMMENDED to update the page and design the page so that it is easier to identify, for example by specifying an id attribute for the target element.
Target Elements
The HTML elements subject to External Resource Integrity verification MUST be limited to those listed in the following table, among the elements defined in HTML Standard 4.8 Embedded content. Verifiers MUST NOT fetch resources of elements not listed in the table. Elements not listed in the table are excluded from verification. When the currentSrc used for the URL to fetch is an empty string (for example, before the resource is loaded or in an environment that does not implement currentSrc), verifiers MAY instead use the resolved URL of the src attribute of the img, video, or audio element that has that currentSrc.
The URL of the src attribute in the table is resolved with encoding-parse a URL relative to the element's node document.
The Request destination column of the table shows, for reference, the request destination used when the user agent loads the resource of the element. This document does not specify the request destination used when verifiers fetch resources.
| Element | URL to fetch | Request destination (for reference) | Notes |
|---|---|---|---|
img | HTMLImageElement.currentSrc | image | The candidate selected by the user agent from the src and srcset attributes and the source elements in the picture element |
picture | currentSrc of the child img element | image | The picture element itself has no URL, so the child img element's URL is used |
source (parent is video / audio) | src attribute | video or audio, depending on the parent element | Regardless of whether it is selected by the parent element |
video | HTMLMediaElement.currentSrc | video | The URL of the poster attribute is not covered |
audio | HTMLMediaElement.currentSrc | audio | |
track | src attribute | track | Subtitle and chapter files such as WebVTT |
The following elements and attributes are under consideration for inclusion in a future version.
sourceelement in apictureelement: verifying the candidates of thesrcsetattribute regardless of whether it is selected. The resource of the selected candidate can be verified as thepictureorimgelement in the table aboveembedelement: covering it only when thetypeattribute is an image, video, or audio MIME typeobjectelement: covering it only when thedataandtypeattributes are similarly restrictedmapandareaelements: thehrefattribute of theareaelement will be considered together with verification of external links (#127)- Inline SVG (
svgelement): it can be handled by HTML Fragment Integrity, but that may not be suitable, so consideration continues. External SVG files referenced from animgelement can be handled as theimgelement in the table above - MathML (
mathelement) - The
posterattribute of thevideoelement
The following elements and resources are not covered. Use the following means instead.
iframeelement: place a CA Set in the nested document and verify it in that documentscript,link, andstyleelements: use theintegrityattribute of Subresource Integrityaelement: the resource of thehrefattribute is navigated to or downloaded by user action and is not loaded automatically. For the status of verification of external links, see #127- Form-related elements (such as
input type="image") and resources fetched by CSS (such as thebackground-imageandcontentproperties): display images to be verified with theimgelement - Resources fetched by scripts with
fetch()orXMLHttpRequest: insert them into the document as one of the elements listed in the table and display them - Videos delivered with Adaptive Bitrate Streaming (such as HLS and DASH): deliver them as a single video file and display them with the
videoelement. A mechanism for assuring the integrity of streamed videos is under consideration separately from External Resource Integrity
Target elements are restricted so that verifiers do not cause requests to URLs the page author did not intend. Such requests may lead to the cross-origin data leakage described in SRI Section 5.3.
In addition, in order to prevent an attacker from inferring the content of a cross-origin resource in violation of the Same-Origin Policy by using a known or precomputed hash, when using the SRI validation result to determine the loadability of the cross-origin resource, ensure beforehand that the cross-origin resource is explicitly shared with the request initiator using the CORS protocol.
How to set it up
Make the HTML elements to be verified identifiable by either of the following methods:
- When using the
cssSelectorproperty: Specify in thecssSelectorproperty a CSS selector that matches the list of HTML elements referencing the resource to be verified. - When not using the
cssSelectorproperty: Specify the same value as theintegrityproperty for theintegrityattribute of the HTML element.
Example
Identifying elements by the cssSelector property
Below is an example of referencing the img element in a picture element with the cssSelector property. Since the currentSrc of the img element is the candidate selected by the user agent from the source and img elements, include the hashes of all resources that may be selected in the integrity property.
External Resource Integrity:
{
"type": "ExternalResourceTargetIntegrity",
"cssSelector": "#hero-image",
"integrity": "sha256-t7WZSGxDdqGvGg/FLw6wk9KFQy5StT1MquCf/htwjBo= sha256-4HLmAAYVRClrk+eCIrI1Rlf5/IKK0+wGoYjRs9vzl7U="
}
In this case, make the CSS selector match the HTML img element of the web page as follows:
<picture>
<source srcset="image.jpg" media="(min-width: 400px)" />
<img id="hero-image" src="https://cdn.example.com/image.jpg" />
</picture>
Below is an example of referencing a video element with the cssSelector property.
External Resource Integrity:
[
{
"type": "ExternalResourceTargetIntegrity",
"cssSelector": "#product-video",
"integrity": "sha256-OYP9B9EPFBi1vs0dUqOhSbHmtP+ZSTsUv2/OjSzWK0w= sha256-zc3KMRPJkbv6p7sOq5Di/CNe+4XyqBBuiKjzP3A3NP0="
},
{
"type": "ExternalResourceTargetIntegrity",
"cssSelector": "#product-video-mp4",
"integrity": "sha256-OYP9B9EPFBi1vs0dUqOhSbHmtP+ZSTsUv2/OjSzWK0w="
},
{
"type": "ExternalResourceTargetIntegrity",
"cssSelector": "#product-video-webm",
"integrity": "sha256-zc3KMRPJkbv6p7sOq5Di/CNe+4XyqBBuiKjzP3A3NP0="
}
]
In this case, make the CSS selectors match the HTML video element and source elements of the web page as follows:
<video id="product-video" poster="https://cdn.example.com/poster.jpg">
<source
id="product-video-mp4"
src="https://cdn.example.com/video.mp4"
type="video/mp4"
/>
<source
id="product-video-webm"
src="https://cdn.example.com/video.webm"
type="video/webm"
/>
</video>
Below is an example of referencing multiple elements that reference the same resource from a single External Resource Integrity.
External Resource Integrity:
{
"type": "ExternalResourceTargetIntegrity",
"cssSelector": ".site-logo",
"integrity": "sha256-OYP9B9EPFBi1vs0dUqOhSbHmtP+ZSTsUv2/OjSzWK0w="
}
In this case, make the CSS selector match the HTML img elements of the web page as follows. Since .site-logo matches two img elements, both of them MUST match the integrity property.
<header>
<img class="site-logo" src="https://cdn.example.com/logo.png" />
</header>
<footer>
<img class="site-logo" src="https://cdn.example.com/logo.png" />
</footer>
Identifying elements by the integrity attribute
Below is an example of referencing the img element in a picture element from an External Resource Integrity:
External Resource Integrity:
{
"type": "ExternalResourceTargetIntegrity",
"integrity": "sha256-t7WZSGxDdqGvGg/FLw6wk9KFQy5StT1MquCf/htwjBo= sha256-4HLmAAYVRClrk+eCIrI1Rlf5/IKK0+wGoYjRs9vzl7U="
}
In this case, add the integrity attribute to the HTML img element of the web page as follows:
<picture>
<source srcset="image.jpg" media="(min-width: 400px)" />
<img
src="https://cdn.example.com/image.jpg"
integrity="sha256-t7WZSGxDdqGvGg/FLw6wk9KFQy5StT1MquCf/htwjBo= sha256-4HLmAAYVRClrk+eCIrI1Rlf5/IKK0+wGoYjRs9vzl7U="
/>
</picture>
Below is an example of referencing a video element from an External Resource Integrity.
External Resource Integrity:
[
{
"type": "ExternalResourceTargetIntegrity",
"integrity": "sha256-OYP9B9EPFBi1vs0dUqOhSbHmtP+ZSTsUv2/OjSzWK0w= sha256-zc3KMRPJkbv6p7sOq5Di/CNe+4XyqBBuiKjzP3A3NP0="
},
{
"type": "ExternalResourceTargetIntegrity",
"integrity": "sha256-zc3KMRPJkbv6p7sOq5Di/CNe+4XyqBBuiKjzP3A3NP0="
}
]
In this case, add the integrity attribute to the HTML source element and video element of the web page as follows:
<video
integrity="sha256-OYP9B9EPFBi1vs0dUqOhSbHmtP+ZSTsUv2/OjSzWK0w= sha256-zc3KMRPJkbv6p7sOq5Di/CNe+4XyqBBuiKjzP3A3NP0="
poster="https://cdn.example.com/poster.jpg"
>
<source
src="https://cdn.example.com/video.mp4"
integrity="sha256-OYP9B9EPFBi1vs0dUqOhSbHmtP+ZSTsUv2/OjSzWK0w="
type="video/mp4"
/>
<source
src="https://cdn.example.com/video.webm"
integrity="sha256-zc3KMRPJkbv6p7sOq5Di/CNe+4XyqBBuiKjzP3A3NP0="
type="video/webm"
/>
</video>
Validation Process
- Searches for the HTML elements corresponding to the External Resource Integrity.
- If the
cssSelectorproperty is present, it searches for elements specified by the CSS selector in thecssSelectorproperty. The target elements are searched for using thequerySelectorAll()method, starting from the root element of the page'sdocument(for example, the<html>element for an HTML document).- If there is a syntax error in the
cssSelectorproperty, it may be treated as a verification failure (e.g.DOMExceptionSyntaxError).
- If there is a syntax error in the
- If the
cssSelectorproperty is absent, it searches for elements whoseintegrityattribute has the same value as theintegrityproperty, using the specified method. - If no elements are found, it may be treated as a verification failure.
- If the
- Retrieves the resources corresponding to all the elements identified in step 1.
- Elements not listed in the Target Elements table are excluded from verification without fetching their resources. If no elements remain after the exclusion, it may be treated as a verification failure.
- The resources are fetched from the URL shown in the Target Elements table.
- If a network error occurs, it may be treated as a verification failure (e.g.
TypeErrorFailed to fetch., etc.).
- Validate each of the resources retrieved in step 2 and the
integrityproperty using the methods specified in SRI section 3.3.5.- If an unsupported hash algorithm is used, it may be treated as a verification failure.
How to identify element location
Elements are identified as follows, depending on whether the cssSelector property is present:
- If the
cssSelectorproperty is present: Searches for elements specified by the CSS selector in thecssSelectorproperty. - If the
cssSelectorproperty is absent: Searches for an exact match of theintegrityproperty value in theintegrityattribute of HTML elements.
If the cssSelector property is absent, care must be taken to ensure that both the integrity property and the value of the integrity attribute of the HTML element have the same value in the following cases:
- The value of the
integrityproperty consists of two or more SRI hashes - Whitespace or line break characters are used in the value of the
integrityattribute of the HTML element for readability
References
- W3C Subresource Integrity
- webappsec-subresource-integrity/signature-based-restrictions-explainer.markdown at main · w3c/webappsec-subresource-integrity
- Content Security Policy Level 3
- Apply subresource integrity to
<img>tags · Issue #113 · w3c/webappsec-subresource-integrity - integrity for downloads · Issue #68 · w3c/webappsec-subresource-integrity
- SRI: Integrity enforcement on downloads · Issue #497 · w3c/webappsec
- [SRI] Support signatures/asymm key · Issue #449 · w3c/webappsec
- Consideration: Allow integrity-check based on signature instead of actual hash · Issue #85 · w3c/webappsec-subresource-integrity