-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Media: Remove IMG from crossorigin attribute injection #11291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
adamsilverstein
wants to merge
5
commits into
WordPress:trunk
from
adamsilverstein:fix/remove-img-crossorigin-isolation
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ffcd151
Remove IMG from crossorigin attribute injection
adamsilverstein d033f2a
Expand crossorigin isolation test coverage
adamsilverstein af42cbf
Refactor crossorigin tests to use data providers
adamsilverstein b05a897
Fix array alignment in crossorigin test data
adamsilverstein 6c61915
Fix same-origin test to use dynamic site_url()
adamsilverstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -186,28 +186,180 @@ public function test_client_side_processing_enabled_on_localhost() { | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * This test must run in a separate process because the output buffer | ||||||||||||||
| * callback sends HTTP headers via header(), which would fail in the | ||||||||||||||
| * main PHPUnit process where output has already started. | ||||||||||||||
| * Verifies that cross-origin elements get crossorigin="anonymous" added. | ||||||||||||||
| * | ||||||||||||||
| * @ticket 64766 | ||||||||||||||
| * | ||||||||||||||
| * @runInSeparateProcess | ||||||||||||||
| * @preserveGlobalState disabled | ||||||||||||||
| * | ||||||||||||||
| * @dataProvider data_elements_that_should_get_crossorigin | ||||||||||||||
| * | ||||||||||||||
| * @param string $html HTML input to process. | ||||||||||||||
| */ | ||||||||||||||
| public function test_output_buffer_adds_crossorigin_attributes() { | ||||||||||||||
| public function test_output_buffer_adds_crossorigin( $html ) { | ||||||||||||||
| $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36'; | ||||||||||||||
|
|
||||||||||||||
| // Start an outer buffer to capture the callback-processed output. | ||||||||||||||
| ob_start(); | ||||||||||||||
|
|
||||||||||||||
| wp_start_cross_origin_isolation_output_buffer(); | ||||||||||||||
| echo '<img src="https://external.example.com/image.jpg" />'; | ||||||||||||||
| echo $html; | ||||||||||||||
|
|
||||||||||||||
| // Flush the inner buffer to trigger the callback, sending processed output to the outer buffer. | ||||||||||||||
| ob_end_flush(); | ||||||||||||||
| $output = ob_get_clean(); | ||||||||||||||
|
|
||||||||||||||
| $this->assertStringContainsString( 'crossorigin="anonymous"', $output ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Data provider for elements that should receive crossorigin="anonymous". | ||||||||||||||
| * | ||||||||||||||
| * @return array[] | ||||||||||||||
| */ | ||||||||||||||
| public function data_elements_that_should_get_crossorigin() { | ||||||||||||||
|
Comment on lines
+217
to
+219
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| return array( | ||||||||||||||
| 'cross-origin script' => array( | ||||||||||||||
| '<script src="https://external.example.com/script.js"></script>', | ||||||||||||||
| ), | ||||||||||||||
| 'cross-origin audio' => array( | ||||||||||||||
| '<audio src="https://external.example.com/audio.mp3"></audio>', | ||||||||||||||
| ), | ||||||||||||||
| 'cross-origin video' => array( | ||||||||||||||
| '<video src="https://external.example.com/video.mp4"></video>', | ||||||||||||||
| ), | ||||||||||||||
| 'cross-origin link stylesheet' => array( | ||||||||||||||
| '<link rel="stylesheet" href="https://external.example.com/style.css" />', | ||||||||||||||
| ), | ||||||||||||||
| 'cross-origin source inside video' => array( | ||||||||||||||
| '<video><source src="https://external.example.com/video.mp4" type="video/mp4" /></video>', | ||||||||||||||
| ), | ||||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Verifies that certain elements do not get crossorigin="anonymous" added. | ||||||||||||||
| * | ||||||||||||||
| * Images are excluded because under Document-Isolation-Policy: | ||||||||||||||
| * isolate-and-credentialless, the browser handles cross-origin images | ||||||||||||||
| * in credentialless mode without needing explicit CORS headers. | ||||||||||||||
| * | ||||||||||||||
| * @ticket 64766 | ||||||||||||||
| * | ||||||||||||||
| * @runInSeparateProcess | ||||||||||||||
| * @preserveGlobalState disabled | ||||||||||||||
| * | ||||||||||||||
| * @dataProvider data_elements_that_should_not_get_crossorigin | ||||||||||||||
| * | ||||||||||||||
| * @param string $html HTML input to process. | ||||||||||||||
| */ | ||||||||||||||
| public function test_output_buffer_does_not_add_crossorigin( $html ) { | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36'; | ||||||||||||||
|
|
||||||||||||||
| ob_start(); | ||||||||||||||
|
|
||||||||||||||
| wp_start_cross_origin_isolation_output_buffer(); | ||||||||||||||
| echo $html; | ||||||||||||||
|
|
||||||||||||||
| ob_end_flush(); | ||||||||||||||
| $output = ob_get_clean(); | ||||||||||||||
|
|
||||||||||||||
| $this->assertStringNotContainsString( 'crossorigin="anonymous"', $output ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Data provider for elements that should not receive crossorigin="anonymous". | ||||||||||||||
| * | ||||||||||||||
| * @return array[] | ||||||||||||||
| */ | ||||||||||||||
| public function data_elements_that_should_not_get_crossorigin() { | ||||||||||||||
| return array( | ||||||||||||||
| 'cross-origin img' => array( | ||||||||||||||
| '<img src="https://external.example.com/image.jpg" />', | ||||||||||||||
| ), | ||||||||||||||
| 'cross-origin img with srcset' => array( | ||||||||||||||
| '<img src="https://external.example.com/image.jpg" srcset="https://external.example.com/image-2x.jpg 2x" />', | ||||||||||||||
| ), | ||||||||||||||
| 'link with cross-origin imagesrcset only' => array( | ||||||||||||||
| '<link rel="preload" as="image" imagesrcset="https://external.example.com/image.jpg 1x" href="/local-fallback.jpg" />', | ||||||||||||||
| ), | ||||||||||||||
| 'relative URL script' => array( | ||||||||||||||
| '<script src="/wp-includes/js/wp-embed.min.js"></script>', | ||||||||||||||
| ), | ||||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Same-origin URLs should not get crossorigin="anonymous". | ||||||||||||||
| * | ||||||||||||||
| * Uses site_url() at runtime since the test domain varies by CI config. | ||||||||||||||
| * | ||||||||||||||
| * @ticket 64766 | ||||||||||||||
| * | ||||||||||||||
| * @runInSeparateProcess | ||||||||||||||
| * @preserveGlobalState disabled | ||||||||||||||
| */ | ||||||||||||||
| public function test_output_buffer_does_not_add_crossorigin_to_same_origin() { | ||||||||||||||
| $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36'; | ||||||||||||||
|
|
||||||||||||||
| ob_start(); | ||||||||||||||
|
|
||||||||||||||
| wp_start_cross_origin_isolation_output_buffer(); | ||||||||||||||
| echo '<script src="' . site_url( '/wp-includes/js/wp-embed.min.js' ) . '"></script>'; | ||||||||||||||
|
|
||||||||||||||
| ob_end_flush(); | ||||||||||||||
| $output = ob_get_clean(); | ||||||||||||||
|
|
||||||||||||||
| $this->assertStringNotContainsString( 'crossorigin="anonymous"', $output ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Elements that already have a crossorigin attribute should not be modified. | ||||||||||||||
| * | ||||||||||||||
| * @ticket 64766 | ||||||||||||||
| * | ||||||||||||||
| * @runInSeparateProcess | ||||||||||||||
| * @preserveGlobalState disabled | ||||||||||||||
| */ | ||||||||||||||
| public function test_output_buffer_does_not_override_existing_crossorigin() { | ||||||||||||||
| $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36'; | ||||||||||||||
|
|
||||||||||||||
| ob_start(); | ||||||||||||||
|
|
||||||||||||||
| wp_start_cross_origin_isolation_output_buffer(); | ||||||||||||||
| echo '<script src="https://external.example.com/script.js" crossorigin="use-credentials"></script>'; | ||||||||||||||
|
|
||||||||||||||
| ob_end_flush(); | ||||||||||||||
| $output = ob_get_clean(); | ||||||||||||||
|
|
||||||||||||||
| $this->assertStringContainsString( 'crossorigin="use-credentials"', $output, 'Existing crossorigin attribute should not be overridden.' ); | ||||||||||||||
| $this->assertStringNotContainsString( 'crossorigin="anonymous"', $output ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Multiple tags in the same output should each be handled correctly. | ||||||||||||||
| * | ||||||||||||||
| * @ticket 64766 | ||||||||||||||
| * | ||||||||||||||
| * @runInSeparateProcess | ||||||||||||||
| * @preserveGlobalState disabled | ||||||||||||||
| */ | ||||||||||||||
| public function test_output_buffer_handles_mixed_tags() { | ||||||||||||||
| $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36'; | ||||||||||||||
|
|
||||||||||||||
| ob_start(); | ||||||||||||||
|
|
||||||||||||||
| wp_start_cross_origin_isolation_output_buffer(); | ||||||||||||||
| echo '<img src="https://external.example.com/image.jpg" />'; | ||||||||||||||
| echo '<script src="https://external.example.com/script.js"></script>'; | ||||||||||||||
| echo '<audio src="https://external.example.com/audio.mp3"></audio>'; | ||||||||||||||
|
|
||||||||||||||
| ob_end_flush(); | ||||||||||||||
| $output = ob_get_clean(); | ||||||||||||||
|
|
||||||||||||||
| // IMG should NOT have crossorigin. | ||||||||||||||
| $this->assertStringContainsString( '<img src="https://external.example.com/image.jpg" />', $output, 'IMG should not be modified.' ); | ||||||||||||||
|
|
||||||||||||||
| // Script and audio should have crossorigin. | ||||||||||||||
| $this->assertSame( 2, substr_count( $output, 'crossorigin="anonymous"' ), 'Script and audio should both get crossorigin, but not img.' ); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.