Make WordPress Core

Opened 4 months ago

Closed 4 months ago

Last modified 3 weeks ago

#65673 closed defect (bug) (fixed)

Media: Previews for offloaded media broken by crossorigin="anonymous"

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

Description (last modified by ianmjones)

This is a follow-up to #64886.

In WordPress 7.1beta2 this behaviour of adding crossorigin="anonymous" to img tags in the media library picker has returned, breaking previews for media being served from a CDN due to CORS errors.

Attachments (3)

media-library-picker-preview-broken-for-offloaded-media.png​ (996.9 KB ) - added by ianmjones 4 months ago.
Screenshot of broken previews with img tag for 1st item highlighted
wp-7_0_2-gutenberg-23_5_3-CORS.png​ (534.6 KB ) - added by ianmjones 4 months ago.
wp-7_0_2-gutenberg-23_5_3-img--cross-origin-attribute.png​ (870.9 KB ) - added by ianmjones 4 months ago.

Change History (19)

#1 @ianmjones
4 months ago

  • Description modified (diff)

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


4 months ago
#2

  • Keywords has-patch has-unit-tests added

Media library picker previews break for media served from a CDN (or any host without CORS headers) because wp_print_media_templates() injects crossorigin="anonymous" on IMG tags in the media manager Backbone templates.

What the problem was:

  • When the client-side media processing feature was re-introduced in trunk, wp_print_media_templates() began adding crossorigin="anonymous" to AUDIO, IMG, and VIDEO tags in the media templates.
  • This reintroduced, for the media library picker, the exact issue already fixed for wp_add_crossorigin_attributes() in [62048] (#64886).
  • Under Document-Isolation-Policy: isolate-and-credentialless the browser loads cross-origin images in credentialless mode without CORS headers. Forcing crossorigin="anonymous" on IMG triggers a CORS request that fails for images served without Access-Control-Allow-Origin headers, so offloaded media previews break.

What the fix does:

  • Removes IMG from the tags that receive crossorigin="anonymous" in wp_print_media_templates(), so it matches wp_add_crossorigin_attributes().
  • AUDIO and VIDEO tags continue to receive the attribute, unchanged.
  • Adds a code comment explaining why IMG is excluded, to prevent recurrence.

Approach and why:

  • Mirrors the established, intentional behaviour from [62048]; it is the minimal one-list change that resolves the regression without touching audio/video handling or the output-buffer path.

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

## Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8
Used for: Ticket analysis, writing the unit tests, and PR Description. All changes were reviewed by me.

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


4 months ago
#3

## Trac ticket

https://core.trac.wordpress.org/ticket/65673

## Problem

Under Document-Isolation-Policy: isolate-and-credentialless, the browser already loads cross-origin images in credentialless mode without requiring CORS headers. Adding crossorigin="anonymous" overrides that and forces a CORS request, so images served from a host without Access-Control-Allow-Origin (e.g. offloaded/CDN media) fail to load.

The fix in https://core.trac.wordpress.org/changeset/62048 removed IMG from wp_add_crossorigin_attributes() in media.php, but the media library picker never uses that function. It has a separate injection path in ​https://github.com/i-am-chitti/wordpress-develop/blob/9ace411f9e9c39a16d989dce7b617835ef864561/src/wp-includes/media-template.php#L156 that rewrites the Backbone <script type="text/html"> templates and adds crossorigin="anonymous" unconditionally to AUDIO, IMG, and VIDEO. That IMG entry was never part of the https://core.trac.wordpress.org/ticket/64886 fix — it has been present since the original backport, and returned to shipping builds when the client-side media processing feature was re-introduced in 7.1beta2. As a result, every <img> in the picker receives crossorigin="anonymous" and offloaded-media previews break.

## Fix

Remove IMG from the tag list in wp_print_media_templates(), keeping AUDIO and VIDEO, which still need the attribute for media processing and error reporting. This mirrors https://core.trac.wordpress.org/changeset/62048.

## Testing

Added regression coverage to Tests_Media_wpCrossOriginIsolation:

  • test_media_templates_do_not_add_crossorigin_to_img — no <img> in the rendered media templates receives a crossorigin attribute.
  • test_media_templates_add_crossorigin_to_audio_and_video — <audio>/<video> still receive crossorigin="anonymous".

Manually verified against the rendered templates: <img> tags with crossorigin dropped from 17 → 0 with the fix, while <audio>/<video> stayed at 3 each.

### Manual reproduction

  1. Rewrite attachment URLs to a non-CORS host (e.g. filter wp_prepare_attachment_for_js to point url/sizes[*].url at https://placehold.jp/1024x768.jpg). Mu-plugin here - ​https://gist.github.com/i-am-chitti/3bcbd7d41221a1fd7d8d65bb4ca61d2f
  2. Open the media modal (Add Media / Image block → Media Library) and inspect a grid thumbnail: before the fix it carries crossorigin="anonymous" and fails with a CORS error; after the fix the attribute is gone and the preview loads.

## Screenshots

Before -

https://github.com/user-attachments/assets/11fec533-23db-42aa-98ab-20995a110bd3

After -

https://github.com/user-attachments/assets/62032e73-f818-4345-974c-0b0cf0005efe

## Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Opus 4.8
Used for: used to help investigate this issue and prepare the patch. All changes were reviewed, tested, and verified by me before submitting.

This ticket was mentioned in ​Slack in #core-media by ianmjones. ​View the logs.


4 months ago

#5 @swissspidy
4 months ago

  • Owner set to adamsilverstein
  • Status new → assigned

#6 follow-up: @adamsilverstein
4 months ago

Thanks for the bug report @ianmjones!

Questions:

  • Do you still see the issue when the Gutenberg plugin is enabled?
  • I see there are two PRs open for this, do either/both fix the issue for you?

We tried to remove this in ​https://github.com/WordPress/gutenberg/pull/76618 / https://core.trac.wordpress.org/changeset/62048 but I guess we missed the context of the media modal.

#7 @adamsilverstein
4 months ago

I see we have two nearly identical PRs to fix this. Props to both of you! The first one was ​https://github.com/WordPress/wordpress-develop/pull/12615 and it looks good to me.

I'm investigating if this fix needs to be ported to Gutenberg as well. The reporter also mentioned separately seeing a similar issue with the Playlist block I am also going to look into.

This ticket was mentioned in ​Slack in #core-media by adamsilverstein. ​View the logs.


4 months ago

#9 in reply to: ↑ 6 ; follow-up: @ianmjones
4 months ago

Replying to adamsilverstein:

Thanks for the bug report @ianmjones!

Questions:

  • Do you still see the issue when the Gutenberg plugin is enabled?
  • I see there are two PRs open for this, do either/both fix the issue for you?

I'm not set up for testing WP PRs just now, but in WP 7.0.2 with Gutenberg 23.5.3 I can confirm it's still an issue, even in Media Library grid mode.

#10 in reply to: ↑ 9 @adamsilverstein
4 months ago

I'm not set up for testing WP PRs just now, but in WP 7.0.2 with Gutenberg 23.5.3 I can confirm it's still an issue, even in Media Library grid mode.

Ah, I see. Although the DIP headers are not active in the media library, the attribute is still being added. Thanks for clarifying.

Regarding testing: you can test core prs using Playground it works well for most testing, eg. ​https://playground.wordpress.net/wordpress.html?pr=12615 (or insert the pr # you want to test). Not sure if you can set up your CDN integration there, but worth a quick try to confirm the fixes work.

#11 @adamsilverstein
4 months ago

Following up after digging into this.

Root cause: there are two separate crossorigin injection paths, and [62048] / #64886 only fixed one of them. wp_print_media_templates() has its own injection for the Backbone media templates that still included IMG — that's what returned in 7.1beta2. The stale IMG entry originated in the Gutenberg plugin's gutenberg_override_media_templates(), which was missed when ​Gutenberg #76618 removed img everywhere else, and was then carried into Core by the client-side media processing backport.

I confirmed the issue also reproduces with the Gutenberg plugin enabled — the plugin re-adds the attribute on top of Core's, producing duplicate crossorigin="anonymous" attributes on 7.1.

Gutenberg side: ​Gutenberg #80532 fixes the plugin's own injection path and also strips the attribute Core added, so plugin users on 7.1 betas get working previews before the Core fix lands.

Playlist block: @ianmjones also reported (Slack) that the new Playlist block can't play CDN-hosted audio while the Audio block can. That's a separate root cause — the bundled waveform-player library hardcodes crossOrigin = 'anonymous' on its audio element — so no additional Core ticket is needed. Fixed in ​Gutenberg #80533, tracked in ​Gutenberg #80536; the fix will reach Core through the regular editor package updates.

#12 @adamsilverstein
4 months ago

Playlist block

The waveform-player library has shipped a fix that will let us avoid this issue, see ​https://github.com/arraypress/waveform-player/issues/18

Last edited 4 months ago by adamsilverstein (previous) (diff)

#13 @adamsilverstein
4 months ago

  • Keywords commit added
  • Milestone Awaiting Review → 7.1

#14 @adamsilverstein
4 months ago

  • Resolution → fixed
  • Status assigned → closed

In 62819:

Media: Exclude IMG tags from crossorigin injection in media templates.

The client-side media processing feature added crossorigin="anonymous" to AUDIO, IMG, and VIDEO tags in the Backbone media templates printed by wp_print_media_templates(). Forcing the attribute on IMG tags triggers CORS requests that fail for images served without Access-Control-Allow-Origin headers, breaking media library previews for media offloaded to a CDN.

This is the same problem previously fixed for wp_add_crossorigin_attributes() in [62048]: under Document-Isolation-Policy: isolate-and-credentialless, browsers load cross-origin images in credentialless mode, so the attribute is unnecessary on IMG tags. The media templates have a separate injection path that was missed at the time. Remove IMG from the list of tags receiving the attribute so both paths match. AUDIO and VIDEO tags are unchanged.

Follow-up to [62048].

Props khokansardar, iamchitti, ianmjones, swissspidy.
Fixes #65673.

#15 @adamsilverstein
2 months ago

Following up here because the AUDIO and VIDEO tags turned out to have the same problem as IMG did.

Claude tested this in the browser, short version:

Under isolate-and-credentialless, cross-origin audio and video load without a crossorigin attribute exactly as they do without the header, and with crossorigin="anonymous" they fail (media error code 4) whenever the host does not send Access-Control-Allow-Origin. Verified in Chromium 151 and Chrome 152. So the media modal templates and the editor page output buffer are still breaking CDN-hosted audio and video previews the same way they were breaking images before [62819].

​https://github.com/WordPress/wordpress-develop/pull/13446 on #65930 removes the injection entirely, media templates included. If anyone here has a site with offloaded audio or video, testing that build in the media modal would be a big help: ​https://playground.wordpress.net/?core-pr=13446

#16 @adamsilverstein
3 weeks ago

In 64489:

Media: stop adding crossorigin attributes in the editor.

Fix an issue where editing a post in Chromium 137+ could end in a fatal error and a blank screen when the page loaded any cross-origin resource. wp_add_crossorigin_attributes() ran WP_HTML_Tag_Processor over the whole document inside an ob_start() display handler, where errors cannot be caught. The injected crossorigin="anonymous" was not needed in the first place: Document-Isolation-Policy: isolate-and-credentialless loads cross-origin scripts, stylesheets, audio and video without credentials instead of blocking them, and forcing a CORS request broke any host that does not send Access-Control-Allow-Origin, such as media offloaded to a CDN.

The header is now sent directly by the new wp_send_document_isolation_policy_header(), and the editor page is no longer buffered or rewritten. wp_start_cross_origin_isolation_output_buffer() and wp_add_crossorigin_attributes() are deprecated; the first only sends the header and the second returns the HTML unchanged. wp_print_media_templates() no longer adds the attribute to AUDIO and VIDEO tags. Cross-origin isolation and client-side media processing are unaffected.

Follow-up to [61844], [62048], [62819].

Props b0b3k, khokansardar, arkaprabhachowdhury, westonruter, andrewserong, algorithmsunlocks, sabernhardt, swissspidy, adamsilverstein.
Fixes #65930. See #65673.

Note: See TracTickets for help on using tickets.