Sanatize remaining invalid URL characters somehow returned by InfoProviders

This fixes issue #521
This commit is contained in:
Jan Böhmer 2024-02-24 23:55:31 +01:00
parent 4b7d200aa2
commit 12e9497ccf
2 changed files with 64 additions and 2 deletions

View file

@ -29,14 +29,24 @@ namespace App\Services\InfoProviderSystem\DTOs;
*/
class FileDTO
{
/**
* @var string The URL where to get this file
*/
public readonly string $url;
/**
* @param string $url The URL where to get this file
* @param string|null $name Optionally the name of this file
*/
public function __construct(
public readonly string $url,
string $url,
public readonly ?string $name = null,
) {}
) {
//Find all occurrences of non URL safe characters and replace them with their URL encoded version.
//We only want to replace characters which can not have a valid meaning in a URL (what would break the URL).
//Digikey provided some wrong URLs with a ^ in them, which is not a valid URL character. (https://github.com/Part-DB/Part-DB-server/issues/521)
$this->url = preg_replace_callback('/[^a-zA-Z0-9_\-.$+!*();\/?:@=&#%]/', fn($matches) => urlencode($matches[0]), $url);
}
}