fix(memory): reject absolute and escaping memory names in get_memory_file_path#1665
Open
AmirF194 wants to merge 1 commit into
Open
fix(memory): reject absolute and escaping memory names in get_memory_file_path#1665AmirF194 wants to merge 1 commit into
AmirF194 wants to merge 1 commit into
Conversation
a497adc to
f4e81bd
Compare
f4e81bd to
3ab9ff3
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.
What
MemoryManager.get_memory_file_pathguards against..segments "for security reasons", but not against absolute memory names. Because it builds the path withbase_dir / "/".join(parts[:-1]), an absolute name discards the base directory:Path(project_memory_dir) / "/etc/cron.d"resolves to/etc/cron.d, escaping the.serena/memoriessandbox. Sincesave_memory/load_memory/delete_memory/move_memoryall route through this method, a crafted (e.g. prompt-injected) memory name could read, write, or delete arbitrary*.mdfiles outside the sandbox. Both the project andglobal/branches were affected.Reproduced on current
main:get_memory_file_path("/etc/cron.d/backdoor")returns/etc/cron.d/backdoor.mdand even attempts tomkdirthat directory (only stopped by OS permissions), while the existing controlget_memory_file_path("../../etc/x")correctly raises.Fix
..check (os.path.isabs(name) or "" in parts). This runs on the full name, so it covers both branches before any directory is created._resolve_memory_pathhelper that adds a lexical containment backstop (os.path.normpath+is_relative_to, before anymkdir) enforcing the invariant that every returned path stays inside its base memories directory.The containment check is deliberately lexical rather than
Path.resolve()-based: serena intentionally supports directory symlinks insidememories/(e.g. a monorepo symlinking each submodule's memory dir), andresolve()would follow those symlinks out of the base and wrongly reject them. The existingTestListMemoriesFollowsSymlinksbehavior is preserved.Verification
TestGetMemoryFilePathContainment(offline): absolute project name, absolute system path, absolute global name,..still rejected, plus containment assertions that accepted names resolve inside their base dir.mainand pass on this branch; fulltest/serena/test_memories_manager.pyis 112 passed (including the symlink feature test).uv run poe lint(ruff format + check) anduv run poe type-check(ty) are clean.Checklist
CONTRIBUTING.mdregarding the scope of PRs (single logical change).CHANGELOG.md.Implementation done with AI assistance.