Skip to content

Commit 8371c5c

Browse files
committed
Collaboration: Cap client_id at the storage column width.
Add a maxLength constraint to the client_id argument schema so overlong values are rejected at the REST layer rather than being silently truncated (or erroring) at the database. The 32-character limit matches the client_id varchar(32) column in schema.php and mirrors the approach already used for the room argument. Also document why both minimum and minLength are present: client_id has a union type (string|integer), and WordPress REST API validation dispatches string values to minLength/maxLength and integer values to minimum, so both keywords are required to bound each branch of the union.
1 parent f2730e7 commit 8371c5c

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,15 @@ public function register_routes(): void {
151151
'required' => true,
152152
'type' => array( 'object', 'null' ),
153153
),
154+
/*
155+
* client_id accepts both string and integer values:
156+
* - 'minimum' bounds the integer form.
157+
* - 'minLength' / 'maxLength' bound the string form.
158+
*/
154159
'client_id' => array(
155160
'minimum' => 1,
156161
'minLength' => 1,
162+
'maxLength' => 32, // Matches the client_id column width in wp-admin/includes/schema.php.
157163
'required' => true,
158164
'type' => array( 'string', 'integer' ),
159165
'sanitize_callback' => function ( $value ) {

tests/phpunit/tests/rest-api/rest-collaboration-server.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,40 @@ public function test_collaboration_client_id_integer_coercion(): void {
512512
$this->assertArrayHasKey( '42', $data['rooms'][0]['awareness'], 'Numeric client_id should be coerced to string key in awareness.' );
513513
}
514514

515+
/**
516+
* Validates that REST accepts client IDs at the column width boundary (32 chars).
517+
*
518+
* @ticket 64696
519+
*/
520+
public function test_collaboration_client_id_accepts_string_at_max_length(): void {
521+
wp_set_current_user( self::$editor_id );
522+
523+
$client_id = str_repeat( 'a', 32 );
524+
$this->assertSame( 32, strlen( $client_id ), 'Client ID should be 32 characters.' );
525+
526+
$rooms = array( $this->build_room( $this->get_post_room(), $client_id ) );
527+
$response = $this->dispatch_collaboration( $rooms );
528+
529+
$this->assertSame( 200, $response->get_status(), 'REST should accept client IDs at 32 characters.' );
530+
}
531+
532+
/**
533+
* Validates that REST rejects client IDs exceeding the column width (32 chars).
534+
*
535+
* @ticket 64696
536+
*/
537+
public function test_collaboration_client_id_rejects_string_over_max_length(): void {
538+
wp_set_current_user( self::$editor_id );
539+
540+
$client_id = str_repeat( 'a', 33 );
541+
$this->assertSame( 33, strlen( $client_id ), 'Client ID should be 33 characters.' );
542+
543+
$rooms = array( $this->build_room( $this->get_post_room(), $client_id ) );
544+
$response = $this->dispatch_collaboration( $rooms );
545+
546+
$this->assertSame( 400, $response->get_status(), 'REST should reject client IDs exceeding 32 characters.' );
547+
}
548+
515549
/**
516550
* Verifies that dispatching with an empty rooms array returns HTTP 200.
517551
*

0 commit comments

Comments
 (0)