Skip to content

Commit e0c3b67

Browse files
mexonMatthew Exonpfefferle
authored
Check response body for tombstone type (#1209) (#1222)
* Check response body for tombstone type (#1209) * some small improvements * fix PHPCS --------- Co-authored-by: Matthew Exon <[email protected]> Co-authored-by: Matthias Pfefferle <[email protected]>
1 parent 5f73af8 commit e0c3b67

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
* Improved content negotiation and AUTHORIZED_FETCH support for third-party plugins
1313

14+
### Fixed
15+
16+
* Handle deletes from remote servers that leave behind an accessible Tombstone object.
17+
1418
## [4.7.3] - 2025-01-21
1519

1620
### Fixed

includes/class-http.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,19 @@ public static function is_tombstone( $url ) {
192192
*/
193193
\do_action( 'activitypub_pre_http_is_tombstone', $url );
194194

195-
$response = \wp_safe_remote_get( $url );
195+
$response = \wp_safe_remote_get( $url, array( 'headers' => array( 'Accept' => 'application/activity+json' ) ) );
196196
$code = \wp_remote_retrieve_response_code( $response );
197197

198198
if ( in_array( (int) $code, array( 404, 410 ), true ) ) {
199199
return true;
200200
}
201201

202+
$data = \wp_remote_retrieve_body( $response );
203+
$data = \json_decode( $data, true );
204+
if ( $data && isset( $data['type'] ) && 'Tombstone' === $data['type'] ) {
205+
return true;
206+
}
207+
202208
return false;
203209
}
204210

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ For reasons of data protection, it is not possible to see the followers of other
134134
= Unreleased =
135135

136136
* Changed: Improved content negotiation and AUTHORIZED_FETCH support for third-party plugins
137+
* Fixed: Handle deletes from remote servers that leave behind an accessible Tombstone object.
137138

138139
= 4.7.3 =
139140

tests/includes/class-test-http.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Test file for Activitypub HTTP Class
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Activitypub\Tests;
9+
10+
use Activitypub\Http;
11+
12+
/**
13+
* Test class for ActivityPub HTTP Class
14+
*
15+
* @coversDefaultClass \Activitypub\Http
16+
*/
17+
class Test_Http extends \WP_UnitTestCase {
18+
19+
/**
20+
* Response code is 404 -> is_tombstone returns true
21+
*
22+
* @covers ::is_tombstone
23+
*
24+
* @dataProvider data_is_tombstone
25+
*
26+
* @param array $request The request array.
27+
* @param bool $result The expected result.
28+
*/
29+
public function test_is_tombstone( $request, $result ) {
30+
$fake_request = function () use ( $request ) {
31+
return $request;
32+
};
33+
add_filter( 'pre_http_request', $fake_request, 10, 3 );
34+
$response = Http::is_tombstone( 'https://fake.test/object/123' );
35+
$this->assertEquals( $result, $response );
36+
remove_filter( 'pre_http_request', $fake_request, 10 );
37+
}
38+
39+
/**
40+
* Data provider for test_is_tombstone.
41+
*
42+
* @return array
43+
*/
44+
public function data_is_tombstone() {
45+
return array(
46+
array( array( 'response' => array( 'code' => 404 ) ), true ),
47+
array( array( 'response' => array( 'code' => 410 ) ), true ),
48+
array(
49+
array(
50+
'response' => array( 'code' => 200 ),
51+
'body' => '',
52+
),
53+
false,
54+
),
55+
array(
56+
array(
57+
'response' => array( 'code' => 200 ),
58+
'body' => '{}',
59+
),
60+
false,
61+
),
62+
array(
63+
array(
64+
'response' => array( 'code' => 200 ),
65+
'body' => '{"type": "Note"}',
66+
),
67+
false,
68+
),
69+
array(
70+
array(
71+
'response' => array( 'code' => 200 ),
72+
'body' => '{"type": "Tombstone"}',
73+
),
74+
true,
75+
),
76+
array(
77+
array(
78+
'response' => array( 'code' => 200 ),
79+
'body' => '{"foo": "bar"}',
80+
),
81+
false,
82+
),
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)