Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Service/VideoManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class VideoManager
{
public const VIDEO_MIMETYPES = ['video/mp4', 'video/webm'];
public const UNSUPPORTED_STREAM_EXTENSIONS = ['m3u8', 'm3u', 'mpd', 'ism', 'isml'];

public static function isVideoUrl(string $url): bool
{
Expand All @@ -22,6 +23,18 @@ public static function isVideoUrl(string $url): bool
return \in_array($urlExt, $types, false);
}

public static function isUnsupportedStreamUrl(string $url): bool
{
if (str_starts_with($url, 'data:')) {
return false;
}

$path = (string) parse_url($url, PHP_URL_PATH);
$urlExt = strtolower(pathinfo($path, PATHINFO_EXTENSION));

return \in_array($urlExt, self::UNSUPPORTED_STREAM_EXTENSIONS, true);
}

public static function isSupportedVideoMimeType(?string $mimeType): bool
{
if (null === $mimeType || '' === trim($mimeType)) {
Expand Down
15 changes: 15 additions & 0 deletions src/Utils/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ private function cleanIframe(?string $html): ?string
libxml_clear_errors();

$videoElements = $dom->getElementsByTagName('video');
$iframeElements = $dom->getElementsByTagName('iframe');

foreach ($videoElements as $videoElement) {
$sourceUrl = $videoElement->getAttribute('src');
Expand Down Expand Up @@ -186,6 +187,20 @@ private function cleanIframe(?string $html): ?string
}
}

foreach ($iframeElements as $iframeElement) {
$iframeSrc = $iframeElement->getAttribute('src');

if (
$iframeSrc
&& (
VideoManager::isVideoUrl($iframeSrc)
|| VideoManager::isUnsupportedStreamUrl($iframeSrc)
)
) {
return null;
}
}

return $html;
}

Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Utils/EmbedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,36 @@ public function testUnsupportedVideoContentWithOtherMarkupIsRejected(): void
self::assertNull($method->invoke($embed, $html));
}

public function testUnsupportedIframeStreamUrlIsRejected(): void
{
$embed = $this->createEmbed();
$method = new \ReflectionMethod(Embed::class, 'cleanIframe');

$html = '<iframe src="https://www.cbsnews.com/mediaplayer/video/master.m3u8" allowfullscreen></iframe>';

self::assertNull($method->invoke($embed, $html));
}

public function testIframeVideoUrlIsRejected(): void
{
$embed = $this->createEmbed();
$method = new \ReflectionMethod(Embed::class, 'cleanIframe');

$html = '<iframe src="https://example.com/video.mp4" allowfullscreen></iframe>';

self::assertNull($method->invoke($embed, $html));
}

public function testSupportedIframeEmbedUrlIsKept(): void
{
$embed = $this->createEmbed();
$method = new \ReflectionMethod(Embed::class, 'cleanIframe');

$html = '<iframe src="https://www.youtube.com/embed/abc123" allowfullscreen></iframe>';

self::assertSame($html, $method->invoke($embed, $html));
}

private function createEmbed(): Embed
{
return new Embed(
Expand Down
Loading