InboxWorker.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 InboxWorker implements ShouldQueue
  19. {
  20. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  21. protected $headers;
  22. protected $payload;
  23. public $timeout = 300;
  24. public $tries = 1;
  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. if(empty($headers) || empty($this->payload) || !isset($headers['signature']) || !isset($headers['date'])) {
  46. return;
  47. }
  48. $payload = json_decode($this->payload, true, 8);
  49. if(isset($payload['id'])) {
  50. $lockKey = 'ap:icid:' . hash('sha256', $payload['id']);
  51. if(Cache::get($lockKey) !== null) {
  52. // Job processed already
  53. return 1;
  54. }
  55. Cache::put($lockKey, 1, 3600);
  56. }
  57. if($this->verifySignature($headers, $payload) == true) {
  58. ActivityHandler::dispatch($headers, $profile, $payload)->onQueue('shared');
  59. return;
  60. } else {
  61. return;
  62. }
  63. }
  64. protected function verifySignature($headers, $payload)
  65. {
  66. $body = $this->payload;
  67. $bodyDecoded = $payload;
  68. $signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
  69. $date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
  70. if(!$signature) {
  71. return false;
  72. }
  73. if(!$date) {
  74. return false;
  75. }
  76. if(!now()->parse($date)->gt(now()->subDays(1)) ||
  77. !now()->parse($date)->lt(now()->addDays(1))
  78. ) {
  79. return false;
  80. }
  81. if(!isset($bodyDecoded['id'])) {
  82. return false;
  83. }
  84. $signatureData = HttpSignature::parseSignatureHeader($signature);
  85. $keyId = Helpers::validateUrl($signatureData['keyId']);
  86. $id = Helpers::validateUrl($bodyDecoded['id']);
  87. $keyDomain = parse_url($keyId, PHP_URL_HOST);
  88. $idDomain = parse_url($id, PHP_URL_HOST);
  89. if(isset($bodyDecoded['object'])
  90. && is_array($bodyDecoded['object'])
  91. && isset($bodyDecoded['object']['attributedTo'])
  92. ) {
  93. $attr = Helpers::pluckval($bodyDecoded['object']['attributedTo']);
  94. if(is_array($attr)) {
  95. if(isset($attr['id'])) {
  96. $attr = $attr['id'];
  97. } else {
  98. $attr = "";
  99. }
  100. }
  101. if(parse_url($attr, PHP_URL_HOST) !== $keyDomain) {
  102. return false;
  103. }
  104. }
  105. if(!$keyDomain || !$idDomain || $keyDomain !== $idDomain) {
  106. return false;
  107. }
  108. $actor = Profile::whereKeyId($keyId)->first();
  109. if(!$actor) {
  110. $actorUrl = Helpers::pluckval($bodyDecoded['actor']);
  111. $actor = Helpers::profileFirstOrNew($actorUrl);
  112. }
  113. if(!$actor) {
  114. return false;
  115. }
  116. $pkey = openssl_pkey_get_public($actor->public_key);
  117. if(!$pkey) {
  118. return false;
  119. }
  120. $inboxPath = "/f/inbox";
  121. list($verified, $headers) = HttpSignature::verify($pkey, $signatureData, $headers, $inboxPath, $body);
  122. if($verified == 1) {
  123. return true;
  124. } else {
  125. return false;
  126. }
  127. }
  128. protected function blindKeyRotation($headers, $payload)
  129. {
  130. $signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
  131. $date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
  132. if(!$signature) {
  133. return;
  134. }
  135. if(!$date) {
  136. return;
  137. }
  138. if(!now()->parse($date)->gt(now()->subDays(1)) ||
  139. !now()->parse($date)->lt(now()->addDays(1))
  140. ) {
  141. return;
  142. }
  143. $signatureData = HttpSignature::parseSignatureHeader($signature);
  144. $keyId = Helpers::validateUrl($signatureData['keyId']);
  145. $actor = Profile::whereKeyId($keyId)->whereNotNull('remote_url')->first();
  146. if(!$actor) {
  147. return;
  148. }
  149. if(Helpers::validateUrl($actor->remote_url) == false) {
  150. return;
  151. }
  152. try {
  153. $res = Http::timeout(20)->withHeaders([
  154. 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  155. 'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
  156. ])->get($actor->remote_url);
  157. } catch (ConnectionException $e) {
  158. return false;
  159. }
  160. if(!$res->ok()) {
  161. return false;
  162. }
  163. $res = json_decode($res->body(), true, 8);
  164. if(!$res || empty($res) || !isset($res['publicKey']) || !isset($res['publicKey']['id'])) {
  165. return;
  166. }
  167. if($res['publicKey']['id'] !== $actor->key_id) {
  168. return;
  169. }
  170. $actor->public_key = $res['publicKey']['publicKeyPem'];
  171. $actor->save();
  172. return $this->verifySignature($headers, $payload);
  173. }
  174. }