mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-07-27 15:37:44 +00:00
docs: Translate server.py comments to English
- Translate resolve_model_path() docstring from Chinese to English - Add detailed security validation steps documentation - Follows project language policy (all comments in English) No functional changes - documentation only.
This commit is contained in:
@@ -89,38 +89,48 @@ class GradingServer:
|
||||
return get_evaluator(self.task)
|
||||
|
||||
def resolve_model_path(self, model_path: str) -> Path:
|
||||
"""将模型路径约束在 workspace 下,防止访问任意文件系统路径。"""
|
||||
# Security fix: Validate and sanitize user-provided path to prevent path traversal
|
||||
"""
|
||||
Resolve model path safely within workspace directory.
|
||||
|
||||
Security: Prevents path traversal attacks by validating user-provided paths.
|
||||
Only allows access to files within the workspace directory.
|
||||
|
||||
Validation steps:
|
||||
1. Reject null bytes
|
||||
2. Normalize path to remove .. and . components
|
||||
3. Reject Windows drive letters
|
||||
4. Reject absolute paths
|
||||
5. Resolve to absolute path
|
||||
6. Verify resolved path is within workspace_root using relative_to()
|
||||
"""
|
||||
# Reject null bytes
|
||||
if "\x00" in model_path:
|
||||
raise ValueError("Invalid model_path: contains null byte")
|
||||
|
||||
workspace_root = self.workspace.expanduser().resolve()
|
||||
|
||||
|
||||
# Normalize path to remove .. and . components
|
||||
normalized = os.path.normpath(model_path)
|
||||
|
||||
|
||||
# Reject Windows drive letters (e.g., C:\)
|
||||
if os.path.splitdrive(normalized)[0]:
|
||||
raise ValueError("Invalid model_path: contains drive letter")
|
||||
|
||||
|
||||
# Reject absolute paths
|
||||
if os.path.isabs(normalized):
|
||||
raise ValueError("Invalid model_path: must be relative path")
|
||||
|
||||
# Join with workspace root
|
||||
|
||||
# Join with workspace root and resolve to absolute path
|
||||
candidate = os.path.join(str(workspace_root), normalized)
|
||||
|
||||
# Resolve to absolute path and verify it's under workspace_root
|
||||
resolved_path = Path(candidate).expanduser().resolve(strict=False)
|
||||
|
||||
|
||||
# Security check: ensure resolved path is within workspace_root
|
||||
# This validates that the user-supplied model_path didn't escape the workspace
|
||||
try:
|
||||
resolved_path.relative_to(workspace_root)
|
||||
except ValueError as exc:
|
||||
raise ValueError(f"Invalid model_path: path traversal detected - {model_path}") from exc
|
||||
|
||||
|
||||
return resolved_path
|
||||
|
||||
def submit(self, model_path: str, gpu: Optional[str] = None) -> dict:
|
||||
|
||||
Reference in New Issue
Block a user