DeleteWorker.php 5.5 KB

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