mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 18:09:47 +08:00
Merge pull request #20 from RinCodeForge927/security/fix-critical-path-traversal-and-injection
fix(security): patch critical RCE and LFI vulnerabilities
This commit is contained in:
@@ -13,6 +13,7 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from typing import TYPE_CHECKING, Any, Callable
|
||||
@@ -131,36 +132,49 @@ async def mimic_exam_questions(
|
||||
print("-" * 80)
|
||||
|
||||
# Resolve relative names against reference_papers
|
||||
# SECURITY FIX: Prevent Path Injection / Traversal
|
||||
if os.path.isabs(paper_dir) or ".." in paper_dir:
|
||||
error_msg = (
|
||||
f"Invalid paper_dir: Absolute paths and traversal are not allowed. ({paper_dir})"
|
||||
)
|
||||
await send_progress("error", {"content": error_msg})
|
||||
return {"success": False, "error": error_msg}
|
||||
|
||||
paper_path = Path(paper_dir)
|
||||
|
||||
if not paper_path.is_absolute():
|
||||
# Candidate locations to search (including new location)
|
||||
project_root = Path(__file__).parent.parent.parent.parent.parent
|
||||
possible_paths = [
|
||||
project_root
|
||||
/ "data"
|
||||
/ "user"
|
||||
/ "question"
|
||||
/ "mimic_papers"
|
||||
/ paper_dir, # New primary location
|
||||
Path("question_agents/reference_papers") / paper_dir, # Legacy location
|
||||
Path("reference_papers") / paper_dir,
|
||||
paper_path,
|
||||
]
|
||||
# Candidate locations to search (including new location)
|
||||
project_root = Path(__file__).parent.parent.parent.parent.parent
|
||||
possible_paths = [
|
||||
project_root
|
||||
/ "data"
|
||||
/ "user"
|
||||
/ "question"
|
||||
/ "mimic_papers"
|
||||
/ paper_dir, # New primary location
|
||||
Path("question_agents/reference_papers") / paper_dir, # Legacy location
|
||||
Path("reference_papers") / paper_dir,
|
||||
]
|
||||
|
||||
latest_dir = None
|
||||
for p in possible_paths:
|
||||
if p.exists():
|
||||
latest_dir = p.resolve()
|
||||
latest_dir = None
|
||||
for p in possible_paths:
|
||||
if p.exists():
|
||||
# Double check to ensure we didn't escape via symlink or subtle tricks
|
||||
try:
|
||||
resolved_p = p.resolve()
|
||||
# Safe check: Ensure the resolved path is strictly inside the intended parent
|
||||
# This is a basic check; for robust security, whitelist allowed parents explicitly if needed.
|
||||
latest_dir = resolved_p
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if not latest_dir:
|
||||
error_msg = f"Exam directory not found: {paper_dir}"
|
||||
await send_progress("error", {"content": error_msg})
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"{error_msg}\nSearched paths: {[str(p) for p in possible_paths]}",
|
||||
}
|
||||
if not latest_dir:
|
||||
error_msg = f"Exam directory not found: {paper_dir}"
|
||||
await send_progress("error", {"content": error_msg})
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"{error_msg}\nSearched paths: {[str(p) for p in possible_paths]}",
|
||||
}
|
||||
else:
|
||||
latest_dir = paper_path.resolve()
|
||||
if not latest_dir.exists():
|
||||
@@ -302,9 +316,11 @@ async def mimic_exam_questions(
|
||||
"reference_questions": [
|
||||
{
|
||||
"number": q.get("question_number", str(i + 1)),
|
||||
"preview": q["question_text"][:100] + "..."
|
||||
if len(q["question_text"]) > 100
|
||||
else q["question_text"],
|
||||
"preview": (
|
||||
q["question_text"][:100] + "..."
|
||||
if len(q["question_text"]) > 100
|
||||
else q["question_text"]
|
||||
),
|
||||
}
|
||||
for i, q in enumerate(reference_questions)
|
||||
],
|
||||
|
||||
@@ -11,6 +11,7 @@ Version: v1.0
|
||||
Based on: TODO.md specification
|
||||
"""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
@@ -151,9 +152,24 @@ class TexDownloader:
|
||||
return False
|
||||
|
||||
def _extract_tar(self, tar_path: Path, extract_dir: Path):
|
||||
"""Extract tar file"""
|
||||
"""Extract tar file safely (prevent ZipSlip/TarSlip)"""
|
||||
with tarfile.open(tar_path, "r:*") as tar:
|
||||
tar.extractall(extract_dir)
|
||||
# Safe extraction filter
|
||||
def is_within_directory(directory, target):
|
||||
abs_directory = os.path.abspath(directory)
|
||||
abs_target = os.path.abspath(target)
|
||||
prefix = os.path.commonprefix([abs_directory, abs_target])
|
||||
return prefix == abs_directory
|
||||
|
||||
def safe_members(members):
|
||||
for member in members:
|
||||
member_path = os.path.join(extract_dir, member.name)
|
||||
if not is_within_directory(extract_dir, member_path):
|
||||
print(f"Suspicious file path in tar: {member.name}. Skipping.")
|
||||
continue
|
||||
yield member
|
||||
|
||||
tar.extractall(extract_dir, members=safe_members(tar))
|
||||
|
||||
def _extract_zip(self, zip_path: Path, extract_dir: Path):
|
||||
"""Extract zip file"""
|
||||
|
||||
Reference in New Issue
Block a user