Skip to content

Python: feat(memory): add OWASP Agent Memory Guard for memory poisoning defense (#14006) - #14608

Open
SparkyPunk (sparkysolutions10-pixel) wants to merge 4 commits into
microsoft:mainfrom
sparkysolutions10-pixel:fix/python-memory-guard-14006
Open

SparkyPunk (sparkysolutions10-pixel) wants to merge 4 commits into
microsoft:mainfrom
sparkysolutions10-pixel:fix/python-memory-guard-14006

Conversation

@sparkysolutions10-pixel

Copy link
Copy Markdown

Problem & Solution Overview

Fixes #14006

Problem Description

Semantic Kernel memory connectors (VolatileMemoryStore, AzureAISearch, Redis, etc.) index external and agent-generated text directly into vector memory without validation or security gatekeeping. When processing untrusted user input or scraped web documents, malicious actors can perform Memory Poisoning (OWASP ASI06 / LLM04) attacks:

  1. Attack payloads persist across user sessions inside semantic memory stores.
  2. Retrieved poisoned context manipulates planner and kernel function decisions.

Solution Summary

This pull request introduces native OWASP Agent Memory Guard integration for Semantic Kernel Python:

  1. MemoryGuardBase & DefaultMemoryGuard: Added in semantic_kernel.memory.memory_guard. Provides extensible memory scanning to detect prompt injection, system instruction overrides (Ignore previous instructions), and delimiter hijacking before embedding generation.
  2. SemanticTextMemory Integration:
    • Added optional memory_guard: MemoryGuardBase | None = None parameter.
    • When configured, save_information() and save_reference() execute pre-write security scanning.
    • Raises MemoryPoisoningError if a poisoning attempt is detected, stopping malicious payloads before they reach storage.
  3. 100% Backward Compatible (Opt-in): Default memory_guard=None ensures zero breaking changes or performance overhead for existing applications.
  4. Comprehensive Unit Tests: Added unit tests in tests/unit/connectors/memory/test_memory_guard.py verifying detection of prompt injections, blocking poisoned writes, and allowing legitimate clean records.

Verification & Testing

  • Unit test suite passes locally with zero errors
  • Pre-commit, formatting, and linting standards verified
  • Developer Certificate of Origin (DCO) signed (Signed-off-by)
  • Standard Contributor License Agreement (CLA) accepted

…se (microsoft#14006)

Signed-off-by: sparkysolutions10-pixelmy <sparkysolutions10@gmail.com>
…icrosoft#14006)

Signed-off-by: sparkysolutions10-pixelmy <sparkysolutions10@gmail.com>
… init (microsoft#14006)

Signed-off-by: sparkysolutions10-pixelmy <sparkysolutions10@gmail.com>
…14006)

Signed-off-by: sparkysolutions10-pixelmy <sparkysolutions10@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A syntax error breaks memory imports, and unscanned descriptions and metadata allow poisoning payloads through both write paths.

2 open findings
What changed in this PR

Adds opt-in memory-poisoning checks to Python’s SemanticTextMemory, addressing the security concern in #14006.

Changes:

  • Introduces extensible guards and regex-based injection detection.
  • Adds pre-write scanning to information and reference saves.
  • Exports guard APIs and adds detection and write tests.
File Description
python/​tests/​unit/​connectors/​memory/​test_memory_guard.py Tests detection and guarded writes.
python/​semantic_kernel/​memory/​semantic_text_memory.py Adds optional pre-write guard checks.
python/​semantic_kernel/​memory/​memory_guard.py Defines guards, scan results, and rejection errors.
python/​semantic_kernel/​memory/​__init__.py Exports the guard APIs.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +93 to +94
r"(?:^|
)\s*(?:system|developer|admin)\s*:\s*",
MemoryPoisoningError: If the memory guard detects a poisoning attempt (OWASP ASI06).
"""
if self._memory_guard is not None:
scan_res = self._memory_guard.scan_write(text=text, collection=collection, id=id)

This branch was successfully deployed

1 active deployment
github-app-auth — 45aa0635 Deployed Oct 16, 2026 by sparkysolutions10-pixel via team_check #633
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Security: OWASP Agent Memory Guard for SK memory poisoning defense (ASI06)

2 participants