Skip to content

Commit 3a0d1e4

Browse files
committed
Collaboration: Harden storage layer and REST schema
- Document intentional omission of hooks/filters in storage class (polling frequency makes hook overhead unacceptable) - Document capability model in check_permissions: access follows existing edit capabilities, no dedicated collaborate cap - Replace current_time('mysql', true) with gmdate() in storage to match cron cleanup and avoid pre_get_current_time filter drift - Include $wpdb->last_error in storage failure WP_Error responses so DB-level failures are diagnosable without SAVEQUERIES - Add maxLength (1 MB) to the update data field in REST schema to prevent unbounded payloads on a high-frequency endpoint - Fix missing period in permission error message
1 parent 80ed666 commit 3a0d1e4

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/wp-includes/collaboration/class-wp-collaboration-table-storage.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
*
1313
* Data is stored in the `collaboration` and `awareness` database tables.
1414
*
15+
* This class intentionally fires no actions or filters. Collaboration
16+
* queries run on every poll (0.5–1 s per editor tab), so hook overhead
17+
* would degrade the real-time editing loop for all active sessions.
18+
*
1519
* @since 7.0.0
1620
*
1721
* @access private
@@ -54,7 +58,7 @@ public function add_update( string $room, $update ): bool {
5458
array(
5559
'room' => $room,
5660
'update_value' => wp_json_encode( $update ),
57-
'created_at' => current_time( 'mysql', true ),
61+
'created_at' => gmdate( 'Y-m-d H:i:s' ),
5862
),
5963
array( '%s', '%s', '%s' )
6064
);
@@ -266,7 +270,7 @@ public function set_awareness_state( string $room, int $client_id, array $state,
266270
$client_id,
267271
$wp_user_id,
268272
$update_value,
269-
current_time( 'mysql', true )
273+
gmdate( 'Y-m-d H:i:s' )
270274
)
271275
);
272276

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ public function register_routes(): void {
9898
$typed_update_args = array(
9999
'properties' => array(
100100
'data' => array(
101-
'type' => 'string',
102-
'required' => true,
101+
'type' => 'string',
102+
'required' => true,
103+
'maxLength' => 1048576, // 1 MB — generous ceiling for base64-encoded Yjs updates.
103104
),
104105
'type' => array(
105106
'type' => 'string',
@@ -185,6 +186,11 @@ public function register_routes(): void {
185186
/**
186187
* Checks if the current user has permission to access a room.
187188
*
189+
* Requires `edit_posts` (contributor+), then delegates to
190+
* can_user_collaborate_on_entity_type() for per-entity checks.
191+
* There is no dedicated `collaborate` capability; access follows
192+
* existing edit capabilities for the entity type.
193+
*
188194
* @since 7.0.0
189195
*
190196
* @param WP_REST_Request $request The REST request.
@@ -195,7 +201,7 @@ public function check_permissions( WP_REST_Request $request ) {
195201
if ( ! current_user_can( 'edit_posts' ) ) {
196202
return new WP_Error(
197203
'rest_cannot_edit',
198-
__( 'You do not have permission to perform this action' ),
204+
__( 'You do not have permission to perform this action.' ),
199205
array( 'status' => rest_authorization_required_code() )
200206
);
201207
}
@@ -446,10 +452,14 @@ private function process_collaboration_update( string $room, int $client_id, int
446452

447453
if ( ! $has_newer_compaction ) {
448454
if ( ! $this->storage->remove_updates_before_cursor( $room, $cursor ) ) {
455+
global $wpdb;
449456
return new WP_Error(
450457
'rest_collaboration_storage_error',
451458
__( 'Failed to remove updates during compaction.' ),
452-
array( 'status' => 500 )
459+
array(
460+
'status' => 500,
461+
'db_error' => $wpdb->last_error,
462+
)
453463
);
454464
}
455465

@@ -501,10 +511,14 @@ private function add_update( string $room, int $client_id, string $type, string
501511
);
502512

503513
if ( ! $this->storage->add_update( $room, $update ) ) {
514+
global $wpdb;
504515
return new WP_Error(
505516
'rest_collaboration_storage_error',
506517
__( 'Failed to store collaboration update.' ),
507-
array( 'status' => 500 )
518+
array(
519+
'status' => 500,
520+
'db_error' => $wpdb->last_error,
521+
)
508522
);
509523
}
510524

0 commit comments

Comments
 (0)