Extend MD_STD_IN::sanitize_url to automatically set protocol / scheme

names in lowercase
This commit is contained in:
Joshua Ramon Enslin 2023-11-09 16:40:28 +01:00
parent 66e704de47
commit 0fb368b96d
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 13 additions and 2 deletions

View File

@ -233,8 +233,16 @@ final class MD_STD_IN {
} }
// Check for valid schemes // Check for valid schemes
try {
if (MD_STD::startsWithAny($output, ['https://', 'http://', 'ftp://']) === false) { if (MD_STD::startsWithAny($output, ['https://', 'http://', 'ftp://']) === false) {
throw new MDInvalidUrl("Invalid input URL"); throw new MDInvalidUrl("Invalid input URL" . PHP_EOL . $output . PHP_EOL . strtolower($output));
}
}
catch (MDInvalidUrl $e) {
if (MD_STD::startsWithAny(strtolower($output), ['https://', 'http://', 'ftp://']) === true) {
$output = strtolower(substr($output, 0, 8)) . substr($output, 8);
}
else throw $e;
} }
if (\str_contains($output, '.') === false) { if (\str_contains($output, '.') === false) {

View File

@ -45,6 +45,7 @@ final class MD_STD_TEST_PROVIDERS {
return [ return [
'Regular URL without path or query' => ['https://www.museum-digital.org', 'https://www.museum-digital.org'], 'Regular URL without path or query' => ['https://www.museum-digital.org', 'https://www.museum-digital.org'],
'URL with uppercase character in scheme' => ['Https://www.museum-digital.org', 'https://www.museum-digital.org'],
'URL with cyrillic characters, HTML-encoded ' => [ 'URL with cyrillic characters, HTML-encoded ' => [
'https://sr.wikipedia.org/wiki/%D0%91%D0%B5%D0%BE%D0%B3%D1%80%D0%B0%D0%B4', 'https://sr.wikipedia.org/wiki/%D0%91%D0%B5%D0%BE%D0%B3%D1%80%D0%B0%D0%B4',
'https://sr.wikipedia.org/wiki/%D0%91%D0%B5%D0%BE%D0%B3%D1%80%D0%B0%D0%B4', 'https://sr.wikipedia.org/wiki/%D0%91%D0%B5%D0%BE%D0%B3%D1%80%D0%B0%D0%B4',
@ -98,6 +99,8 @@ final class MD_STD_TEST_PROVIDERS {
]; ];
} }
$output['Mail address is too long'] = [str_repeat("a", 10000) . '@example.com'];
return $output; return $output;
} }