AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): add SSRF protection and download limits to VideoDownloadBlock (#12702)
Nicholas Tindle6f1f4aaa5bd2
diff --git a/autogpt_platform/backend/backend/blocks/video/download.py b/autogpt_platform/backend/backend/blocks/video/download.pyindex c6d2617f73..b6f87726f3 100644--- a/autogpt_platform/backend/backend/blocks/video/download.py+++ b/autogpt_platform/backend/backend/blocks/video/download.py@@ -19,7 +19,13 @@ from backend.blocks._base import (from backend.data.execution import ExecutionContextfrom backend.data.model import SchemaFieldfrom backend.util.exceptions import BlockExecutionError-from backend.util.file import MediaFileType, get_exec_file_path, store_media_file+from backend.util.file import (+ MAX_FILE_SIZE_BYTES,+ MediaFileType,+ get_exec_file_path,+ store_media_file,+)+from backend.util.request import validate_url_hostclass VideoDownloadBlock(Block):@@ -64,6 +70,7 @@ class VideoDownloadBlock(Block):("source_url", str),],test_mock={+ "validate_url": lambda *args, **kwargs: None,"_download_video": lambda *args: ("video.mp4",212.0,@@ -73,6 +80,17 @@ class VideoDownloadBlock(Block):},)+ async def validate_url(self, url: str) -> None:+ """Validate URL for SSRF protection. Raises ValueError if blocked.++ NOTE: this is best-effort. yt-dlp resolves DNS and follows redirects with+ its own HTTP client, so a public URL that redirects to a blocked target+ (or a rebound DNS record) is not caught here. Closing that gap requires+ pinning connections to the resolved IPs the way `backend.util.request`+ does, or sandboxing yt-dlp — which is why this block stays `disabled`.+ """+ await validate_url_host(url)+async def _store_output_video(self, execution_context: ExecutionContext, file: MediaFileType) -> MediaFileType:@@ -112,6 +130,11 @@ class VideoDownloadBlock(Block):"merge_output_format": output_format,"quiet": True,"no_warnings": True,+ # Advisory: yt-dlp applies this per-stream and only when the format+ # advertises a size, so DASH/HLS and merged (video+audio) downloads can+ # still exceed it. The post-download check below is the real backstop.+ "max_filesize": MAX_FILE_SIZE_BYTES,+ "noplaylist": True,}with yt_dlp.YoutubeDL(ydl_opts) as ydl:@@ -122,6 +145,17 @@ class VideoDownloadBlock(Block):if not video_path.endswith(f".{output_format}"):video_path = video_path.rsplit(".", 1)[0] + f".{output_format}"+ # Enforce the size limit on the file actually produced, before it is+ # read into memory by store_media_file().+ if os.path.exists(video_path):+ actual_size = os.path.getsize(video_path)+ if actual_size > MAX_FILE_SIZE_BYTES:+ os.remove(video_path)+ raise ValueError(+ f"Downloaded video is {actual_size} bytes, "+ f"which exceeds the {MAX_FILE_SIZE_BYTES} byte limit"+ )+# Return just the filename, not the full pathfilename = os.path.basename(video_path)@@ -142,6 +176,16 @@ class VideoDownloadBlock(Block):try:assert execution_context.graph_exec_id is not None+ # Validate URL before yt-dlp touches it (SSRF protection)+ try:+ await self.validate_url(input_data.url)+ except ValueError as e:+ raise BlockExecutionError(+ message=f"URL validation failed: {e}",+ block_name=self.name,+ block_id=str(self.id),+ ) from e+# Get the exec file directoryoutput_dir = get_exec_file_path(execution_context.graph_exec_id, "")os.makedirs(output_dir, exist_ok=True)@@ -164,6 +208,8 @@ class VideoDownloadBlock(Block):yield "title", titleyield "source_url", input_data.url+ except BlockExecutionError:+ raiseexcept Exception as e:raise BlockExecutionError(message=f"Failed to download video: {e}",diff --git a/autogpt_platform/backend/backend/blocks/video/download_test.py b/autogpt_platform/backend/backend/blocks/video/download_test.pynew file mode 100644index 0000000000..12530ff9d8--- /dev/null+++ b/autogpt_platform/backend/backend/blocks/video/download_test.py@@ -0,0 +1,159 @@+"""Tests for VideoDownloadBlock: SSRF protection, download size limits,+and playlist restriction (SECRT-1898)."""++import os+from pathlib import Path+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.blocks.video.download import VideoDownloadBlock+from backend.data.execution import ExecutionContext+from backend.util.exceptions import BlockExecutionError+from backend.util.file import MAX_FILE_SIZE_BYTES+++def _make_execution_context() -> ExecutionContext:+ return ExecutionContext(+ user_id="test-user",+ graph_exec_id="test-graph-exec-id",+ graph_id="test-graph-id",+ )+++async def _collect_outputs(+ block: VideoDownloadBlock,+ input_data: VideoDownloadBlock.Input,+ exec_ctx: ExecutionContext,+ node_exec_id: str = "node-1",+) -> dict:+ outputs: dict = {}+ async for name, value in block.run(+ input_data, execution_context=exec_ctx, node_exec_id=node_exec_id+ ):+ outputs[name] = value+ return outputs+++class TestSSRFProtection:+ """SSRF protection: blocked URLs must be rejected before reaching yt-dlp."""++ @pytest.mark.parametrize(+ "url",+ [+ "file:///etc/passwd",+ "http://127.0.0.1/video.mp4",+ "http://10.0.0.1/video.mp4",+ "http://169.254.169.254/latest/meta-data/",+ "http://172.16.0.1/video.mp4",+ "http://192.168.1.1/video.mp4",+ "http://[::1]/video.mp4",+ ],+ ids=[+ "file_protocol",+ "loopback_127",+ "private_10",+ "cloud_metadata",+ "private_172",+ "private_192",+ "ipv6_loopback",+ ],+ )+ async def test_blocked_urls_rejected(self, url: str):+ block = VideoDownloadBlock()+ block.validate_url = AsyncMock( # type: ignore[method-assign]+ side_effect=ValueError(f"Blocked: {url}")+ )+ input_data = VideoDownloadBlock.Input(url=url, quality="480p")++ with patch("backend.blocks.video.download.os.makedirs"):+ with pytest.raises(BlockExecutionError, match="URL validation failed"):+ await _collect_outputs(block, input_data, _make_execution_context())+++def ydl_opts(tmp_path: Path):+ """Run _download_video against a mocked yt-dlp and return the options it used."""+ with patch("backend.blocks.video.download.yt_dlp.YoutubeDL") as mock_cls:+ mock_ydl = MagicMock()+ mock_ydl.__enter__ = MagicMock(return_value=mock_ydl)+ mock_ydl.__exit__ = MagicMock(return_value=False)+ mock_ydl.extract_info.return_value = {"duration": 10, "title": "t"}+ mock_ydl.prepare_filename.return_value = os.path.join(str(tmp_path), "test.mp4")+ mock_cls.return_value = mock_ydl++ VideoDownloadBlock()._download_video(+ "https://example.com/v.mp4", "720p", "mp4", str(tmp_path), "n1"+ )++ yield mock_cls.call_args[0][0]+++class TestDownloadSizeLimit:+ """yt-dlp must enforce max_filesize during download, not just after."""++ def test_max_filesize_in_ydl_opts(self, ydl_opts: dict):+ assert ydl_opts["max_filesize"] == MAX_FILE_SIZE_BYTES++ def test_oversized_download_is_rejected_and_deleted(self, tmp_path: Path):+ """max_filesize is advisory for DASH/HLS and merged formats, so the+ produced file is re-checked and discarded when it exceeds the limit."""+ video_path = tmp_path / "test.mp4"+ video_path.write_bytes(b"x" * 32)++ with patch("backend.blocks.video.download.yt_dlp.YoutubeDL") as mock_cls:+ mock_ydl = MagicMock()+ mock_ydl.__enter__ = MagicMock(return_value=mock_ydl)+ mock_ydl.__exit__ = MagicMock(return_value=False)+ mock_ydl.extract_info.return_value = {"duration": 10, "title": "t"}+ mock_ydl.prepare_filename.return_value = str(video_path)+ mock_cls.return_value = mock_ydl++ with patch("backend.blocks.video.download.MAX_FILE_SIZE_BYTES", 16):+ with pytest.raises(ValueError, match="exceeds"):+ VideoDownloadBlock()._download_video(+ "https://example.com/v.mp4", "720p", "mp4", str(tmp_path), "n1"+ )++ assert not video_path.exists()+++class TestPlaylistRestriction:+ """yt-dlp must not download playlists — only the single requested video."""++ def test_noplaylist_in_ydl_opts(self, ydl_opts: dict):+ assert ydl_opts.get("noplaylist") is True+++class TestValidURLAccepted:+ """A valid public URL should pass validation and yield all outputs."""++ async def test_valid_public_url_produces_output(self):+ block = VideoDownloadBlock()+ block.validate_url = AsyncMock(return_value=None) # type: ignore[method-assign]+ block._download_video = MagicMock( # type: ignore[method-assign]+ return_value=("video.mp4", 120.0, "Test Video")+ )+ block._store_output_video = AsyncMock(return_value="video.mp4") # type: ignore[method-assign]++ input_data = VideoDownloadBlock.Input(+ url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",+ quality="480p",+ )++ with patch("backend.blocks.video.download.os.makedirs"):+ with patch(+ "backend.blocks.video.download.get_exec_file_path",+ return_value="/tmp/exec",+ ):+ outputs = await _collect_outputs(+ block, input_data, _make_execution_context()+ )++ block.validate_url.assert_called_once_with(+ "https://www.youtube.com/watch?v=dQw4w9WgXcQ"+ )+ assert outputs["video_file"] == "video.mp4"+ assert outputs["duration"] == 120.0+ assert outputs["title"] == "Test Video"+ assert outputs["source_url"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ"diff --git a/autogpt_platform/backend/backend/util/file.py b/autogpt_platform/backend/backend/util/file.pyindex 23e6dcea8e..c8e58ee1d6 100644--- a/autogpt_platform/backend/backend/util/file.py+++ b/autogpt_platform/backend/backend/util/file.py@@ -16,6 +16,8 @@ from backend.util.settings import Configfrom backend.util.type import MediaFileTypefrom backend.util.virus_scanner import scan_content_safe+MAX_FILE_SIZE_BYTES = Config().max_file_size_mb * 1024 * 1024+if TYPE_CHECKING:from backend.data.execution import ExecutionContext@@ -165,7 +167,6 @@ async def store_media_file(base_path.mkdir(parents=True, exist_ok=True)# Security fix: Add disk space limits to prevent DoS- MAX_FILE_SIZE_BYTES = Config().max_file_size_mb * 1024 * 1024MAX_TOTAL_DISK_USAGE = 1024 * 1024 * 1024 # 1GB total per execution directory# Check total disk usage in base_path