* 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>
101 lines
3.1 KiB
Python
101 lines
3.1 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
|
|
|
|
"""Logging handlers and middleware for structured logging.
|
|
|
|
This module provides FastAPI middleware and structlog processors for:
|
|
- Request/response logging with timing
|
|
- Sensitive data filtering in logs
|
|
- Structured logging configuration
|
|
- Error handling with detailed context
|
|
|
|
Key Components:
|
|
- LoggingMiddleware: FastAPI middleware for request/response logging
|
|
- filter_sensitive_data: Structlog processor for data sanitization
|
|
- get_logger: Factory function for structured loggers
|
|
"""
|
|
|
|
import time
|
|
from typing import Callable
|
|
|
|
import structlog
|
|
from fastapi import Request, Response
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
|
|
logger = structlog.get_logger(__name__)
|
|
|
|
|
|
class LoggingMiddleware(BaseHTTPMiddleware):
|
|
async def dispatch(self, request: Request, call_next: Callable) -> Response:
|
|
start_time = time.time()
|
|
|
|
try:
|
|
response = await call_next(request)
|
|
|
|
# Log response
|
|
process_time = (time.time() - start_time) * 1000
|
|
|
|
EXCLUDED_PATHS = {
|
|
"/api/train/status",
|
|
"/api/train/metrics",
|
|
"/api/train/hardware",
|
|
"/api/system",
|
|
}
|
|
is_excluded = (
|
|
request.url.path in EXCLUDED_PATHS
|
|
or request.url.path.startswith("/assets/")
|
|
or request.url.path.endswith(
|
|
(".png", ".jpg", ".jpeg", ".ico", ".woff", ".woff2", ".ttf")
|
|
)
|
|
)
|
|
|
|
if not is_excluded:
|
|
logger.info(
|
|
"request_completed",
|
|
method = request.method,
|
|
path = request.url.path,
|
|
status_code = response.status_code,
|
|
process_time_ms = round(process_time, 2),
|
|
)
|
|
|
|
return response
|
|
|
|
except Exception as e:
|
|
logger.error(
|
|
"request_failed",
|
|
path = request.url.path,
|
|
method = request.method,
|
|
error = str(e),
|
|
exc_info = True,
|
|
)
|
|
raise
|
|
|
|
|
|
def filter_sensitive_data(logger, method_name, event_dict):
|
|
"""Structlog processor to filter out base64 data from logs."""
|
|
|
|
def filter_value(value):
|
|
if (
|
|
isinstance(value, str)
|
|
and len(value) > 100
|
|
and ("," in value or "/" in value)
|
|
):
|
|
# Likely base64 data, truncate it
|
|
return value[:20] + "..."
|
|
elif isinstance(value, dict):
|
|
return {k: filter_value(v) for k, v in value.items()}
|
|
elif isinstance(value, list):
|
|
return [filter_value(item) for item in value]
|
|
return value
|
|
|
|
return {k: filter_value(v) for k, v in event_dict.items()}
|
|
|
|
|
|
def get_logger(name: str) -> structlog.BoundLogger:
|
|
"""Get a logger instance for a specific module.
|
|
Args:
|
|
name: Usually __name__ of the module
|
|
Returns:
|
|
A bound structured logger
|
|
"""
|
|
return structlog.get_logger(name)
|