generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.php
386 lines (334 loc) · 11.4 KB
/
plugin.php
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
* Plugin Name: REST API Guard
* Plugin URI: https://github.com/alleyinteractive/wp-rest-api-guard
* Description: Restrict and control access to the REST API
* Version: 1.3.2
* Author: Sean Fisher
* Author URI: https://alley.com/
* Requires at least: 6.0
* Tested up to: 6.3
*
* Text Domain: plugin_domain
* Domain Path: /languages/
*
* @package rest-api-guard
*/
namespace Alley\WP\REST_API_Guard;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use InvalidArgumentException;
use WP_Error;
use WP_REST_Request;
use WP_REST_Server;
use WP_User;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Instantiate the plugin.
*/
function main() {
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
require_once __DIR__ . '/settings.php';
add_filter( 'rest_pre_dispatch', __NAMESPACE__ . '\on_rest_pre_dispatch', 10, 3 );
}
main();
/**
* Check if anonymous access should be prevented for the current request.
*
* @param WP_REST_Server $server Server instance.
* @param WP_REST_Request $request The request object.
* @return WP_Error|bool
*
* @throws InvalidArgumentException If the JWT is invalid.
*/
function should_prevent_anonymous_access( WP_REST_Server $server, WP_REST_Request $request ): WP_Error|bool {
$settings = (array) get_option( SETTINGS_KEY );
if ( ! is_array( $settings ) ) {
$settings = [];
}
/**
* Filters whether the REST API Guard should check OPTIONS requests.
*
* This is useful for CORS preflight requests.
*
* @param bool $check Whether to check OPTIONS requests. Default false.
* @param \WP_REST_Request $request REST API Request.
*/
if ( 'OPTIONS' === $request->get_method() && ! apply_filters( 'rest_api_guard_check_options_requests', $settings['check_options_requests'] ?? false, $request ) ) {
return false;
}
if ( class_exists( JWT::class ) && ! is_user_logged_in() ) {
/**
* Check if the anonymous request requires a JSON Web Token (JWT).
*
* @param bool $require Whether to require a JWT, default false.
* @param \WP_REST_Request $request REST API Request.
*/
$require_anonymous_jwt = true === apply_filters( 'rest_api_guard_authentication_jwt', $settings['authentication_jwt'] ?? false, $request );
$allow_user_jwt = true === apply_filters( 'rest_api_guard_user_authentication_jwt', $settings['user_authentication_jwt'] ?? false, $request );
if ( $require_anonymous_jwt || $allow_user_jwt ) {
try {
$jwt = $request->get_header( 'Authorization' );
if ( empty( $jwt ) && $require_anonymous_jwt ) {
throw new InvalidArgumentException( __( 'No authorization header token was found and is required for this request.', 'rest-api-guard' ) );
}
if ( ! empty( $jwt ) ) {
if ( 0 !== strpos( $jwt, 'Bearer ' ) ) {
throw new InvalidArgumentException( __( 'Invalid authorization header.', 'rest-api-guard' ) );
}
$decoded = JWT::decode(
substr( $jwt, 7 ),
new Key( get_jwt_secret(), 'HS256' ),
);
// Verify the contents of the JWT.
if ( empty( $decoded->iss ) || get_jwt_issuer() !== $decoded->iss ) {
throw new InvalidArgumentException( __( 'Invalid JWT issuer.', 'rest-api-guard' ) );
}
if ( empty( $decoded->aud ) || get_jwt_audience() !== $decoded->aud ) {
throw new InvalidArgumentException( __( 'Invalid JWT audience.', 'rest-api-guard' ) );
}
if ( $allow_user_jwt && ! empty( $decoded->sub ) ) {
$user = get_user_by( 'id', $decoded->sub );
if ( ! $user instanceof WP_User ) {
throw new InvalidArgumentException( __( 'Invalid user in JWT sub.', 'rest-api-guard' ) );
}
wp_set_current_user( $user->ID );
return false;
}
}
} catch ( \Exception $error ) {
return new WP_Error(
'rest_api_guard_unauthorized',
/**
* Filter the authorization error message.
*
* @param string $message The error message being returned.
* @param \Throwable $error The error that occurred.
*/
apply_filters(
'rest_api_guard_invalid_jwt_message',
sprintf(
/* translators: %s: The error message. */
__( 'Error authentication with token: %s', 'rest-api-guard' ),
$error->getMessage(),
),
$error,
),
[
'status' => rest_authorization_required_code(),
]
);
}
}
}
/**
* Check if anonymous access is prevent by default.
*
* @param bool $prevent Whether to prevent anonymous access, default false.
* @param \WP_REST_Request $request REST API Request.
*/
if ( true === apply_filters( 'rest_api_guard_prevent_anonymous_access', $settings['prevent_anonymous_access'] ?? false, $request ) ) {
return true;
}
$endpoint = $request->get_route();
/**
* Prevent access to the root of the REST API.
*
* @param bool $prevent Whether to allow anonymous access to the REST API index. Default false.
* @param string $endpoint The endpoint of the request.
*/
if ( '/' === $endpoint && false === apply_filters( 'rest_api_guard_allow_index_access', $settings['allow_index_access'] ?? false, $endpoint ) ) {
return true;
}
if (
in_array( substr( $endpoint, 1 ), $server->get_namespaces(), true )
/**
* Prevent access to the namespace index of the REST API.
*
* @param bool $prevent Whether to prevent anonymous access, default false.
* @param string $namespace The namespace of the request.
*/
&& false === apply_filters( 'rest_api_guard_allow_namespace_access', $settings['allow_namespace_access'] ?? false, substr( $endpoint, 1 ) )
) {
return true;
}
/**
* Prevent access to the /wp/v2/users endpoints by default.
*
* @param bool $pre Whether to allow access to the /wp/v2/users endpoints.
* @param string $endpoint The endpoint of the request.
*/
if ( preg_match( '#^/wp/v\d+/users($|/)#', $endpoint ) && false === apply_filters( 'rest_api_guard_allow_user_access', $settings['allow_user_access'] ?? false, $endpoint ) ) {
return true;
}
/**
* Filter the allowlist for allowed anonymous requests.
*
* @param string[] $allowlist Allowlist of requests.
* @param \WP_REST_Request $request REST API Request.
*/
$allowlist = apply_filters( 'rest_api_guard_anonymous_requests_allowlist', $settings['anonymous_requests_allowlist'] ?? [] );
if ( ! empty( $allowlist ) ) {
if ( ! is_array( $allowlist ) ) {
$allowlist = preg_split( '/\r\n|\r|\n/', $allowlist );
}
foreach ( $allowlist as $allowlist_endpoint ) {
// Strip off /wp-json from the beginning of the endpoint if it was included.
if ( 0 === strpos( $allowlist_endpoint, '/wp-json' ) ) {
$allowlist_endpoint = substr( $allowlist_endpoint, 8 );
}
if ( preg_match( '/' . str_replace( '\*', '.*', preg_quote( $allowlist_endpoint, '/' ) ) . '/', $endpoint ) ) {
return false;
}
}
// If no route on the allowlist was matched, prevent anonymous access.
return true;
}
/**
* Filter the denylist for allowed anonymous requests.
*
* @param string[] $denylist Denylist of requests.
* @param \WP_REST_Request $request REST API Request.
*/
$denylist = apply_filters( 'rest_api_guard_anonymous_requests_denylist', $settings['anonymous_requests_denylist'] ?? [] );
if ( ! empty( $denylist ) ) {
if ( ! is_array( $denylist ) ) {
$denylist = preg_split( '/\r\n|\r|\n/', $denylist );
}
foreach ( $denylist as $denylist_endpoint ) {
// Strip off /wp-json from the beginning of the endpoint if it was included.
if ( 0 === strpos( $denylist_endpoint, '/wp-json' ) ) {
$denylist_endpoint = substr( $denylist_endpoint, 8 );
}
if ( preg_match( '/' . str_replace( '\*', '.*', preg_quote( $denylist_endpoint, '/' ) ) . '/', $endpoint ) ) {
return true;
}
}
}
return false;
}
/**
* Short-circuit the REST API request if the user is not allowed to access it.
*
* @param mixed $pre Dispatched value. Will be used if not empty.
* @param WP_REST_Server $server Server instance.
* @param WP_REST_Request $request REST API Request.
* @return mixed
*/
function on_rest_pre_dispatch( $pre, $server, $request ) {
if ( ! empty( $pre ) || is_user_logged_in() ) {
return $pre;
}
$should_prevent = should_prevent_anonymous_access( $server, $request );
if ( is_wp_error( $should_prevent ) ) {
return $should_prevent;
} elseif ( $should_prevent ) {
return new WP_Error(
'rest_api_guard_unauthorized',
/**
* Filter the authorization error message.
*
* @param string $message The error message.
*/
apply_filters(
'rest_api_guard_unauthorized_message',
__( 'Sorry, you are not allowed to access this page.', 'rest-api-guard' ),
),
[
'status' => rest_authorization_required_code(),
]
);
}
return $pre;
}
/**
* Get the JSON Web Token (JWT) issuer.
*
* @return string
*/
function get_jwt_issuer(): string {
/**
* Filter the issuer of the JWT.
*
* @param string $issuer The issuer of the JWT.
*/
return apply_filters( 'rest_api_guard_jwt_issuer', get_bloginfo( 'url' ) );
}
/**
* Get the JSON Web Token (JWT) audience.
*
* @return string
*/
function get_jwt_audience(): string {
/**
* Filter the audience of the JWT.
*
* @param string $audience The audience of the JWT.
*/
return apply_filters( 'rest_api_guard_jwt_audience', 'wordpress-rest-api' );
}
/**
* Get the JSON Web Token (JWT) secret.
*
* @return string
*/
function get_jwt_secret(): string {
// Generate the JWT secret if it does not exist.
if ( empty( get_option( 'rest_api_guard_jwt_secret' ) ) ) {
update_option( 'rest_api_guard_jwt_secret', wp_generate_password( 32, false ) );
}
/**
* Filter the secret of the JWT. By default, the WordPress secret key is used.
*
* @param string $secret The secret of the JWT.
*/
return apply_filters( 'rest_api_guard_jwt_secret', get_option( 'rest_api_guard_jwt_secret' ) );
}
/**
* Generate a JSON Web Token (JWT).
*
* @param int|null $expiration The expiration time of the JWT in seconds or null for no expiration.
* @param WP_User|int|null $user The user to include in the JWT or null for no user.
* @return string
*
* @throws InvalidArgumentException If the user is invalid or unknown.
*/
function generate_jwt( ?int $expiration = null, WP_User|int|null $user = null ): string {
$payload = [
'iss' => get_jwt_issuer(),
'aud' => get_jwt_audience(),
'iat' => time(),
];
if ( null !== $expiration ) {
$payload['exp'] = time() + $expiration;
}
if ( null !== $user ) {
$user = $user instanceof WP_User ? $user : get_user_by( 'id', $user );
if ( ! $user instanceof WP_User ) {
throw new InvalidArgumentException( esc_html__( 'Invalid user.', 'rest-api-guard' ) );
}
$payload['sub'] = $user->ID;
$payload['user_login'] = $user->user_login;
/**
* Filter the additional claims to include in the JWT.
*
* The filer cannot modify any existing claims, only add new ones.
*
* @param array<string, mixed> $additional_claims The additional claims to include in the JWT.
* @param WP_User|null $user The user to include in the JWT.
* @param array<string, mixed> $payload The payload of the JWT.
*/
$additional_claims = apply_filters( 'rest_api_guard_jwt_additional_claims', [], $user, $payload );
if ( is_array( $additional_claims ) ) {
$payload = array_merge( $additional_claims, $payload );
}
}
return JWT::encode( $payload, get_jwt_secret(), 'HS256' );
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once __DIR__ . '/cli.php';
}