DeleteWorker.php 6.0 KB

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