The WordPress REST API revolutionized how developers interact with WordPress, allowing decoupled front-ends (Headless WordPress), mobile applications, and complex integrations. However, this powerful API also opened a massive new attack surface that is frequently targeted by automated scanners.
This ultimate guide provides a deep technical dive into securing the WordPress REST API, preventing data scraping (enumeration), and stopping unauthenticated vulnerability exploitation.
Need immediate help?
If your site is currently hacked or showing warnings, our incident response team can help right now.
1. Understanding the REST API Attack Surface
By default, the WordPress REST API is accessible at yourdomain.com/wp-json/wp/v2/. It is public and unauthenticated by design. This means anyone—or any bot—can query your site to extract data.
The primary security risks associated with the REST API are:
Upgrade to Nexura Pro
Get enterprise-grade protection. Block zero-day exploits, advanced malware, and brute-force attacks instantly.
- User Enumeration: Bots can query
/wp-json/wp/v2/usersto extract the exact usernames of all authors and administrators, which are then used in brute-force attacks. - Data Scraping: Competitors or bots can rapidly download all your published posts, categories, and tags without rendering the frontend HTML.
- Vulnerability Exploitation: Many third-party plugins register their own custom REST API endpoints. If these endpoints lack proper permission checks (
current_user_can()), attackers can exploit them. The infamous WordPress 4.7.0 Privilege Escalation vulnerability occurred precisely because of an improperly secured REST API endpoint.
2. Stopping User Enumeration
The most critical immediate fix is blocking public access to the users endpoint. You do not want attackers knowing the username of your main administrator account.
You can block this by hooking into the rest_endpoints filter in your theme's functions.php or a custom plugin:
Disable Users Endpoint
add_filter( 'rest_endpoints', function( $endpoints ){
if ( isset( $endpoints['/wp/v2/users'] ) ) {
unset( $endpoints['/wp/v2/users'] );
}
if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) {
unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
}
return $endpoints;
});
3. Restricting the REST API to Authenticated Users Only
If you are not running a Headless WordPress setup or a mobile app that requires public API access, you should completely lock down the REST API so that it only responds to logged-in users.
Require Authentication for REST API
add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) {
return $result;
}
if ( ! is_user_logged_in() ) {
return new WP_Error(
'rest_not_logged_in',
'You are not currently logged in.',
array( 'status' => 401 )
);
}
return $result;
});
Note: Be careful implementing this if you use plugins like Contact Form 7 or WooCommerce, as they may rely on unauthenticated REST API calls to submit data. Always test on a staging site first.
4. Securing Custom Endpoints (For Developers)
If you are developing a custom plugin and registering your own endpoints using register_rest_route(), you must include a permission_callback.
Failing to include a permission callback means anyone on the internet can trigger your function. Even if the function just reads data, it is a data leak. If the function modifies data (POST/PUT/DELETE), it is a critical vulnerability.
Secure Endpoint Registration
register_rest_route( 'myplugin/v1', '/settings', array(
'methods' => 'POST',
'callback' => 'myplugin_update_settings',
// CRITICAL: Ensure only admins can execute this
'permission_callback' => function () {
return current_user_can( 'manage_options' );
}
) );
5. Automated REST API Protection via Nexura Security
Configuring custom PHP filters to block specific endpoints can be tedious and prone to breaking site functionality. Nexura Security Pro provides a dedicated REST API Security module.
With a single click, Nexura can:
- Automatically block User Enumeration via the REST API.
- Disable JSON-P support (which is obsolete and vulnerable to CSRF).
- Intelligently monitor custom plugin endpoints for malicious SQLi or XSS payloads via the WAF.
Conclusion
The REST API is a fantastic tool for modern development, but it must be treated as a highly sensitive attack surface. By blocking user enumeration, requiring authentication where necessary, and strictly enforcing permission callbacks on custom routes, you secure the API against automated exploitation and data theft.
