Give your AI agent eyes to see the entire internet. Read & search Twitter, Reddit, YouTube, GitHub, Bilibili, XiaoHongShu — one CLI, zero API fees.
Python agent-infrastructureai-agentai-searchautomationbilibiliclaude-codeclicursorfree-apillm-toolsmcppythonreddit-scrapertwitter-scraperweb-scraperxiaohongshuyoutube-transcript
security(transcribe): bound media processing
Pnant77d1abe482de
diff --git a/agent_reach/transcribe.py b/agent_reach/transcribe.pyindex 0344a05e..a5a28c8c 100644--- a/agent_reach/transcribe.py+++ b/agent_reach/transcribe.py@@ -28,6 +28,9 @@ from agent_reach.config import Config# Whisper API limit is 25MB; leave headroom for multipart overhead.SIZE_LIMIT_BYTES = 24 * 1024 * 1024CHUNK_SECONDS = 600 # 10 min — small enough that boundary cuts rarely lose meaning+MAX_SOURCE_BYTES = 512 * 1024 * 1024+MAX_CHUNKS = 24 # 4 hours at the standard 10-minute segment size+MAX_TOTAL_CHUNK_BYTES = 96 * 1024 * 1024PROVIDERS = {"groq": {@@ -66,6 +69,15 @@ def _require(binary: str) -> None:raise MissingDependency(f"{binary} not found in PATH")+def _require_size_at_most(path: Path, limit: int, label: str) -> int:+ """Return file size or fail before expensive downstream processing."""+ size = path.stat().st_size+ if size > limit:+ limit_mib = limit / (1024 * 1024)+ raise TranscribeError(f"{label} exceeds safety limit of {limit_mib:g} MiB")+ return size++def _run(cmd: List[str], timeout: int = 600) -> None:"""Run a subprocess, raising TranscribeError on nonzero exit or timeout.@@ -135,6 +147,9 @@ def download_audio(url: str, out_dir: Path) -> Path:"m4a","--audio-quality","0",+ "--no-playlist",+ "--max-filesize",+ str(MAX_SOURCE_BYTES),"-o",str(template),"--",@@ -144,8 +159,13 @@ def download_audio(url: str, out_dir: Path) -> Path:)files = sorted(out_dir.glob("source.*"))if not files:- raise TranscribeError("yt-dlp produced no output file")- return files[0]+ limit_mib = MAX_SOURCE_BYTES // (1024 * 1024)+ raise TranscribeError(+ f"yt-dlp produced no output file (source may exceed {limit_mib} MiB limit)"+ )+ audio = files[0]+ _require_size_at_most(audio, MAX_SOURCE_BYTES, "downloaded source")+ return audiodef compress_audio(src: Path, out_dir: Path) -> Path:@@ -290,12 +310,31 @@ def _transcribe_in_dir(source: str, order: List[str], cfg: Config, work_dir: Patelse:audio = download_audio(source, work_dir)+ _require_size_at_most(audio, MAX_SOURCE_BYTES, "source")compressed = compress_audio(audio, work_dir)if compressed.stat().st_size <= SIZE_LIMIT_BYTES:chunks = [compressed]else:chunks = chunk_audio(compressed, work_dir)+ if len(chunks) > MAX_CHUNKS:+ max_minutes = MAX_CHUNKS * CHUNK_SECONDS // 60+ raise TranscribeError(+ f"audio produced {len(chunks)} chunks; safety limit is "+ f"{MAX_CHUNKS} (~{max_minutes} minutes)"+ )+ chunk_sizes = [+ _require_size_at_most(chunk, SIZE_LIMIT_BYTES, f"chunk {chunk.name}")+ for chunk in chunks+ ]+ total_chunk_bytes = sum(chunk_sizes)+ if total_chunk_bytes > MAX_TOTAL_CHUNK_BYTES:+ limit_mib = MAX_TOTAL_CHUNK_BYTES / (1024 * 1024)+ raise TranscribeError(+ f"audio chunks total {total_chunk_bytes} bytes; "+ f"safety limit is {limit_mib:g} MiB"+ )+pieces: List[str] = []for chunk in chunks:text = _transcribe_with_fallback(chunk, order, cfg)diff --git a/tests/test_transcribe.py b/tests/test_transcribe.pyindex 748a8011..d4b43ff3 100644--- a/tests/test_transcribe.py+++ b/tests/test_transcribe.py@@ -161,6 +161,22 @@ class TestFallback:class TestOrchestrator:+ def test_rejects_oversized_source_before_compression(+ self, monkeypatch, fake_config, chunk_file+ ):+ fake_config.set("groq_api_key", "gsk_test")+ monkeypatch.setattr(tr, "MAX_SOURCE_BYTES", 4)+ monkeypatch.setattr(+ tr,+ "compress_audio",+ lambda *_args, **_kwargs: (_ for _ in ()).throw(+ AssertionError("oversized source must fail before ffmpeg")+ ),+ )++ with pytest.raises(tr.TranscribeError, match="source.*limit"):+ tr.transcribe(str(chunk_file), config=fake_config)+def test_local_file_skips_yt_dlp(self, monkeypatch, fake_config, tmp_path, chunk_file):fake_config.set("groq_api_key", "gsk_test")@@ -217,6 +233,62 @@ class TestOrchestrator:)assert text == "part one\npart two"+ def test_rejects_too_many_chunks_before_any_provider_call(+ self, monkeypatch, fake_config, tmp_path, chunk_file+ ):+ fake_config.set("groq_api_key", "gsk_test")+ monkeypatch.setattr(tr, "SIZE_LIMIT_BYTES", 1)+ compressed = tmp_path / "compressed.m4a"+ compressed.write_bytes(b"xx")+ monkeypatch.setattr(tr, "compress_audio", lambda *_args: compressed)++ chunks = []+ for index in range(tr.MAX_CHUNKS + 1):+ chunk = tmp_path / f"chunk_{index:03d}.m4a"+ chunk.write_bytes(b"x")+ chunks.append(chunk)+ monkeypatch.setattr(tr, "chunk_audio", lambda *_args: chunks)++ provider_calls = []+ monkeypatch.setattr(+ tr,+ "_transcribe_with_fallback",+ lambda *_args: provider_calls.append("called") or "text",+ )++ with pytest.raises(tr.TranscribeError, match="chunks.*limit"):+ tr.transcribe(str(chunk_file), out_dir=tmp_path / "work", config=fake_config)++ assert provider_calls == []++ def test_rejects_excessive_total_chunk_bytes_before_provider_calls(+ self, monkeypatch, fake_config, tmp_path, chunk_file+ ):+ fake_config.set("groq_api_key", "gsk_test")+ monkeypatch.setattr(tr, "SIZE_LIMIT_BYTES", 10)+ monkeypatch.setattr(tr, "MAX_TOTAL_CHUNK_BYTES", 5)+ compressed = tmp_path / "compressed.m4a"+ compressed.write_bytes(b"x" * 11)+ monkeypatch.setattr(tr, "compress_audio", lambda *_args: compressed)++ first = tmp_path / "chunk_000.m4a"+ second = tmp_path / "chunk_001.m4a"+ first.write_bytes(b"aaa")+ second.write_bytes(b"bbb")+ monkeypatch.setattr(tr, "chunk_audio", lambda *_args: [first, second])++ provider_calls = []+ monkeypatch.setattr(+ tr,+ "_transcribe_with_fallback",+ lambda *_args: provider_calls.append("called") or "text",+ )++ with pytest.raises(tr.TranscribeError, match="total.*limit"):+ tr.transcribe(str(chunk_file), out_dir=tmp_path / "work", config=fake_config)++ assert provider_calls == []+def test_no_provider_configured_fails_fast(self, fake_config, chunk_file):with pytest.raises(tr.NoProviderConfigured):tr.transcribe(str(chunk_file), config=fake_config)@@ -298,6 +370,20 @@ class TestOrchestrator:class TestDownloadAudioSafety:+ def test_rejects_download_that_exceeds_limit(+ self, monkeypatch, tmp_path+ ):+ monkeypatch.setattr(tr, "_require", lambda _binary: None)+ monkeypatch.setattr(tr, "MAX_SOURCE_BYTES", 4)++ def fake_run(_cmd, timeout=600):+ (tmp_path / "source.m4a").write_bytes(b"audio")++ monkeypatch.setattr(tr, "_run", fake_run)++ with pytest.raises(tr.TranscribeError, match="downloaded source.*limit"):+ tr.download_audio("https://example.com/watch?v=123", tmp_path)+def test_rejects_private_network_url_before_yt_dlp(self, monkeypatch, tmp_path):monkeypatch.setattr(tr, "_require", lambda binary: None)@@ -323,8 +409,11 @@ class TestDownloadAudioSafety:assert audio == tmp_path / "source.m4a"assert "--" in captured["cmd"]+ assert "--no-playlist" in captured["cmd"]marker_index = captured["cmd"].index("--")assert captured["cmd"][marker_index + 1] == "https://example.com/watch?v=123"+ max_size_index = captured["cmd"].index("--max-filesize")+ assert captured["cmd"][max_size_index + 1] == str(tr.MAX_SOURCE_BYTES)def test_preserves_bare_public_urls_supported_by_yt_dlp(self, monkeypatch, tmp_path):monkeypatch.setattr(tr, "_require", lambda binary: None)