Problem/Motivation
Drupal's URL form element uses UrlHelper::isValid() to validate absolute URLs. The Link field follows a different path. Its widget converts user input to a URI, and its field constraints check whether Drupal can construct a URL from that URI, whether the field accepts external links, and whether the site allows the protocol. An external HTTP(S) URL can therefore pass link field validation even when the URL form element rejects its syntax.
Link fields already accept internationalized hostnames and support schemes such as mailto:. Using UrlHelper::isValid($uri, TRUE) unconditionally today would reject valid links that editors can currently save.
Proposed resolution
Use UrlHelper::isValid() for external URL syntax validation at the field level so that the same rules apply when validating a link through the widget or programmatically.
Keep the site's allowed-protocol check. A syntactically valid URL may still use a protocol the site disallows. Preserve support for internal paths, entity links, and special values such as <nolink>.
Remaining tasks
Postponed on:
- #3045484: Add support for Internationalized Domain Names (IDN)
- #295021: Expand/augment UrlHelper::isValid() to improve correctness and handle more URL types
Add a regression test for a malformed external URL currently accepted by a link field. Cover valid Unicode and Punycode hostnames, an allowed non-HTTP scheme, and a disallowed scheme. Determine the impact on existing field values when an entity is edited and validated again.
User interface changes
Link fields display a validation error for malformed external URLs that previously passed validation.
Introduced terminology
None.
API changes
None.
Data model changes
None.
Release notes snippet
Link fields now use the same syntax validation as URL form elements for external URLs.
Comments
Comment #2
panchoComment #3
dcam commentedThis is a duplicate of #2652236: Insufficient link validation for external URLs in link widget.