* Rebuild Studio branch on top of main * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix security and code quality issues for Studio PR #4237 - Validate models_dir query param against allowed directory roots to prevent path traversal in /api/models/local endpoint - Replace string startswith() with Path.is_relative_to() for frontend path traversal check in serve_frontend - Sanitize SSE error messages to not leak exception details to clients (4 locations in inference.py) - Bind port-discovery socket to 127.0.0.1 instead of all interfaces in llama_cpp backend - Import datasets_root and resolve_output_dir in embedding training function to fix NameError and use managed output directory - Remove stale .gitignore entries for package-lock.json and test directories so tests can be tracked in version control - Add venv-reexecution logic to ui CLI command matching the studio command behavior * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Move models_dir path validation before try/except block The HTTPException(403) was inside the try/except Exception handler, so it would be caught and re-raised as a 500. Moving the validation before the try block ensures the 403 is returned directly and also makes the control flow clearer for static analysis (path is validated before any filesystem operations). * Use os.path.realpath + startswith for models_dir validation CodeQL py/path-injection does not recognize Path.is_relative_to() as a sanitizer. Switched to os.path.realpath + str.startswith which is a recognized sanitizer pattern in CodeQL's taint analysis. The startswith check uses root_str + os.sep to prevent prefix collisions (e.g. /app/models_evil matching /app/models). * Never pass user input to Path constructor in models_dir validation CodeQL traces taint through Path(resolved) even after a startswith barrier guard. Fix: the user-supplied models_dir is only used as a string for comparison against allowed roots. The Path object passed to _scan_models_dir comes from the trusted allowed_roots list, not from user input. This fully breaks the taint chain. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
99 lines
2.9 KiB
Python
99 lines
2.9 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
|
|
|
|
"""
|
|
Colab-specific helpers for running Unsloth Studio.
|
|
Uses Colab's built-in proxy - no external tunneling needed!
|
|
"""
|
|
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
# Add backend to path early so local modules like loggers can be imported
|
|
backend_path = str(Path(__file__).parent)
|
|
if backend_path not in sys.path:
|
|
sys.path.insert(0, backend_path)
|
|
|
|
from loggers import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
def get_colab_url(port: int = 8000) -> str:
|
|
"""
|
|
Get the actual Colab proxy URL for a port.
|
|
"""
|
|
try:
|
|
from google.colab.output import eval_js
|
|
|
|
# Use Colab's proxy mechanism
|
|
url = eval_js(f"google.colab.kernel.proxyPort({port})", timeout_sec = 5)
|
|
return url if url else f"http://localhost:{port}"
|
|
except Exception as e:
|
|
logger.info(f"Note: Could not get Colab URL ({e})")
|
|
return f"http://localhost:{port}"
|
|
|
|
|
|
def show_link(port: int = 8000):
|
|
"""Display a styled clickable link to the UI."""
|
|
from IPython.display import display, HTML
|
|
|
|
# Get real Colab proxy URL
|
|
url = get_colab_url(port)
|
|
|
|
html = f"""
|
|
<div style="padding: 20px; background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
|
|
border-radius: 12px; margin: 10px 0; font-family: system-ui, -apple-system, sans-serif;">
|
|
<h2 style="color: white; margin: 0 0 12px 0; font-size: 24px;">
|
|
🦥 Unsloth Studio is Ready!
|
|
</h2>
|
|
<a href="{url}" target="_blank"
|
|
style="display: inline-block; padding: 14px 28px; background: white; color: #16a34a;
|
|
text-decoration: none; border-radius: 8px; font-weight: 600; font-size: 16px;
|
|
box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
|
|
🚀 Open Unsloth Studio
|
|
</a>
|
|
<p style="color: rgba(255,255,255,0.9); margin: 16px 0 0 0; font-size: 13px;
|
|
word-break: break-all; font-family: monospace;">
|
|
{url}
|
|
</p>
|
|
</div>
|
|
"""
|
|
display(HTML(html))
|
|
|
|
|
|
def start(port: int = 8000):
|
|
"""
|
|
Start Unsloth Studio server in Colab and display the URL.
|
|
|
|
Usage:
|
|
from colab import start
|
|
start()
|
|
"""
|
|
import sys
|
|
|
|
logger.info("🦥 Starting Unsloth Studio...")
|
|
|
|
logger.info(" Loading backend...")
|
|
from run import run_server
|
|
|
|
# Auto-detect frontend path
|
|
repo_root = Path(__file__).parent.parent
|
|
frontend_path = repo_root / "frontend" / "dist"
|
|
|
|
if not frontend_path.exists():
|
|
logger.info("❌ Frontend not built! Please run the setup cell first.")
|
|
return
|
|
|
|
logger.info(" Starting server...")
|
|
# Start server silently
|
|
run_server(host = "0.0.0.0", port = port, frontend_path = frontend_path, silent = True)
|
|
|
|
logger.info(" Server started!")
|
|
|
|
# Show the clickable link with real URL
|
|
show_link(port)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
start()
|