Files
xinmi/mcp-server/upload_paths.py
T
yawan f7fc992f57
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
2026-09-22 16:19:06 +08:00

67 lines
2.0 KiB
Python

"""Local file path validation for MCP upload tools."""
from __future__ import annotations
import os
from typing import List, Optional
_active_transport: Optional[str] = None
def set_active_transport(transport: str) -> None:
"""Record the transport selected at server startup (CLI or run_* entry)."""
global _active_transport
_active_transport = transport.strip().lower()
def clear_active_transport() -> None:
"""Reset startup transport override (for tests)."""
global _active_transport
_active_transport = None
def _current_transport() -> str:
if _active_transport is not None:
return _active_transport
return os.getenv("MCP_TRANSPORT", "stdio").strip().lower()
def _path_within_root(resolved_path: str, root: str) -> bool:
root = os.path.realpath(root)
resolved_path = os.path.realpath(resolved_path)
try:
common = os.path.commonpath([root, resolved_path])
except ValueError:
return False
return common == root
def _allowed_upload_roots() -> List[str]:
"""Return directories local files may be read from for upload tools."""
raw = os.getenv("MCP_ALLOWED_UPLOAD_DIRS", "").strip()
if raw:
return [os.path.realpath(part.strip()) for part in raw.split(",") if part.strip()]
transport = _current_transport()
if transport in ("sse", "http"):
return [os.path.realpath(os.getcwd())]
return []
def resolve_upload_file_path(file_path: str) -> str:
"""Resolve and validate a local file path for create_knowledge_from_file."""
raw = (file_path or "").strip()
if not raw:
raise ValueError("file path is required")
if "\x00" in raw:
raise ValueError("file path contains invalid characters")
resolved = os.path.realpath(raw)
if not os.path.isfile(resolved):
raise ValueError(f"file not found: {file_path}")
roots = _allowed_upload_roots()
if roots and not any(_path_within_root(resolved, root) for root in roots):
raise ValueError("file path is outside allowed upload directories")
return resolved