- Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPlugin.php
More file actions
322 lines (282 loc) · 6.57 KB
/
Plugin.php
File metadata and controls
322 lines (282 loc) · 6.57 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* WP REST Cop plugin.
*
* @package Cedaro\WPRESTCop
* @copyright Copyright (c) 2015, Cedaro, LLC
* @license GPL-2.0+
* @since 1.0.0
*/
namespace Cedaro\WPRESTCop;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
/**
* Main plugin class.
*
* @package Cedaro\WPRESTCop
* @since 1.0.0
*/
class Plugin extends AbstractPlugin {
/**
* Number of requests allowed per interval.
*
* @since 1.0.0
* @var integer
*/
protected $limit;
/**
* Seconds per interval.
*
* @since 1.0.0
* @var integer
*/
protected $interval;
/**
* IP address rules.
*
* @since 1.0.0
* @var \Cedaro\WPRESTCop\IPRulesInterface
*/
protected $ip_rules;
/**
* Constructor method.
*
* @param \Cedaro\WPRESTCop\IPRulesInterface $ip_rules IP address rules.
*/
public function __construct( IPRulesInterface $ip_rules ) {
$this->set_ip_rules( $ip_rules );
}
/**
* Load the plugin.
*
* @since 1.0.0
*
* @return $this
*/
public function load() {
$settings = get_option( 'wprestcop_settings' );
if ( false === $settings ) {
$settings = [ 'limit' => 500, 'interval' => 3600 ];
update_option( 'wprestcop_settings', $settings );
}
$this
->set_limit( $settings['limit'] )
->set_interval( $settings['interval'] );
add_action( 'rest_api_init', [ $this, 'initialize_ip_rules' ] );
add_filter( 'rest_authentication_errors', [ $this, 'check_ip_rules' ] );
add_filter( 'rest_pre_dispatch', [ $this, 'throttle_request' ], 10, 3 );
add_filter( 'rest_dispatch_request', [ $this, 'check_route_ip_rules' ], 10, 2 );
do_action( 'wprestcop_plugin_loaded', $this );
return $this;
}
/**
* Initialize IP rules from options.
*
* These will usually be set with WP CLI.
*
* @since 1.0.0
*
* @return $this
*/
public function initialize_ip_rules() {
$allow = get_option( 'wprestcop_allowed_ips' );
if ( false === $allow ) {
update_option( 'wprestcop_allowed_ips', [] );
}
$deny = get_option( 'wprestcop_denied_ips' );
if ( false === $deny ) {
update_option( 'wprestcop_denied_ips', [] );
}
$this->get_ip_rules()->allow( $allow );
$this->get_ip_rules()->deny( $deny );
return $this;
}
/**
* Retrieve an identifier for the current client.
*
* If a user is logged in, their user ID will be used, otherwise, defaults
* to the current client's IP address.
*
* @since 1.0.0
*
* @return string|int
*/
public function get_client_id() {
$key = $this->get_ip_address();
if ( is_user_logged_in() ) {
$key = get_current_user_id();
}
return apply_filters( 'wprestcop_current_user_key', $key );
}
/**
* Retrieve the current client's IP address.
*
* @since 1.0.0
*
* @return string
*/
public function get_ip_address() {
return $_SERVER['REMOTE_ADDR'];
}
/**
* Retreive IP rules.
*
* @since 1.0.0
*
* @return \Cedaro\WPRESTCop\IPRulesInterface
*/
public function get_ip_rules() {
return $this->ip_rules;
}
/**
* Set the IP rules.
*
* @since 1.0.0
*
* @param \Cedaro\WPRESTCop\IPRulesInterface $ip_rules Instance of IP rules.
* @return $this
*/
public function set_ip_rules( $ip_rules ) {
$this->ip_rules = $ip_rules;
return $this;
}
/**
* Retrieve the global rate limit.
*
* @since 1.0.0
*
* @return int
*/
public function get_rate_limit() {
return $this->limit;
}
/**
* Set the number of requests allowed per interval.
*
* @since 1.0.0
*
* @param int $limit Number of requests.
* @return $this
*/
public function set_limit( $limit ) {
$this->limit = intval( $limit );
return $this;
}
/**
* Retrieve the global rate limit interval.
*
* @since 1.0.0
*
* @return int
*/
public function get_interval() {
return $this->interval;
}
/**
* Set the number of seconds per interval.
*
* @since 1.0.0
*
* @param int $interval Seconds per interval.
* @return $this
*/
public function set_interval( $interval ) {
$this->interval = intval( $interval );
return $this;
}
/**
* Global request handler.
*
* @since 1.0.0
*
* @param mixed $response Response.
* @param \WP_REST_Server $server Server instance.
* @param \WP_REST_Request $request Request used to generate the response.
* @return \WP_REST_Response|WP_Error
*/
public function throttle_request( $response, WP_REST_Server $server, WP_REST_Request $request ) {
// Bail if the interval is -1.
if ( -1 === $this->get_interval() ) {
return $response;
}
$meter = MeterMaid::make(
$this->get_client_id(),
$this->get_rate_limit(),
$this->get_interval()
);
// Don't throttle HEAD requests to let clients check details.
if ( 'HEAD' !== $request->get_method() ) {
$meter->tick();
}
$server->send_headers( $meter->get_headers() );
if ( $meter->is_limit_exceeded() ) {
$data = [
'code' => 'rate_limit_exceeded',
'message' => esc_html__( 'Too many requests.', 'wprestcop' ),
'data' => array_merge(
$meter->to_array(),
[ 'status' => 429 ]
),
];
$response = new WP_REST_Response( $data, 429 );
}
MeterMaid::save( $meter );
return $response;
}
/**
* Check global IP address settings.
*
* @since 1.0.0
*
* @param WP_Error|null|boolean $error WP_Error if authentication error, null if
* authentication method wasn't used, true if
* authentication succeeded.
* @return null|WP_Error
*/
public function check_ip_rules( $error ) {
if ( ! $this->get_ip_rules()->check( $this->get_ip_address() ) ) {
$error = $this->get_forbidden_error();
}
return $error;
}
/**
* Check route-level settings for IP addresses.
*
* @since 1.0.0
*
* @param mixed $response Existing response.
* @param \WP_REST_Request $request Request used to generate the response.
* @return mixed
*/
public function check_route_ip_rules( $response, WP_REST_Request $request ) {
$settings = [];
if ( ! empty( $request->get_attributes()['ips'] ) ) {
$settings = $request->get_attributes()['ips'];
}
if ( $settings instanceof IPRulesInterface ) {
$rules = $settings;
} else {
$rules = new IPRules( $settings );
}
if ( ! $rules->check( $this->get_ip_address() ) ) {
$response = $this->get_forbidden_error();
}
return $response;
}
/**
* Retrieve the default forbidden error.
*
* @since 1.0.0
*
* @return WP_Error
*/
protected function get_forbidden_error() {
return new WP_Error(
'rest_forbidden',
esc_html( "You don't have permission to do this.", 'wprestcop' ),
[ 'status' => 403 ]
);
}
}