fix: Add project isolation to ContextService.find_related() method (#261)#262
Merged
Merged
Conversation
) 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>
51649c8 to
0416cd0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()andbuild_context()MCP toolsRoot Cause: Raw SQL in
ContextService.find_related()completely ignored project boundariesImpact: Data leakage across projects, violation of principle of least privilege
Technical Analysis
Before (Vulnerable)
After (Secure)
Changes Made
Core Security Fixes
Code Changes
src/basic_memory/services/context_service.py:249: Added project_id to query parameterssrc/basic_memory/services/context_service.py:291,357: Added project filtering to entity queriessrc/basic_memory/services/context_service.py:322,327: Added project filtering to relation queriesTest Coverage
test_project_isolation_in_find_related)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
Related Issues
Fixes #261 - Project boundary violation in recent_activity()
Verification Steps
Test project isolation:
Run full test suite:
Manual verification:
recent_activity()andbuild_context()only return entities from the active projectSecurity Classification
Severity: High
Type: Data leakage vulnerability
CVSS: Potential unauthorized access to private project data
Impact: Project isolation compromise
🤖 Generated with Claude Code