InboxWorker.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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($this->verifySignature($headers, $payload) == true) {
  59. (new Inbox($headers, $profile, $payload))->handle();
  60. return;
  61. } else if($this->blindKeyRotation($headers, $payload) == true) {
  62. (new Inbox($headers, $profile, $payload))->handle();
  63. return;
  64. } else {
  65. return;
  66. }
  67. }
  68. protected function verifySignature($headers, $payload)
  69. {
  70. $body = $this->payload;
  71. $bodyDecoded = $payload;
  72. $signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
  73. $date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
  74. if(!$signature) {
  75. return;
  76. }
  77. if(!$date) {
  78. return;
  79. }
  80. if(!now()->parse($date)->gt(now()->subDays(1)) ||
  81. !now()->parse($date)->lt(now()->addDays(1))
  82. ) {
  83. return;
  84. }
  85. if(!isset($bodyDecoded['id'])) {
  86. return;
  87. }
  88. $signatureData = HttpSignature::parseSignatureHeader($signature);
  89. $keyId = Helpers::validateUrl($signatureData['keyId']);
  90. $id = Helpers::validateUrl($bodyDecoded['id']);
  91. $keyDomain = parse_url($keyId, PHP_URL_HOST);
  92. $idDomain = parse_url($id, PHP_URL_HOST);
  93. if(isset($bodyDecoded['object'])
  94. && is_array($bodyDecoded['object'])
  95. && isset($bodyDecoded['object']['attributedTo'])
  96. ) {
  97. $attr = Helpers::pluckval($bodyDecoded['object']['attributedTo']);
  98. if(is_array($attr)) {
  99. if(isset($attr['id'])) {
  100. $attr = $attr['id'];
  101. } else {
  102. $attr = "";
  103. }
  104. }
  105. if(parse_url($attr, PHP_URL_HOST) !== $keyDomain) {
  106. return;
  107. }
  108. }
  109. if(!$keyDomain || !$idDomain || $keyDomain !== $idDomain) {
  110. return;
  111. }
  112. $actor = Profile::whereKeyId($keyId)->first();
  113. if(!$actor) {
  114. $actorUrl = Helpers::pluckval($bodyDecoded['actor']);
  115. $actor = Helpers::profileFirstOrNew($actorUrl);
  116. }
  117. if(!$actor) {
  118. return;
  119. }
  120. $pkey = openssl_pkey_get_public($actor->public_key);
  121. if(!$pkey) {
  122. return 0;
  123. }
  124. $inboxPath = "/f/inbox";
  125. list($verified, $headers) = HttpSignature::verify($pkey, $signatureData, $headers, $inboxPath, $body);
  126. if($verified == 1) {
  127. return true;
  128. } else {
  129. return false;
  130. }
  131. }
  132. protected function blindKeyRotation($headers, $payload)
  133. {
  134. $signature = is_array($headers['signature']) ? $headers['signature'][0] : $headers['signature'];
  135. $date = is_array($headers['date']) ? $headers['date'][0] : $headers['date'];
  136. if(!$signature) {
  137. return;
  138. }
  139. if(!$date) {
  140. return;
  141. }
  142. if(!now()->parse($date)->gt(now()->subDays(1)) ||
  143. !now()->parse($date)->lt(now()->addDays(1))
  144. ) {
  145. return;
  146. }
  147. $signatureData = HttpSignature::parseSignatureHeader($signature);
  148. $keyId = Helpers::validateUrl($signatureData['keyId']);
  149. $actor = Profile::whereKeyId($keyId)->whereNotNull('remote_url')->first();
  150. if(!$actor) {
  151. return;
  152. }
  153. if(Helpers::validateUrl($actor->remote_url) == false) {
  154. return;
  155. }
  156. $res = Zttp::timeout(60)->withHeaders([
  157. 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  158. 'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
  159. ])->get($actor->remote_url);
  160. $res = json_decode($res->body(), true, 8);
  161. if(!$res || empty($res) || !isset($res['publicKey']) || !isset($res['publicKey']['id'])) {
  162. return;
  163. }
  164. if($res['publicKey']['id'] !== $actor->key_id) {
  165. return;
  166. }
  167. $actor->public_key = $res['publicKey']['publicKeyPem'];
  168. $actor->save();
  169. return $this->verifySignature($headers, $payload);
  170. }
  171. }