DeleteWorker.php 5.8 KB

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