Skip to main content

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.

note

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​

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:

NameTypeDescription
typestringREQUIRED. It MUST be ExternalResourceTargetIntegrity.
integritystringREQUIRED. It MUST be the sriString data type. For available hash functions, it MUST conform to Hash Algorithm. Example: sha256-4HLmAAYVRClrk+eCIrI1Rlf5/IKK0+wGoYjRs9vzl7U=
cssSelectorstringOPTIONAL. 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
note

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.

ElementURL to fetchRequest destination (for reference)Notes
imgHTMLImageElement.currentSrcimageThe candidate selected by the user agent from the src and srcset attributes and the source elements in the picture element
picturecurrentSrc of the child img elementimageThe picture element itself has no URL, so the child img element's URL is used
source (parent is video / audio)src attributevideo or audio, depending on the parent elementRegardless of whether it is selected by the parent element
videoHTMLMediaElement.currentSrcvideoThe URL of the poster attribute is not covered
audioHTMLMediaElement.currentSrcaudio
tracksrc attributetrackSubtitle and chapter files such as WebVTT
Elements under consideration

The following elements and attributes are under consideration for inclusion in a future version.

  • source element in a picture element: verifying the candidates of the srcset attribute regardless of whether it is selected. The resource of the selected candidate can be verified as the picture or img element in the table above
  • embed element: covering it only when the type attribute is an image, video, or audio MIME type
  • object element: covering it only when the data and type attributes are similarly restricted
  • map and area elements: the href attribute of the area element will be considered together with verification of external links (#127)
  • Inline SVG (svg element): it can be handled by HTML Fragment Integrity, but that may not be suitable, so consideration continues. External SVG files referenced from an img element can be handled as the img element in the table above
  • MathML (math element)
  • The poster attribute of the video element
Elements not covered

The following elements and resources are not covered. Use the following means instead.

  • iframe element: place a CA Set in the nested document and verify it in that document
  • script, link, and style elements: use the integrity attribute of Subresource Integrity
  • a element: the resource of the href attribute 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 the background-image and content properties): display images to be verified with the img element
  • Resources fetched by scripts with fetch() or XMLHttpRequest: 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 video element. A mechanism for assuring the integrity of streamed videos is under consideration separately from External Resource Integrity
Security Considerations

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 cssSelector property: Specify in the cssSelector property a CSS selector that matches the list of HTML elements referencing the resource to be verified.
  • When not using the cssSelector property: Specify the same value as the integrity property for the integrity attribute 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​

  1. Searches for the HTML elements corresponding to the External Resource Integrity.
    • If the cssSelector property is present, it searches for elements specified by the CSS selector in the cssSelector property. The target elements are searched for using the querySelectorAll() method, starting from the root element of the page's document (for example, the <html> element for an HTML document).
      • If there is a syntax error in the cssSelector property, it may be treated as a verification failure (e.g. DOMException SyntaxError).
    • If the cssSelector property is absent, it searches for elements whose integrity attribute has the same value as the integrity property, using the specified method.
    • If no elements are found, it may be treated as a verification failure.
  2. 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. TypeError Failed to fetch., etc.).
  3. Validate each of the resources retrieved in step 2 and the integrity property 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 cssSelector property is present: Searches for elements specified by the CSS selector in the cssSelector property.
  • If the cssSelector property is absent: Searches for an exact match of the integrity property value in the integrity attribute of HTML elements.
info

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 integrity property consists of two or more SRI hashes
  • Whitespace or line break characters are used in the value of the integrity attribute of the HTML element for readability

References​