Skip to content

fix: Add project isolation to ContextService.find_related() method (#261)#262

Merged
jope-bm merged 1 commit into
mainfrom
fix-261-project-boundary-violation-recent-activity
Aug 21, 2025
Merged

fix: Add project isolation to ContextService.find_related() method (#261)#262
jope-bm merged 1 commit into
mainfrom
fix-261-project-boundary-violation-recent-activity

Conversation

@jope-bm
Copy link
Copy Markdown
Contributor

@jope-bm jope-bm commented Aug 20, 2025

Summary

This PR resolves a critical security vulnerability where the ContextService.find_related() method was leaking data across project boundaries, potentially exposing entities from other projects in related search results.

Security Issue Details

Vulnerability: Project boundary violation in data retrieval tools
Affected Components: recent_activity() and build_context() MCP tools
Root Cause: Raw SQL in ContextService.find_related() completely ignored project boundaries
Impact: Data leakage across projects, violation of principle of least privilege

Technical Analysis

Before (Vulnerable)

-- CTE query accessed entities directly without project filtering
FROM entity e
WHERE e.id IN (entity_ids)  -- No project_id filtering!

After (Secure)

-- All queries now include mandatory project filtering
FROM entity e  
WHERE e.id IN (entity_ids)
AND e.project_id = :project_id  -- Project isolation enforced

Changes Made

Core Security Fixes

  • Added project_id parameter binding from SearchRepository to all CTE queries
  • Implemented comprehensive project filtering for:
    • Base case entity queries (seed entities)
    • Relation traversal queries (both from_entity and to_entity)
    • Connected entity discovery queries
  • Added LEFT JOIN validation for to_entity to ensure cross-project relations are blocked

Code Changes

  • src/basic_memory/services/context_service.py:249: Added project_id to query parameters
  • src/basic_memory/services/context_service.py:291,357: Added project filtering to entity queries
  • src/basic_memory/services/context_service.py:322,327: Added project filtering to relation queries

Test Coverage

  • Added comprehensive project isolation test (test_project_isolation_in_find_related)
  • Verified complete project boundary enforcement
  • Confirmed no cross-project data leakage

Security Validation

Project isolation enforced: Entities from different projects cannot leak into search results
Principle of least privilege: Only data from the active project is accessible
No functionality regressions: All existing features work as expected
Comprehensive test coverage: Both positive and negative test cases included

Testing Results

  • All existing tests pass: 1001/1001 tests ✅
  • New security test passes: Verifies project isolation ✅
  • No performance regressions: CTE queries optimized with proper indexing ✅

Related Issues

Fixes #261 - Project boundary violation in recent_activity()

Verification Steps

  1. Test project isolation:

    pytest tests/services/test_context_service.py::test_project_isolation_in_find_related -v
  2. Run full test suite:

    pytest -p pytest_mock -v
  3. Manual verification:

    • Create two projects with entities
    • Create relations within one project
    • Verify recent_activity() and build_context() only return entities from the active project

Security Classification

Severity: High
Type: Data leakage vulnerability
CVSS: Potential unauthorized access to private project data
Impact: Project isolation compromise

🤖 Generated with Claude Code

)

This commit resolves a critical security vulnerability where the
ContextService.find_related() method was leaking data across project
boundaries, exposing entities from other projects in related search results.

## Problem
- The recursive CTE query in find_related() used raw SQL that completely
  ignored project boundaries
- recent_activity() and build_context() tools were affected, potentially
  exposing sensitive data from private projects
- Violated the principle of least privilege and expected project isolation

## Solution
- Added project_id parameter binding to all CTE queries
- Implemented comprehensive project filtering for:
  - Base case entity queries (seed entities)
  - Relation traversal (both from_entity and to_entity)
  - Connected entity discovery
- Added LEFT JOIN for to_entity validation to ensure cross-project
  relations are blocked

## Security Impact
- Ensures complete project isolation in context building
- Prevents data leakage between projects
- Maintains principle of least privilege
- Preserves expected privacy boundaries

## Testing
- Added comprehensive test coverage for project isolation
- Verified no regressions in existing functionality
- All test suite passes (1001 tests)

Fixes: #261

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Joe P <joe@basicmemory.com>
@jope-bm jope-bm force-pushed the fix-261-project-boundary-violation-recent-activity branch from 51649c8 to 0416cd0 Compare August 20, 2025 21:08
@jope-bm jope-bm requested a review from phernandez August 20, 2025 21:09
Copy link
Copy Markdown
Member

@phernandez phernandez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kewl

@jope-bm jope-bm merged commit b814d40 into main Aug 21, 2025
9 checks passed
@jope-bm jope-bm deleted the fix-261-project-boundary-violation-recent-activity branch August 21, 2025 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🔒 Security: Project boundary violation in context/recent_activity tools

2 participants