DeleteWorker.php 5.6 KB

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