Skip to content

Commit ccc4386

Browse files
authored
feat: introduce BASIC_MEMORY_PROJECT_ROOT for path constraints (#334)
Signed-off-by: phernandez <paul@basicmachines.co>
1 parent 7616b2b commit ccc4386

5 files changed

Lines changed: 113 additions & 62 deletions

File tree

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ WORKDIR /app
2424
RUN uv sync --locked
2525

2626
# Create necessary directories and set ownership
27-
RUN mkdir -p /app/data /app/.basic-memory && \
27+
RUN mkdir -p /app/data/basic-memory /app/.basic-memory && \
2828
chown -R appuser:${GID} /app
2929

3030
# Set default data directory and add venv to PATH
31-
ENV BASIC_MEMORY_HOME=/app/data \
31+
ENV BASIC_MEMORY_HOME=/app/data/basic-memory \
32+
BASIC_MEMORY_PROJECT_ROOT=/app/data \
3233
PATH="/app/.venv/bin:$PATH"
3334

3435
# Switch to the non-root user

src/basic_memory/config.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ class BasicMemoryConfig(BaseSettings):
103103
description="Skip expensive initialization synchronization. Useful for cloud/stateless deployments where project reconciliation is not needed.",
104104
)
105105

106+
# Project path constraints
107+
project_root: Optional[str] = Field(
108+
default=None,
109+
description="If set, all projects must be created underneath this directory. Paths will be sanitized and constrained to this root. If not set, projects can be created anywhere (default behavior).",
110+
)
111+
106112
# API connection configuration
107113
api_url: Optional[str] = Field(
108114
default=None,
@@ -232,6 +238,10 @@ def data_dir_path(self):
232238
return Path.home() / DATA_DIR_NAME
233239

234240

241+
# Module-level cache for configuration
242+
_CONFIG_CACHE: Optional[BasicMemoryConfig] = None
243+
244+
235245
class ConfigManager:
236246
"""Manages Basic Memory configuration."""
237247

@@ -253,12 +263,45 @@ def config(self) -> BasicMemoryConfig:
253263
return self.load_config()
254264

255265
def load_config(self) -> BasicMemoryConfig:
256-
"""Load configuration from file or create default."""
266+
"""Load configuration from file or create default.
267+
268+
Environment variables take precedence over file config values,
269+
following Pydantic Settings best practices.
270+
271+
Uses module-level cache for performance across ConfigManager instances.
272+
"""
273+
global _CONFIG_CACHE
274+
275+
# Return cached config if available
276+
if _CONFIG_CACHE is not None:
277+
return _CONFIG_CACHE
257278

258279
if self.config_file.exists():
259280
try:
260-
data = json.loads(self.config_file.read_text(encoding="utf-8"))
261-
return BasicMemoryConfig(**data)
281+
file_data = json.loads(self.config_file.read_text(encoding="utf-8"))
282+
283+
# First, create config from environment variables (Pydantic will read them)
284+
# Then overlay with file data for fields that aren't set via env vars
285+
# This ensures env vars take precedence
286+
287+
# Get env-based config fields that are actually set
288+
env_config = BasicMemoryConfig()
289+
env_dict = env_config.model_dump()
290+
291+
# Merge: file data as base, but only use it for fields not set by env
292+
# We detect env-set fields by comparing to default values
293+
merged_data = file_data.copy()
294+
295+
# For fields that have env var overrides, use those instead of file values
296+
# The env_prefix is "BASIC_MEMORY_" so we check those
297+
for field_name in BasicMemoryConfig.model_fields.keys():
298+
env_var_name = f"BASIC_MEMORY_{field_name.upper()}"
299+
if env_var_name in os.environ:
300+
# Environment variable is set, use it
301+
merged_data[field_name] = env_dict[field_name]
302+
303+
_CONFIG_CACHE = BasicMemoryConfig(**merged_data)
304+
return _CONFIG_CACHE
262305
except Exception as e: # pragma: no cover
263306
logger.exception(f"Failed to load config: {e}")
264307
raise e
@@ -268,8 +311,11 @@ def load_config(self) -> BasicMemoryConfig:
268311
return config
269312

270313
def save_config(self, config: BasicMemoryConfig) -> None:
271-
"""Save configuration to file."""
314+
"""Save configuration to file and invalidate cache."""
315+
global _CONFIG_CACHE
272316
save_basic_memory_config(self.config_file, config)
317+
# Invalidate cache so next load_config() reads fresh data
318+
_CONFIG_CACHE = None
273319

274320
@property
275321
def projects(self) -> Dict[str, str]:

src/basic_memory/services/project_service.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,12 @@ async def add_project(self, name: str, path: str, set_default: bool = False) ->
9999
Raises:
100100
ValueError: If the project already exists
101101
"""
102-
# in cloud mode, don't allow arbitrary paths.
103-
if self.config_manager.config.cloud_mode_enabled:
104-
basic_memory_home = os.getenv("BASIC_MEMORY_HOME")
105-
assert basic_memory_home is not None
106-
base_path = Path(basic_memory_home)
102+
# If project_root is set, constrain all projects to that directory
103+
project_root = self.config_manager.config.project_root
104+
if project_root:
105+
base_path = Path(project_root)
107106

108-
# Sanitize the input path for cloud mode
107+
# Sanitize the input path
109108
# Strip leading slashes, home directory references, and parent directory references
110109
clean_path = path.lstrip("/").replace("~/", "").replace("~", "")
111110

@@ -116,13 +115,14 @@ async def add_project(self, name: str, path: str, set_default: bool = False) ->
116115
path_parts.append(part)
117116
clean_path = "/".join(path_parts) if path_parts else ""
118117

119-
# Construct path relative to BASIC_MEMORY_HOME
118+
# Construct path relative to project_root
120119
resolved_path = (base_path / clean_path).resolve().as_posix()
121120

122-
# Verify the resolved path is actually under BASIC_MEMORY_HOME
121+
# Verify the resolved path is actually under project_root
123122
if not resolved_path.startswith(base_path.resolve().as_posix()):
124123
raise ValueError(
125-
f"Cloud mode requires projects under {basic_memory_home}. Invalid path: {path}"
124+
f"BASIC_MEMORY_PROJECT_ROOT is set to {project_root}. "
125+
f"All projects must be created under this directory. Invalid path: {path}"
126126
)
127127
else:
128128
resolved_path = Path(os.path.abspath(os.path.expanduser(path))).as_posix()

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ def app_config(config_home, tmp_path, monkeypatch) -> BasicMemoryConfig:
7878
def config_manager(
7979
app_config: BasicMemoryConfig, project_config: ProjectConfig, config_home: Path, monkeypatch
8080
) -> ConfigManager:
81+
# Invalidate config cache to ensure clean state for each test
82+
from basic_memory import config as config_module
83+
84+
config_module._CONFIG_CACHE = None
85+
8186
# Create a new ConfigManager that uses the test home directory
8287
config_manager = ConfigManager()
8388
# Update its paths to use the test directory

tests/services/test_project_service.py

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -716,48 +716,47 @@ async def test_synchronize_projects_handles_case_sensitivity_bug(
716716
await project_service.repository.delete(db_project.id)
717717

718718

719-
@pytest.mark.skipif(os.name == "nt", reason="Cloud mode only runs on POSIX systems")
719+
@pytest.mark.skipif(os.name == "nt", reason="Project root constraints only tested on POSIX systems")
720720
@pytest.mark.asyncio
721-
async def test_add_project_cloud_mode_sanitizes_paths(
721+
async def test_add_project_with_project_root_sanitizes_paths(
722722
project_service: ProjectService, config_manager: ConfigManager, tmp_path, monkeypatch
723723
):
724-
"""Test that cloud mode sanitizes and validates project paths."""
725-
# Set up cloud mode environment
726-
cloud_home = tmp_path / "app" / "data" / "basic-memory"
727-
cloud_home.mkdir(parents=True, exist_ok=True)
724+
"""Test that BASIC_MEMORY_PROJECT_ROOT sanitizes and validates project paths."""
725+
# Set up project root environment
726+
project_root_path = tmp_path / "app" / "data"
727+
project_root_path.mkdir(parents=True, exist_ok=True)
728728

729-
monkeypatch.setenv("BASIC_MEMORY_HOME", str(cloud_home))
730-
monkeypatch.setenv("BASIC_MEMORY_CLOUD_MODE", "true")
729+
monkeypatch.setenv("BASIC_MEMORY_PROJECT_ROOT", str(project_root_path))
731730

732-
# Force reload config to pick up cloud mode
733-
from basic_memory.services import project_service as ps_module
731+
# Invalidate config cache so it picks up the new env var
732+
from basic_memory import config as config_module
734733

735-
monkeypatch.setattr(ps_module, "config", config_manager.load_config())
734+
config_module._CONFIG_CACHE = None
736735

737736
test_cases = [
738737
# (input_path, expected_result_path, should_succeed)
739-
("test", str(cloud_home / "test"), True), # Simple relative path
740-
("~/Documents/test", str(cloud_home / "Documents" / "test"), True), # Home directory
738+
("test", str(project_root_path / "test"), True), # Simple relative path
739+
("~/Documents/test", str(project_root_path / "Documents" / "test"), True), # Home directory
741740
(
742741
"/tmp/test",
743-
str(cloud_home / "tmp" / "test"),
742+
str(project_root_path / "tmp" / "test"),
744743
True,
745744
), # Absolute path (sanitized to relative)
746745
(
747746
"../../../etc/passwd",
748-
str(cloud_home),
747+
str(project_root_path),
749748
True,
750-
), # Path traversal (all ../ removed, results in cloud_home)
751-
("folder/subfolder", str(cloud_home / "folder" / "subfolder"), True), # Nested path
749+
), # Path traversal (all ../ removed, results in project_root)
750+
("folder/subfolder", str(project_root_path / "folder" / "subfolder"), True), # Nested path
752751
(
753752
"~/folder/../test",
754-
str(cloud_home / "test"),
753+
str(project_root_path / "test"),
755754
True,
756755
), # Mixed patterns (sanitized to just 'test')
757756
]
758757

759758
for i, (input_path, expected_path, should_succeed) in enumerate(test_cases):
760-
test_project_name = f"cloud-test-{i}"
759+
test_project_name = f"project-root-test-{i}"
761760

762761
try:
763762
# Add the project
@@ -768,9 +767,9 @@ async def test_add_project_cloud_mode_sanitizes_paths(
768767
assert test_project_name in project_service.projects
769768
actual_path = project_service.projects[test_project_name]
770769

771-
# The path should be under cloud_home
772-
assert actual_path.startswith(str(cloud_home)), (
773-
f"Path {actual_path} should start with {cloud_home} for input {input_path}"
770+
# The path should be under project_root
771+
assert actual_path.startswith(str(project_root_path)), (
772+
f"Path {actual_path} should start with {project_root_path} for input {input_path}"
774773
)
775774

776775
# Clean up
@@ -784,29 +783,28 @@ async def test_add_project_cloud_mode_sanitizes_paths(
784783
# Expected failure - continue to next test case
785784

786785

787-
@pytest.mark.skipif(os.name == "nt", reason="Cloud mode only runs on POSIX systems")
786+
@pytest.mark.skipif(os.name == "nt", reason="Project root constraints only tested on POSIX systems")
788787
@pytest.mark.asyncio
789-
async def test_add_project_cloud_mode_rejects_escape_attempts(
788+
async def test_add_project_with_project_root_rejects_escape_attempts(
790789
project_service: ProjectService, config_manager: ConfigManager, tmp_path, monkeypatch
791790
):
792-
"""Test that cloud mode rejects paths that try to escape cloud storage."""
793-
# Set up cloud mode environment
794-
cloud_home = tmp_path / "app" / "data" / "basic-memory"
795-
cloud_home.mkdir(parents=True, exist_ok=True)
791+
"""Test that BASIC_MEMORY_PROJECT_ROOT rejects paths that try to escape the project root."""
792+
# Set up project root environment
793+
project_root_path = tmp_path / "app" / "data"
794+
project_root_path.mkdir(parents=True, exist_ok=True)
796795

797-
# Create a directory outside cloud_home to verify it's not accessible
796+
# Create a directory outside project_root to verify it's not accessible
798797
outside_dir = tmp_path / "outside"
799798
outside_dir.mkdir(parents=True, exist_ok=True)
800799

801-
monkeypatch.setenv("BASIC_MEMORY_HOME", str(cloud_home))
802-
monkeypatch.setenv("BASIC_MEMORY_CLOUD_MODE", "true")
800+
monkeypatch.setenv("BASIC_MEMORY_PROJECT_ROOT", str(project_root_path))
803801

804-
# Force reload config to pick up cloud mode
805-
from basic_memory.services import project_service as ps_module
802+
# Invalidate config cache so it picks up the new env var
803+
from basic_memory import config as config_module
806804

807-
monkeypatch.setattr(ps_module, "config", config_manager.load_config())
805+
config_module._CONFIG_CACHE = None
808806

809-
# All of these should succeed by being sanitized to paths under cloud_home
807+
# All of these should succeed by being sanitized to paths under project_root
810808
# The sanitization removes dangerous patterns, so they don't escape
811809
safe_after_sanitization = [
812810
"../../../etc/passwd",
@@ -815,16 +813,16 @@ async def test_add_project_cloud_mode_rejects_escape_attempts(
815813
]
816814

817815
for i, attack_path in enumerate(safe_after_sanitization):
818-
test_project_name = f"cloud-attack-test-{i}"
816+
test_project_name = f"project-root-attack-test-{i}"
819817

820818
try:
821819
# Add the project
822820
await project_service.add_project(test_project_name, attack_path)
823821

824-
# Verify it was sanitized to be under cloud_home
822+
# Verify it was sanitized to be under project_root
825823
actual_path = project_service.projects[test_project_name]
826-
assert actual_path.startswith(str(cloud_home)), (
827-
f"Sanitized path {actual_path} should be under {cloud_home}"
824+
assert actual_path.startswith(str(project_root_path)), (
825+
f"Sanitized path {actual_path} should be under {project_root_path}"
828826
)
829827

830828
# Clean up
@@ -835,16 +833,17 @@ async def test_add_project_cloud_mode_rejects_escape_attempts(
835833
pass
836834

837835

838-
@pytest.mark.skipif(os.name == "nt", reason="Cloud mode only runs on POSIX systems")
836+
@pytest.mark.skipif(os.name == "nt", reason="Project root constraints only tested on POSIX systems")
839837
@pytest.mark.asyncio
840-
async def test_add_project_local_mode_allows_arbitrary_paths(
838+
async def test_add_project_without_project_root_allows_arbitrary_paths(
841839
project_service: ProjectService, config_manager: ConfigManager, tmp_path, monkeypatch
842840
):
843-
"""Test that local mode (non-cloud) still allows arbitrary paths."""
844-
# Ensure cloud mode is disabled
845-
monkeypatch.setenv("BASIC_MEMORY_CLOUD_MODE", "false")
841+
"""Test that without BASIC_MEMORY_PROJECT_ROOT set, arbitrary paths are allowed."""
842+
# Ensure project_root is not set
843+
if "BASIC_MEMORY_PROJECT_ROOT" in os.environ:
844+
monkeypatch.delenv("BASIC_MEMORY_PROJECT_ROOT")
846845

847-
# Force reload config to pick up local mode
846+
# Force reload config without project_root
848847
from basic_memory.services import project_service as ps_module
849848

850849
monkeypatch.setattr(ps_module, "config", config_manager.load_config())
@@ -853,10 +852,10 @@ async def test_add_project_local_mode_allows_arbitrary_paths(
853852
test_dir = tmp_path / "arbitrary-location"
854853
test_dir.mkdir(parents=True, exist_ok=True)
855854

856-
test_project_name = "local-mode-test"
855+
test_project_name = "no-project-root-test"
857856

858857
try:
859-
# In local mode, we should be able to use arbitrary absolute paths
858+
# Without project_root, we should be able to use arbitrary absolute paths
860859
await project_service.add_project(test_project_name, str(test_dir))
861860

862861
# Verify the path was accepted as-is

0 commit comments

Comments
 (0)