Problem/Motivation
UriHostConstraintValidator already validates host names and IP address literals for mailer configuration.
The language negotiation form performs its own check by prepending http:// and extracting the host with parse_url(). This currently allows entries such as bad host.example.
UrlHelper::isValid() checks URL syntax. A port is perfectly valid in a URL such as https://example.org:8080/, but a host field must reject example.org:8080. The form already handles that case. It does, however, accept -a.b.co, even though a DNS host name label cannot start with a hyphen. Checking https://-a.b.co with isValid() would also accept it.
The uri-host PHPStan type proposed in #3642960: [PP-1] Introduce reusable PHPStan types, including string patterns with typed placeholders could use UrlHelper::isValidHost() to check host names in PHPDoc string patterns, giving static analysis and runtime validation the same rules.
Proposed resolution
Add UrlHelper::isValidHost(), built on the Symfony Validator dependency introduced in #3045484: Add support for Internationalized Domain Names (IDN).
Use Symfony's Hostname constraint for DNS names, allow single-label names such as default, and retain support for bracketed IPv6 addresses.
Use UrlHelper::isValidHost() in both the constraint and the form, preserving the form's support for Unicode domain names by converting them to ASCII for validation.
Consolidate the relevant hosts among testcases from #3045484: Add support for Internationalized Domain Names (IDN), including Unicode, Punycode, and IPv6. Ensure the language negotiation form rejects a host containing a space.
Remaining tasks
Postponed on #3045484: Add support for Internationalized Domain Names (IDN).
Review.
User interface changes
The language negotiation form rejects invalid host names that previously passed validation.
API changes
Adds a new public method UrlHelper::isValidHost(string $host): bool.
Data model changes
None.
Release notes snippet
The new method UrlHelper::isValidHost() validates host names and IP address literals.
Issue fork drupal-3643290
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
pancho