feat: introduce BASIC_MEMORY_PROJECT_ROOT for path constraints (#334)

Signed-off-by: phernandez <paul@basicmachines.co>
This commit is contained in:
Paul Hernandez
2025-10-05 10:42:06 -05:00
committed by GitHub
parent 7616b2bb08
commit ccc4386627
5 changed files with 113 additions and 62 deletions
+9 -9
View File
@@ -99,13 +99,12 @@ class ProjectService:
Raises:
ValueError: If the project already exists
"""
# in cloud mode, don't allow arbitrary paths.
if self.config_manager.config.cloud_mode_enabled:
basic_memory_home = os.getenv("BASIC_MEMORY_HOME")
assert basic_memory_home is not None
base_path = Path(basic_memory_home)
# If project_root is set, constrain all projects to that directory
project_root = self.config_manager.config.project_root
if project_root:
base_path = Path(project_root)
# Sanitize the input path for cloud mode
# Sanitize the input path
# Strip leading slashes, home directory references, and parent directory references
clean_path = path.lstrip("/").replace("~/", "").replace("~", "")
@@ -116,13 +115,14 @@ class ProjectService:
path_parts.append(part)
clean_path = "/".join(path_parts) if path_parts else ""
# Construct path relative to BASIC_MEMORY_HOME
# Construct path relative to project_root
resolved_path = (base_path / clean_path).resolve().as_posix()
# Verify the resolved path is actually under BASIC_MEMORY_HOME
# Verify the resolved path is actually under project_root
if not resolved_path.startswith(base_path.resolve().as_posix()):
raise ValueError(
f"Cloud mode requires projects under {basic_memory_home}. Invalid path: {path}"
f"BASIC_MEMORY_PROJECT_ROOT is set to {project_root}. "
f"All projects must be created under this directory. Invalid path: {path}"
)
else:
resolved_path = Path(os.path.abspath(os.path.expanduser(path))).as_posix()