Media: Exclude IMG from cross-origin injection in media templates#12616
Media: Exclude IMG from cross-origin injection in media templates#12616i-am-chitti wants to merge 1 commit into
Conversation
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
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. Addingcrossorigin="anonymous"overrides that and forces a CORS request, so images served from a host withoutAccess-Control-Allow-Origin(e.g. offloaded/CDN media) fail to load.The fix in https://core.trac.wordpress.org/changeset/62048 removed
IMGfromwp_add_crossorigin_attributes()inmedia.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 addscrossorigin="anonymous"unconditionally toAUDIO,IMG, andVIDEO. ThatIMGentry 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 receivescrossorigin="anonymous"and offloaded-media previews break.Fix
Remove
IMGfrom the tag list inwp_print_media_templates(), keepingAUDIOandVIDEO, 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 acrossoriginattribute.test_media_templates_add_crossorigin_to_audio_and_video—<audio>/<video>still receivecrossorigin="anonymous".Manually verified against the rendered templates:
<img>tags withcrossorigindropped from 17 → 0 with the fix, while<audio>/<video>stayed at 3 each.Manual reproduction
wp_prepare_attachment_for_jsto pointurl/sizes[*].urlathttps://placehold.jp/1024x768.jpg). Mu-plugin here - https://gist.github.com/i-am-chitti/3bcbd7d41221a1fd7d8d65bb4ca61d2fcrossorigin="anonymous"and fails with a CORS error; after the fix the attribute is gone and the preview loads.Screenshots
Before -
After -
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 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.