Skip to content

Media: Exclude IMG from crossorigin injection in media templates#12615

Open
itzmekhokan wants to merge 1 commit into
WordPress:trunkfrom
itzmekhokan:fix/65673-img-crossorigin-preview
Open

Media: Exclude IMG from crossorigin injection in media templates#12615
itzmekhokan wants to merge 1 commit into
WordPress:trunkfrom
itzmekhokan:fix/65673-img-crossorigin-preview

Conversation

@itzmekhokan

Copy link
Copy Markdown

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 Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

When the client-side media processing feature was re-introduced,
`wp_print_media_templates()` began adding `crossorigin="anonymous"` to
`AUDIO`, `IMG`, and `VIDEO` tags inside the media manager Backbone
templates. This reintroduced, for the media library picker, the exact
issue fixed for `wp_add_crossorigin_attributes()` in [62048].

Under `Document-Isolation-Policy: isolate-and-credentialless` the
browser already loads cross-origin images in credentialless mode
without CORS headers. Forcing `crossorigin="anonymous"` on `IMG`
triggers a CORS request that breaks previews of images served without
`Access-Control-Allow-Origin` headers, such as media offloaded to a
CDN.

Remove `IMG` from the injected tags so it matches
`wp_add_crossorigin_attributes()`, and add a regression test.

Follow-up to #64886.
Fixes #65673.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props khokansardar.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant