Summary
Semantic Kernel's Python SDK contains a code injection vulnerability in the InMemoryVectorStore filter logic that can lead to remote code execution in affected versions before 1.39.4.
When untrusted data is allowed to influence filter expressions evaluated by the in-memory store, an attacker may execute arbitrary code in the context of the consuming application.
Affected context
The vulnerability exists in applications that embed Semantic Kernel's Python SDK and make use of the InMemoryVectorStore filter functionality, particularly where filter expressions or predicates can be derived from user-controlled or otherwise untrusted data.
It is most relevant in deployments where Semantic Kernel is exposed behind web APIs, bots, or agent-style services that dynamically construct or apply filters to in-memory vector searches, and where the application process has access to sensitive data or powerful execution privileges.
Exploit preconditions
- Attack surface: Application endpoints, agents, or integrations that accept external input and translate it into
InMemoryVectorStorefilter expressions or query parameters. - Required access: Ability to supply or influence filter criteria, query objects, or any dynamic expression that flows into the
InMemoryVectorStorefilter mechanism (e.g., via HTTP APIs, chat inputs, or orchestration prompts). - Trigger point: Execution of a vector search or filtering operation in
InMemoryVectorStorewhere the vulnerable filter logic processes attacker-influenced data. - Why exploitation is possible: The filter implementation performs evaluation of expressions in a way that treats data as executable code rather than as inert values, enabling crafted input to escape intended query semantics and trigger arbitrary code execution.
Why this happens
This issue stems from a classic CWE-94: Improper Control of Generation of Code ('Code Injection') pattern.
The InMemoryVectorStore filter functionality constructs or evaluates filter logic in a manner that does not reliably separate code from data, allowing untrusted values to be interpreted as part of an executable expression.
Instead of treating filter components as strictly structured data (e.g., parsed into a safe AST, or evaluated via a constrained DSL), the implementation permits dynamic evaluation without robust sanitization or strict binding, making it susceptible to injection of arbitrary operations when attacker-controlled content is incorporated.
Potential impact
- Execution of arbitrary Python code within the Semantic Kernel host process.
- Full compromise of the application that embeds Semantic Kernel, including access to its configuration, in-memory secrets, and connected resources.
- Unauthorized access to or modification of data handled by the application, such as vector embeddings, documents, and external connectors.
- Use of the compromised process as a pivot point for further lateral movement inside the hosting environment.
- Interference with or manipulation of AI agent behavior, including poisoning of retrieval results or orchestration flows.
General mitigation guidance
Effective mitigation focuses on eliminating code-like evaluation of filters and hardening how filter logic is constructed:
- Prefer query and filter APIs that treat all input as data, not as executable expressions, using strongly-typed parameters and structured predicates.
- Introduce a constrained, non-Turing-complete filter language or schema where all operators are predefined and values are strictly validated against expected types and ranges.
- Apply rigorous input validation and encoding on any externally-supplied filter components, rejecting or normalizing unexpected operators, symbols, or constructs.
- Isolate Semantic Kernel workloads with least-privilege execution: minimize filesystem, network, and secret access for processes that evaluate user-controlled filters.
- Avoid using in-memory stores intended for development or experimentation as the primary search backend in untrusted, internet-facing, or multi-tenant environments.
- Adopt a defense-in-depth approach: combine safe filter handling with process sandboxing, containerization, and strict runtime permissions.
How teams detect issues like this
Organizations can approach detection of this class of issue through several complementary techniques:
- Static analysis: Configure rules to flag use of dynamic expression evaluation, reflection, or unsafe parsing within filtering and query-building code paths, particularly where untrusted data flows into evaluators.
- Code review heuristics: During review, scrutinize any logic that builds filter strings, lambdas, or expressions from external input; require clear separation of operators and values and explicit parameterization.
- Automated policy checks: Integrate CI pipelines with security linters or SAST tools to detect patterns resembling code generation or dynamic exec/eval in query or filter utilities.
- Runtime monitoring: Instrument logging around filter application, capturing suspicious patterns such as unexpected operators or unusually complex filter expressions, and set alerts for anomalous query shapes.
- Architecture governance: Maintain guidelines that prohibit dynamic code evaluation in query layers and periodically audit SDK integration points (like vector stores) for adherence to these constraints.

