-
Notifications
You must be signed in to change notification settings - Fork 2
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
TECH-132: [alleyvate] Limit access to XML-RPC #50 #114
Merged
Merged
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
bac9b3a
WIP: TECH-132
jakewrfoster 2aaab6b
add http-foundation to composer deps
jakewrfoster fae3c85
add feature to disable xmlrpc for non-jetpack requests
jakewrfoster 335bae1
load feature
jakewrfoster be89faf
phpcs
jakewrfoster 7798cbe
cache bad requests for an hour to avoid making tons of requests to a …
jakewrfoster 71a07fc
use vip mu-plugins ignores
jakewrfoster a95703b
use correct access in scoped anon functions
jakewrfoster 9776231
add slash
jakewrfoster eb870e2
fix native func invocation
jakewrfoster 532fd88
phpstan
jakewrfoster 66e743b
Merge branch 'main' into feature/TECH-132/limit-access-to-xml-rpc
jakewrfoster 06aaf18
add sayHello XMLRPC test
jakewrfoster ebe5038
re-add setup
jakewrfoster cc6699b
TECH-132 Restructure DisableXMLRPCTest to call xmlrpc.php directly
kevinfodness 2e65912
Merge remote-tracking branch 'origin/feature/TECH-132/limit-access-to…
kevinfodness 6809280
TECH-132 Restructure DisableXMLRPCTest to only test the XMLRPC filter…
kevinfodness 3f15bbb
namespace native func
jakewrfoster c52989c
Update tests/Alley/WP/Alleyvate/Features/DisableXMLRPCTest.php
jakewrfoster 4ca33fa
Update tests/Alley/WP/Alleyvate/Features/DisableXMLRPCTest.php
jakewrfoster 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 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
112 changes: 112 additions & 0 deletions
112
src/alley/wp/alleyvate/features/class-disable-xmlrpc.php
This file contains 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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Class file for Disable_XMLRPC. | ||
* | ||
* (c) Alley <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @package wp-alleyvate | ||
*/ | ||
|
||
namespace Alley\WP\Alleyvate\Features; | ||
|
||
use Symfony\Component\HttpFoundation\IpUtils; | ||
use Alley\WP\Types\Feature; | ||
|
||
/** | ||
* Disables XMLRPC requests and methods for all requests except those coming from known Jetpack IPs. | ||
*/ | ||
final class Disable_XMLRPC implements Feature { | ||
/** | ||
* Boot the feature. | ||
*/ | ||
public function boot(): void { | ||
|
||
// Disable XML-RPC for non-Jetpack requests. | ||
add_filter( | ||
'xmlrpc_enabled', | ||
fn( $enabled ) => Disable_XMLRPC::is_jetpack_enabled() && Disable_XMLRPC::is_jetpack_xmlrpc_request() ? $enabled : false, | ||
PHP_INT_MAX, | ||
); | ||
|
||
// Remove all XML-RPC methods for non-Jetpack requests. | ||
add_filter( | ||
'xmlrpc_methods', | ||
fn( $methods ) => Disable_XMLRPC::is_jetpack_enabled() && Disable_XMLRPC::is_jetpack_xmlrpc_request() ? $methods : [], | ||
PHP_INT_MAX, | ||
); | ||
} | ||
|
||
/** | ||
* Determine if Jetpack is enabled. | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_jetpack_enabled(): bool { | ||
return defined( 'JETPACK__VERSION' ); | ||
} | ||
|
||
/** | ||
* Determine if the current request is a Jetpack XML-RPC request. | ||
* | ||
* @return bool | ||
*/ | ||
public static function is_jetpack_xmlrpc_request(): bool { | ||
// Bail if there's no remote address. | ||
if ( empty( $_SERVER['REMOTE_ADDR'] ) ) { | ||
return false; | ||
} | ||
|
||
// Get Jetpack IPs. | ||
$jetpack_ips = self::get_jetpack_ips(); | ||
|
||
// Bail if we don't have any Jetpack IPs. | ||
if ( empty( $jetpack_ips ) ) { | ||
return false; | ||
} | ||
|
||
// Check if the request is from a Jetpack IP. | ||
if ( IpUtils::checkIp( $_SERVER['REMOTE_ADDR'], $jetpack_ips ) ) { | ||
return true; | ||
} | ||
|
||
// Check if the request is from a forwarded Jetpack IP. | ||
return isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) && IpUtils::checkIp( $_SERVER['HTTP_X_FORWARDED_FOR'], $jetpack_ips ); | ||
} | ||
|
||
/** | ||
* Get the Jetpack IPs. | ||
* | ||
* @return array | ||
*/ | ||
public static function get_jetpack_ips(): array { | ||
// Look for cache. | ||
$jetpack_ips = wp_cache_get( 'jetpack_ips', 'alleyvate_disable_xmlrpc' ); | ||
|
||
// If there's no cache, fetch the Jetpack IPs. | ||
if ( empty( $jetpack_ips ) ) { | ||
$response = wp_safe_remote_get( 'https://jetpack.com/ips-v4.json' ); | ||
if ( ! is_wp_error( $response ) ) { | ||
srtfisher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Ensure good response. | ||
if ( 200 === (int) wp_remote_retrieve_response_code( $response ) ) { | ||
$body = wp_remote_retrieve_body( $response ); | ||
$jetpack_ips = json_decode( $body, true ); | ||
|
||
// Update cache. | ||
wp_cache_set( | ||
'jetpack_ips', | ||
$jetpack_ips, | ||
'alleyvate_disable_xmlrpc', | ||
is_array( $jetpack_ips ) ? WEEK_IN_SECONDS : HOUR_IN_SECONDS | ||
); | ||
|
||
return ( is_array( $jetpack_ips ) && ! empty( $jetpack_ips ) ) ? $jetpack_ips : []; | ||
} | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains 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
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.
latest version compatible with mantle testkit.