InvalidArgumentException in Drupal\Core\Url::fromUserInput()

Created on 13 September 2018, almost 6 years ago
Updated 20 June 2024, 8 days ago

In Drupal 8.6, mimemail used as formatter generates the exception:

InvalidArgumentException: The user-entered string 'node/1' must begin with a '/', '?', or '#'. in Drupal\Core\Url::fromUserInput() (rule 204 of .../core/lib/Drupal/Core/Url.php).

This causes the mailing to break.

πŸ› Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium Fernly

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.

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.

  • πŸ‡ͺπŸ‡ΈSpain jaramoshu

    In patch with code:

    - $path = implode('/', $args);
    +$path = '/' . implode('/', $args);

    It doesn't solve my problem. However, the following modification does:

     if ((strpos($path, '/') !== 0) && (strpos($path, '#') !== 0) && (strpos($path, '?') !== 0)) {
           $url = $path;
      } else {
           $url = Url::fromUserInput($path, $options)->toString();
      }
  • First commit to issue fork.
  • πŸ‡ΊπŸ‡¦Ukraine nnevill Lutsk

    It's not the best patch in the world but it works.

    But it's not applicable for 8.x-1.0-alpha6 so I've prepared patch for that specific version.

  • First commit to issue fork.
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.3 & MySQL 5.7
    last update 8 days ago
    28 pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.3 & MySQL 5.7
    last update 8 days ago
    28 pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.3 & MySQL 5.7
    last update 8 days ago
    28 pass
  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    Patch solves the issue correctly as it matches the conditions where an InvalidArgumentException would get thrown to prevent those conditions from being met and resulting in the fatal error. Ie, here is the excerpt from the ::fromUserInput() method

    if (!str_starts_with($user_input, '/') && !str_starts_with($user_input, '#') && !str_starts_with($user_input, '?')) {
      throw new \InvalidArgumentException("The user-entered string '$user_input' must begin with a '/', '?', or '#'.");
    }
    

    Thanks! Going to quickly convert it to a merge request, but since I'm not making any changes, just facilitating the maintainer's job, suggesting RTBC as status.

  • πŸ‡¬πŸ‡§United Kingdom scott_euser

    Nvm #4 not addressed, needs work

  • Pipeline finished with Success
    8 days ago
    Total: 221s
    #203752
Production build 0.69.0 2024