Skip to content

DeepTeam is a framework to red team LLMs and AI agents.

feat: init code vuln scanning

tanayvaswanicd4868daec08
diff --git a/deepteam/code_scanner/__init__.py b/deepteam/code_scanner/__init__.py
new file mode 100644
index 00000000..ddc823b4
--- /dev/null
+++ b/deepteam/code_scanner/__init__.py
@@ -0,0 +1,23 @@
+from .schema import (
+ CodeChunk,
+ CodeFinding,
+ CodeFindingsList,
+ Severity,
+)
+from .taxonomy import (
+ KNOWN_VULNERABILITIES,
+ allowed_types,
+ is_known,
+ vulnerability_names,
+)
+
+__all__ = [
+ "CodeChunk",
+ "CodeFinding",
+ "CodeFindingsList",
+ "Severity",
+ "KNOWN_VULNERABILITIES",
+ "allowed_types",
+ "is_known",
+ "vulnerability_names",
+]
diff --git a/deepteam/code_scanner/schema.py b/deepteam/code_scanner/schema.py
new file mode 100644
index 00000000..1e6b88d7
--- /dev/null
+++ b/deepteam/code_scanner/schema.py
@@ -0,0 +1,93 @@
+from pydantic import BaseModel, Field
+from typing import List, Literal, Optional
+
+Severity = Literal[
+ "low",
+ "medium",
+ "high",
+ "critical"
+]
+
+
+class CodeChunk(BaseModel):
+ """
+ A unit of source code handed to the scanner for evaluation.
+ """
+
+ file_path: str = Field(
+ ...,
+ description="Repository-relative path of the file this code came from.",
+ )
+ content: str = Field(
+ ...,
+ description="The source code to scan.",
+ )
+ language: Optional[str] = Field(
+ default=None,
+ description="Detected language, e.g. 'python' or 'typescript'.",
+ )
+ start_line: int = Field(
+ default=1,
+ description=(
+ "Absolute line number of the first line in `content`. 1 for a "
+ "whole file; the hunk's starting line for a diff."
+ ),
+ )
+
+
+class CodeFinding(BaseModel):
+ """
+ A single vulnerability located in source code.
+ """
+
+ filePath: str = Field(
+ ...,
+ description="Repository-relative path of the file containing the issue.",
+ )
+ lineStart: int = Field(
+ ...,
+ description="1-indexed line where the vulnerable code begins.",
+ )
+ lineEnd: Optional[int] = Field(
+ default=None,
+ description="1-indexed line where the vulnerable code ends, if multi-line.",
+ )
+ vulnerability: str = Field(
+ ...,
+ description=(
+ "The vulnerability category. MUST be one of deepteam's known "
+ "vulnerability names (see VULNERABILITY_CLASSES_MAP), e.g. "
+ "'Unexpected Code Execution', 'SSRF', 'Excessive Agency'."
+ ),
+ )
+ vulnerabilityType: str = Field(
+ ...,
+ description=(
+ "The specific subtype within the category, drawn from that "
+ "vulnerability's allowed types, e.g. 'eval_usage' or "
+ "'shell_command_execution'."
+ ),
+ )
+ severity: Severity = Field(
+ ...,
+ description="Impact rating: 'low', 'medium', 'high', or 'critical'.",
+ )
+ reason: str = Field(
+ ...,
+ description=(
+ "Why this code is vulnerable: the specific construct and how it "
+ "could be exploited."
+ ),
+ )
+ recommendation: Optional[str] = Field(
+ default=None,
+ description="Concrete suggested fix for the issue.",
+ )
+ codeSnippet: Optional[str] = Field(
+ default=None,
+ description="The exact offending code excerpt.",
+ )
+
+
+class CodeFindingsList(BaseModel):
+ findings: List[CodeFinding]
diff --git a/deepteam/code_scanner/taxonomy.py b/deepteam/code_scanner/taxonomy.py
new file mode 100644
index 00000000..9b677e34
--- /dev/null
+++ b/deepteam/code_scanner/taxonomy.py
@@ -0,0 +1,29 @@
+from typing import Dict, List, Optional
+
+from deepteam.vulnerabilities.constants import VULNERABILITY_INFO_MAP
+
+KNOWN_VULNERABILITIES: Dict[str, List[str]] = {
+ name: list(info.allowed_types)
+ for name, info in VULNERABILITY_INFO_MAP.items()
+}
+
+
+def vulnerability_names() -> List[str]:
+ """All known deepteam vulnerability category names."""
+ return list(KNOWN_VULNERABILITIES.keys())
+
+
+def allowed_types(vulnerability: str) -> List[str]:
+ """Allowed subtypes for a category ([] if the category is unknown)."""
+ return KNOWN_VULNERABILITIES.get(vulnerability, [])
+
+
+def is_known(
+ vulnerability: str, vulnerability_type: Optional[str] = None
+) -> bool:
+ """Whether a (category[, subtype]) pair exists in deepteam's taxonomy."""
+ if vulnerability not in KNOWN_VULNERABILITIES:
+ return False
+ if vulnerability_type is None:
+ return True
+ return vulnerability_type in KNOWN_VULNERABILITIES[vulnerability]