fix: add namespace validation and fix TOCTOU in elasticsearch memory …#447
Merged
mkmeral merged 2 commits intostrands-agents:mainfrom Apr 9, 2026
Merged
Conversation
…tool - Add _validate_namespace() to reject non-string and special character namespace values (same pattern as mongodb_memory fix in PR strands-agents#321) - Fix _get_memory to use bool query with memory_id + namespace instead of get-by-ID with client-side namespace check - Fix _delete_memory to use atomic delete_by_query with namespace constraint, eliminating TOCTOU race condition between check and delete - Validate namespace early in elasticsearch_memory() entry point - Add tests for injection prevention, strict validation, and atomic delete
Contributor
Author
|
/strands review |
pgrayy
approved these changes
Apr 9, 2026
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.
Description
This PR adds namespace validation and fixes a TOCTOU (Time-of-Check to Time-of-Use) vulnerability in the Elasticsearch memory tool — the same class of vulnerability that was fixed for MongoDB in PR #321.
The
_delete_memoryfunction previously performed deletion using document ID only with no server-side namespace constraint. Combined with the lack of namespace input validation and a client-side-only namespace check in_get_memory, this allowed potential cross-tenant memory access and deletion.Changes
Namespace validation (
_validate_namespace){"$ne": ""})^[A-Za-z0-9_-]{1,64}$— same pattern as the MongoDB fixelasticsearch_memory()entry point before any operations_get_memory— server-side namespace enforcementes_client.get(id=memory_id)then client-sideif source.get("namespace") != namespacees_client.search()with aboolquery requiring bothmemory_idANDnamespaceterms_delete_memory— atomic delete with namespace constraint_get_memory()check →es_client.delete(id=memory_id)(TOCTOU race window, no namespace in delete)es_client.delete_by_query()with bothmemory_idANDnamespacein the queryRelated Issues
Type of Change
Bug fix
Testing
hatch run test tests/test_elasticsearch_memory.py -vAlso ran integ tests with the script below
Checklist