Security review fixes: keep pending-capture ids, ship uninstall.php (+verify step), webhook guard, IPv6 /64 + global order throttle, token-safe redirects, honest refund wording, request-id and overlay-token hardening
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
68bd917660
commit
473fe192cd
28 files changed
+747
-34
No files matched your search
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Update;
|
||||
|
||||
/**
|
||||
* Pure decisions for redirects of token-bearing update requests. The access token may only ever travel to the pinned
|
||||
* host over https; a redirect is followed (by Updater) only when it stays on that host, with a small hop limit.
|
||||
*/
|
||||
final class RedirectPolicy {
|
||||
|
||||
public const MAX_HOPS = 3;
|
||||
|
||||
/**
|
||||
* @param string $current URL that answered with a 3xx.
|
||||
* @param string $location Its Location header (absolute or relative).
|
||||
* @param string $pinnedHost Configured update host (lower case).
|
||||
* @param int $hop Redirects already followed (0 for the first 3xx).
|
||||
* @return string|null Absolute URL to follow with the token, or null to stop and NOT follow.
|
||||
*/
|
||||
public static function nextUrl( string $current, string $location, string $pinnedHost, int $hop ): ?string {
|
||||
if ( $hop >= self::MAX_HOPS || '' === trim( $location ) || preg_match( '/[\x00-\x1f\x7f]/', $location ) ) {
|
||||
return null;
|
||||
}
|
||||
$abs = self::resolve( $current, trim( $location ) );
|
||||
if ( null === $abs || ! ReleaseParser::isAllowedUrl( $abs, $pinnedHost ) ) {
|
||||
return null;
|
||||
}
|
||||
$port = parse_url( $abs, PHP_URL_PORT );
|
||||
return null === $port || 443 === (int) $port ? $abs : null;
|
||||
}
|
||||
|
||||
/** Whether the token may be attached to a request for $url. */
|
||||
public static function tokenAllowed( string $url, string $pinnedHost ): bool {
|
||||
if ( ! ReleaseParser::isAllowedUrl( $url, $pinnedHost ) ) {
|
||||
return false;
|
||||
}
|
||||
$port = parse_url( $url, PHP_URL_PORT );
|
||||
return null === $port || 443 === (int) $port;
|
||||
}
|
||||
|
||||
private static function resolve( string $base, string $loc ): ?string {
|
||||
if ( 1 === preg_match( '#^[a-z][a-z0-9+.\-]*://#i', $loc ) ) {
|
||||
return $loc;
|
||||
}
|
||||
$b = parse_url( $base );
|
||||
if ( ! is_array( $b ) || ! isset( $b['scheme'], $b['host'] ) ) {
|
||||
return null;
|
||||
}
|
||||
if ( str_starts_with( $loc, '//' ) ) {
|
||||
return $b['scheme'] . ':' . $loc;
|
||||
}
|
||||
$origin = $b['scheme'] . '://' . $b['host'] . ( isset( $b['port'] ) ? ':' . $b['port'] : '' );
|
||||
if ( str_starts_with( $loc, '/' ) ) {
|
||||
return $origin . $loc;
|
||||
}
|
||||
$dir = preg_replace( '#/[^/]*$#', '/', (string) ( $b['path'] ?? '/' ) );
|
||||
return $origin . $dir . $loc;
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ final class Updater {
|
||||
add_filter( 'pre_set_site_transient_update_plugins', array( self::class, 'inject' ) );
|
||||
add_filter( 'plugins_api', array( self::class, 'pluginInfo' ), 10, 3 );
|
||||
add_filter( 'http_request_args', array( self::class, 'authorize' ), 10, 2 );
|
||||
add_filter( 'pre_http_request', array( self::class, 'followSafely' ), 10, 3 );
|
||||
add_action( 'upgrader_process_complete', array( self::class, 'afterUpgrade' ), 10, 2 );
|
||||
add_action( 'admin_post_' . self::ACTION_CHECK, array( self::class, 'handleCheck' ) );
|
||||
}
|
||||
@@ -170,7 +171,7 @@ final class Updater {
|
||||
}
|
||||
$token = trim( (string) Settings::get( 'update_token', '' ) );
|
||||
$host = self::repo()['host'];
|
||||
if ( '' === $token || ! ReleaseParser::isAllowedUrl( $url, $host ) ) {
|
||||
if ( '' === $token || ! RedirectPolicy::tokenAllowed( $url, $host ) ) {
|
||||
return $args;
|
||||
}
|
||||
$args['headers'] = is_array( $args['headers'] ?? null ) ? $args['headers'] : array();
|
||||
@@ -181,6 +182,50 @@ final class Updater {
|
||||
return $args;
|
||||
}
|
||||
|
||||
private static bool $following = false;
|
||||
|
||||
/**
|
||||
* Token-bearing requests never let HTTP redirect handling carry the Authorization header anywhere: we send the
|
||||
* request with redirection disabled and follow a 3xx ourselves ONLY while it stays on the pinned host (max 3 hops).
|
||||
* A redirect to any other host is not followed at all, so the token cannot leak and the update fails closed.
|
||||
*/
|
||||
public static function followSafely( mixed $pre, mixed $args, mixed $url ): mixed {
|
||||
if ( false !== $pre || self::$following || ! is_array( $args ) || ! is_string( $url ) ) {
|
||||
return $pre;
|
||||
}
|
||||
$headers = is_array( $args['headers'] ?? null ) ? $args['headers'] : array();
|
||||
$has = false;
|
||||
foreach ( array_keys( $headers ) as $k ) {
|
||||
$has = $has || 'authorization' === strtolower( (string) $k );
|
||||
}
|
||||
$host = self::repo()['host'];
|
||||
if ( ! $has || ! RedirectPolicy::tokenAllowed( $url, $host ) ) {
|
||||
return $pre;
|
||||
}
|
||||
self::$following = true;
|
||||
try {
|
||||
$current = $url;
|
||||
$resp = null;
|
||||
for ( $hop = 0; $hop <= RedirectPolicy::MAX_HOPS; $hop++ ) {
|
||||
$a = $args;
|
||||
$a['redirection'] = 0;
|
||||
$resp = wp_remote_request( $current, $a );
|
||||
$code = is_wp_error( $resp ) ? 0 : (int) wp_remote_retrieve_response_code( $resp );
|
||||
if ( $code < 300 || $code >= 400 || is_wp_error( $resp ) ) {
|
||||
return $resp;
|
||||
}
|
||||
$next = RedirectPolicy::nextUrl( $current, (string) wp_remote_retrieve_header( $resp, 'location' ), $host, $hop );
|
||||
if ( null === $next ) {
|
||||
return $resp; // not followed: the caller sees the 3xx and fails
|
||||
}
|
||||
$current = $next;
|
||||
}
|
||||
return $resp;
|
||||
} finally {
|
||||
self::$following = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function afterUpgrade( mixed $upgrader, mixed $options ): void {
|
||||
if ( is_array( $options ) && ( $options['type'] ?? '' ) === 'plugin' ) {
|
||||
self::clearCache();
|
||||
|
||||
Reference in new issue
Block a user