-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdefault-wp-cerber.php
More file actions
55 lines (51 loc) · 2.08 KB
/
Copy pathdefault-wp-cerber.php
File metadata and controls
55 lines (51 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* Plugin Name: Be API - Default WP Cerber
* Plugin URI: https://github.com/BeAPI/bea-plugin-defaults
* Description: Compatibility layer for WP Cerber and the WordPress REST API. Adjusts REST URL prefix detection so Cerber correctly identifies REST requests when the site uses a custom prefix or subfolder structure.
* Version: 1.0.1
* Requires at least: 5.9
* Requires PHP: 7.4
* Author: BeAPI Technical team
* Author URI: https://beapi.fr
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: bea-plugin-defaults
*/
namespace BEAPI\Plugin_Defaults\Wp_Cerber;
if ( ! defined( 'ABSPATH' ) ) {
die( 'Cannot access pages directly.' );
}
add_filter( 'application_password_is_api_request', __NAMESPACE__ . '\\application_password_is_api_request' );
/**
* Determines if the current request is an API request and fixes WP Cerber REST API blocking too early.
*
* This function checks if the request URI indicates a REST API route,
* validates the request method against a set of allowed methods, and
* ensures that authentication credentials are provided.
*
* @param bool $is_api_request The initial determination of whether the request is an API request.
*
* @return bool True if the request is a valid API request; otherwise, the original $is_api_request value.
*/
function application_password_is_api_request( $is_api_request ) {
$request_uri = $_SERVER['REQUEST_URI'] ?? '';
if ( empty( $request_uri ) ) {
return $is_api_request;
}
// Check if it's an API route
if ( ! str_contains( $request_uri, '/wp-json/' ) ) {
return $is_api_request;
}
$request_method = $_SERVER['REQUEST_METHOD'] ?? '';
$request_method_allowed = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH' ];
// Check if method REQUEST is allowed
if ( ! in_array( $request_method, $request_method_allowed, true ) ) {
return $is_api_request;
}
// Check if authentication is sent
if ( ! isset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ) ) {
return $is_api_request;
}
return true;
}