Padmanav commited on
Commit
3ab2a42
·
1 Parent(s): d31d5dc

security: apply sanitize_for_prompt before LLM submission in all three code-reading agents

Browse files
app/agents/bug_detection_agent.py CHANGED
@@ -5,6 +5,8 @@ from app.tools.file_scanner import get_python_files
5
  from app.core.llm import call_llm_for_json
6
  from app.core.prompts import BUG_DETECTION_PROMPT
7
  from app.models.issue import IssueReport, Bug, Warning, Suggestion
 
 
8
 
9
  logger = get_logger(__name__)
10
 
@@ -27,6 +29,7 @@ async def run(local_path: str) -> IssueReport:
27
  try:
28
  with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
29
  content = f.read()[:2000] # Limit to 2000 chars per file
 
30
  code_samples.append(f"### {file_path}\n{content}")
31
  except Exception as e: # nosec B110
32
  logger.warning("Failed to read file", extra={"file": file_path, "error": str(e)})
 
5
  from app.core.llm import call_llm_for_json
6
  from app.core.prompts import BUG_DETECTION_PROMPT
7
  from app.models.issue import IssueReport, Bug, Warning, Suggestion
8
+ from app.tools.file_scanner import scan_repository, get_python_files, sanitize_for_prompt
9
+
10
 
11
  logger = get_logger(__name__)
12
 
 
29
  try:
30
  with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
31
  content = f.read()[:2000] # Limit to 2000 chars per file
32
+ content = sanitize_for_prompt(content)
33
  code_samples.append(f"### {file_path}\n{content}")
34
  except Exception as e: # nosec B110
35
  logger.warning("Failed to read file", extra={"file": file_path, "error": str(e)})
app/agents/code_review_agent.py CHANGED
@@ -1,5 +1,5 @@
1
  from app.core.logger import get_logger
2
-
3
  import os
4
  from app.tools.file_scanner import get_python_files
5
  from app.core.llm import call_llm_for_json
@@ -25,6 +25,7 @@ async def run(local_path: str, metadata: RepositoryMetadata) -> ReviewSuggestion
25
  try:
26
  with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
27
  content = f.read()[:2500]
 
28
  source_samples.append(f"### {file_path}\n{content}")
29
  except Exception as e: # nosec B110
30
  logger.warning("Failed to read file", extra={"file": file_path, "error": str(e)})
 
1
  from app.core.logger import get_logger
2
+ from app.tools.file_scanner import scan_repository, get_python_files, sanitize_for_prompt
3
  import os
4
  from app.tools.file_scanner import get_python_files
5
  from app.core.llm import call_llm_for_json
 
25
  try:
26
  with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
27
  content = f.read()[:2500]
28
+ content = sanitize_for_prompt(content)
29
  source_samples.append(f"### {file_path}\n{content}")
30
  except Exception as e: # nosec B110
31
  logger.warning("Failed to read file", extra={"file": file_path, "error": str(e)})
app/agents/test_generation_agent.py CHANGED
@@ -7,6 +7,8 @@ from app.core.prompts import TEST_GENERATION_PROMPT
7
  from app.models.review import GeneratedTests
8
  import subprocess # nosec B404
9
  import sys
 
 
10
 
11
  logger = get_logger(__name__)
12
 
@@ -52,6 +54,7 @@ async def run(local_path: str) -> GeneratedTests:
52
  try:
53
  with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
54
  content = f.read()[:3000]
 
55
  source_code_samples.append(f"### {file_path}\n{content}")
56
  except Exception: # nosec B110
57
  pass
@@ -77,7 +80,7 @@ async def run(local_path: str) -> GeneratedTests:
77
  line for i, line in enumerate(lines)
78
  if not (i == 0 and line.startswith("```")) and line != "```"
79
  )
80
-
81
  # Step 4 - Count generated tests
82
  test_count = llm_response.count("def test_")
83
  coverage_before = _measure_coverage(local_path)
 
7
  from app.models.review import GeneratedTests
8
  import subprocess # nosec B404
9
  import sys
10
+ from app.tools.file_scanner import scan_repository, get_python_files, sanitize_for_prompt
11
+
12
 
13
  logger = get_logger(__name__)
14
 
 
54
  try:
55
  with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
56
  content = f.read()[:3000]
57
+ content = sanitize_for_prompt(content)
58
  source_code_samples.append(f"### {file_path}\n{content}")
59
  except Exception: # nosec B110
60
  pass
 
80
  line for i, line in enumerate(lines)
81
  if not (i == 0 and line.startswith("```")) and line != "```"
82
  )
83
+
84
  # Step 4 - Count generated tests
85
  test_count = llm_response.count("def test_")
86
  coverage_before = _measure_coverage(local_path)