DeleteWorker.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Jobs\InboxPipeline;
  3. use Cache;
  4. use App\Profile;
  5. use App\Util\ActivityPub\{
  6. Helpers,
  7. HttpSignature,
  8. Inbox
  9. };
  10. use Illuminate\Bus\Queueable;
  11. use Illuminate\Contracts\Queue\ShouldQueue;
  12. use Illuminate\Foundation\Bus\Dispatchable;
  13. use Illuminate\Queue\InteractsWithQueue;
  14. use Illuminate\Queue\SerializesModels;
  15. use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
  16. use Illuminate\Support\Facades\Http;
  17. use Illuminate\Http\Client\ConnectionException;
  18. class DeleteWorker implements ShouldQueue
  19. {
  20. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  21. protected $headers;
  22. protected $payload;
  23. public $timeout = 300;
  24. public $tries = 1;
  25. public $maxExceptions = 1;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct($headers, $payload)
  32. {
  33. $this->headers = $headers;
  34. $this->payload = $payload;
  35. }
  36. /**
  37. * Execute the job.
  38. *
  39. * @return void
  40. */
  41. public function handle()
  42. {
  43. $profile = null;
  44. $headers = $this->headers;
  45. $payload = json_decode($this->payload, true, 8);
  46. if(!isset($headers['signature']) || !isset($headers['date'])) {
  47. return;
  48. }
  49. if(empty($headers) || empty($payload)) {
  50. return;
  51. }
  52. if( $payload['type'] === 'Delete' &&
  53. ( ( is_string($payload['object']) &&
  54. $payload['object'] === $payload['actor'] ) ||
  55. ( is_array($payload['object']) &&
  56. isset($payload['object']['id'], $payload['object']['type']) &&
  57. $payload['object']['type'] === 'Person' &&
  58. $payload['actor'] === $payload['object']['id']
  59. ))
  60. ) {
  61. $actor = $payload['actor'];
  62. if($this->verifySignature($headers, $payload) == true) {
  63. $actorDelete = Profile::whereRemoteUrl($actor)->exists();
  64. if($actorDelete) {
  65. if($this->verifySignature($headers, $payload) == true) {
  66. Cache::set($key, false);
  67. $profile = Profile::whereNotNull('domain')
  68. ->whereNull('status')
  69. ->whereRemoteUrl($actor)
  70. ->first();
  71. if($profile) {
  72. DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('inbox');
  73. }
  74. return 1;
  75. } else {
  76. // Signature verification failed, exit.
  77. return 1;
  78. }
  79. } else {
  80. // Remote user doesn't exist, exit early.
  81. return 1;
  82. }
  83. return 1;
  84. } else {
  85. return 1;
  86. }
  87. } else {
  88. $profile = null;
  89. if($this->verifySignature($headers, $payload) == true) {
  90. ActivityHandler::dispatch($headers, $profile, $payload)->onQueue('delete');
  91. return 1;
  92. } else {
  93. return 1;
  94. }
  95. }
  96. }
  97. protected function verifySignature($headers, $payload)
  98. {
  99. $body = $this->payload;
  100. $bodyDecoded = $payload;
  101. $signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
  102. $date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
  103. if(!$signature) {
  104. return false;
  105. }
  106. if(!$date) {
  107. return false;
  108. }
  109. if(!now()->parse($date)->gt(now()->subDays(1)) ||
  110. !now()->parse($date)->lt(now()->addDays(1))
  111. ) {
  112. return false;
  113. }
  114. $signatureData = HttpSignature::parseSignatureHeader($signature);
  115. $keyId = Helpers::validateUrl($signatureData['keyId']);
  116. $id = Helpers::validateUrl($bodyDecoded['id']);
  117. $keyDomain = parse_url($keyId, PHP_URL_HOST);
  118. $idDomain = parse_url($id, PHP_URL_HOST);
  119. if(isset($bodyDecoded['object'])
  120. && is_array($bodyDecoded['object'])
  121. && isset($bodyDecoded['object']['attributedTo'])
  122. ) {
  123. $attr = Helpers::pluckval($bodyDecoded['object']['attributedTo']);
  124. if(is_array($attr)) {
  125. if(isset($attr['id'])) {
  126. $attr = $attr['id'];
  127. } else {
  128. $attr = "";
  129. }
  130. }
  131. if(parse_url($attr, PHP_URL_HOST) !== $keyDomain) {
  132. return false;
  133. }
  134. }
  135. if(!$keyDomain || !$idDomain || $keyDomain !== $idDomain) {
  136. return false;
  137. }
  138. $actor = Profile::whereKeyId($keyId)->first();
  139. if(!$actor) {
  140. $actorUrl = is_array($bodyDecoded['actor']) ? $bodyDecoded['actor'][0] : $bodyDecoded['actor'];
  141. $actor = Helpers::profileFirstOrNew($actorUrl);
  142. }
  143. if(!$actor) {
  144. return false;
  145. }
  146. $pkey = openssl_pkey_get_public($actor->public_key);
  147. if(!$pkey) {
  148. return false;
  149. }
  150. $inboxPath = "/f/inbox";
  151. list($verified, $headers) = HttpSignature::verify($pkey, $signatureData, $headers, $inboxPath, $body);
  152. if($verified == 1) {
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. }
  158. protected function blindKeyRotation($headers, $payload)
  159. {
  160. $signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
  161. $date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
  162. if(!$signature) {
  163. return;
  164. }
  165. if(!$date) {
  166. return;
  167. }
  168. if(!now()->parse($date)->gt(now()->subDays(1)) ||
  169. !now()->parse($date)->lt(now()->addDays(1))
  170. ) {
  171. return;
  172. }
  173. $signatureData = HttpSignature::parseSignatureHeader($signature);
  174. $keyId = Helpers::validateUrl($signatureData['keyId']);
  175. $actor = Profile::whereKeyId($keyId)->whereNotNull('remote_url')->first();
  176. if(!$actor) {
  177. return;
  178. }
  179. if(Helpers::validateUrl($actor->remote_url) == false) {
  180. return;
  181. }
  182. try {
  183. $res = Http::timeout(20)->withHeaders([
  184. 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  185. 'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
  186. ])->get($actor->remote_url);
  187. } catch (ConnectionException $e) {
  188. return false;
  189. }
  190. if(!$res->ok()) {
  191. return false;
  192. }
  193. $res = json_decode($res->body(), true, 8);
  194. if(!isset($res['publicKey'], $res['publicKey']['id'])) {
  195. return;
  196. }
  197. if($res['publicKey']['id'] !== $actor->key_id) {
  198. return;
  199. }
  200. $actor->public_key = $res['publicKey']['publicKeyPem'];
  201. $actor->save();
  202. return $this->verifySignature($headers, $payload);
  203. }
  204. }