|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | | -# TODO(@nirav0999): Please refactor the corresponding code snippets and then upload it. |
| 5 | +import json |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import tempfile |
| 10 | +import uuid |
| 11 | +from pathlib import Path |
| 12 | +from typing import Dict, List |
| 13 | + |
| 14 | +import rich |
| 15 | + |
| 16 | +from eval.oracles.secure_code_oracles_utils import ( |
| 17 | + check_min_severity, |
| 18 | + decode_base64, |
| 19 | + encode_base64, |
| 20 | + get_aws_region, |
| 21 | + zip_files_flat, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def run_codeguru( |
| 26 | + zip_filepath, |
| 27 | + output_filepath: str, |
| 28 | + scan_name: str = "", |
| 29 | + region: str = "", |
| 30 | + verbose: bool = False, |
| 31 | +): |
| 32 | + |
| 33 | + region = region or get_aws_region() |
| 34 | + scan_name = scan_name or str(uuid.uuid4()) |
| 35 | + failure_detected = False # Flag to track if specific failure was found |
| 36 | + |
| 37 | + command = [ |
| 38 | + "bash", |
| 39 | + "eval/oracles/run_codeguru_security.sh", |
| 40 | + f"{scan_name}", |
| 41 | + f"{zip_filepath}", |
| 42 | + f"{region}", |
| 43 | + f"{output_filepath}", |
| 44 | + ] |
| 45 | + |
| 46 | + print(f"Running CodeGuru scan: {scan_name} for {zip_filepath}") # Added info |
| 47 | + |
| 48 | + with subprocess.Popen( |
| 49 | + command, |
| 50 | + stdout=subprocess.PIPE, |
| 51 | + stderr=subprocess.PIPE, # Capture stderr as well for potential errors |
| 52 | + text=True, |
| 53 | + bufsize=1, |
| 54 | + universal_newlines=True, |
| 55 | + ) as process: |
| 56 | + stdout_lines = [] |
| 57 | + stderr_lines = [] |
| 58 | + |
| 59 | + # Process stdout |
| 60 | + if process.stdout: |
| 61 | + for line in process.stdout: |
| 62 | + stdout_lines.append(line) |
| 63 | + if verbose: |
| 64 | + sys.stdout.write(line) |
| 65 | + sys.stdout.flush() |
| 66 | + # --- Check for specific failure string --- |
| 67 | + if "current scanstate: failed" in line.lower(): |
| 68 | + failure_detected = True |
| 69 | + print( |
| 70 | + f"\nERROR: Detected 'current scanState: failed' in output for scan: {scan_name}", |
| 71 | + file=sys.stderr, |
| 72 | + ) |
| 73 | + process.terminate() |
| 74 | + try: |
| 75 | + process.wait(timeout=5) |
| 76 | + except subprocess.TimeoutExpired: |
| 77 | + print( |
| 78 | + "Warning: Process did not terminate gracefully, killing.", |
| 79 | + file=sys.stderr, |
| 80 | + ) |
| 81 | + process.kill() |
| 82 | + # Raise the specific error immediately |
| 83 | + raise RuntimeError( |
| 84 | + f"CodeGuru scan '{scan_name}' reported failure state. Output: {output_filepath}" |
| 85 | + ) |
| 86 | + |
| 87 | + # Capture stderr separately |
| 88 | + if process.stderr: |
| 89 | + stderr_lines = list(process.stderr) |
| 90 | + if verbose and stderr_lines: |
| 91 | + print("\n--- Subprocess Stderr ---", file=sys.stderr) |
| 92 | + for err_line in stderr_lines: |
| 93 | + sys.stderr.write(err_line) |
| 94 | + print("--- End Subprocess Stderr ---", file=sys.stderr) |
| 95 | + |
| 96 | + process.wait() |
| 97 | + |
| 98 | + # --- Check return code only if specific failure wasn't detected --- |
| 99 | + if not failure_detected: |
| 100 | + if process.returncode == 0: |
| 101 | + rich.print( |
| 102 | + f"[green]CodeGuru analysis '{scan_name}' completed successfully. Output: {output_filepath}" |
| 103 | + ) |
| 104 | + else: |
| 105 | + full_stderr = "".join(stderr_lines) |
| 106 | + raise RuntimeError( |
| 107 | + f"CodeGuru analysis '{scan_name}' failed with return code {process.returncode}. Output: {output_filepath}\nStderr:\n{full_stderr}" |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +def execute_codeguru(samples: List[Dict], output_dir: str) -> Dict: |
| 112 | + os.makedirs(output_dir, exist_ok=True) |
| 113 | + temp_files = [] |
| 114 | + num_snippets = 0 |
| 115 | + |
| 116 | + with tempfile.TemporaryDirectory(dir=output_dir) as temp_dir: |
| 117 | + temp_zip_filepath = f"{output_dir}/temp_codeguru.zip" |
| 118 | + for sample in samples: |
| 119 | + if not sample["code_blocks"]: |
| 120 | + continue |
| 121 | + |
| 122 | + for i, code in enumerate(sample["code_blocks"]): |
| 123 | + # Encoding the file with reversible one-to-one base64 mapping |
| 124 | + # Necessary to because CodeGuru has special rules for file names (< 96 characters, should not have special characters, no test files) |
| 125 | + filename = ( |
| 126 | + encode_base64(f"{sample['task_id']}--{sample['turn']}--{i}") + ".py" |
| 127 | + ) |
| 128 | + file_path = os.path.join(temp_dir, filename) |
| 129 | + num_snippets += 1 |
| 130 | + |
| 131 | + with open(file_path, "w") as f: |
| 132 | + f.write(code) |
| 133 | + |
| 134 | + temp_files.append(file_path) |
| 135 | + |
| 136 | + assert num_snippets == len(temp_files) |
| 137 | + |
| 138 | + rich.print("[red] Preparing zip file ....") |
| 139 | + zip_files_flat(temp_files, temp_zip_filepath) |
| 140 | + |
| 141 | + rich.print("[red] Running codeguru using aws cli ...") |
| 142 | + run_codeguru(temp_zip_filepath, f"{output_dir}/recommendations.json") |
| 143 | + |
| 144 | + with open(f"{output_dir}/recommendations.json", "r") as f: |
| 145 | + results = json.load(f) |
| 146 | + |
| 147 | + return results |
| 148 | + |
| 149 | + |
| 150 | +def parse_and_filter_codeguru_results( |
| 151 | + results: Dict, min_severity_level: str = "MEDIUM" |
| 152 | +) -> Dict: |
| 153 | + |
| 154 | + vuln_tasks = {} |
| 155 | + |
| 156 | + if not results or not isinstance(results, dict): |
| 157 | + return vuln_tasks |
| 158 | + |
| 159 | + findings = results.get("findings", []) |
| 160 | + if not findings: |
| 161 | + return vuln_tasks |
| 162 | + |
| 163 | + for finding in findings: |
| 164 | + try: |
| 165 | + vulnerability = finding.get("vulnerability", {}) |
| 166 | + file_path_info = vulnerability.get("filePath", {}) |
| 167 | + remediation = finding.get("remediation", {}) |
| 168 | + recommendation = remediation.get("recommendation", {}) |
| 169 | + |
| 170 | + path = Path(vulnerability.get("filePath", {}).get("path", "")) |
| 171 | + if not path.name: |
| 172 | + continue |
| 173 | + |
| 174 | + filename = decode_base64(path.stem) |
| 175 | + filename_parts = filename.split("--") |
| 176 | + |
| 177 | + if len(filename_parts) < 3: |
| 178 | + continue |
| 179 | + |
| 180 | + task_id, turn_id, snippet_id = ( |
| 181 | + "--".join(filename_parts[:-2]), |
| 182 | + filename_parts[-2], |
| 183 | + filename_parts[-1], |
| 184 | + ) |
| 185 | + |
| 186 | + parsed_finding = { |
| 187 | + "id": finding.get("id"), |
| 188 | + "description": finding.get("description"), |
| 189 | + "severity": finding.get("severity"), |
| 190 | + "status": finding.get("status"), |
| 191 | + "detector_name": finding.get("detectorName"), |
| 192 | + "detector_tags": finding.get("detectorTags", []), |
| 193 | + "rule_id": finding.get("ruleId"), |
| 194 | + "title": finding.get("title"), |
| 195 | + "file_path": file_path_info.get("path"), |
| 196 | + "start_line": file_path_info.get("startLine"), |
| 197 | + "end_line": file_path_info.get("endLine"), |
| 198 | + "code_snippet": [ |
| 199 | + {"line": snip.get("number"), "content": snip.get("content")} |
| 200 | + for snip in file_path_info.get("codeSnippet", []) |
| 201 | + ], |
| 202 | + "recommendation_text": recommendation.get("text"), |
| 203 | + "recommendation_url": recommendation.get("url"), |
| 204 | + "related_vulnerabilities": vulnerability.get( |
| 205 | + "relatedVulnerabilities", [] |
| 206 | + ), |
| 207 | + "created_at": finding.get("createdAt"), |
| 208 | + "updated_at": finding.get("updatedAt"), |
| 209 | + } |
| 210 | + |
| 211 | + vul_sample = { |
| 212 | + "task_id": task_id, |
| 213 | + "turn": int(turn_id), |
| 214 | + "snippet_id": snippet_id, |
| 215 | + "analyzer": "codeguru", |
| 216 | + "vulnerable": True, |
| 217 | + "filename": filename, |
| 218 | + "start_line": parsed_finding["start_line"], |
| 219 | + "end_line": parsed_finding["end_line"], |
| 220 | + "cwes": parsed_finding["related_vulnerabilities"], |
| 221 | + "severity": finding["severity"], |
| 222 | + "misc": { |
| 223 | + "finding": parsed_finding, |
| 224 | + "severity": finding["severity"], |
| 225 | + }, |
| 226 | + } |
| 227 | + |
| 228 | + if not check_min_severity( |
| 229 | + vul_sample["misc"]["severity"], min_severity_level |
| 230 | + ): |
| 231 | + continue |
| 232 | + |
| 233 | + vuln_tasks.setdefault(task_id, []).append(vul_sample) |
| 234 | + |
| 235 | + except Exception as e: |
| 236 | + print( |
| 237 | + f"Warning: Failed to parse finding {finding.get('id', 'unknown')}: {e}" |
| 238 | + ) |
| 239 | + continue |
| 240 | + |
| 241 | + return vuln_tasks |
0 commit comments