Skip to content

Commit

Permalink
Update AP Helpers, improve url validation and add optional dns verifi…
Browse files Browse the repository at this point in the history
…cation, disabled by default
  • Loading branch information
dansup committed Jul 14, 2023
1 parent a00a520 commit 2bef3e4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
28 changes: 28 additions & 0 deletions app/Services/DomainService.php
@@ -0,0 +1,28 @@
<?php

namespace App\Services;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class DomainService
{
const CACHE_KEY = 'pf:services:domains:';

public static function hasValidDns($domain)
{
if(!$domain || !strlen($domain) || strpos($domain, '.') == -1) {
return false;
}

if(config('security.url.trusted_domains')) {
if(in_array($domain, explode(',', config('security.url.trusted_domains')))) {
return true;
}
}

return Cache::remember(self::CACHE_KEY . 'valid-dns:' . $domain, 14400, function() use($domain) {
return count(dns_get_record($domain, DNS_A | DNS_AAAA)) > 0;
});
}
}
34 changes: 17 additions & 17 deletions app/Util/ActivityPub/Helpers.php
Expand Up @@ -40,6 +40,7 @@
use Illuminate\Contracts\Cache\LockTimeoutException;
use App\Jobs\ProfilePipeline\IncrementPostCount;
use App\Jobs\ProfilePipeline\DecrementPostCount;
use App\Services\DomainService;
use App\Services\UserFilterService;

class Helpers {
Expand Down Expand Up @@ -168,17 +169,24 @@ public static function validateUrl($url)

$hash = hash('sha256', $url);
$key = "helpers:url:valid:sha256-{$hash}";
$ttl = now()->addMinutes(5);

$valid = Cache::remember($key, $ttl, function() use($url) {
$valid = Cache::remember($key, 900, function() use($url) {
$localhosts = [
'127.0.0.1', 'localhost', '::1'
];

if(mb_substr($url, 0, 8) !== 'https://') {
if(strtolower(mb_substr($url, 0, 8)) !== 'https://') {
return false;
}

if(substr_count($url, '://') !== 1) {
return false;
}

if(mb_substr($url, 0, 8) !== 'https://') {
$url = 'https://' . substr($url, 8);
}

$valid = filter_var($url, FILTER_VALIDATE_URL);

if(!$valid) {
Expand All @@ -187,15 +195,12 @@ public static function validateUrl($url)

$host = parse_url($valid, PHP_URL_HOST);

// if(count(dns_get_record($host, DNS_A | DNS_AAAA)) == 0) {
// return false;
// }
if(in_array($host, $localhosts)) {
return false;
}

if(config('costar.enabled') == true) {
if(
(config('costar.domain.block') != null && Str::contains($host, config('costar.domain.block')) == true) ||
(config('costar.actor.block') != null && in_array($url, config('costar.actor.block')) == true)
) {
if(config('security.url.verify_dns')) {
if(DomainService::hasValidDns($host) === false) {
return false;
}
}
Expand All @@ -207,11 +212,6 @@ public static function validateUrl($url)
}
}


if(in_array($host, $localhosts)) {
return false;
}

return $url;
});

Expand All @@ -224,7 +224,7 @@ public static function validateLocalUrl($url)
if($url == true) {
$domain = config('pixelfed.domain.app');
$host = parse_url($url, PHP_URL_HOST);
$url = $domain === $host ? $url : false;
$url = strtolower($domain) === strtolower($host) ? $url : false;
return $url;
}
return false;
Expand Down
9 changes: 9 additions & 0 deletions config/security.php
@@ -0,0 +1,9 @@
<?php

return [
'url' => [
'verify_dns' => env('PF_SECURITY_URL_VERIFY_DNS', false),

'trusted_domains' => env('PF_SECURITY_URL_TRUSTED_DOMAINS', 'pixelfed.social,pixelfed.art,mastodon.social'),
]
];

0 comments on commit 2bef3e4

Please sign in to comment.