Skip to content

Commit

Permalink
added check for private ip range
Browse files Browse the repository at this point in the history
  • Loading branch information
geek-at committed Nov 2, 2023
1 parent 5c3ee9e commit d349ee8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/geturl.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@

$url = trim($_REQUEST['url']);

if(checkURLForPrivateIPRange($url))
exit(json_encode(array('status'=>'err','reason'=>'Private IP range')));

if(!$url || !startsWith($url, 'http'))
exit(json_encode(array('status'=>'err','reason'=>'Invalid URL')));

Expand Down
37 changes: 37 additions & 0 deletions inc/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -928,3 +928,40 @@ function executeUploadPermission()
}
}
}

/**
* Checks if a URL is valid
* @param string $url
* @return boolean (true if valid, false if not)
*/
function checkURLForPrivateIPRange($url)
{
$host = getHost($url);
$ip = gethostbyname($host);
if(is_public_ipv4($ip) || is_public_ipv6($ip)) return false;
return true;
}

function getHost($url){
$URIs = parse_url(trim($url));
$host = !empty($URIs['host'])? $URIs['host'] : explode('/', $URIs['path'])[0];
return $host;
}

function is_public_ipv4($ip=NULL)
{
return filter_var(
$ip,
FILTER_VALIDATE_IP,
FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
) === $ip ? TRUE : FALSE;
}

function is_public_ipv6($ip=NULL)
{
return filter_var(
$ip,
FILTER_VALIDATE_IP,
FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
) === $ip ? TRUE : FALSE;
}

0 comments on commit d349ee8

Please sign in to comment.