DeepTeam is a framework to red team LLMs and AI agents.
feat: add CodeScanTemplate and DEFAULT_CODE_SCAN_VULNERABILITIES
tanayvaswani002a980986cf
diff --git a/deepteam/code_scanner/__init__.py b/deepteam/code_scanner/__init__.pyindex ddc823b4..d0024047 100644--- a/deepteam/code_scanner/__init__.py+++ b/deepteam/code_scanner/__init__.py@@ -10,6 +10,8 @@ from .taxonomy import (is_known,vulnerability_names,)+from .constants import DEFAULT_CODE_SCAN_VULNERABILITIES+from .template import CodeScanTemplate__all__ = ["CodeChunk",@@ -20,4 +22,6 @@ __all__ = ["allowed_types","is_known","vulnerability_names",+ "DEFAULT_CODE_SCAN_VULNERABILITIES",+ "CodeScanTemplate",]diff --git a/deepteam/code_scanner/constants.py b/deepteam/code_scanner/constants.pynew file mode 100644index 00000000..8d75eb20--- /dev/null+++ b/deepteam/code_scanner/constants.py@@ -0,0 +1,18 @@+from typing import List++DEFAULT_CODE_SCAN_VULNERABILITIES: List[str] = [+ "Unexpected Code Execution",+ "Shell Injection",+ "SQL Injection",+ "SSRF",+ "Excessive Agency",+ "Indirect Instruction",+ "Tool Metadata Poisoning",+ "Tool Orchestration Abuse",+ "BFLA",+ "BOLA",+ "RBAC",+ "Debug Access",+ "Prompt Leakage",+ "PII Leakage",+]diff --git a/deepteam/code_scanner/schema.py b/deepteam/code_scanner/schema.pyindex 1e6b88d7..2b44afd8 100644--- a/deepteam/code_scanner/schema.py+++ b/deepteam/code_scanner/schema.py@@ -1,12 +1,7 @@from pydantic import BaseModel, Fieldfrom typing import List, Literal, Optional-Severity = Literal[- "low",- "medium",- "high",- "critical"-]+Severity = Literal["low", "medium", "high", "critical"]class CodeChunk(BaseModel):diff --git a/deepteam/code_scanner/template.py b/deepteam/code_scanner/template.pynew file mode 100644index 00000000..f19e3434--- /dev/null+++ b/deepteam/code_scanner/template.py@@ -0,0 +1,122 @@+from typing import List, Optional++from deepteam.code_scanner.constants import (+ DEFAULT_CODE_SCAN_VULNERABILITIES,+)+from deepteam.code_scanner.taxonomy import allowed_types+++class CodeScanTemplate:+ """+ The rubric for static AI-security code scanning.+ """++ @staticmethod+ def _render_catalog(vulnerabilities: List[str]) -> str:+ """+ Render `- Name: type1, type2` lines, pulling the allowed subtypes+ straight from the taxonomy so the judge can only use known labels.+ """+ lines = []+ for name in vulnerabilities:+ types = allowed_types(name)+ if not types:+ continue+ lines.append(f" - {name}: {', '.join(types)}")+ return "\n".join(lines)++ @staticmethod+ def generate_code_batch_evaluation(+ batch_data: str,+ vulnerabilities: Optional[List[str]] = None,+ guidance: Optional[str] = None,+ ) -> str:+ """Build the evaluation prompt for one batch of code chunks.++ Args:+ batch_data: JSON array of code chunks. Each has `file_path`, `content`, optional `language`, and `start_line` (the absolute line number `content`'s first line maps to).+ vulnerabilities: Category names to scan for. Defaults to `DEFAULT_CODE_SCAN_VULNERABILITIES`. Pass a subset to scope.+ guidance: Optional free-text project guidance (e.g. "treat any PII exposure as high severity").+ """+ vulnerabilities = vulnerabilities or DEFAULT_CODE_SCAN_VULNERABILITIES+ catalog = CodeScanTemplate._render_catalog(vulnerabilities)++ guidance_block = ""+ if guidance and guidance.strip():+ guidance_block = (+ "\n ADDITIONAL PROJECT GUIDANCE (apply on top of the "+ f"above):\n {guidance.strip()}\n"+ )++ return f"""+ You are an expert application security reviewer specializing in AI / LLM+ applications. Review the following batch of source-code chunks and report+ ONLY vulnerabilities that match one of the categories below. This is+ STATIC analysis, reason about how the code could be exploited, not+ about any particular runtime input.++ VULNERABILITIES TO LOOK FOR (category: allowed types): {catalog}++ WHAT TO FOCUS ON (these are AI-app risks, not generic code smells):+ - Untrusted model output flowing into a dangerous sink: eval/exec, shell+ commands, SQL, file paths, or outbound HTTP built from an LLM's output.+ - Over-permissioned agents/tools: capabilities, permissions, or autonomy+ broader than the task needs.+ - Unsanitised external content (RAG documents, tool outputs, retrieved+ data) injected into prompts.+ - Hardcoded secrets / provider API keys / credentials in source -> report+ as "Prompt Leakage" / "secrets_and_credentials".+ - Exposed debug/admin endpoints, or missing authorization checks around+ actions the agent can take.++ CRITICAL INSTRUCTIONS:+ 1. Flag ONLY an unambiguous, primary match to one of the categories AND+ one of its exact listed types. When in doubt, do NOT flag. Do not+ report generic style issues, performance, or non-AI code quality.+ 2. Line numbers are ABSOLUTE. Each chunk carries `file_path` and a+ `start_line`; line K of a chunk's `content` is absolute line+ (start_line + K - 1). Report `filePath`, `lineStart`, and `lineEnd`+ against the real file.+ 3. Severity reflects exploitability and impact: "low", "medium",+ "high", or "critical".+ 4. `vulnerabilityType` MUST be one of the exact types listed for the+ chosen category above.+ 5. Give a precise `reason`, an actionable `recommendation`, and the+ offending `codeSnippet`.{guidance_block}+ **+ EXAMPLE JSON OUTPUT++ Example with a finding:+ {{+ "findings": [+ {{+ "filePath": "app/agent.py",+ "lineStart": 42,+ "lineEnd": 42,+ "vulnerability": "Unexpected Code Execution",+ "vulnerabilityType": "eval_usage",+ "severity": "critical",+ "reason": "eval() is called on text returned by the LLM, so a prompt-injected response can execute arbitrary Python.",+ "recommendation": "Never eval LLM output; parse with ast.literal_eval or a strict schema.",+ "codeSnippet": "result = eval(llm_response)"+ }}+ ]+ }}++ Example with no findings:+ {{+ "findings": []+ }}+ **++ Batch Data (JSON array of code chunks):+ {batch_data}++ Before returning, drop any finding that is not a direct, unambiguous+ match to one of the listed categories and one of its exact types.++ Return ONLY a JSON object with a 'findings' key containing a list of+ finding objects.++ JSON:+ """