Repository navigation
HTTP API: Don't trust the insecure origin of an HTTPS site #15002
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
2ed7dd8
4bcae67
d924a8b
fef9125
e06b5a3
26d2813
330f678
5695cb8
c4d68c0
6ace9e6
ace3161
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -444,27 +444,40 @@ function get_http_origin() { | |
| * @since 3.4.0 | ||
| * | ||
| * @return string[] Array of origin URLs. | ||
| * | ||
| * @phpstan-return array<non-falsy-string> | ||
| */ | ||
| function get_allowed_http_origins() { | ||
| $admin_origin = parse_url( admin_url() ); | ||
| $home_origin = parse_url( home_url() ); | ||
| $parsed_urls = array( | ||
| parse_url( admin_url() ), | ||
| parse_url( home_url() ), | ||
| ); | ||
|
|
||
| // @todo Preserve port? | ||
|
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. 🤖 Comment by Claude Opus 5.5 (medium effort) Nit (pre-existing): this rewrite could resolve the DetailsAppend
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. It would be nice to finally do this todo 😄
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. Related, in Performance Lab we have this workaround: /**
* Determines if the HTTP origin is authorized.
*
* Note that `is_allowed_http_origin()` is not used directly because the underlying `get_allowed_http_origins()` does
* not account for the URL port (although there is a to-do comment committed in core to address this). Additionally,
* the `is_allowed_http_origin()` function in core for some reason returns a string rather than a boolean.
*
* @since 1.0.0
* @see is_allowed_http_origin()
* @access private
*
* @param string $origin Origin to check.
* @return bool Whether the origin is allowed.
*/
protected static function is_allowed_http_origin( string $origin ): bool {
// Strip out the port number since core does not account for it yet as noted in get_allowed_http_origins().
$origin = preg_replace( '/:\d+$/', '', $origin );
return '' !== is_allowed_http_origin( $origin );
} |
||
| $allowed_origins = array_unique( | ||
| array( | ||
| 'http://' . $admin_origin['host'], | ||
| 'https://' . $admin_origin['host'], | ||
| 'http://' . $home_origin['host'], | ||
| 'https://' . $home_origin['host'], | ||
| ) | ||
| ); | ||
| $allowed_origins = array(); | ||
|
|
||
| foreach ( $parsed_urls as $parsed_url ) { | ||
| if ( ! is_array( $parsed_url ) || ! isset( $parsed_url['host'] ) ) { | ||
| continue; | ||
| } | ||
| $allowed_origins[] = 'https://' . $parsed_url['host']; | ||
|
|
||
| // An insecure origin is only allowed when the URL is not served over HTTPS. | ||
| if ( isset( $parsed_url['scheme'] ) && 'https' !== $parsed_url['scheme'] ) { | ||
| $allowed_origins[] = 'http://' . $parsed_url['host']; | ||
| } | ||
| } | ||
|
westonruter marked this conversation as resolved.
Resolved
|
||
|
|
||
| $allowed_origins = array_values( array_unique( $allowed_origins ) ); | ||
|
|
||
| /** | ||
| * Changes the origin types allowed for HTTP requests. | ||
| * | ||
| * @since 3.4.0 | ||
| * | ||
| * @param string[] $allowed_origins Array of allowed HTTP origins. | ||
| * | ||
| * @phpstan-param array<non-falsy-string> $allowed_origins | ||
| */ | ||
| return apply_filters( 'allowed_http_origins', $allowed_origins ); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Tests for get_allowed_http_origins(). | ||
| * | ||
| * @group http | ||
| * | ||
| * @covers ::get_allowed_http_origins | ||
| */ | ||
| class Tests_HTTP_GetAllowedHttpOrigins extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Forces the values returned by admin_url() and home_url(). | ||
| * | ||
| * @param non-falsy-string $admin_url Value to return from admin_url(). | ||
| * @param non-falsy-string $home_url Value to return from home_url(). | ||
| */ | ||
| private function force_urls( string $admin_url, string $home_url ): void { | ||
|
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. 🤖 Comment by Claude Opus 5.5 (medium effort) The DetailsThe suggested tests set the
Edited: the first two bullets originally asserted the |
||
| add_filter( | ||
| 'admin_url', | ||
| static function () use ( $admin_url ) { | ||
| return $admin_url; | ||
| } | ||
| ); | ||
| add_filter( | ||
| 'home_url', | ||
| static function () use ( $home_url ) { | ||
| return $home_url; | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * An HTTPS site must not trust the insecure variant of its own origin, | ||
| * otherwise a network attacker on the plaintext origin can read | ||
| * credentialed cross-origin responses served over HTTPS. | ||
| * | ||
| * @ticket 66391 | ||
| */ | ||
| public function test_https_site_excludes_insecure_origin() { | ||
| $this->force_urls( 'https://example.org/wp-admin/', 'https://example.org' ); | ||
|
|
||
| $origins = get_allowed_http_origins(); | ||
|
|
||
| $this->assertContains( 'https://example.org', $origins ); | ||
| $this->assertNotContains( 'http://example.org', $origins ); | ||
| } | ||
|
|
||
| /** | ||
| * A site configured for HTTP keeps the insecure origin, and the secure | ||
| * variant is always allowed. | ||
| * | ||
| * @ticket 66391 | ||
| */ | ||
| public function test_http_site_allows_both_schemes() { | ||
| $this->force_urls( 'http://example.org/wp-admin/', 'http://example.org' ); | ||
|
|
||
| $origins = get_allowed_http_origins(); | ||
|
|
||
| $this->assertContains( 'http://example.org', $origins ); | ||
| $this->assertContains( 'https://example.org', $origins ); | ||
| } | ||
|
|
||
| /** | ||
| * The admin and home schemes are evaluated independently, so an HTTPS | ||
| * admin host does not gain an insecure origin when the home host is HTTP. | ||
| * | ||
| * @ticket 66391 | ||
| */ | ||
| public function test_admin_and_home_schemes_are_independent() { | ||
| $this->force_urls( 'https://admin.example.org/wp-admin/', 'http://home.example.org' ); | ||
|
|
||
| $origins = get_allowed_http_origins(); | ||
|
|
||
| $this->assertContains( 'https://admin.example.org', $origins ); | ||
| $this->assertNotContains( 'http://admin.example.org', $origins ); | ||
|
|
||
| $this->assertContains( 'https://home.example.org', $origins ); | ||
| $this->assertContains( 'http://home.example.org', $origins ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
🤖 Comment by Claude Opus 5.5 (medium effort)
A
@sincechangelog entry is suggested, because sites that rely on thehttp://origin will see their CORS requests start failing after upgrading.Details
For example (with the actual version):
The
allowed_http_originshook docs could also mention that thehttp://variant can be added back through that filter.The order of the returned origins also changes. Trunk returned
http://admin,https://admin,http://home,https://home; nowhttps://comes first. This only matters to code that reads the list by position.