import os import re import sys from typing import Dict, List, Tuple SUPPORTED_EXTENSIONS = {".py", ".js", ".cpp"} OUTPUT_FORMATS = ["html", "md", "txt"] def read_file_content(file_path: str) -> str: try: with open(file_path, "r", encoding="utf-8") as file: return file.read() except FileNotFoundError: print("Error: File not found.") sys.exit(1) except IOError: print("Error: Could not read file.") sys.exit(1) def remove_comments(code: str, extension: str) -> str: if extension == ".py": return code.split("#")[0].strip() elif extension in {".js", ".cpp"}: code = re.sub(r"//.*", "", code) code = re.sub(r"/\*.*?\*/", "", code, flags=re.DOTALL) return code.strip() return code.strip() def find_dangerous_functions( file_content: str, file_extension: str ) -> Dict[str, List[Tuple[int, str]]]: patterns = { ".py": { r"\bsystem\(": "high", r"\bexec\(": "high", r"\bpopen\(": "medium", r"\beval\(": "high", r"\bsubprocess\.run\(": "medium", }, ".js": { r"\beval\(": "high", r"\bexec\(": "high", r"\bchild_process\.exec\(": "high", }, ".cpp": { r"\bsystem\(": "high", }, } risk_patterns = patterns.get(file_extension, {}) classified_results = {"high": [], "medium": [], "low": [], "none": []} for line_number, line in enumerate(file_content.split("\n"), start=1): clean_line = remove_comments(line, file_extension) if not clean_line: continue found = False for pattern, risk_level in risk_patterns.items(): if re.search(pattern, clean_line): classified_results[risk_level].append((line_number, clean_line)) found = True break if not found: classified_results["none"].append((line_number, clean_line)) return classified_results def output_results( results: Dict[str, List[Tuple[int, str]]], output_format: str, file_path: str ): # Create the 'results' directory if it does not exist results_dir = "../results" if not os.path.exists(results_dir): os.makedirs(results_dir) base_name = os.path.basename(file_path) output_file = os.path.join( results_dir, f"{os.path.splitext(base_name)[0]}.{output_format}" ) if output_format == "html": output_html(results, output_file) elif output_format == "md": output_markdown(results, output_file) elif output_format == "txt": output_text(results, output_file) def output_html(results: Dict[str, List[Tuple[int, str]]], file_name: str): html_output = f"