Skip to content
Python

fix(logging): keep user_id namespaces inside the pipeline log root (#83)

u7-u709e4bc8a307e
diff --git a/hy_memory/utils/pipeline_log_writer.py b/hy_memory/utils/pipeline_log_writer.py
index dccc7ad..20220bb 100644
--- a/hy_memory/utils/pipeline_log_writer.py
+++ b/hy_memory/utils/pipeline_log_writer.py
@@ -18,12 +18,32 @@ from __future__ import annotations
import json
import logging
+import re
from datetime import datetime
from pathlib import Path
from typing import Any, List, Optional
logger = logging.getLogger(__name__)
+#: A namespace is a single path component, not a path. `\w` keeps Unicode
+#: names working while excluding separators, drive letters and NUL.
+_SAFE_SUBDIR = re.compile(r"[\w.-]{1,64}", re.UNICODE)
+
+
+def _safe_subdir(value: str) -> Optional[str]:
+ """Return ``value`` if it is safe as one directory name, else ``None``.
+
+ ``user_id`` is caller-controlled. Without this check a value such as
+ ``"/tmp/evil__u1"`` makes ``Path(base) / "/tmp/evil"`` discard ``base``
+ entirely; ``"../.."`` escapes it the same way.
+ """
+ candidate = (value or "").strip()
+ if not candidate or candidate in {".", ".."}:
+ return None
+ if not _SAFE_SUBDIR.fullmatch(candidate):
+ return None
+ return candidate
+
# 兼容旧 import
TraceFileWriter = None # set after class definition
@@ -40,7 +60,7 @@ class PipelineLogWriter:
def __init__(self, log_dir: str, default_subdir: str = "default"):
self._log_dir = log_dir
- self._default_subdir = default_subdir or "default"
+ self._default_subdir = _safe_subdir(default_subdir) or "default"
try:
(Path(self._log_dir) / self._default_subdir).mkdir(parents=True, exist_ok=True)
except Exception:
@@ -49,7 +69,7 @@ class PipelineLogWriter:
def _resolve_subdir(self, user_id: str) -> str:
"""从 user_id 的 "{prefix}__{user_id}" 前缀取 prefix 作为子目录;无前缀则回落。"""
if user_id and "__" in user_id:
- prefix = user_id.split("__", 1)[0].strip()
+ prefix = _safe_subdir(user_id.split("__", 1)[0])
if prefix:
return prefix
return self._default_subdir
@@ -115,7 +135,14 @@ class PipelineLogWriter:
line = json.dumps(record, ensure_ascii=False, default=str)
date_str = datetime.now().strftime("%Y-%m-%d")
subdir = self._resolve_subdir(user_id)
- log_dir = Path(self._log_dir) / subdir
+ base_dir = Path(self._log_dir).resolve()
+ log_dir = (base_dir / subdir).resolve()
+ # Defence in depth: even if a future change lets a suspicious
+ # component through, never write outside the configured root.
+ try:
+ log_dir.relative_to(base_dir)
+ except ValueError:
+ log_dir = base_dir / self._default_subdir
log_dir.mkdir(parents=True, exist_ok=True)
log_file = log_dir / f"{date_str}.log"
with open(log_file, "a", encoding="utf-8") as f:
diff --git a/tests/test_pipeline_observability.py b/tests/test_pipeline_observability.py
index 69fdfa8..40f5b30 100644
--- a/tests/test_pipeline_observability.py
+++ b/tests/test_pipeline_observability.py
@@ -291,6 +291,35 @@ class TestPipelineLogSubdirSplit:
w.write_step(user_id="app1__user__with__underscores", step="X", response="r")
assert (tmp_path / "app1").is_dir()
+ def test_user_id_prefix_cannot_escape_log_root(self, tmp_path):
+ from hy_memory.utils.pipeline_log_writer import PipelineLogWriter
+
+ log_root = tmp_path / "logs"
+ outside = tmp_path / "outside"
+ w = PipelineLogWriter(str(log_root), "default")
+ for user_id in (
+ f"{outside / 'evil'}__u1",
+ f"../../{outside.name}__u1",
+ "..__u1",
+ f"{outside.name}/nested__u1",
+ ):
+ w.write_step(user_id=user_id, step="X", response="r")
+
+ assert not outside.exists()
+ files = list(log_root.rglob("*.log"))
+ assert files
+ assert all(path.is_relative_to(log_root) for path in files)
+
+ def test_default_subdir_is_sanitized(self, tmp_path):
+ from hy_memory.utils.pipeline_log_writer import PipelineLogWriter
+
+ log_root = tmp_path / "logs"
+ w = PipelineLogWriter(str(log_root), "../../evil")
+ w.write_step(user_id="no_prefix", step="X", response="r")
+
+ assert not (tmp_path / "evil").exists()
+ assert (log_root / "default").is_dir()
+
def test_pipeline_jsonl_does_not_add_total_tokens_twice(tmp_path):
from datetime import date