Make WordPress Core

Opened 8 months ago

Closed 8 months ago

Last modified 8 months ago

#64886 closed defect (bug) (fixed)

Media: Remove `IMG` from cross-origin isolation `crossorigin="anonymous"` injection.

Reported by: adamsilverstein Owned by: adamsilverstein
Priority: normal Milestone: 7.0
Component: Media Version: 7.0
Severity: normal Keywords: has-patch has-unit-tests commit
Cc: Focuses:

Description

Description

In #64766, Document-Isolation-Policy: isolate-and-credentialless was introduced to enable cross-origin isolation for client-side media processing. As part of that change, wp_add_crossorigin_attributes() injects crossorigin="anonymous" on several HTML elements including IMG.

However, under Document-Isolation-Policy: isolate-and-credentialless, the browser's credentialless mode already handles cross-origin image loading without requiring CORS headers on the remote server. When crossorigin="anonymous" is explicitly added to an <img> element, it overrides this behavior and forces a CORS preflight request. If the remote server does not include Access-Control-Allow-Origin in its response headers, the image fails to load entirely.

This causes image previews to break in the block editor sidebar when an image block references an external URL from a server that does not serve CORS headers. For example, https://placehold.jp/1024x768.jpg triggers this bug.

Steps to reproduce

  1. Open the block editor (WordPress 7.0, Chromium 137+).
  2. Add an Image block.
  3. Select "Insert from URL" and enter an external image URL that does not serve CORS headers (e.g. https://placehold.jp/1024x768.jpg).
  4. Observe that the sidebar image preview fails to render with a CORS error in the console.

Expected behavior

The image preview should render. Under Document-Isolation-Policy: isolate-and-credentialless, the browser handles cross-origin images in credentialless mode without needing explicit CORS headers.

Actual behavior

The image fails to load because crossorigin="anonymous" forces a CORS request, and the remote server does not include the required Access-Control-Allow-Origin header.

Proposed fix

Remove IMG (and the related imagesrcset handling on LINK elements) from the $cross_origin_tag_attributes array in wp_add_crossorigin_attributes(). The other elements (AUDIO, VIDEO, SCRIPT, LINK, SOURCE) still need the attribute for proper error reporting, media processing, and font loading.

The corresponding client-side JavaScript change (removing img from the MutationObserver in @wordpress/block-editor) is handled in the Gutenberg PR.

Change History (7)

#1 @adamsilverstein
8 months ago

  • Component General → Media
  • Owner set to adamsilverstein
  • Status new → assigned

This ticket was mentioned in ​PR #11291 on ​WordPress/wordpress-develop by ​@adamsilverstein.


8 months ago
#2

  • Keywords has-unit-tests added

Trac ticket: https://core.trac.wordpress.org/ticket/64886

Related Gutenberg issue: ​https://github.com/WordPress/gutenberg/issues/76476

## Description

Removes IMG from the list of elements that receive crossorigin="anonymous" in wp_add_crossorigin_attributes(). Also removes the related imagesrcset handling from LINK elements.

Under Document-Isolation-Policy: isolate-and-credentialless (introduced in r61844), the browser's credentialless mode already handles cross-origin image loading without requiring CORS headers on the remote server. Explicitly adding crossorigin="anonymous" to <img> elements overrides this behavior and forces a CORS preflight — breaking images from servers that don't include Access-Control-Allow-Origin in their response headers.

This causes image previews to break in the block editor sidebar when an image block references an external URL (e.g. https://placehold.jp/1024x768.jpg).

Related Gutenberg PR: ​https://github.com/WordPress/gutenberg/pull/76618

## Changes

  • Remove IMG entry from $cross_origin_tag_attributes in wp_add_crossorigin_attributes()
  • Remove imagesrcset from LINK entry (used for <link> preload tags for images — same issue)
  • Update existing test to use <script> instead of <img> for the positive case
  • Add new test asserting <img> does NOT get crossorigin="anonymous"

## Testing

  1. Open block editor in Chromium 137+
  2. Add an Image block → "Insert from URL" → https://placehold.jp/1024x768.jpg
  3. Sidebar preview should render correctly (no CORS error)
  4. Verify Document-Isolation-Policy: isolate-and-credentialless header is still sent
  5. Drag an image from desktop onto the editor — client-side media processing still works

#3 @adamsilverstein
8 months ago

  • Keywords commit added

​@adamsilverstein commented on ​PR #11291:


8 months ago
#4

Added a proposed commit message to the PR description.

​@westonruter commented on ​PR #11291:


8 months ago
#5

Why are images the one exception that don't need crossorign=anonymous? Why don't VIDEO, AUDIO, et al specifically _not_ require it?

#6 @adamsilverstein
8 months ago

  • Resolution → fixed
  • Status assigned → closed

In 62048:

Media: Remove IMG from crossorigin attribute injection.

Under Document-Isolation-Policy: isolate-and-credentialless, the browser's credentialless mode already handles cross-origin image loading without requiring CORS headers. Explicitly adding crossorigin="anonymous" to <img> elements overrides this behavior and forces a CORS preflight request, breaking images from servers that don't include Access-Control-Allow-Origin in their response headers.

This also removes the related imagesrcset handling from LINK elements, which had the same issue for <link> preload tags for images.

See related Gutenberg issue: ​https://github.com/WordPress/gutenberg/issues/76476.

Follow-up to [61844], [61846].

Props adamsilverstein, swissspidy.
Fixes #64886.

#7 @ianmjones
8 months ago

#64868 was marked as a duplicate.

Note: See TracTickets for help on using tickets.