Skip to content

JS: Add prompt injection detection (CWE-1427) for OpenAI, Anthropic, …#21780

Closed
BazookaMusic wants to merge 1 commit intogithub:mainfrom
BazookaMusic:bazookamusic/prompt-injection-cwe1427-js
Closed

JS: Add prompt injection detection (CWE-1427) for OpenAI, Anthropic, …#21780
BazookaMusic wants to merge 1 commit intogithub:mainfrom
BazookaMusic:bazookamusic/prompt-injection-cwe1427-js

Conversation

@BazookaMusic
Copy link
Copy Markdown

…and Google GenAI SDKs

Add experimental CodeQL query detecting prompt injection vulnerabilities in JavaScript/TypeScript applications using AI SDK libraries.

Modeled frameworks:

  • openai (OpenAI, AzureOpenAI): responses, chat.completions, completions, images, embeddings, beta.assistants, beta.threads, audio APIs
  • @openai/agents: Agent instructions, handoffDescription, run/Runner.run, asTool, tool()
  • @anthropic-ai/sdk: messages.create, beta.messages.create, beta.agents.create/update
  • @google/genai (GoogleGenAI): generateContent, generateContentStream, generateImages, editImage, chats, live.connect

Includes role-based filtering (system/developer/assistant/model roles) and constant-comparison sanitizer guard.

…and Google GenAI SDKs

Add experimental CodeQL query detecting prompt injection vulnerabilities
in JavaScript/TypeScript applications using AI SDK libraries.

Modeled frameworks:
- openai (OpenAI, AzureOpenAI): responses, chat.completions, completions,
  images, embeddings, beta.assistants, beta.threads, audio APIs
- @openai/agents: Agent instructions, handoffDescription, run/Runner.run,
  asTool, tool()
- @anthropic-ai/sdk: messages.create, beta.messages.create,
  beta.agents.create/update
- @google/genai (GoogleGenAI): generateContent, generateContentStream,
  generateImages, editImage, chats, live.connect

Includes role-based filtering (system/developer/assistant/model roles)
and constant-comparison sanitizer guard.
@github-actions
Copy link
Copy Markdown
Contributor

QHelp previews:

javascript/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

*
* See https://github.com/openai/openai-agents-js.
*/
module AgentSDK {
@@ -0,0 +1 @@
./experimental/Security/CWE-1427/PromptInjection.ql
@BazookaMusic BazookaMusic deleted the bazookamusic/prompt-injection-cwe1427-js branch April 30, 2026 15:52
@BazookaMusic
Copy link
Copy Markdown
Author

Ignore, was trying to create it for the fork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants