Repository navigation
HTTP API: Don't trust the insecure origin of an HTTPS site - #15002
johnbillion wants to merge 11 commits into
Conversation
`get_allowed_http_origins()` added both the `http://` and `https://` variants of the admin and home host unconditionally. On an HTTPS-only site the `http://` origin was trusted even though the site never uses it, allowing a network attacker on plaintext traffic to read credentialed cross-origin responses. Add the `http://` origin only when the corresponding `admin_url()`/`home_url()` is not served over HTTPS. The admin and home schemes are evaluated independently. Fixes #66391.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
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. |
| $allowed_origins[] = 'http://' . $home_origin['host']; | ||
| } | ||
|
|
||
| $allowed_origins = array_unique( $allowed_origins ); |
There was a problem hiding this comment.
There are PHPStan errors here which I think we can take an opportunity to fix:
454 Cannot access offset 'host' on array{scheme?: string, host?: string, port?: int<0, 65535>, user?: string, pass?:
string, path?: string, query?: string, fragment?: string}|false.
🪪 offsetAccess.nonOffsetAccessible
455 Cannot access offset 'host' on array{scheme?: string, host?: string, port?: int<0, 65535>, user?: string, pass?:
string, path?: string, query?: string, fragment?: string}|false.
🪪 offsetAccess.nonOffsetAccessible
459 Cannot access offset 'scheme' on array{scheme?: string, host?: string, port?: int<0, 65535>, user?: string, pass?:
string, path?: string, query?: string, fragment?: string}|false.
🪪 offsetAccess.nonOffsetAccessible
460 Offset 'host' might not exist on array{scheme: string, host?: string, port?: int<0, 65535>, user?: string, pass?:
string, path?: string, query?: string, fragment?: string}.
🪪 offsetAccess.notFound
462 Cannot access offset 'scheme' on array{scheme?: string, host?: string, port?: int<0, 65535>, user?: string, pass?:
string, path?: string, query?: string, fragment?: string}|false.
🪪 offsetAccess.nonOffsetAccessible
463 Offset 'host' might not exist on array{scheme: string, host?: string, port?: int<0, 65535>, user?: string, pass?:
string, path?: string, query?: string, fragment?: string}.
🪪 offsetAccess.notFound
There was a problem hiding this comment.
🤖 Comment by Claude Opus 5.5 (medium effort)
Correction: the main concern in this review was retracted (see the reply on r4419422381). Rows 2 and 3 below show the fix working as intended: during an HTTPS request, the http:// origin is not trusted, which is what protects FORCE_SSL_ADMIN sites with an http:// home.
Details
These are the results from a temporary PHPUnit probe for host example.org:
| Scenario | get_allowed_http_origins() |
|---|---|
home/siteurl http://, HTTP request |
https://, http:// |
home/siteurl http:// + force_ssl_admin( true ), HTTPS request |
https:// only |
home/siteurl http://, HTTPS request (no FORCE_SSL_ADMIN) |
https:// only |
home/siteurl https://, request not detected as HTTPS (e.g. proxy without is_ssl() detection) |
https://, http:// (admin_url() is http://…/wp-admin/) |
home_url/admin_url filtered to HTTPS://example.org |
https://, http:// |
filtered to //example.org |
https:// only |
Rows 4 to 6 only arise with an undetected-HTTPS misconfiguration or unusual URL filters. The other inline comments (the (array) cast, the changelog entry, and the port) are unaffected by this correction.
Edited: this originally called rows 2 and 3 a regression.
| * @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 { |
There was a problem hiding this comment.
🤖 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 anhttp://configuration, assertinghttp://example.orgis not allowed; - also call
force_ssl_admin( true )with anhttp://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 anhttp://configuration, assertinghttp://example.orgis 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.
| * @param string[] $allowed_origins Array of allowed HTTP origins. | ||
| */ | ||
| return apply_filters( 'allowed_http_origins', $allowed_origins ); | ||
| return (array) apply_filters( 'allowed_http_origins', $allowed_origins ); |
There was a problem hiding this comment.
🤖 Comment by Claude Opus 5.5 (medium effort)
The (array) cast silently turns an invalid filter return, such as a string, into an allow-list. On trunk the same mistake raised a TypeError that made it visible.
Details
A callback that returns the string 'https://a.example' becomes array( 'https://a.example' ) (verified), and an object becomes an array of its public properties. On trunk the TypeError came from in_array() in is_allowed_http_origin(). The options are to drop the cast, or to check is_array() and fall back to $allowed_origins, perhaps with _doing_it_wrong().
There was a problem hiding this comment.
This is just to prevent a fatal error with the addition of the native array return type hint. Maybe this would be better:
| return (array) apply_filters( 'allowed_http_origins', $allowed_origins ); | |
| $origins = (array) apply_filters( 'allowed_http_origins', $allowed_origins ); | |
| return array_filter( $origins, 'is_string' ); |
Or we can just drop the change.
There was a problem hiding this comment.
This is not a pattern that should be encouraged, although it is used in a few places already. Type casting just serves to mask an underlying mistake.
There was a problem hiding this comment.
Yeah, this could be more holistically addressed with Core-51525.
The main benefit of the cast here is that it prevents a fatal error.
I've reverted this in 6ace9e6
| @@ -445,19 +445,28 @@ function get_http_origin() { | |||
| * | |||
| * @return string[] Array of origin URLs. | |||
There was a problem hiding this comment.
🤖 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.
| parse_url( home_url() ), | ||
| ); | ||
|
|
||
| // @todo Preserve port? |
There was a problem hiding this comment.
🤖 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.
There was a problem hiding this comment.
It would be nice to finally do this todo 😄
There was a problem hiding this comment.
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 );
}|
Tested 5695cb8 on 7.2-beta3-64990. Home and admin share a host here. I forced HTTPS per request with
End to end,
So an HTTPS site no longer echoes an |
get_allowed_http_origins()added both thehttp://andhttps://variants of the admin and home host unconditionally, so an HTTPS-only site incorrectly trusted anhttp://origin. This adds thehttp://origin only when the correspondingadmin_url()/home_url()is not HTTPS.Trac ticket: https://core.trac.wordpress.org/ticket/66391