HimanshuGoyal2004 commited on
Commit
380d02f
Β·
1 Parent(s): 6f1acab

client fix

Browse files
Files changed (1) hide show
  1. app.py +76 -35
app.py CHANGED
@@ -9,11 +9,31 @@ load_dotenv()
9
  # MCP Server URL for GitHub tools
10
  MCP_SERVER_URL = "https://himanshugoyal2004-github-mcp-server.hf.space/gradio_api/mcp/"
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def analyze_vulnerabilities(message, history):
13
- """Analyze GitHub repository for vulnerabilities using AI agent"""
14
  try:
15
- # Connect to MCP server and get GitHub tools
16
- mcp_client = MCPClient({"url": MCP_SERVER_URL, "timeout": 120})
 
 
17
  tools = mcp_client.get_tools()
18
 
19
  # Initialize AI model
@@ -23,11 +43,47 @@ def analyze_vulnerabilities(message, history):
23
  agent = CodeAgent(
24
  tools=[*tools],
25
  model=model,
26
- additional_authorized_imports=["json", "ast", "urllib", "base64", "re"]
 
27
  )
28
 
29
- # Enhanced prompt for vulnerability analysis
30
- enhanced_prompt = f"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  You are a cybersecurity expert. Analyze the GitHub repository for security vulnerabilities.
32
 
33
  Repository: {message}
@@ -35,32 +91,21 @@ Repository: {message}
35
  Please:
36
  1. First, get repository information to verify it exists
37
  2. Scan the repository for code files (.py, .js, .ts, .php, .java, .cpp, .c, .cs, .go, .rb, .rs, .swift, .kt, .scala, .sh, .bash, .ps1, .ipynb, .sql, .xml, .yaml, .yml, .json, .config, .ini, .env)
38
- 3. For each code file found, get its content and analyze for security vulnerabilities
39
- 4. Focus on detecting:
40
- - SQL injection vulnerabilities
41
- - Command injection (os.system, exec, eval)
42
- - Cross-site scripting (XSS)
43
- - Path traversal attacks
44
- - Hardcoded secrets/credentials
45
- - Insecure deserialization
46
- - Weak cryptography
47
- - Authentication/authorization flaws
48
- - Input validation issues
49
- - Unsafe file operations
50
 
51
  5. Generate a comprehensive security report with:
52
- - Repository overview
53
- - Total files analyzed
54
- - Vulnerability count by severity (Critical/High/Medium/Low)
55
- - Detailed findings with:
56
- - File path and line number
57
- - Vulnerability type
58
- - Code snippet
59
- - Security impact
60
- - Remediation advice
61
- - Related CVE examples when applicable
62
 
63
- Format the report professionally with emojis and clear sections.
64
  """
65
 
66
  # Run the AI agent analysis
@@ -79,14 +124,10 @@ demo = gr.ChatInterface(
79
  fn=analyze_vulnerabilities,
80
  type="messages",
81
  examples=[
82
- "https://github.com/WebGoat/WebGoat",
83
- "https://github.com/OWASP/NodeGoat",
84
- "https://github.com/digininja/DVWA",
85
- "https://github.com/juice-shop/juice-shop",
86
- "https://github.com/vulhub/vulhub"
87
  ],
88
  title="πŸ›‘οΈ AI-Powered GitHub Vulnerability Scanner",
89
- description="Paste a GitHub repository URL to scan for security vulnerabilities using AI agents with MCP tools. The AI will intelligently analyze code and provide detailed security reports.",
90
  )
91
 
92
  if __name__ == "__main__":
 
9
  # MCP Server URL for GitHub tools
10
  MCP_SERVER_URL = "https://himanshugoyal2004-github-mcp-server.hf.space/gradio_api/mcp/"
11
 
12
+ def parse_github_url(url):
13
+ """Parse GitHub URL to extract owner, repo, and file path"""
14
+ import re
15
+
16
+ # Handle repository URLs
17
+ repo_pattern = r'https://github\.com/([^/]+)/([^/]+)/?$'
18
+ repo_match = re.match(repo_pattern, url.strip())
19
+ if repo_match:
20
+ return repo_match.group(1), repo_match.group(2), None
21
+
22
+ # Handle file URLs
23
+ file_pattern = r'https://github\.com/([^/]+)/([^/]+)/blob/[^/]+/(.+)$'
24
+ file_match = re.match(file_pattern, url.strip())
25
+ if file_match:
26
+ return file_match.group(1), file_match.group(2), file_match.group(3)
27
+
28
+ return None, None, None
29
+
30
  def analyze_vulnerabilities(message, history):
31
+ """Analyze GitHub repository or specific file for vulnerabilities using AI agent"""
32
  try:
33
+ mcp_client = MCPClient({
34
+ "url": MCP_SERVER_URL,
35
+ "timeout": 120
36
+ })
37
  tools = mcp_client.get_tools()
38
 
39
  # Initialize AI model
 
43
  agent = CodeAgent(
44
  tools=[*tools],
45
  model=model,
46
+ additional_authorized_imports=["json", "ast", "urllib", "base64", "re"],
47
+ max_steps=10
48
  )
49
 
50
+ # Parse the GitHub URL
51
+ owner, repo, file_path = parse_github_url(message)
52
+
53
+ if not owner or not repo:
54
+ return "❌ Invalid GitHub URL. Please provide a valid GitHub repository or file URL."
55
+
56
+ # Generate different prompts based on whether it's a file or repository
57
+ if file_path:
58
+ # Single file analysis
59
+ enhanced_prompt = f"""
60
+ You are a cybersecurity expert. Analyze the specific GitHub file for security vulnerabilities.
61
+
62
+ GitHub URL: {message}
63
+ Repository: {owner}/{repo}
64
+ File Path: {file_path}
65
+
66
+ Please:
67
+ 1. First, get repository information to verify it exists
68
+ 2. Get the content of the specific file: {file_path}
69
+ 3. Analyze the file content line by line for security vulnerabilities
70
+ 4. Look for these security issues:
71
+ - Command injection: os.system, exec, eval calls
72
+ - Input validation: unvalidated user inputs
73
+ - Error handling: unhandled exceptions that could leak info
74
+ - Hardcoded secrets: API keys, passwords, tokens
75
+ - Unsafe operations: file operations without validation
76
+
77
+ 5. Create a professional security report with:
78
+ - πŸ” File Overview (path, language, size)
79
+ - πŸ“Š Vulnerability Summary (counts by severity)
80
+ - 🚨 Detailed Findings (line numbers, code snippets, impacts, fixes)
81
+
82
+ Use simple string operations and avoid complex regex patterns. Focus on clear, actionable security findings.
83
+ """
84
+ else:
85
+ # Full repository analysis
86
+ enhanced_prompt = f"""
87
  You are a cybersecurity expert. Analyze the GitHub repository for security vulnerabilities.
88
 
89
  Repository: {message}
 
91
  Please:
92
  1. First, get repository information to verify it exists
93
  2. Scan the repository for code files (.py, .js, .ts, .php, .java, .cpp, .c, .cs, .go, .rb, .rs, .swift, .kt, .scala, .sh, .bash, .ps1, .ipynb, .sql, .xml, .yaml, .yml, .json, .config, .ini, .env)
94
+ 3. For the first 5-10 most important code files, get their content and analyze for security issues
95
+ 4. Look for these security vulnerabilities:
96
+ - Command injection: os.system, exec, eval calls
97
+ - Input validation: unvalidated user inputs, missing parameter checks
98
+ - Error handling: unhandled exceptions, information disclosure
99
+ - Hardcoded secrets: API keys, passwords, database credentials
100
+ - Unsafe operations: file operations, deserialization without validation
 
 
 
 
 
101
 
102
  5. Generate a comprehensive security report with:
103
+ - πŸ” Repository Overview
104
+ - πŸ“ Files Analyzed
105
+ - πŸ“Š Vulnerability Summary (counts by severity)
106
+ - 🚨 Detailed Findings (file paths, line numbers, code snippets, impacts, remediation)
 
 
 
 
 
 
107
 
108
+ Use simple string operations and focus on the most critical security issues. Limit analysis to prevent timeouts.
109
  """
110
 
111
  # Run the AI agent analysis
 
124
  fn=analyze_vulnerabilities,
125
  type="messages",
126
  examples=[
127
+ "https://github.com/banno-0720/documentation-agent/blob/main/code.py"
 
 
 
 
128
  ],
129
  title="πŸ›‘οΈ AI-Powered GitHub Vulnerability Scanner",
130
+ description="Paste a GitHub repository URL to scan the entire repo, or paste a specific file URL to analyze just that file for security vulnerabilities using AI agents with MCP tools. The AI will intelligently analyze code and provide detailed security reports.",
131
  )
132
 
133
  if __name__ == "__main__":