Skip to content

Python: Model openai packages for Prompt injection#3

Closed
mbaluda wants to merge 26 commits intomainfrom
prompt-injection
Closed

Python: Model openai packages for Prompt injection#3
mbaluda wants to merge 26 commits intomainfrom
prompt-injection

Conversation

@mbaluda
Copy link
Copy Markdown
Owner

@mbaluda mbaluda commented Jan 2, 2026

This pull request introduces a new CodeQL query for detecting prompt injection vulnerabilities in Python code, specifically targeting AI prompting APIs such as agents and openai. The changes include new taint flow and type models, a customizable dataflow configuration, documentation, and comprehensive test coverage.

@github-actions github-actions bot added documentation Improvements or additions to documentation Python labels Jan 2, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 2, 2026

QHelp previews:

python/ql/src/experimental/Security/CWE-1427/PromptInjection.qhelp

Prompt injection

Prompts can be constructed to bypass the original purposes of an agent and lead to sensitive data leak or operations that were not intended.

Recommendation

Sanitize user input and also avoid using user input in developer or system level prompts.

Example

In the following examples, the cases marked GOOD show secure prompt construction; whereas in the case marked BAD they may be susceptible to prompt injection.

from flask import Flask, request
from agents import Agent
from guardrails import GuardrailAgent

@app.route("/parameter-route")
def get_input():
    input = request.args.get("input")

    goodAgent = GuardrailAgent(  # GOOD: Agent created with guardrails automatically configured.
        config=Path("guardrails_config.json"),
        name="Assistant",
        instructions="This prompt is customized for " + input)

    badAgent = Agent(
        name="Assistant",
        instructions="This prompt is customized for " + input  # BAD: user input in agent instruction.
    )

References

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements a new prompt injection security query for Python code that detects when user-controlled input flows into AI model prompts. The implementation adds taint tracking models for the OpenAI and agents libraries to identify potential CWE-1427 (Prompt Injection) vulnerabilities.

  • Adds a new py/prompt-injection query with supporting data flow analysis
  • Implements models for openai and agents Python modules to track prompt injection sinks
  • Introduces the AIPrompt concept to the codebase for modeling AI prompting mechanisms

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
python/ql/test/query-tests/Security/CWE-1427-PromptInjection/openai_test.py Test file demonstrating prompt injection vulnerabilities in OpenAI API usage
python/ql/test/query-tests/Security/CWE-1427-PromptInjection/agent_instructions.py Test file for agent instruction parameter injection
python/ql/test/query-tests/Security/CWE-1427-PromptInjection/PromptInjection.qlref Query reference configuration for test execution
python/ql/test/query-tests/Security/CWE-1427-PromptInjection/PromptInjection.expected Expected test results for the query
python/ql/src/Security/CWE-1427/examples/example.py Example code showing good and bad practices for prompt construction
python/ql/src/Security/CWE-1427/PromptInjection.ql Main query implementation for detecting prompt injection vulnerabilities
python/ql/src/Security/CWE-1427/PromptInjection.qhelp Documentation and guidance for the prompt injection query
python/ql/lib/semmle/python/security/dataflow/PromptInjectionQuery.qll Taint-tracking configuration for prompt injection detection
python/ql/lib/semmle/python/security/dataflow/PromptInjectionCustomizations.qll Sources, sinks, and sanitizers for prompt injection analysis
python/ql/lib/semmle/python/frameworks/openai.model.yml Model definitions for OpenAI API prompt injection sinks
python/ql/lib/semmle/python/frameworks/agent.model.yml Model definitions for agents library prompt injection sinks
python/ql/lib/semmle/python/Concepts.qll Adds AIPrompt concept for modeling AI prompting mechanisms
python/ql/lib/change-notes/2026-01-02-prompt-injection.md Change notes documenting the new feature

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread python/ql/src/Security/CWE-1427/examples/example.py Outdated
Comment thread python/ql/lib/semmle/python/Concepts.qll Outdated
Comment thread python/ql/src/Security/CWE-1427/PromptInjection.qhelp Outdated
Comment thread python/ql/lib/change-notes/2026-01-02-prompt-injection.md Outdated
Comment thread python/ql/src/Security/CWE-1427/examples/example.py Outdated
Comment thread python/ql/src/Security/CWE-1427/examples/example.py Outdated
mbaluda and others added 6 commits January 6, 2026 11:39
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@mbaluda mbaluda changed the title Models python module openai for Prompt Injection Python: Model openai packages for Prompt injection Jan 6, 2026
@github-actions github-actions bot added the Java label Jan 7, 2026
@github-actions github-actions bot removed the Java label Jan 7, 2026
mbaluda and others added 5 commits January 7, 2026 21:59
* Add testcase and coverage for agents sdk runner run with input param

* Rename agent sdk module for clarity

* Add case for unnamed param use in runner run from agent sdk
@mbaluda mbaluda closed this Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants