Add support for additional protocols in Link field definition

Created on 29 June 2013, almost 12 years ago
Updated 5 January 2024, over 1 year ago

Problem/Motivation

The link module only validates http and https, but not other scheme names such as git or ftp.

In Drupal 7, the validation can be turned off, thereby allowing the input of other scheme names. D7 sites could use this to use other scheme names, which will then not be valid in D8 any more.

Even better would be a option in the field configuration to set which scheme names are valid for this field.

Proposed resolution

  • Allow custom protocols.
  • Let's do this in a follow-up issue.

Remaining tasks

  1. To be handled in the follow-up issue.
  2. Done.
  3. Done.
  4. Add tests.

User interface changes

A new text field added to the Link field configuration form, empty by default, that accepts a comma-separated list of protocols that will be allowed in addition to the hard-coded ones.

API changes

+++ b/core/lib/Drupal/Component/Utility/UrlHelper.php
@@ -209,11 +209,13 @@ public static function encodePath($path) {
-  public static function isExternal($path) {
+  public static function isExternal($path, $protocol_whitelist = []) {
+++ b/core/lib/Drupal/Component/Utility/UrlHelper.php
@@ -353,8 +357,8 @@ public static function setAllowedProtocols(array $protocols = []) {
-  public static function stripDangerousProtocols($uri) {
+  public static function stripDangerousProtocols($uri, $whitelist = []) {
+++ b/core/lib/Drupal/Core/Utility/UnroutedUrlAssemblerInterface.php
@@ -40,6 +40,8 @@ interface UnroutedUrlAssemblerInterface {
+   *   - 'protocol_whitelist': An explicit whitelist of protocols to be allowed
+   *     in addition to those traditionally allowed. Defaults to an empty array.

This adds a new option to the $options array.

Data model changes

Added to the Link field configuration schema:

+++ b/core/modules/link/config/schema/link.schema.yml
@@ -49,6 +49,9 @@ field.field_settings.link:
+    protocol_whitelist:
+      type: string
+      label: 'Protocol whitelist'
📌 Task
Status

Needs work

Version

11.0 🔥

Component
Link  →

Last updated about 3 hours ago

Created by

🇳🇱Netherlands ifrik

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

  • Needs screenshots

    The change alters the user interface, so before and after screenshots should be added to document the UI change. Make sure to capture the relevant region only. Use a tool such as Aviary on Windows or Skitch on Mac OS X.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇦Canada liquidcms

    Patch from #47 doesn't apply to D9.4 (due to the smallest change in Umani demo config files).

    This patch works for 9.4. Haven't tested with 9.5 but suspect it works there as well.

  • First commit to issue fork.
  • Merge request !6038Issue #2031149: State as patch in #48 → (Open) created by s_leu
  • Status changed to Needs review over 1 year ago
  • Pipeline finished with Failed
    over 1 year ago
    Total: 627s
    #72141
  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    Seems to have a test failure

    Left some comments on MR.

    Previously tagged for issue summary and screenshots.

    Leaving tests tag as they will be needed for post_update hook.

  • Pipeline finished with Failed
    12 months ago
    Total: 9818s
    #146956
  • 🇨🇦Canada joseph.olstad

    Rolled a patch for 10.2.5 from merge request 6038

    clean for 10.2.5

  • First commit to issue fork.
  • Pipeline finished with Failed
    3 months ago
    Total: 234s
    #384971
  • 🇪🇸Spain vidorado Logroño (La Rioja)

    vidorado → changed the visibility of the branch 11.x to hidden.

  • Pipeline finished with Failed
    3 months ago
    Total: 1508s
    #385033
  • 🇪🇸Spain vidorado Logroño (La Rioja)
    • Updated Issue Summary.
    • Rebased the MR over the most recent 11.x branch.
    • Resolved all GitLab threads.
    • Added a post_update hook and a test for it.

    The MR Gitlab pipeline is failing and I believe it has to do with some unrelated problem in the 11.x branch. Not setting this issue to "Needs Review" until this is solved.

  • Pipeline finished with Failed
    3 months ago
    Total: 1046s
    #385865
  • Pipeline finished with Failed
    3 months ago
    Total: 1002s
    #385870
  • Pipeline finished with Success
    3 months ago
    Total: 966s
    #385890
  • 🇪🇸Spain vidorado Logroño (La Rioja)

    Just figured out that the failing of:

    • Drupal\Tests\package_manager\Build\PackageInstallTest
    • Drupal\Tests\package_manager\Build\PackageInstallSubmoduleTest
    • Drupal\Tests\package_manager\Build\PackageUpdateTest

    with the error message
    Some modules have database schema updates to install. You should run the database update script immediately
    was caused by the incorrect placement of the post_update hook function in the link.module file instead of the required link.post_update.php file.

  • 🇺🇸United States smustgrave

    Left some comments on MR.

    Hiding patches

  • 🇪🇸Spain vidorado Logroño (La Rioja)

    Addresed :)

  • Pipeline finished with Success
    3 months ago
    Total: 7037s
    #387636
  • 🇳🇿New Zealand jonathan_hunt

    This patch applies to Drupal 10.4.2 ok, but I want to use a link field to store links to files such as public:///interactive/zoomify/mytiles and I get "The URI 'public:///interactive/zoomify/mytiles' is invalid. You must use a valid URI scheme. Use base: for a path, e.g., to a Drupal file that needs the base path. Do not use this for internal paths controlled by Drupal."

    Should I be able to set Allowed protocols to public, private? If so, there may be more work required. If not, this should be made clear in the issue and UI?

  • 🇪🇸Spain vidorado Logroño (La Rioja)

    @jonathan_hunt, if you add the "public" protocol in a link field configuration (after applying the changes in this issue fork: https://git.drupalcode.org/issue/drupal-2031149) you will be able to add links like public://whatever/you/want/file.pdf, but those URIs will not be converted to an URL. You will have to process them in a custom hook.

    Maybe the module Linkit ( https://www.drupal.org/project/linkit → ) would address what you want? Although a relative path would be stored in the field, instead of a public:// style URI, the public directory is not likely to change, so it would be enough for you, depending on the case.

  • The Needs Review Queue Bot → tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide → to find step-by-step guides for working with issues.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 148s
    #442698
  • Pipeline finished with Failed
    about 1 month ago
    Total: 2343s
    #442712
  • 🇪🇸Spain vidorado Logroño (La Rioja)

    Rebased the MR :)

  • The Needs Review Queue Bot → tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide → to find step-by-step guides for working with issues.

  • Pipeline finished with Success
    16 days ago
    Total: 386s
    #456325
  • 🇪🇸Spain vidorado Logroño (La Rioja)

    Rebased again

  • 🇨🇦Canada joseph.olstad

    patch file for D10.4.6 , upgrading a D10.3.x site for a client

Production build 0.71.5 2024