DeleteWorker.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. $attr = Helpers::pluckval($bodyDecoded['object']['attributedTo']);
  129. if(is_array($attr)) {
  130. if(isset($attr['id'])) {
  131. $attr = $attr['id'];
  132. } else {
  133. $attr = "";
  134. }
  135. }
  136. if(parse_url($attr, PHP_URL_HOST) !== $keyDomain) {
  137. return;
  138. }
  139. }
  140. if(!$keyDomain || !$idDomain || $keyDomain !== $idDomain) {
  141. return;
  142. }
  143. $actor = Profile::whereKeyId($keyId)->first();
  144. if(!$actor) {
  145. $actorUrl = is_array($bodyDecoded['actor']) ? $bodyDecoded['actor'][0] : $bodyDecoded['actor'];
  146. $actor = Helpers::profileFirstOrNew($actorUrl);
  147. }
  148. if(!$actor) {
  149. return;
  150. }
  151. $pkey = openssl_pkey_get_public($actor->public_key);
  152. if(!$pkey) {
  153. return 0;
  154. }
  155. $inboxPath = "/f/inbox";
  156. list($verified, $headers) = HttpSignature::verify($pkey, $signatureData, $headers, $inboxPath, $body);
  157. if($verified == 1) {
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. protected function blindKeyRotation($headers, $payload)
  164. {
  165. $signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
  166. $date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
  167. if(!$signature) {
  168. return;
  169. }
  170. if(!$date) {
  171. return;
  172. }
  173. if(!now()->parse($date)->gt(now()->subDays(1)) ||
  174. !now()->parse($date)->lt(now()->addDays(1))
  175. ) {
  176. return;
  177. }
  178. $signatureData = HttpSignature::parseSignatureHeader($signature);
  179. $keyId = Helpers::validateUrl($signatureData['keyId']);
  180. $actor = Profile::whereKeyId($keyId)->whereNotNull('remote_url')->first();
  181. if(!$actor) {
  182. return;
  183. }
  184. if(Helpers::validateUrl($actor->remote_url) == false) {
  185. return;
  186. }
  187. $res = Zttp::timeout(60)->withHeaders([
  188. 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  189. 'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
  190. ])->get($actor->remote_url);
  191. $res = json_decode($res->body(), true, 8);
  192. if(!isset($res['publicKey'], $res['publicKey']['id'])) {
  193. return;
  194. }
  195. if($res['publicKey']['id'] !== $actor->key_id) {
  196. return;
  197. }
  198. $actor->public_key = $res['publicKey']['publicKeyPem'];
  199. $actor->save();
  200. return $this->verifySignature($headers, $payload);
  201. }
  202. }