saloon icon indicating copy to clipboard operation
saloon copied to clipboard

Use `parse_url` instead of `FILTER_VALIDATE_URL` in `URLHelper` to allow underscores in domains

Open KSneijders opened this issue 8 months ago • 0 comments

Domains including an underscore like:

https://example_site.laravel-sail.site:8080

Being used in a SoloRequest will result in a request being sent to:

/https://example_site.laravel-sail.site:8080

This can be verified by adding the following test which will fail:

// tests/Unit/URLHelperTest.php:16
['', 'https://example_domain.com', 'https://example_domain.com'],
Failed asserting that two strings are identical.
Expected :'https://example_domain.com'
Actual   :'/https://example_domain.com'

This is because the FILTER_VALIDATE_URL does not accept underscores in URLs even though it does work in domain names and is actively being used.

See this PHP issue about the topic and where it is also suggested to use parse_url instead.

Replacing the isValidUrl function to the below passas all tests including the new one above.

/**
 * Check if the URL is a valid URL
 */
public static function isValidUrl(string $url): bool
{
    $parsed = parse_url($url);
    return array_key_exists('host', $parsed)
        && array_key_exists('scheme', $parsed);
}

KSneijders avatar May 15 '25 14:05 KSneijders