@@ -230,8 +230,13 @@ public function handle_rest_request( WP_REST_Request $request ) {
230
230
);
231
231
}
232
232
233
- // Limit JSON payload size to safeguard against clients sending possibly malicious payloads much larger than allowed.
234
- $ max_size = od_get_maximum_url_metric_size ();
233
+ /*
234
+ * The limit for data sent via navigator.sendBeacon() is 64 KiB. This limit is checked in detect.js so that the
235
+ * request will not even be attempted if the payload is too large. This server-side restriction is added as a
236
+ * safeguard against clients sending possibly malicious payloads much larger than 64 KiB which should never be
237
+ * getting sent.
238
+ */
239
+ $ max_size = 64 * 1024 ; // 64 KB
235
240
$ content_length = strlen ( (string ) wp_json_encode ( $ url_metric ) );
236
241
if ( $ content_length > $ max_size ) {
237
242
return new WP_Error (
@@ -307,65 +312,4 @@ public function handle_rest_request( WP_REST_Request $request ) {
307
312
)
308
313
);
309
314
}
310
-
311
- /**
312
- * Decompresses the REST API request body for the URL Metrics endpoint.
313
- *
314
- * @since 1.0.0
315
- * @access private
316
- *
317
- * @phpstan-param WP_REST_Request<array<string, mixed>> $request
318
- *
319
- * @param mixed $result Response to replace the requested version with. Can be anything a normal endpoint can return, or null to not hijack the request.
320
- * @param WP_REST_Server $server Server instance.
321
- * @param WP_REST_Request $request Request used to generate the response.
322
- * @return mixed Passed through $result if successful, or otherwise a WP_Error.
323
- */
324
- public function decompress_rest_request_body ( $ result , WP_REST_Server $ server , WP_REST_Request $ request ) {
325
- unset( $ server ); // Unused.
326
-
327
- if (
328
- $ request ->get_route () === '/ ' . self ::ROUTE_NAMESPACE . self ::ROUTE_BASE &&
329
- 'application/gzip ' === $ request ->get_header ( 'Content-Type ' ) &&
330
- function_exists ( 'gzdecode ' )
331
- ) {
332
- $ compressed_body = $ request ->get_body ();
333
-
334
- /*
335
- * The limit for data sent via navigator.sendBeacon() is 64 KiB. This limit is checked in detect.js so that the
336
- * request will not even be attempted if the payload is too large. This server-side restriction is added as a
337
- * safeguard against clients sending possibly malicious payloads much larger than 64 KiB which should never be
338
- * getting sent.
339
- */
340
- $ max_size = 64 * 1024 ; // 64 KB
341
- $ content_length = strlen ( $ compressed_body );
342
- if ( $ content_length > $ max_size ) {
343
- return new WP_Error (
344
- 'rest_content_too_large ' ,
345
- sprintf (
346
- /* translators: 1: the size of the payload, 2: the maximum allowed payload size */
347
- __ ( 'Compressed JSON payload size is %1$s bytes which is larger than the maximum allowed size of %2$s bytes. ' , 'optimization-detective ' ),
348
- number_format_i18n ( $ content_length ),
349
- number_format_i18n ( $ max_size )
350
- ),
351
- array ( 'status ' => 413 )
352
- );
353
- }
354
-
355
- $ decompressed_body = @gzdecode ( $ compressed_body ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- We need to suppress errors here.
356
-
357
- if ( false === $ decompressed_body ) {
358
- return new WP_Error (
359
- 'rest_invalid_payload ' ,
360
- __ ( 'Unable to decompress the gzip payload. ' , 'optimization-detective ' ),
361
- array ( 'status ' => 400 )
362
- );
363
- }
364
-
365
- // Update the request so later handlers see the decompressed JSON.
366
- $ request ->set_body ( $ decompressed_body );
367
- $ request ->set_header ( 'Content-Type ' , 'application/json ' );
368
- }
369
- return $ result ;
370
- }
371
315
}
0 commit comments