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
fix: replace FileResponse with Response for index.html to prevent Content-Length mismatch and add path traversal guard
Roland Tannousa6f1153f9a59
diff --git a/studio/backend/main.py b/studio/backend/main.pyindex 466be151d89..331962fb670 100644--- a/studio/backend/main.py+++ b/studio/backend/main.py@@ -8,7 +8,7 @@ from contextlib import asynccontextmanagerfrom fastapi import FastAPIfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.staticfiles import StaticFiles-from fastapi.responses import FileResponse+from fastapi.responses import FileResponse, HTMLResponse, Responsefrom pathlib import Pathfrom datetime import datetime@@ -156,26 +156,44 @@ async def get_hardware_info():def setup_frontend(app: FastAPI, build_path: Path):"""Mount frontend static files (optional)"""- if build_path.exists():- # Mount assets- assets_dir = build_path / "assets"- if assets_dir.exists():- app.mount("/assets", StaticFiles(directory=assets_dir), name="assets")+ if not build_path.exists():+ return False++ # Mount assets+ assets_dir = build_path / "assets"+ if assets_dir.exists():+ app.mount("/assets", StaticFiles(directory=assets_dir), name="assets")++ @app.get("/")+ async def serve_root():+ content = (build_path / "index.html").read_bytes()+ return Response(+ content=content,+ media_type="text/html",+ headers={"Cache-Control": "no-cache, no-store, must-revalidate"},+ )++ @app.get("/{full_path:path}")+ async def serve_frontend(full_path: str):+ if full_path.startswith("api"):+ return {"error": "API endpoint not found"}++ file_path = (build_path / full_path).resolve()++ # Block path traversal — ensure resolved path stays inside build_path+ if not str(file_path).startswith(str(build_path.resolve())):+ return Response(status_code=403)++ if file_path.is_file():+ return FileResponse(file_path)++ # Serve index.html as bytes — avoids Content-Length mismatch+ content = (build_path / "index.html").read_bytes()+ return Response(+ content=content,+ media_type="text/html",+ headers={"Cache-Control": "no-cache, no-store, must-revalidate"},+ )++ return True- @app.get("/")- async def serve_root():- return FileResponse(build_path / "index.html", headers={"Cache-Control": "no-cache, no-store, must-revalidate"})-- @app.get("/{full_path:path}")- async def serve_frontend(full_path: str):- if full_path.startswith("api"):- return {"error": "API endpoint not found"}-- file_path = build_path / full_path- if file_path.is_file():- return FileResponse(file_path)-- return FileResponse(build_path / "index.html", headers={"Cache-Control": "no-cache, no-store, must-revalidate"})-- return True- return False