Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: refuse playlists at the entry of the remaining upload decoders (#14755)
Daniel Han9e8c2deacb8d
diff --git a/studio/backend/core/inference/llama_video_input.py b/studio/backend/core/inference/llama_video_input.pyindex 5b6a5d3e99c..fe4fe76d97a 100644--- a/studio/backend/core/inference/llama_video_input.py+++ b/studio/backend/core/inference/llama_video_input.py@@ -200,18 +200,18 @@ def shrink_video_for_llama(``sampled_fps`` raises the rate ceiling. Keeps only video and the timing of everyreal frame. Missing tools, failures and an over-``max_bytes`` result pass through."""- ffmpeg = shutil.which("ffmpeg")- ffprobe = shutil.which("ffprobe")- if not ffmpeg or not ffprobe:- return video_b64+ from utils.media_playlist import refuse_playlist+try:raw = base64.b64decode(video_b64)except (binascii.Error, ValueError):return video_b64- from utils.media_playlist import refuse_playlist-- # `-protocol_whitelist file` still lets a playlist read local files: refuse one outright.+ # Before the tool check: without ffmpeg the clip still reaches llama-server's own FFmpeg.refuse_playlist(raw)+ ffmpeg = shutil.which("ffmpeg")+ ffprobe = shutil.which("ffprobe")+ if not ffmpeg or not ffprobe:+ return video_b64try:with tempfile.TemporaryDirectory(prefix = "unsloth-video-") as tmp:source = Path(tmp) / "clip"diff --git a/studio/backend/core/inference/mlx_inference.py b/studio/backend/core/inference/mlx_inference.pyindex c27dda451cd..86b7ff14912 100644--- a/studio/backend/core/inference/mlx_inference.py+++ b/studio/backend/core/inference/mlx_inference.py@@ -1579,7 +1579,11 @@ def _write_video_clip(video_b64: str) -> str:import base64import tempfile+ from utils.media_playlist import refuse_playlist+data = base64.b64decode(video_b64)+ # cv2.VideoCapture and mlx-vlm both open this through FFmpeg, which follows playlist entries.+ refuse_playlist(data)with tempfile.NamedTemporaryFile(prefix = "unsloth-video-", delete = False) as handle:handle.write(data)return handle.namediff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex f72de9da3c3..9506fa54a83 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -28201,9 +28201,12 @@ def _decode_audio_base64(b64: str) -> "np.ndarray":import torchaudioimport tempfileimport os+ from utils.media_playlist import refuse_playlistfrom utils.paths import ensure_dir, tmp_rootraw = base64.b64decode(b64)+ # torchaudio and torchcodec open the file through FFmpeg, which follows playlist entries.+ refuse_playlist(raw)# torchaudio.load needs a path or file-like with a format hint; write a# temp file so it can auto-detect the format.with tempfile.NamedTemporaryFile(@@ -28974,6 +28977,10 @@ def _decode_audio_mono(raw: bytes) -> "tuple[np.ndarray, int]":for containers including m4a/webm/WMA/AMR. librosa remains the final fallbackin full ML environments, but is absent from no-torch GGUF-only installs."""+ from utils.media_playlist import refuse_playlist++ # Checked once up front so no decoder below, the librosa fallback included, sees a playlist.+ refuse_playlist(raw)try:arr, sr = _decode_audio_mono_with_soundfile(raw)except _DecodedAudioTooLongError:@@ -28993,11 +29000,8 @@ def _decode_audio_mono(raw: bytes) -> "tuple[np.ndarray, int]":) from av_errorimport osimport tempfile- from utils.media_playlist import refuse_playlistfrom utils.paths import ensure_dir, tmp_root- # librosa may hand this to unrestricted FFmpeg, which follows playlist entries.- refuse_playlist(raw)with tempfile.NamedTemporaryFile(suffix = ".audio",delete = False,diff --git a/studio/backend/tests/test_media_decode_playlist_entry.py b/studio/backend/tests/test_media_decode_playlist_entry.pynew file mode 100644index 00000000000..6a2627d980d--- /dev/null+++ b/studio/backend/tests/test_media_decode_playlist_entry.py@@ -0,0 +1,196 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Every upload decoder refuses an HLS or DASH playlist before any decoder sees the bytes."""++from __future__ import annotations++import base64+import os+import sys+import types++import pytest++np = pytest.importorskip("numpy")+torch = pytest.importorskip("torch")++import routes.inference as inference_route+from core.inference import llama_video_input+from utils.media_playlist import PlaylistRefused++HLS = b"#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-TARGETDURATION:1\n"+DASH = (+ b'<?xml version="1.0"?>\n<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" '+ b'profiles="urn:mpeg:dash:profile:isoff-on-demand:2011"><Period/></MPD>\n'+)+PLAYLISTS = pytest.mark.parametrize("payload", [HLS, DASH], ids = ["hls", "dash"])+# Leading bytes of real containers; the decoders are mocked, so only the playlist check reads them.+VALID_MEDIA = {+ "wav": b"RIFF\x24\x00\x00\x00WAVEfmt \x10\x00\x00\x00",+ "mp3": b"ID3\x04\x00\x00\x00\x00\x00\x00\xff\xfb\x90\x00",+ "flac": b"fLaC\x00\x00\x00\x22",+ "ogg": b"OggS\x00\x02\x00\x00\x00\x00",+ "m4a": b"\x00\x00\x00\x20ftypM4A \x00\x00\x00\x00",+ "mp4": b"\x00\x00\x00\x18ftypmp42\x00\x00\x00\x00",+ "webm": b"\x1aE\xdf\xa3\x9fB\x86\x81\x01webm",+ "mov": b"\x00\x00\x00\x14ftypqt \x00\x00\x00\x00",+}+VALID = pytest.mark.parametrize("payload", list(VALID_MEDIA.values()), ids = list(VALID_MEDIA))+++def _b64(raw: bytes) -> str:+ return base64.b64encode(raw).decode()+++def studio_home(tmp_path, monkeypatch):+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "studio"))+++def fake_torchaudio(monkeypatch):+ calls = []++ def info(path):+ calls.append(("info", path))+ return types.SimpleNamespace(sample_rate = 16000, num_frames = 16000, num_channels = 1)++ def load(path, num_frames = -1):+ calls.append(("load", path))+ return torch.zeros(1, 16000), 16000++ monkeypatch.setitem(sys.modules, "torchaudio", types.SimpleNamespace(info = info, load = load))+ return calls+++def fake_torchcodec(monkeypatch):+ """torchaudio 2.9+ without librosa: info() is gone and the last resort is torchcodec."""+ calls = []++ class AudioDecoder:+ def __init__(self, path):+ calls.append(path)+ self.metadata = types.SimpleNamespace(+ sample_rate = 16000, duration_seconds = 1.0, num_channels = 1+ )++ def get_samples_played_in_range(self, start, stop):+ return types.SimpleNamespace(data = torch.zeros(1, 16000))++ def info(path):+ raise AttributeError("torchaudio.info was removed")++ def no_decoder(raw):+ raise RuntimeError("no soundfile, PyAV or librosa")++ decoders = types.ModuleType("torchcodec.decoders")+ decoders.AudioDecoder = AudioDecoder+ torchcodec = types.ModuleType("torchcodec")+ torchcodec.decoders = decoders+ monkeypatch.setitem(sys.modules, "torchcodec", torchcodec)+ monkeypatch.setitem(sys.modules, "torchcodec.decoders", decoders)+ monkeypatch.setitem(sys.modules, "torchaudio", types.SimpleNamespace(info = info, load = None))+ monkeypatch.setattr(inference_route, "_decode_audio_mono", no_decoder)+ return calls+++@PLAYLISTS+def test_torchaudio_never_opens_a_playlist(studio_home, fake_torchaudio, payload):+ with pytest.raises(PlaylistRefused, match = "Playlists are not accepted"):+ inference_route._decode_audio_base64(_b64(payload))+ assert fake_torchaudio == []+++@PLAYLISTS+def test_torchcodec_fallback_never_opens_a_playlist(studio_home, fake_torchcodec, payload):+ with pytest.raises(PlaylistRefused, match = "Playlists are not accepted"):+ inference_route._decode_audio_base64(_b64(payload))+ assert fake_torchcodec == []+++@VALID+def test_torchaudio_still_decodes_real_audio(studio_home, fake_torchaudio, payload):+ assert inference_route._decode_audio_base64(_b64(payload)).shape == (16000,)+ assert [name for name, _ in fake_torchaudio] == ["info", "load"]+++@VALID+def test_torchcodec_fallback_still_decodes_real_audio(studio_home, fake_torchcodec, payload):+ assert inference_route._decode_audio_base64(_b64(payload)).shape == (16000,)+ assert len(fake_torchcodec) == 1+++def mono_decoders(monkeypatch):+ calls = []++ def decode(name):+ def run(raw):+ calls.append(name)+ return np.zeros(8000, dtype = np.float32), 8000++ return run++ monkeypatch.setattr(inference_route, "_decode_audio_mono_with_soundfile", decode("soundfile"))+ monkeypatch.setattr(inference_route, "_decode_audio_mono_with_av", decode("av"))+ return calls+++@PLAYLISTS+def test_mono_decoder_refuses_a_playlist_before_any_backend(mono_decoders, payload):+ with pytest.raises(PlaylistRefused, match = "Playlists are not accepted"):+ inference_route._decode_audio_mono(payload)+ assert mono_decoders == []+++@VALID+def test_mono_decoder_still_decodes_real_audio(mono_decoders, payload):+ samples, rate = inference_route._decode_audio_mono(payload)+ assert rate == 8000 and len(samples) == 8000+ assert mono_decoders == ["soundfile"]+++def no_ffmpeg(monkeypatch):+ monkeypatch.setattr(llama_video_input.shutil, "which", lambda name: None)+++@PLAYLISTS+def test_llama_video_refuses_a_playlist_without_ffmpeg(no_ffmpeg, payload):+ with pytest.raises(PlaylistRefused, match = "Playlists are not accepted"):+ llama_video_input.shrink_video_for_llama(_b64(payload), max_bytes = 1 << 20)++[email protected]("kind", ["mp4", "webm", "mov"])+def test_llama_video_without_ffmpeg_still_passes_a_clip_through(no_ffmpeg, kind):+ clip = _b64(VALID_MEDIA[kind])+ assert llama_video_input.shrink_video_for_llama(clip, max_bytes = 1 << 20) == clip+++@PLAYLISTS+def test_mlx_video_clip_refuses_a_playlist_before_writing(tmp_path, monkeypatch, payload):+ import tempfile++ from core.inference import mlx_inference++ monkeypatch.setattr(tempfile, "tempdir", str(tmp_path))+ with pytest.raises(PlaylistRefused, match = "Playlists are not accepted"):+ mlx_inference._write_video_clip(_b64(payload))+ assert list(tmp_path.iterdir()) == []++[email protected]("kind", ["mp4", "webm", "mov"])+def test_mlx_video_clip_still_writes_a_real_clip(tmp_path, monkeypatch, kind):+ import tempfile++ from core.inference import mlx_inference++ monkeypatch.setattr(tempfile, "tempdir", str(tmp_path))+ path = mlx_inference._write_video_clip(_b64(VALID_MEDIA[kind]))+ try:+ with open(path, "rb") as handle:+ assert handle.read() == VALID_MEDIA[kind]+ finally:+ os.unlink(path)