InboxValidator.php 6.2 KB

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