Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/wp-includes/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,27 +444,40 @@ function get_http_origin() {
* @since 3.4.0
*
* @return string[] Array of origin URLs.

@westonruter westonruter Oct 30, 2026 •

Copy link
Copy Markdown
Member

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 @since changelog entry is suggested, because sites that rely on the http:// origin will see their CORS requests start failing after upgrading.

Details

For example (with the actual version):

 * @since X.Y.0 The insecure `http://` origin is no longer included for a URL served over HTTPS.

The allowed_http_origins hook docs could also mention that the http:// 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; now https:// comes first. This only matters to code that reads the list by position.

*
* @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?

@westonruter westonruter Oct 30, 2026 •

Copy link
Copy Markdown
Member

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)

Nit (pre-existing): this rewrite could resolve the @todo, because a site on a non-default port, such as http://localhost:8000, never matches its own Origin header. It's fine to leave for a separate ticket.

Details

Append ':' . $port to the origin when parse_url( $url, PHP_URL_PORT ) is set.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to finally do this todo 😄

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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'];
}
}
Comment thread
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 );
}
Expand Down
81 changes: 81 additions & 0 deletions tests/phpunit/tests/http/getAllowedHttpOrigins.php
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 {

@westonruter westonruter Oct 30, 2026 •

Copy link
Copy Markdown
Member

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)

The admin_url/home_url filters run after the scheme is chosen, so these tests never reach the request-dependent is_ssl()/force_ssl_admin() logic. Tests that set $_SERVER['HTTPS'] and call force_ssl_admin() are suggested, to lock in that the http:// origin is rejected during HTTPS requests.

Details

The suggested tests set the home/siteurl options directly and then:

  • set $_SERVER['HTTPS'] = 'on' with an http:// configuration, asserting http://example.org is not allowed;
  • also call force_ssl_admin( true ) with an http:// home, asserting the same, since this is the case the protection matters most for (admin-ajax is authenticated only by the secure auth cookie there);
  • leave is_ssl() false with an http:// configuration, asserting http://example.org is allowed.

$_SERVER['HTTPS'] and force_ssl_admin( false ) would need to be reset in tear_down().

Edited: the first two bullets originally asserted the http:// origin was still allowed, based on the retracted concern in r4419422381.

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 );
}
}
Loading