fix: patch CVE dependencies and path traversal risks

Upgrade aiohttp and related packages, harden local file and storage path checks, tighten password/JWT comparisons, and default Docker forwarded IP trust to empty.
This commit is contained in:
vastsa committed 2026-07-21 14:49:30 +08:00
1 parent c676458370
commit 00f3abd3b2
7 files changed
+199 -36

No files matched your search

+23 -11
View File
@@ -48,15 +48,20 @@ def create_token(data: dict, expires_in: int | None = None) -> str:
token_lifetime = (
get_admin_session_expire_seconds() if expires_in is None else expires_in
)
header = base64.b64encode(
json.dumps({"alg": "HS256", "typ": "JWT"}).encode()
).decode()
payload = base64.b64encode(
json.dumps({**data, "exp": int(time.time()) + token_lifetime}).encode()
).decode()
header = base64.urlsafe_b64encode(
json.dumps({"alg": "HS256", "typ": "JWT"}, separators=(",", ":")).encode()
).decode().rstrip("=")
payload = base64.urlsafe_b64encode(
json.dumps(
{**data, "exp": int(time.time()) + token_lifetime},
separators=(",", ":"),
).encode()
).decode().rstrip("=")
signature = hmac.new(_get_jwt_secret(), f"{header}.{payload}".encode(), "sha256").digest()
signature = base64.b64encode(signature).decode()
signature = hmac.new(
_get_jwt_secret(), f"{header}.{payload}".encode(), "sha256"
).digest()
signature = base64.urlsafe_b64encode(signature).decode().rstrip("=")
return f"{header}.{payload}.{signature}"
@@ -76,13 +81,20 @@ def verify_token(token: str) -> dict:
f"{header_b64}.{payload_b64}".encode(),
"sha256",
).digest()
expected_signature_b64 = base64.b64encode(expected_signature).decode()
expected_signature_b64 = (
base64.urlsafe_b64encode(expected_signature).decode().rstrip("=")
)
if not hmac.compare_digest(signature_b64, expected_signature_b64):
raise ValueError("无效的签名")
# 解码payload
payload = json.loads(base64.b64decode(payload_b64))
# 解码payload(兼容历史标准 base64 与 urlsafe base64)
padded = payload_b64 + "=" * (-len(payload_b64) % 4)
try:
payload_bytes = base64.urlsafe_b64decode(padded)
except Exception:
payload_bytes = base64.b64decode(padded)
payload = json.loads(payload_bytes)
# 检查是否过期
if payload.get("exp", 0) < time.time():
+31 -4
View File
@@ -1,4 +1,5 @@
import hashlib
from pathlib import Path
import os
import time
import uuid
@@ -1627,9 +1628,35 @@ class LocalFileService:
class LocalFileClass:
def __init__(self, file):
self.file = file
self.path = data_root / "local" / file
if os.path.exists(self.path):
# 仅允许 data/local 目录下的单层文件名,阻断路径穿越与绝对路径访问。
raw_name = str(file or "")
normalized = Path(raw_name).as_posix()
# 输入本身不得包含路径分隔符或绝对路径形态。
if (
not raw_name
or raw_name in {".", ".."}
or normalized in {".", ".."}
or "/" in normalized
or normalized.startswith("~")
or Path(raw_name).is_absolute()
or Path(raw_name).name != raw_name
):
raise HTTPException(status_code=400, detail="非法文件名")
safe_name = Path(raw_name).name
if not safe_name or safe_name in {".", ".."}:
raise HTTPException(status_code=400, detail="非法文件名")
local_root = (data_root / "local").resolve()
candidate = (local_root / safe_name).resolve()
try:
candidate.relative_to(local_root)
except ValueError:
raise HTTPException(status_code=400, detail="非法文件路径")
self.file = safe_name
self.path = candidate
if self.path.is_file():
self.ctime = time.strftime(
"%Y-%m-%d %H:%M:%S", time.localtime(os.path.getctime(self.path))
)
@@ -1649,4 +1676,4 @@ class LocalFileClass:
os.remove(self.path)
async def exists(self):
return os.path.exists(self.path)
return self.path.is_file()