From d780224836c735e18a094f940f5933bc5fb16700 Mon Sep 17 00:00:00 2001 From: Serhii Maistrenko Date: Thu, 30 Jul 2026 13:48:46 +0300 Subject: [PATCH] update uri validation --- .../ConvertUrlElementFinderModifier.php | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Helper/ConvertUrlElementFinderModifier.php b/src/Helper/ConvertUrlElementFinderModifier.php index 2b7cc6d..53ce4ee 100644 --- a/src/Helper/ConvertUrlElementFinderModifier.php +++ b/src/Helper/ConvertUrlElementFinderModifier.php @@ -8,6 +8,7 @@ use DOMNodeList; use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\UriResolver; +use InvalidArgumentException; use Xparse\ElementFinder\DomNodeListAction\DomNodeListActionInterface; /** @@ -28,17 +29,17 @@ final public function execute(DOMNodeList $nodeList): void foreach ($nodeList as $element) { assert($element instanceof DOMElement); $attribute = $this->attributeName($element); - $relative = $element->getAttribute($attribute); - $isValid = parse_url($relative) !== false; - if ( - $isValid - && - ! preg_match('!^\s*javascript\s*:\s*!', $relative) - ) { - if ($this->baseUrl !== '' && ! preg_match('!^(/|http)!i', $relative)) { - $relative = UriResolver::resolve(new Uri($this->baseUrl), new Uri($relative))->__toString(); + $relative = trim($element->getAttribute($attribute)); + try { + $relativeUri = new Uri($relative); + } catch (InvalidArgumentException) { + continue; + } + if (! preg_match('!^\s*javascript\s*:\s*!', $relative)) { + if ($this->baseUrl !== '' && ! preg_match('!^(/|http)!i', $relativeUri->getScheme())) { + $relativeUri = UriResolver::resolve(new Uri($this->baseUrl), $relativeUri); } - $url = UriResolver::resolve($affected, new Uri($relative))->__toString(); + $url = UriResolver::resolve($affected, $relativeUri)->__toString(); $element->setAttribute($attribute, $url); } }