AliasManager->getAliasByPath() throw warning when handling empty path string.

Created on 9 May 2022, over 2 years ago
Updated 13 February 2023, over 1 year ago

Problem/Motivation

While play with Traefik edge router redirectRegex middleware, accessing a Drupal 9.3.12 site from a redirection, and with a URL without trailing slash (https://www.domain.com), Drupal throw this exception:

Warning: Uninitialized string offset 0 in Drupal\path_alias\AliasManager->getAliasByPath() (line 184 of core/modules/path_alias/src/AliasManager.php).

Drupal\path_alias\AliasManager->getAliasByPath('') (Line: 154)
Drupal\system\Plugin\Condition\RequestPath->evaluate() (Line: 68)
Drupal\allow_iframed_site\EventSubscriber\RemoveXFrameOptionsSubscriber->RemoveXFrameOptions(Object, 'kernel.response', Object) (Line: 79)
Drupal\webprofiler\EventDispatcher\TraceableEventDispatcher->dispatch(Object, 'kernel.response') (Line: 191)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 137)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 191)
Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1) (Line: 128)
Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 82)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 38)
Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 85)

Steps to reproduce

Unknown, access to drupal without ending / to the https://www.domain.com ?

Proposed resolution

In AliasManager.php, line 183, replace:

public function getAliasByPath($path, $langcode = NULL) {
    if ($path[0] !== '/') {
      throw new \InvalidArgumentException(sprintf('Source path %s has to start with a slash.', $path));
    }

by

public function getAliasByPath($path, $langcode = NULL) {
    if ($path && $path[0] !== '/') {
      throw new \InvalidArgumentException(sprintf('Source path %s has to start with a slash.', $path));
    }

Release notes snippet

Fix AliasManager->getAliasByPath() handling of empty path string.

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
PathΒ  β†’

Last updated 12 days ago

  • Maintained by
  • πŸ‡¬πŸ‡§United Kingdom @catch
Created by

πŸ‡«πŸ‡·France landure

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

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

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Triggering for 10.1

  • Status changed to RTBC over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request β†’ as a guide.

    Tests passed for D10.
    test-only shows a failing test case
    Issue summary has the issue and proposed resolution.

    Thanks!

  • Status changed to Needs work over 1 year ago
  • πŸ‡¬πŸ‡§United Kingdom catch
    +++ b/core/modules/path_alias/tests/src/Unit/AliasManagerTest.php
    @@ -525,6 +525,16 @@ public function testGetAliasByPathUncachedMissWithAlias() {
    +   */
    +  public function testInvalidArgumentException() {
    +    $this->expectException(\InvalidArgumentException::class);
    +    $this->aliasManager->getAliasByPath('');
    +  }
    +
    

    If we're already adding this test, let's also test for the case with no forward slash too.

Production build 0.71.5 2024