diff --git a/README.md b/README.md index 086b83d..4a45c6e 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,23 @@ adapter you built yourself cannot quietly keep one of its own: $encoder = new Encoder(probe: new MyProbe()); ``` +### Untrusted sources + +ffmpeg follows references inside some inputs: an HLS playlist or a concat list opens the files and +URLs it names. When sources come from users, limit the demuxers and protocols every input may use. +Both lists go before each input on the ffmpeg and ffprobe command lines; nested opens inherit them. + +```php +$formats = ['mov', 'matroska', 'avi', 'mpegts', 'ogg', 'wav', 'mp3', 'aac', 'flac']; + +$encoder = new Encoder( + new FFmpeg(protocols: ['file'], formats: $formats), + new FFprobe(protocols: ['file'], formats: $formats), +); +``` + +Use `['https', 'tls', 'tcp']` as the protocols for a source read from a URL. + ### How much a backend says Every adapter has a display level, defaulting to `Adapter::ERROR` — only what went wrong, because diff --git a/src/Video/Adapter.php b/src/Video/Adapter.php index 2284867..66d964d 100644 --- a/src/Video/Adapter.php +++ b/src/Video/Adapter.php @@ -105,6 +105,12 @@ abstract class Adapter protected readonly string $level; + /** @var list Protocols an input may be opened with; empty allows any. */ + protected readonly array $protocols; + + /** @var list Demuxers an input may be read with; empty allows any. */ + protected readonly array $formats; + /** * Who reads media details for this adapter. * @@ -131,19 +137,74 @@ abstract class Adapter /** * @param string|null $level One of the constants above; null keeps the default. + * @param list $protocols Protocols inputs may be opened with, nested + * opens included (`file`, or `https,tls,tcp` + * for a URL); empty allows any. + * @param list $formats Demuxers inputs may be read with (`mov`, + * `matroska`, ...); empty allows any. Leaving + * out playlist formats such as `hls` and + * `concat` stops a source from opening others. * * @throws Unsupported + * @throws Input */ public function __construct( ?string $binary = null, ?int $timeout = null, int $threads = 0, ?string $level = null, + array $protocols = [], + array $formats = [], ) { $this->binary = $binary ?? static::BINARY; $this->timeout = $timeout ?? static::TIMEOUT; $this->threads = $threads; $this->level = $level === null ? static::LEVEL : self::graded($level); + $this->protocols = self::names($protocols); + $this->formats = self::names($formats); + } + + /** + * Options that restrict how the next input is opened, placed before it. + * + * @return list + */ + protected function opens(): array + { + $args = []; + + if ($this->protocols !== []) { + $args[] = '-protocol_whitelist'; + $args[] = \implode(',', $this->protocols); + } + + if ($this->formats !== []) { + $args[] = '-format_whitelist'; + $args[] = \implode(',', $this->formats); + } + + return $args; + } + + /** + * @param array $names + * @return list + * + * @throws Input + */ + private static function names(array $names): array + { + $list = []; + + foreach ($names as $name) { + // Joined with commas into one argument, so a name must not carry one. + if (! \is_string($name) || \preg_match('/^[a-z0-9_]+$/', $name) !== 1) { + throw new Input('Invalid protocol or format name: '.\var_export($name, true)); + } + $list[] = $name; + } + + return \array_values(\array_unique($list)); } public function getName(): string diff --git a/src/Video/Adapter/FFmpeg.php b/src/Video/Adapter/FFmpeg.php index e2c6718..cb019d6 100644 --- a/src/Video/Adapter/FFmpeg.php +++ b/src/Video/Adapter/FFmpeg.php @@ -98,6 +98,7 @@ public function encode(string $path): string $video = $info->hasVideo && ($rep === null || $rep->scaled()); $args = $this->prefix(); + \array_push($args, ...$this->opens()); $args[] = '-i'; $args[] = $source; @@ -177,6 +178,7 @@ public function pack(string $dir): Package $args = $this->prefix(); foreach ($this->paths() as $input) { + \array_push($args, ...$this->opens()); $args[] = '-i'; $args[] = $input; } @@ -236,6 +238,7 @@ public function grab(string $path, string $output, ?Thumb $options = null): stri $args[] = self::number($at); } + \array_push($args, ...$this->opens()); $args[] = '-i'; $args[] = $path; $args[] = '-map'; @@ -296,6 +299,7 @@ public function subtitle(string $path, int $stream, string $output, ?int $timeou $this->directory(\dirname($output)); $args = $this->prefix(progress: false); + \array_push($args, ...$this->opens()); $args[] = '-i'; $args[] = $path; $args[] = '-map'; @@ -334,6 +338,7 @@ public function tile(string $path, string $dir, ?Tile $options = null): Spritesh $height += $height % 2; $args = $this->prefix(progress: false); + \array_push($args, ...$this->opens()); $args[] = '-i'; $args[] = $path; $args[] = '-fps_mode'; diff --git a/src/Video/Adapter/FFprobe.php b/src/Video/Adapter/FFprobe.php index 27525c9..7620c66 100644 --- a/src/Video/Adapter/FFprobe.php +++ b/src/Video/Adapter/FFprobe.php @@ -43,6 +43,7 @@ public function read(string $path): Info '-show_format', '-show_streams', '-show_chapters', + ...$this->opens(), $this->source($path), ]); diff --git a/tests/Video/E2E/WhitelistTest.php b/tests/Video/E2E/WhitelistTest.php new file mode 100644 index 0000000..73525f7 --- /dev/null +++ b/tests/Video/E2E/WhitelistTest.php @@ -0,0 +1,98 @@ +dir.'/segment.ts'; + + Process::run([ + 'ffmpeg', '-y', '-hide_banner', '-loglevel', 'error', + '-i', self::video(), '-c', 'copy', '-t', '2', $segment, + ], timeout: 60); + + $path = $this->dir.'/list.m3u8'; + \file_put_contents($path, "#EXTM3U\n#EXT-X-TARGETDURATION:2\n#EXTINF:2,\n".$segment."\n#EXT-X-ENDLIST\n"); + + return $path; + } + + public function testAnUnrestrictedProbeFollowsAPlaylist(): void + { + $this->assertTrue((new FFprobe())->read($this->playlist())->hasVideo); + } + + public function testARestrictedProbeRefusesAPlaylist(): void + { + $probe = new FFprobe(protocols: self::PROTOCOLS, formats: self::FORMATS); + + $this->expectException(Runtime::class); + $probe->read($this->playlist()); + } + + public function testARestrictedEncoderRefusesAPlaylist(): void + { + // The probe is left open so the refusal can only come from ffmpeg. + $encoder = new Encoder(new FFmpeg(protocols: self::PROTOCOLS, formats: self::FORMATS), new FFprobe()); + + $this->expectException(Runtime::class); + $encoder->grab($this->playlist(), $this->dir.'/poster.jpg'); + } + + public function testAProbeLimitedToOtherProtocolsRefusesALocalFile(): void + { + $probe = new FFprobe(protocols: ['https', 'tls', 'tcp']); + + $this->expectException(Runtime::class); + $probe->read(self::video()); + } + + public function testAnEncoderLimitedToOtherProtocolsRefusesALocalFile(): void + { + $encoder = new Encoder(new FFmpeg(protocols: ['https', 'tls', 'tcp']), new FFprobe()); + + $this->expectException(Runtime::class); + $encoder->grab(self::video(), $this->dir.'/poster.jpg'); + } + + public function testARestrictedEncoderStillEncodesAPlainContainer(): void + { + // The encode path builds its own command line; it must still run. + $encoder = new Encoder( + new FFmpeg(protocols: self::PROTOCOLS, formats: self::FORMATS), + new FFprobe(protocols: self::PROTOCOLS, formats: self::FORMATS), + ); + + $this->assertWritten($encoder->open(self::video())->encode($this->dir.'/out.mp4')); + } + + public function testARestrictedEncoderStillReadsAPlainContainer(): void + { + $encoder = new Encoder( + new FFmpeg(protocols: self::PROTOCOLS, formats: self::FORMATS), + new FFprobe(protocols: self::PROTOCOLS, formats: self::FORMATS), + ); + + $this->assertTrue($encoder->probe(self::video())->hasVideo); + $this->assertWritten($encoder->grab(self::video(), $this->dir.'/poster.jpg')); + } +}