8000 [HtmlSanitizer] reject URLs with URL-encoded non UTF-8 characters in the host part by xabbuh · Pull Request #60090 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HtmlSanitizer] reject URLs with URL-encoded non UTF-8 characters in the host part #60090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ public static function provideParse(): iterable
'http://你好你好' => ['scheme' => 'http', 'host' => '你好你好'],
'https://faß.ExAmPlE/' => ['scheme' => 'https', 'host' => 'faß.ExAmPlE'],
'sc://faß.ExAmPlE/' => ['scheme' => 'sc', 'host' => 'faß.ExAmPlE'],
'http://%30%78%63%30%2e%30%32%35%30.01' => ['scheme' => 'http', 'host' => '%30%78%63%30%2e%30%32%35%30.01'],
'http://%30%78%63%30%2e%30%32%35%30.01%2e' => ['scheme' => 'http', 'host' => '%30%78%63%30%2e%30%32%35%30.01%2e'],
'http://%30%78%63%30%2e%30%32%35%30.01' => null,
'http://%30%78%63%30%2e%30%32%35%30.01%2e' => null,
'http://0Xc0.0250.01' => ['scheme' => 'http', 'host' => '0Xc0.0250.01'],
'http://./' => ['scheme' => 'http', 'host' => '.'],
'http://../' => ['scheme' => 'http', 'host' => '..'],
Expand Down Expand Up @@ -689,7 +689,7 @@ public static function provideParse(): iterable
'urn:ietf:rfc:2648' => ['scheme' => 'urn', 'host' => null],
'tag:joe@example.org,2001:foo/bar' => ['scheme' => 'tag', 'host' => null],
'non-special://%E2%80%A0/' => ['scheme' => 'non-special', 'host' => '%E2%80%A0'],
'non-special://H%4fSt/path' => ['scheme' => 'non-special', 'host' => 'H%4fSt'],
'non-special://H%4fSt/path' => null,
'non-special://[1:2:0:0:5:0:0:0]/' => ['scheme' => 'non-special', 'host' => '[1:2:0:0:5:0:0:0]'],
'non-special://[1:2:0:0:0:0:0:3]/' => ['scheme' => 'non-special', 'host' => '[1:2:0:0:0:0:0:3]'],
'non-special://[1:2::3]:80/' => ['scheme' => 'non-special', 'host' => '[1:2::3]'],
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/HtmlSanitizer/TextSanitizer/UrlSanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
return null;
}

if (isset($parsedUrl['host']) && self::decodeUnreservedCharacters($parsedUrl['host']) !== $parsedUrl['host']) {
return null;
}

return $parsedUrl;
} catch (SyntaxError) {
return null;
Expand Down Expand Up @@ -139,4 +143,16 @@

return true;
}

/**
* Implementation borrowed from League\Uri\Encoder::decodeUnreservedCharacters().
*/
private static function decodeUnreservedCharacters(string $host): string

Check failure on line 150 in src/Symfony/Component/HtmlSanitizer/TextSanitizer/UrlSanitizer.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidNullableReturnType

src/Symfony/Component/HtmlSanitizer/TextSanitizer/UrlSanitizer.php:150:71: InvalidNullableReturnType: The declared return type 'string' for Symfony\Component\HtmlSanitizer\TextSanitizer\UrlSanitizer::decodeUnreservedCharacters is not nullable, but 'null|string' contains null (see https://psalm.dev/144)

Check failure on line 150 in src/Symfony/Component/HtmlSanitizer/TextSanitizer/UrlSanitizer.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidNullableReturnType

src/Symfony/Component/HtmlSanitizer/TextSanitizer/UrlSanitizer.php:150:71: InvalidNullableReturnType: The declared return type 'string' for Symfony\Component\HtmlSanitizer\TextSanitizer\UrlSanitizer::decodeUnreservedCharacters is not nullable, but 'null|string' contains null (see https://psalm.dev/144)
{
return preg_replace_callback(

Check failure on line 152 in src/Symfony/Component/HtmlSanitizer/TextSanitizer/UrlSanitizer.php

View workflow job for this annotation

GitHub Actions / Psalm

NullableReturnStatement

src/Symfony/Component/HtmlSanitizer/TextSanitizer/UrlSanitizer.php:152:16: NullableReturnStatement: The declared return type 'string' for Symfony\Component\HtmlSanitizer\TextSanitizer\UrlSanitizer::decodeUnreservedCharacters is not nullable, but the function returns 'null|string' (see https://psalm.dev/139)

Check failure on line 152 in src/Symfony/Component/HtmlSanitizer/TextSanitizer/UrlSanitizer.php

View workflow job for this annotation

GitHub Actions / Psalm

NullableReturnStatement

src/Symfony/Component/HtmlSanitizer/TextSanitizer/UrlSanitizer.php:152:16: NullableReturnStatement: The declared return type 'string' for Symfony\Component\HtmlSanitizer\TextSanitizer\UrlSanitizer::decodeUnreservedCharacters is not nullable, but the function returns 'null|string' (see https://psalm.dev/139)
',%(2[1-9A-Fa-f]|[3-7][0-9A-Fa-f]|61|62|64|65|66|7[AB]|5F),',
static fn (array $matches): string => rawurldecode($matches[0]),
$host
);
}
}
Loading
0