forked from CodeGraphContext/CodeGraphContext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_sandbox.py
More file actions
90 lines (73 loc) · 2.57 KB
/
Copy pathpath_sandbox.py
File metadata and controls
90 lines (73 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""Path sandbox helpers for CLI and MCP tools."""
from __future__ import annotations
import os
import re
from pathlib import Path
from typing import List
from urllib.parse import urlparse
MAX_DISCOVERY_DEPTH = 10
# Hugging Face dataset raw URLs and GitHub release assets only.
_ALLOWED_DOWNLOAD_HOST_SUFFIXES = (
"huggingface.co",
"hf.co",
"github.com",
"raw.githubusercontent.com",
)
def get_allowed_roots() -> List[Path]:
"""Return directories under which paths may be indexed or loaded."""
roots: List[Path] = [Path.cwd().resolve()]
env_roots = os.environ.get("CGC_ALLOWED_ROOTS", "")
if env_roots:
separator = ";" if os.name == "nt" else ":"
for entry in env_roots.split(separator):
entry = entry.strip()
if entry:
roots.append(Path(entry).resolve())
return roots
def is_path_allowed(path: Path) -> bool:
"""True when *path* resolves under an allowed root."""
resolved = path.resolve()
for root in get_allowed_roots():
try:
resolved.relative_to(root)
return True
except ValueError:
continue
return False
def is_path_under_root(path: Path, root: Path) -> bool:
"""True when *path* resolves under *root*."""
try:
path.resolve().relative_to(root.resolve())
return True
except ValueError:
return False
def sanitize_bundle_filename(filename: str, default: str = "bundle.cgc") -> str:
"""Return a safe basename for a downloaded bundle file."""
if not filename or ".." in filename or "/" in filename or "\\" in filename:
return default
name = Path(filename).name
if not name or name in (".", ".."):
return default
if not re.fullmatch(r"[\w.\-]+\.cgc", name, flags=re.IGNORECASE):
if not name.endswith(".cgc"):
name = f"{re.sub(r'[^\w.\-]+', '_', name)}.cgc"
return name
def is_safe_download_url(url: str) -> bool:
"""True when *url* uses HTTPS and points to an allowed registry host."""
try:
parsed = urlparse(url)
except Exception:
return False
if parsed.scheme != "https":
return False
host = (parsed.hostname or "").lower()
if not host:
return False
return any(host == suffix or host.endswith(f".{suffix}") for suffix in _ALLOWED_DOWNLOAD_HOST_SUFFIXES)
def clamp_discovery_depth(max_depth: int) -> int:
"""Clamp discovery depth to a safe range."""
try:
depth = int(max_depth)
except (TypeError, ValueError):
return 1
return max(0, min(depth, MAX_DISCOVERY_DEPTH))