InboxWorker.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 InboxWorker 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::dispatchNow($profile);
  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. if($this->verifySignature($headers, $payload) == true) {
  99. (new Inbox($headers, $profile, $payload))->handle();
  100. return;
  101. } else if($this->blindKeyRotation($headers, $payload) == true) {
  102. (new Inbox($headers, $profile, $payload))->handle();
  103. return;
  104. } else {
  105. return;
  106. }
  107. }
  108. protected function verifySignature($headers, $payload)
  109. {
  110. $body = $this->payload;
  111. $bodyDecoded = $payload;
  112. $signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
  113. $date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
  114. if(!$signature) {
  115. return;
  116. }
  117. if(!$date) {
  118. return;
  119. }
  120. if(!now()->parse($date)->gt(now()->subDays(1)) ||
  121. !now()->parse($date)->lt(now()->addDays(1))
  122. ) {
  123. return;
  124. }
  125. $signatureData = HttpSignature::parseSignatureHeader($signature);
  126. $keyId = Helpers::validateUrl($signatureData['keyId']);
  127. $id = Helpers::validateUrl($bodyDecoded['id']);
  128. $keyDomain = parse_url($keyId, PHP_URL_HOST);
  129. $idDomain = parse_url($id, PHP_URL_HOST);
  130. if(isset($bodyDecoded['object'])
  131. && is_array($bodyDecoded['object'])
  132. && isset($bodyDecoded['object']['attributedTo'])
  133. ) {
  134. if(parse_url($bodyDecoded['object']['attributedTo'], PHP_URL_HOST) !== $keyDomain) {
  135. return;
  136. }
  137. }
  138. if(!$keyDomain || !$idDomain || $keyDomain !== $idDomain) {
  139. return;
  140. }
  141. $actor = Profile::whereKeyId($keyId)->first();
  142. if(!$actor) {
  143. $actorUrl = is_array($bodyDecoded['actor']) ? $bodyDecoded['actor'][0] : $bodyDecoded['actor'];
  144. $actor = Helpers::profileFirstOrNew($actorUrl);
  145. }
  146. if(!$actor) {
  147. return;
  148. }
  149. $pkey = openssl_pkey_get_public($actor->public_key);
  150. if(!$pkey) {
  151. return 0;
  152. }
  153. $inboxPath = "/f/inbox";
  154. list($verified, $headers) = HttpSignature::verify($pkey, $signatureData, $headers, $inboxPath, $body);
  155. if($verified == 1) {
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. }
  161. protected function blindKeyRotation($headers, $payload)
  162. {
  163. $signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
  164. $date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
  165. if(!$signature) {
  166. return;
  167. }
  168. if(!$date) {
  169. return;
  170. }
  171. if(!now()->parse($date)->gt(now()->subDays(1)) ||
  172. !now()->parse($date)->lt(now()->addDays(1))
  173. ) {
  174. return;
  175. }
  176. $signatureData = HttpSignature::parseSignatureHeader($signature);
  177. $keyId = Helpers::validateUrl($signatureData['keyId']);
  178. $actor = Profile::whereKeyId($keyId)->whereNotNull('remote_url')->first();
  179. if(!$actor) {
  180. return;
  181. }
  182. if(Helpers::validateUrl($actor->remote_url) == false) {
  183. return;
  184. }
  185. $res = Zttp::timeout(5)->withHeaders([
  186. 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  187. 'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
  188. ])->get($actor->remote_url);
  189. $res = json_decode($res->body(), true, 8);
  190. if($res['publicKey']['id'] !== $actor->key_id) {
  191. return;
  192. }
  193. $actor->public_key = $res['publicKey']['publicKeyPem'];
  194. $actor->save();
  195. return $this->verifySignature($headers, $payload);
  196. }
  197. }