Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions includes/handler/class-accept.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,25 @@ public static function handle_accept( $accept, $user_id ) {
* @return bool The validation state: true if valid, false if not.
*/
public static function validate_object( $valid, $param, $request ) {
$json_params = $request->get_json_params();
$activity = $request->get_json_params();

if ( empty( $json_params['type'] ) ) {
if ( empty( $activity['type'] ) ) {
return false;
}

if (
'Accept' !== $json_params['type'] ||
\is_wp_error( $request )
) {
if ( 'Accept' !== $activity['type'] ) {
return $valid;
}

$required_attributes = array(
'actor',
'object',
);

if ( ! empty( \array_diff( $required_attributes, \array_keys( $json_params ) ) ) ) {
if ( ! isset( $activity['actor'], $activity['object'] ) ) {
return false;
}

$required_object_attributes = array(
'id',
'type',
'actor',
'object',
);
if ( ! \is_array( $activity['object'] ) ) {
return false;
}

if ( ! empty( \array_diff( $required_object_attributes, \array_keys( $json_params['object'] ) ) ) ) {
if ( ! isset( $activity['object']['id'], $activity['object']['type'], $activity['object']['actor'], $activity['object']['object'] ) ) {
return false;
}

Expand Down
20 changes: 5 additions & 15 deletions includes/handler/class-create.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,21 @@ public static function handle_create( $activity, $user_id, $activity_object = nu
* @return bool The validation state: true if valid, false if not.
*/
public static function validate_object( $valid, $param, $request ) {
$json_params = $request->get_json_params();
$activity = $request->get_json_params();

if ( empty( $json_params['type'] ) ) {
if ( empty( $activity['type'] ) ) {
return false;
}

if (
'Create' !== $json_params['type'] ||
is_wp_error( $request )
) {
if ( 'Create' !== $activity['type'] ) {
return $valid;
}

$object = $json_params['object'];

if ( ! is_array( $object ) ) {
if ( ! isset( $activity['object'] ) || ! \is_array( $activity['object'] ) ) {
return false;
}

$required = array(
'id',
'content',
);

if ( array_intersect( $required, array_keys( $object ) ) !== $required ) {
if ( ! isset( $activity['object']['id'], $activity['object']['content'] ) ) {
return false;
}

Expand Down
18 changes: 4 additions & 14 deletions includes/handler/class-quote-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,17 @@ public static function queue_reject( $activity_object, $user_id ) {
* @return bool The validation state: true if valid, false if not.
*/
public static function validate_object( $valid, $param, $request ) {
if ( \is_wp_error( $request ) ) {
return $valid;
}

$json_params = $request->get_json_params();
$activity = $request->get_json_params();

if ( empty( $json_params['type'] ) ) {
if ( empty( $activity['type'] ) ) {
return false;
}

if ( 'QuoteRequest' !== $json_params['type'] ) {
if ( 'QuoteRequest' !== $activity['type'] ) {
return $valid;
}

$required_attributes = array(
'actor',
'object',
'instrument',
);

if ( ! empty( \array_diff( $required_attributes, \array_keys( $json_params ) ) ) ) {
if ( ! isset( $activity['actor'], $activity['object'], $activity['instrument'] ) ) {
return false;
}

Expand Down
11 changes: 4 additions & 7 deletions includes/handler/class-reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,17 @@ private static function reject_follow( $reject, $user_id ) {
* @return bool The validation state: true if valid, false if not.
*/
public static function validate_object( $valid, $param, $request ) {
$json_params = $request->get_json_params();
$activity = $request->get_json_params();

if ( empty( $json_params['type'] ) ) {
if ( empty( $activity['type'] ) ) {
return false;
}

if (
'Reject' !== $json_params['type'] ||
\is_wp_error( $request )
) {
if ( 'Reject' !== $activity['type'] ) {
return $valid;
}

if ( empty( $json_params['actor'] ) || empty( $json_params['object'] ) ) {
if ( ! isset( $activity['actor'], $activity['object'] ) ) {
return false;
}

Expand Down
27 changes: 8 additions & 19 deletions includes/handler/class-undo.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,25 @@ public static function handle_undo( $activity, $user_id ) {
* @return bool The validation state: true if valid, false if not.
*/
public static function validate_object( $valid, $param, $request ) {
$json_params = $request->get_json_params();
$activity = $request->get_json_params();

if ( empty( $json_params['type'] ) ) {
if ( empty( $activity['type'] ) ) {
return false;
}

if (
'Undo' !== $json_params['type'] ||
\is_wp_error( $request )
) {
if ( 'Undo' !== $activity['type'] ) {
return $valid;
}

$required_attributes = array(
'actor',
'object',
);

if ( ! empty( \array_diff( $required_attributes, \array_keys( $json_params ) ) ) ) {
if ( ! isset( $activity['actor'], $activity['object'] ) ) {
return false;
}

$required_object_attributes = array(
'id',
'type',
'actor',
'object',
);
if ( ! \is_array( $activity['object'] ) ) {
Copy link
Member

@pfefferle pfefferle Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we first merge #2284 and then have your changes afterwards? My PR adds support for URI-Objects at least for Likes and Announces.

return false;
}

if ( ! empty( \array_diff( $required_object_attributes, \array_keys( $json_params['object'] ) ) ) ) {
if ( ! isset( $activity['object']['id'], $activity['object']['type'], $activity['object']['actor'], $activity['object']['object'] ) ) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions includes/rest/class-actors-inbox-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ public function register_routes() {
/**
* Filter the ActivityPub object validation.
*
* @param bool $validate The validation result.
* @param array $param The object data.
* @param object $request The request object.
* @param string $key The key.
* @param bool $validate The validation result.
* @param array $param The object data.
* @param \WP_REST_Request $request The request object.
* @param string $key The key.
*/
return \apply_filters( 'activitypub_validate_object', true, $param, $request, $key );
},
Expand Down
8 changes: 4 additions & 4 deletions includes/rest/class-inbox-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public function register_routes() {
/**
* Filter the ActivityPub object validation.
*
* @param bool $validate The validation result.
* @param array $param The object data.
* @param object $request The request object.
* @param string $key The key.
* @param bool $validate The validation result.
* @param array $param The object data.
* @param \WP_REST_Request $request The request object.
* @param string $key The key.
*/
return \apply_filters( 'activitypub_validate_object', true, $param, $request, $key );
},
Expand Down
13 changes: 0 additions & 13 deletions phpunit/tests/includes/handler/class-test-quote-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,19 +446,6 @@ public function test_validate_object_no_type() {
$this->assertFalse( $result, 'Request without type should fail validation' );
}

/**
* Test validate_object with WP_Error request.
*
* @covers ::validate_object
*/
public function test_validate_object_with_wp_error() {
$request = new \WP_Error( 'invalid_request', 'Invalid request' );

$result = Quote_Request::validate_object( true, 'object', $request );

$this->assertTrue( $result, 'Should pass through original validation result when request is WP_Error' );
}

/**
* Test that init method properly registers hooks.
*
Expand Down