Merge pull request #13696 from annando/invalid-uri

Fix exception, when a provided URI is invalid
This commit is contained in:
Hypolite Petovan 2023-12-06 12:46:12 -05:00 committed by GitHub
commit e0686ac1d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 7 deletions

View File

@ -25,6 +25,7 @@ use Friendica\BaseFactory;
use Friendica\Capabilities\ICanCreateFromTableRow;
use Friendica\Content\Post\Entity;
use Friendica\Network;
use Friendica\Util\Network as UtilNetwork;
use GuzzleHttp\Psr7\Uri;
use Psr\Log\LoggerInterface;
use stdClass;
@ -48,24 +49,24 @@ class PostMedia extends BaseFactory implements ICanCreateFromTableRow
{
return new Entity\PostMedia(
$row['uri-id'],
$row['url'] ? new Uri($row['url']) : null,
UtilNetwork::createUriFromString($row['url']),
$row['type'],
$this->mimeTypeFactory->createFromContentType($row['mimetype']),
$row['media-uri-id'],
$row['width'],
$row['height'],
$row['size'],
$row['preview'] ? new Uri($row['preview']) : null,
UtilNetwork::createUriFromString($row['preview']),
$row['preview-width'],
$row['preview-height'],
$row['description'],
$row['name'],
$row['author-url'] ? new Uri($row['author-url']) : null,
UtilNetwork::createUriFromString($row['author-url']),
$row['author-name'],
$row['author-image'] ? new Uri($row['author-image']) : null,
$row['publisher-url'] ? new Uri($row['publisher-url']) : null,
UtilNetwork::createUriFromString($row['author-image']),
UtilNetwork::createUriFromString($row['publisher-url']),
$row['publisher-name'],
$row['publisher-image'] ? new Uri($row['publisher-image']) : null,
UtilNetwork::createUriFromString($row['publisher-image']),
$row['blurhash'],
$row['id']
);

View File

@ -45,7 +45,11 @@ class PostMedia extends BaseRepository
$Entities = new Collection\PostMedias();
foreach ($rows as $fields) {
$Entities[] = $this->factory->createFromTableRow($fields);
try {
$Entities[] = $this->factory->createFromTableRow($fields);
} catch (\Exception $e) {
$this->logger->warning('Invalid media row', ['code' => $e->getCode(), 'message' => $e->getMessage(), 'fields' => $fields]);
}
}
return $Entities;

View File

@ -658,4 +658,24 @@ class Network
$scheme = parse_url($url, PHP_URL_SCHEME);
return !empty($scheme) && in_array($scheme, ['http', 'https']) && parse_url($url, PHP_URL_HOST);
}
/**
* Creates an Uri object out of a given Uri string
*
* @param string|null $uri
* @return UriInterface|null
*/
public static function createUriFromString(string $uri = null): ?UriInterface
{
if (empty($uri)) {
return null;
}
try {
return new Uri($uri);
} catch (\Exception $e) {
Logger::debug('Invalid URI', ['code' => $e->getCode(), 'message' => $e->getMessage(), 'uri' => $uri]);
return null;
}
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Test\src\Util;
use Friendica\Util\Network;
use PHPUnit\Framework\TestCase;
/**
* Network utility test class
*/
class NetworkTest extends TestCase
{
public function testValidUri()
{
self::assertNotNull(Network::createUriFromString('https://friendi.ca'));
self::assertNotNull(Network::createUriFromString('magnet:?xs=https%3A%2F%2Ftube.jeena.net%2Flazy-static%2Ftorrents%2F04bec7a8-34de-4847-b080-6ee00c4b3d49-1080-hls.torrent&xt=urn:btih:5def5a24dfa7307e999a0d4f0fcc29c3e2b13be2&dn=My+fediverse+setup+-+I+host+everything+myself&tr=https%3A%2F%2Ftube.jeena.net%2Ftracker%2Fannounce&tr=wss%3A%2F%2Ftube.jeena.net%3A443%2Ftracker%2Fsocket&ws=https%3A%2F%2Ftube.jeena.net%2Fstatic%2Fstreaming-playlists%2Fhls%2F23989f41-e230-4dbf-9111-936bc730bf50%2Fe5905de3-e488-4bb8-a1e8-eb7a53ac24ad-1080-fragmented.mp4'));
self::assertNotNull(Network::createUriFromString('did:plc:geqiabvo4b4jnfv2paplzcge'));
self::assertNull(Network::createUriFromString('https://'));
self::assertNull(Network::createUriFromString(''));
self::assertNull(Network::createUriFromString(null));
}
}