AliasManager->getAliasByPath() throws fatal exception when given a path without a slash

Created on 19 November 2024, 5 months ago

Problem/Motivation

Users can enter a URL containing a path that does not start with a slash. This results in an uncaught PHP exception and "The website encountered an unexpected error. Try again later."

Steps to reproduce

1. Create a block that is shown only on the front page (<front>).
2. Visit example.com/index.php123.
3. Observe the crashed site.

Proposed resolution

Instead of throwing an exception, ignore the unexpected argument.

Current:

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

Proposed:

public function getAliasByPath($path, $langcode = NULL) {
    $path ?= '';
    if (!str_starts_with($path, '/')) {
        $path = '/' . $path;
    }
   ...
}

Also, see the related issue which throws a PHP warning when $path is null.

This issue is major under the policy "Trigger a PHP error through the user interface, but only under rare circumstances or affecting only a small percentage of all users, "

🐛 Bug report
Status

Active

Version

11.1 🔥

Component

path.module

Created by

🇺🇸United States danchadwick Boston

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024