Skip to content
Snippets Groups Projects
Commit 6a61d025 authored by Igor Biki's avatar Igor Biki Committed by Kevin Paxman
Browse files

ISTWCMS-7266: Refactor URL validation logic for internal links.

Extracted base path validation into a reusable private method `getInternalUrlIfValid`, improving code clarity and maintainability. Simplified the handling of leading slashes for external links by updating comments and removing redundant logic.
parent 55096c2a
No related branches found
No related tags found
1 merge request!265ISTWCMS-7266: Refactor link handling logic in UwCblLinks block.
This commit is part of merge request !265. Comments created here will be created in the context of that merge request.
...@@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; ...@@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Path\PathValidatorInterface; use Drupal\Core\Path\PathValidatorInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\media\Entity\Media; use Drupal\media\Entity\Media;
use Drupal\path_alias\AliasManager; use Drupal\path_alias\AliasManager;
use Drupal\uw_cfg_common\Service\UWService; use Drupal\uw_cfg_common\Service\UWService;
...@@ -946,12 +947,8 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface { ...@@ -946,12 +947,8 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface {
$link = substr_replace($link, '', 0, strlen('internal:')); $link = substr_replace($link, '', 0, strlen('internal:'));
} }
// Remove base path, this will be used to validate url. // In some cases, the external link is prefixed with a leading slash. This
if ($base_path && str_starts_with($link, $base_path)) { // removes that leading slash.
$link = substr_replace($link, '', 0, strlen($base_path));
}
// If an external link is detected, and it has a leading slash, remove it.
$updated = preg_replace(self::LEADING_SLASH_REPLACE_REGEX, '$1', $link); $updated = preg_replace(self::LEADING_SLASH_REPLACE_REGEX, '$1', $link);
// In case match is not found, unchanged value is returned. // In case match is not found, unchanged value is returned.
...@@ -960,7 +957,7 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface { ...@@ -960,7 +957,7 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface {
$link = $updated; $link = $updated;
} }
$path = $this->pathValidator->getUrlIfValidWithoutAccessCheck($link); $path = $this->getInternalUrlIfValid($link, $base_path);
// If node alias is used, modify it so a canonical link is used. // If node alias is used, modify it so a canonical link is used.
if ($path && !$path->isExternal()) { if ($path && !$path->isExternal()) {
...@@ -1045,4 +1042,34 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface { ...@@ -1045,4 +1042,34 @@ class UwCblLinks extends BlockBase implements ContainerFactoryPluginInterface {
return $title ?? ""; return $title ?? "";
} }
/**
* Validates and retrieves an internal URL if applicable.
*
* Checks if the provided URL starts with the specified base path
* and attempts to validate it as an internal path without access checks.
*
* @param string $url
* The URL to be validated.
* @param string $base_path
* The base path to check against.
*
* @return \Drupal\Core\Url|null
* The valid internal URL object if the URL is valid and matches the
* base path, or NULL otherwise.
*/
private function getInternalUrlIfValid(string $url, string $base_path): ?Url {
$link = $url;
// Remove base path, this will be used to validate url.
if ($base_path && str_starts_with($link, $base_path)) {
$link = substr_replace($link, '', 0, strlen($base_path));
}
if ($path = $this->pathValidator->getUrlIfValidWithoutAccessCheck($link)) {
return $path;
}
return NULL;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment