ActivityPubFetchService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Services;
  3. use App\Util\ActivityPub\HttpSignature;
  4. use Cache;
  5. use Illuminate\Http\Client\ConnectionException;
  6. use Illuminate\Http\Client\RequestException;
  7. use Illuminate\Support\Facades\Http;
  8. class ActivityPubFetchService
  9. {
  10. const CACHE_KEY = 'pf:services:apfetchs:';
  11. public static function get($url, $validateUrl = true)
  12. {
  13. if (! self::validateUrl($url)) {
  14. return false;
  15. }
  16. $domain = parse_url($url, PHP_URL_HOST);
  17. if (! $domain) {
  18. return false;
  19. }
  20. $domainKey = base64_encode($domain);
  21. $urlKey = hash('sha256', $url);
  22. $key = self::CACHE_KEY.$domainKey.':'.$urlKey;
  23. return Cache::remember($key, 450, function () use ($url) {
  24. return self::fetchRequest($url);
  25. });
  26. }
  27. public static function validateUrl($url)
  28. {
  29. if (is_array($url)) {
  30. $url = $url[0];
  31. }
  32. $localhosts = [
  33. '127.0.0.1', 'localhost', '::1',
  34. ];
  35. if (strtolower(mb_substr($url, 0, 8)) !== 'https://') {
  36. return false;
  37. }
  38. if (substr_count($url, '://') !== 1) {
  39. return false;
  40. }
  41. if (mb_substr($url, 0, 8) !== 'https://') {
  42. $url = 'https://'.substr($url, 8);
  43. }
  44. $valid = filter_var($url, FILTER_VALIDATE_URL);
  45. if (! $valid) {
  46. return false;
  47. }
  48. $host = parse_url($valid, PHP_URL_HOST);
  49. if (in_array($host, $localhosts)) {
  50. return false;
  51. }
  52. if (config('security.url.verify_dns')) {
  53. if (DomainService::hasValidDns($host) === false) {
  54. return false;
  55. }
  56. }
  57. if (app()->environment() === 'production') {
  58. $bannedInstances = InstanceService::getBannedDomains();
  59. if (in_array($host, $bannedInstances)) {
  60. return false;
  61. }
  62. }
  63. return $url;
  64. }
  65. public static function fetchRequest($url, $returnJsonFormat = false)
  66. {
  67. $baseHeaders = [
  68. 'Accept' => 'application/activity+json',
  69. ];
  70. $headers = HttpSignature::instanceActorSign($url, false, $baseHeaders, 'get');
  71. $headers['Accept'] = 'application/activity+json';
  72. $headers['User-Agent'] = 'PixelFedBot/1.0.0 (Pixelfed/'.config('pixelfed.version').'; +'.config('app.url').')';
  73. try {
  74. $res = Http::withOptions([
  75. 'allow_redirects' => [
  76. 'max' => 2,
  77. 'protocols' => ['https'],
  78. ]])
  79. ->withHeaders($headers)
  80. ->timeout(30)
  81. ->connectTimeout(5)
  82. ->retry(3, 500)
  83. ->get($url);
  84. } catch (RequestException $e) {
  85. return;
  86. } catch (ConnectionException $e) {
  87. return;
  88. } catch (Exception $e) {
  89. return;
  90. }
  91. if (! $res->ok()) {
  92. return;
  93. }
  94. if (! $res->hasHeader('Content-Type')) {
  95. return;
  96. }
  97. $acceptedTypes = [
  98. 'application/activity+json; charset=utf-8',
  99. 'application/activity+json',
  100. 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  101. ];
  102. $contentType = $res->getHeader('Content-Type')[0];
  103. if (! $contentType) {
  104. return;
  105. }
  106. if (! in_array($contentType, $acceptedTypes)) {
  107. return;
  108. }
  109. return $returnJsonFormat ? $res->json() : $res->body();
  110. }
  111. }