InboxValidator.php 5.9 KB

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