Skip to content

Commit dbd2747

Browse files
authored
Add wp_check_comment_disallowed_list also to user inbox (#1597)
* Add `wp_check_comment_disallowed_list` also to user inbox This is a follow up PR to #1590 that adds the block ability also to the Actor-Inbox. * ignore phpcs issues
1 parent 1c28c42 commit dbd2747

File tree

3 files changed

+73
-19
lines changed

3 files changed

+73
-19
lines changed

includes/rest/class-actors-inbox-controller.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Activitypub\Activity\Activity;
1111
use Activitypub\Collection\Actors;
12+
use Activitypub\Debug;
1213

1314
use function Activitypub\get_context;
1415
use function Activitypub\get_rest_url_by_path;
@@ -175,24 +176,29 @@ public function create_item( $request ) {
175176
$type = $request->get_param( 'type' );
176177
$type = \strtolower( $type );
177178

178-
/**
179-
* ActivityPub inbox action.
180-
*
181-
* @param array $data The data array.
182-
* @param int|null $user_id The user ID.
183-
* @param string $type The type of the activity.
184-
* @param Activity|\WP_Error $activity The Activity object.
185-
*/
186-
\do_action( 'activitypub_inbox', $data, $user->get__id(), $type, $activity );
187-
188-
/**
189-
* ActivityPub inbox action for specific activity types.
190-
*
191-
* @param array $data The data array.
192-
* @param int|null $user_id The user ID.
193-
* @param Activity|\WP_Error $activity The Activity object.
194-
*/
195-
\do_action( 'activitypub_inbox_' . $type, $data, $user->get__id(), $activity );
179+
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
180+
if ( \wp_check_comment_disallowed_list( $activity->to_json( false ), '', '', '', $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'] ?? '' ) ) {
181+
Debug::write_log( 'Blocked activity from: ' . $activity->get_actor() );
182+
} else {
183+
/**
184+
* ActivityPub inbox action.
185+
*
186+
* @param array $data The data array.
187+
* @param int|null $user_id The user ID.
188+
* @param string $type The type of the activity.
189+
* @param Activity|\WP_Error $activity The Activity object.
190+
*/
191+
\do_action( 'activitypub_inbox', $data, $user->get__id(), $type, $activity );
192+
193+
/**
194+
* ActivityPub inbox action for specific activity types.
195+
*
196+
* @param array $data The data array.
197+
* @param int|null $user_id The user ID.
198+
* @param Activity|\WP_Error $activity The Activity object.
199+
*/
200+
\do_action( 'activitypub_inbox_' . $type, $data, $user->get__id(), $activity );
201+
}
196202

197203
$response = \rest_ensure_response( array() );
198204
$response->set_status( 202 );

includes/rest/class-inbox-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function create_item( $request ) {
131131
$type = \strtolower( $request->get_param( 'type' ) );
132132

133133
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
134-
if ( wp_check_comment_disallowed_list( $activity->to_json( false ), '', '', '', $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'] ?? '' ) ) {
134+
if ( \wp_check_comment_disallowed_list( $activity->to_json( false ), '', '', '', $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'] ?? '' ) ) {
135135
Debug::write_log( 'Blocked activity from: ' . $activity->get_actor() );
136136
} else {
137137
/**

tests/includes/rest/class-test-actors-inbox-controller.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,54 @@ function ( $inbox ) {
367367
$this->assertNotWPError( $valid, 'Response failed schema validation: ' . ( \is_wp_error( $valid ) ? $valid->get_error_message() : '' ) );
368368
}
369369

370+
/**
371+
* Test disallow list block.
372+
*
373+
* @covers ::create_item
374+
*/
375+
public function test_disallow_list_block() {
376+
\add_filter( 'activitypub_defer_signature_verification', '__return_true' );
377+
378+
// Add a keyword that will be in our test content.
379+
\update_option( 'disallowed_keys', 'https://remote.example/@test' );
380+
381+
// Set up mock action.
382+
$inbox_action = new \MockAction();
383+
\add_action( 'activitypub_inbox', array( $inbox_action, 'action' ) );
384+
385+
// Create a valid request with content that contains the disallowed keyword.
386+
$json = array(
387+
'id' => 'https://remote.example/@id',
388+
'type' => 'Create',
389+
'actor' => 'https://remote.example/@test',
390+
'object' => array(
391+
'id' => 'https://remote.example/post/test',
392+
'type' => 'Note',
393+
'content' => 'Hello, World!',
394+
'inReplyTo' => 'https://local.example/post/test',
395+
'published' => '2020-01-01T00:00:00Z',
396+
),
397+
);
398+
399+
$request = new \WP_REST_Request( 'POST', '/' . ACTIVITYPUB_REST_NAMESPACE . '/actors/' . self::$user_id . '/inbox' );
400+
$request->set_header( 'Content-Type', 'application/activity+json' );
401+
$request->set_body( \wp_json_encode( $json ) );
402+
403+
// Dispatch the request.
404+
$response = \rest_do_request( $request );
405+
406+
// Verify the response is still successful (202).
407+
$this->assertEquals( 202, $response->get_status() );
408+
409+
// Verify that the hooks were not called.
410+
$this->assertEquals( 0, $inbox_action->get_call_count(), 'activitypub_inbox hook should not be called when content is disallowed' );
411+
412+
// Clean up.
413+
\delete_option( 'disallowed_keys' );
414+
\remove_filter( 'activitypub_defer_signature_verification', '__return_true' );
415+
\remove_action( 'activitypub_inbox', array( $inbox_action, 'action' ) );
416+
}
417+
370418
/**
371419
* Test get_item method.
372420
*

0 commit comments

Comments
 (0)