Skip to content

Commit 9b45174

Browse files
committed
Collaboration: Add payload limit constants and request validation
Introduce MAX_BODY_SIZE (16 MB), MAX_ROOMS_PER_REQUEST (50), and MAX_UPDATE_DATA_SIZE (1 MB) constants to cap request payloads. Wire a validate_callback on the route to reject oversized request bodies with a 413, add maxItems to the rooms schema, and replace the hardcoded maxLength with the new constant.
1 parent ef00730 commit 9b45174

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

src/wp-includes/collaboration/class-wp-http-polling-collaboration-server.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ class WP_HTTP_Polling_Collaboration_Server {
3838
*/
3939
const COMPACTION_THRESHOLD = 50;
4040

41+
/**
42+
* Maximum allowed request body size in bytes.
43+
*
44+
* @since 7.0.0
45+
* @var int
46+
*/
47+
const MAX_BODY_SIZE = 16 * MB_IN_BYTES;
48+
49+
/**
50+
* Maximum number of rooms allowed per request.
51+
*
52+
* @since 7.0.0
53+
* @var int
54+
*/
55+
const MAX_ROOMS_PER_REQUEST = 50;
56+
57+
/**
58+
* Maximum allowed size for a single update's data field in bytes.
59+
*
60+
* @since 7.0.0
61+
* @var int
62+
*/
63+
const MAX_UPDATE_DATA_SIZE = MB_IN_BYTES;
64+
4165
/**
4266
* Collaboration update type: compaction.
4367
*
@@ -100,7 +124,7 @@ public function register_routes(): void {
100124
'data' => array(
101125
'type' => 'string',
102126
'required' => true,
103-
'maxLength' => 1048576, // 1 MB — generous ceiling for base64-encoded Yjs updates.
127+
'maxLength' => self::MAX_UPDATE_DATA_SIZE,
104128
),
105129
'type' => array(
106130
'type' => 'string',
@@ -152,12 +176,14 @@ public function register_routes(): void {
152176
'methods' => array( WP_REST_Server::CREATABLE ),
153177
'callback' => array( $this, 'handle_request' ),
154178
'permission_callback' => array( $this, 'check_permissions' ),
179+
'validate_callback' => array( $this, 'validate_request' ),
155180
'args' => array(
156181
'rooms' => array(
157182
'items' => array(
158183
'properties' => $room_args,
159184
'type' => 'object',
160185
),
186+
'maxItems' => self::MAX_ROOMS_PER_REQUEST,
161187
'required' => true,
162188
'type' => 'array',
163189
),
@@ -236,6 +262,28 @@ public function check_permissions( WP_REST_Request $request ) {
236262
return true;
237263
}
238264

265+
/**
266+
* Validates the incoming REST request.
267+
*
268+
* Checks that the raw request body does not exceed the maximum allowed size.
269+
*
270+
* @since 7.0.0
271+
*
272+
* @param WP_REST_Request $request The REST request.
273+
* @return true|WP_Error True if valid, WP_Error if body is too large.
274+
*/
275+
public function validate_request( WP_REST_Request $request ) {
276+
$body = $request->get_body();
277+
if ( is_string( $body ) && strlen( $body ) > self::MAX_BODY_SIZE ) {
278+
return new WP_Error(
279+
'rest_collaboration_body_too_large',
280+
__( 'Request body is too large.' ),
281+
array( 'status' => 413 )
282+
);
283+
}
284+
return true;
285+
}
286+
239287
/**
240288
* Handles request: stores updates and awareness data, and returns
241289
* updates the client is missing.

0 commit comments

Comments
 (0)