Skip to content

Commit

Permalink
UHF-10891: Better url validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tuutti committed Oct 28, 2024
1 parent 065eab6 commit 6da0b03
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions src/Drush/Commands/TransliterateFilesCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ private function processEntityType(string $entityType, array $fields) : void {
}
}

/**
* Checks if the given link is valid.
*
* @param string $url
* The URL.
*
* @return bool
* TRUE if link is valid, FALSE if not.
*/
private function isValidLink(string $url) : bool {
$validLinks = [
'blob.core.windows.net',
'/sites/default/files/',
];

return (bool) array_filter($validLinks, fn ($link) => str_contains($url, $link));
}

/**
* Checks if the given remote file exists.
*
Expand All @@ -112,34 +130,11 @@ private function processEntityType(string $entityType, array $fields) : void {
* TRUE if remote file exists, FALSE if not.
*/
private function remoteFileExists(string $url) : bool {
// Skip wps since it seems to require a VPN.
if (str_contains('https://www.hel.fi/wps/', $url)) {
return TRUE;
}

try {
$this->httpClient->request('HEAD', $url, ['timeout' => 15]);

return TRUE;
}
catch (ClientException $e) {
$response = $e->getResponse();

// Skip non-404 responses.
if ($response->getStatusCode() !== 404) {
return TRUE;
}
$skip = [
'text/html',
'text/plain',
];
foreach ($skip as $type) {
// Skip html content.
if (str_contains($response->getHeaderLine('Content-Type'), $type)) {
return TRUE;
}
}
}
catch (GuzzleException) {
}
return FALSE;
Expand Down Expand Up @@ -168,8 +163,8 @@ private function processFieldLinks(ContentEntityInterface $entity, string $field
}
$href = trim($href);

// Do nothing if file exists already.
if ($this->remoteFileExists($href)) {
// Skip invalid links or links that does not result in 404 error.
if (!$this->isValidLink($href) || $this->remoteFileExists($href)) {
continue;
}
$this->io()->note(sprintf('Found a broken link "%s"', $href));
Expand Down

0 comments on commit 6da0b03

Please sign in to comment.