InboxWorker.php 7.3 KB

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