File size: 11,861 Bytes
f8e78b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python3
"""
Security Scanner MCP Server

An MCP server that scans Python code for security vulnerabilities
and provides beginner-friendly explanations.
"""

import argparse
import asyncio
import json
import sys
import tempfile
from pathlib import Path
from typing import Any, Dict, List

# MCP imports
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, TextContent

# Local imports
from utils import (
    load_config,
    setup_logging,
    validate_severity_threshold,
    filter_by_severity,
)

# Initialize logger (will be configured in main)
logger = None


class SecurityScannerServer:
    """MCP server for security scanning."""

    def __init__(self, debug: bool = False):
        """
        Initialize the Security Scanner MCP server.

        Args:
            debug: Enable debug logging
        """
        global logger
        logger = setup_logging(debug=debug)

        self.config = load_config()
        self.server = Server(self.config["server"]["name"])
        self.debug = debug

        logger.info(
            f"Initializing {self.config['server']['name']} "
            f"v{self.config['server']['version']}"
        )

        # Register handlers
        self._register_handlers()

    def _register_handlers(self):
        """Register MCP tool handlers."""

        @self.server.list_tools()
        async def list_tools() -> List[Tool]:
            """List available tools."""
            return [
                Tool(
                    name="scan_security",
                    description=self.config["tools"]["scan_security"]["description"],
                    inputSchema={
                        "type": "object",
                        "properties": {
                            "code": {
                                "type": "string",
                                "description": "๋ถ„์„ํ•  Python ์†Œ์Šค ์ฝ”๋“œ",
                            },
                            "severity_threshold": {
                                "type": "string",
                                "enum": ["CRITICAL", "HIGH", "MEDIUM", "LOW"],
                                "description": "๋ณด๊ณ ํ•  ์ตœ์†Œ ์‹ฌ๊ฐ๋„ ์ˆ˜์ค€",
                                "default": self.config["severity"]["default_threshold"],
                            },
                        },
                        "required": ["code"],
                    },
                )
            ]

        @self.server.call_tool()
        async def call_tool(name: str, arguments: Any) -> List[TextContent]:
            """
            Handle tool calls.

            Args:
                name: Tool name
                arguments: Tool arguments

            Returns:
                List of text content with results
            """
            if name != "scan_security":
                raise ValueError(f"Unknown tool: {name}")

            logger.info(f"Tool called: {name}")
            logger.debug(f"Arguments: {arguments}")

            try:
                result = await self._scan_security(arguments)
                return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]

            except Exception as e:
                logger.error(f"Error during security scan: {e}", exc_info=True)
                error_result = {
                    "error": str(e),
                    "summary": {
                        "total_issues": 0,
                        "critical": 0,
                        "high": 0,
                        "medium": 0,
                        "low": 0,
                    },
                    "vulnerabilities": [],
                }
                return [TextContent(type="text", text=json.dumps(error_result, indent=2, ensure_ascii=False))]

    async def _scan_security(self, arguments: Dict[str, Any]) -> Dict[str, Any]:
        """
        Perform security scan on provided code.

        Args:
            arguments: Dictionary containing 'code' and optional 'severity_threshold'

        Returns:
            Dictionary with scan results
        """
        # Extract and validate arguments
        code = arguments.get("code", "").strip()
        if not code:
            raise ValueError("Code parameter is required and cannot be empty")

        severity_threshold = arguments.get(
            "severity_threshold",
            self.config["severity"]["default_threshold"]
        ).upper()

        if not validate_severity_threshold(severity_threshold):
            raise ValueError(
                f"Invalid severity threshold: {severity_threshold}. "
                f"Must be one of: CRITICAL, HIGH, MEDIUM, LOW"
            )

        logger.info(f"Starting security scan (threshold: {severity_threshold})")
        logger.debug(f"Code length: {len(code)} characters")

        # Check file size limit
        max_size_mb = self.config["performance"]["max_file_size_mb"]
        code_size_mb = len(code.encode("utf-8")) / (1024 * 1024)
        if code_size_mb > max_size_mb:
            raise ValueError(
                f"Code size ({code_size_mb:.2f}MB) exceeds maximum "
                f"allowed size ({max_size_mb}MB)"
            )

        # Create temporary file for scanning
        with tempfile.NamedTemporaryFile(
            mode="w",
            suffix=".py",
            delete=False,
            encoding="utf-8"
        ) as tmp_file:
            tmp_file.write(code)
            tmp_file_path = tmp_file.name

        try:
            # Collect results from all enabled scanners
            all_vulnerabilities = []

            # Run pattern detector
            if self.config["scanners"]["pattern_detector"]["enabled"]:
                logger.info("Running pattern detector...")
                try:
                    from scanner.pattern_detector import scan_patterns
                    pattern_results = scan_patterns(tmp_file_path, code)
                    all_vulnerabilities.extend(pattern_results)
                    logger.info(f"Pattern detector found {len(pattern_results)} issues")
                except ImportError:
                    logger.warning("Pattern detector module not available (not yet implemented)")
                except Exception as e:
                    logger.error(f"Pattern detector error: {e}")

            # Run SQL injection detector
            if self.config["scanners"]["sql_injection"]["enabled"]:
                logger.info("Running SQL injection detector...")
                try:
                    from scanner.sql_injection import scan_sql_injection
                    sql_results = scan_sql_injection(tmp_file_path, code)
                    all_vulnerabilities.extend(sql_results)
                    logger.info(f"SQL injection detector found {len(sql_results)} issues")
                except ImportError:
                    logger.warning("SQL injection detector module not available (not yet implemented)")
                except Exception as e:
                    logger.error(f"SQL injection detector error: {e}")

            # Run bandit
            if self.config["scanners"]["bandit"]["enabled"]:
                logger.info("Running bandit scanner...")
                try:
                    from scanner.bandit_wrapper import scan_with_bandit
                    bandit_results = scan_with_bandit(tmp_file_path)
                    all_vulnerabilities.extend(bandit_results)
                    logger.info(f"Bandit found {len(bandit_results)} issues")
                except ImportError:
                    logger.warning("Bandit wrapper module not available (not yet implemented)")
                except Exception as e:
                    logger.error(f"Bandit scanner error: {e}")

            # Run semgrep
            if self.config["scanners"]["semgrep"]["enabled"]:
                logger.info("Running semgrep scanner...")
                try:
                    from scanner.semgrep_wrapper import scan_with_semgrep
                    semgrep_results = scan_with_semgrep(tmp_file_path)
                    all_vulnerabilities.extend(semgrep_results)
                    logger.info(f"Semgrep found {len(semgrep_results)} issues")
                except ImportError:
                    logger.warning("Semgrep wrapper module not available (not yet implemented)")
                except Exception as e:
                    logger.error(f"Semgrep scanner error: {e}")

            # Filter by severity threshold
            filtered_vulnerabilities = filter_by_severity(
                all_vulnerabilities,
                severity_threshold
            )

            logger.info(
                f"Total issues found: {len(all_vulnerabilities)}, "
                f"after filtering: {len(filtered_vulnerabilities)}"
            )

            # Format results
            try:
                from formatter import format_results
                formatted_results = format_results(
                    filtered_vulnerabilities,
                    severity_threshold
                )
            except ImportError:
                logger.warning("Formatter module not available, using basic format")
                formatted_results = self._basic_format_results(
                    filtered_vulnerabilities,
                    severity_threshold
                )

            return formatted_results

        finally:
            # Clean up temporary file
            try:
                Path(tmp_file_path).unlink()
            except Exception as e:
                logger.warning(f"Failed to delete temporary file: {e}")

    def _basic_format_results(
        self,
        vulnerabilities: List[Dict[str, Any]],
        threshold: str
    ) -> Dict[str, Any]:
        """
        Basic result formatting when formatter module is not available.

        Args:
            vulnerabilities: List of vulnerabilities
            threshold: Severity threshold used

        Returns:
            Formatted results dictionary
        """
        # Count by severity
        severity_counts = {"critical": 0, "high": 0, "medium": 0, "low": 0}

        for vuln in vulnerabilities:
            severity = vuln.get("severity", "LOW").lower()
            if severity in severity_counts:
                severity_counts[severity] += 1

        return {
            "summary": {
                "total_issues": len(vulnerabilities),
                "critical": severity_counts["critical"],
                "high": severity_counts["high"],
                "medium": severity_counts["medium"],
                "low": severity_counts["low"],
                "severity_threshold": threshold,
            },
            "vulnerabilities": vulnerabilities,
        }

    async def run(self):
        """Run the MCP server."""
        logger.info("Starting MCP server...")

        async with stdio_server() as (read_stream, write_stream):
            logger.info("Server is ready and listening on stdio")
            await self.server.run(
                read_stream,
                write_stream,
                self.server.create_initialization_options()
            )


def main():
    """Main entry point for the MCP server."""
    parser = argparse.ArgumentParser(
        description="Security Scanner MCP Server"
    )
    parser.add_argument(
        "--debug",
        action="store_true",
        help="Enable debug logging"
    )

    args = parser.parse_args()

    # Create and run server
    server = SecurityScannerServer(debug=args.debug)

    try:
        asyncio.run(server.run())
    except KeyboardInterrupt:
        if logger:
            logger.info("Server stopped by user")
        sys.exit(0)
    except Exception as e:
        if logger:
            logger.error(f"Server error: {e}", exc_info=True)
        sys.exit(1)


if __name__ == "__main__":
    main()