mirror of
https://github.com/alexjustesen/speedtest-tracker.git
synced 2026-06-23 07:20:09 +00:00
Fix ping url validation (#1754)
* Fix ping url validation * Fix pint issues * remove early return --------- Co-authored-by: Alex Justesen <alexjustesen@users.noreply.github.com>
This commit is contained in:
@@ -112,20 +112,19 @@ class ExecuteOoklaSpeedtest implements ShouldBeUnique, ShouldQueue
|
||||
|
||||
/**
|
||||
* Check for internet connection.
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function checkForInternetConnection(): bool
|
||||
{
|
||||
$url = config('speedtest.ping_url');
|
||||
|
||||
// TODO: skip checking for internet connection, current validation does not take into account different host formats and ip addresses.
|
||||
return true;
|
||||
|
||||
// Skip checking for internet connection if ping url isn't set (disabled)
|
||||
if (blank($url)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! URL::isValidUrl($url)) {
|
||||
if (! $this->isValidPingUrl($url)) {
|
||||
$this->result->update([
|
||||
'data' => [
|
||||
'type' => 'log',
|
||||
@@ -165,4 +164,20 @@ class ExecuteOoklaSpeedtest implements ShouldBeUnique, ShouldQueue
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given URL is a valid ping URL.
|
||||
*/
|
||||
public function isValidPingUrl(string $url): bool
|
||||
{
|
||||
$hasTLD = static function (string $url): bool {
|
||||
// this also ensures the string ends with a TLD
|
||||
return preg_match('/\.[a-z]{2,}$/i', $url);
|
||||
};
|
||||
|
||||
return (filter_var($url, FILTER_VALIDATE_URL) && $hasTLD($url))
|
||||
// to check for things like `google.com`, we need to add the protocol
|
||||
|| (filter_var('https://'.$url, FILTER_VALIDATE_URL) && $hasTLD($url))
|
||||
|| filter_var($url, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 || FILTER_FLAG_IPV6) !== false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*/
|
||||
public function test_that_true_is_true(): void
|
||||
{
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Jobs\Speedtests\ExecuteOoklaSpeedtest;
|
||||
use App\Models\Result;
|
||||
use Tests\TestCase;
|
||||
|
||||
final class ExecuteOoklaSpeedtestTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*/
|
||||
public function test_that_ping_urls_handled(): void
|
||||
{
|
||||
$job = new ExecuteOoklaSpeedtest(new Result);
|
||||
config(['speedtest' => ['ping_url' => 'google.com']]);
|
||||
|
||||
$passingCases = [
|
||||
// ipv6
|
||||
'2a00:1450:4016:80c::200e',
|
||||
// ipv4
|
||||
'1.1.1.1',
|
||||
// hostname
|
||||
'google.com',
|
||||
// hostname with subdomain
|
||||
'www.google.com',
|
||||
// hostname with protocol
|
||||
'http://google.com',
|
||||
'https://google.com',
|
||||
];
|
||||
|
||||
foreach ($passingCases as $case) {
|
||||
$this->assertTrue($job->isValidPingUrl($case), "$case is an invalid ping url");
|
||||
}
|
||||
|
||||
$failingCases = [
|
||||
// invalid hostname
|
||||
'google',
|
||||
// no tld
|
||||
'http://google',
|
||||
'https://google',
|
||||
// invalid ipv4
|
||||
'1.1.1',
|
||||
'1.1.1.1.',
|
||||
'1.1.1.1.1',
|
||||
'.1.1.1.1',
|
||||
// invalid ipv6
|
||||
'2a00:1450:4016:80c::200e::',
|
||||
'2a00:14504:4016:80c::200e',
|
||||
'2a00:1450:401680c::200e',
|
||||
'2v00:1450:4016:80c::200e',
|
||||
'2::1:1450:4016:80c::200e',
|
||||
// path included
|
||||
'https://google.com/test',
|
||||
// path and query included
|
||||
'https://google.com/test?query=1',
|
||||
// path, query and fragment included
|
||||
'https://www.google.com/test?query=1#fragment',
|
||||
// path, query, fragment and port included
|
||||
'https://google.com:8080/test?query=1#fragment',
|
||||
];
|
||||
|
||||
foreach ($failingCases as $case) {
|
||||
$this->assertFalse($job->isValidPingUrl($case), "$case is a valid ping url");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user