Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexecutor_static.py
More file actions
Latest commit
88 lines (73 loc) · 3 KB
/
Copy pathexecutor_static.py
File metadata and controls
88 lines (73 loc) · 3 KB
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
importsubprocess
importre
importjson
importtempfile
importos
fromenumimportEnum
fromutilsimportcall_chatgpt_analyze_static_security
classFResult(Enum):
SAFE=1# validation returns okay
FAILURE=2# validation contains error (something wrong with validation)
ERROR=3# validation returns a potential error (look into)
LLM_WEAKNESS= (
4# the generated input is ill-formed due to the weakness of the language model
)
TIMED_OUT=10# timed out, can be okay in certain targets
defextract_function_name(code):
# Extract the function name from the code using regex
match=re.search(r'def (\w+)\s*\(', code)
ifmatch:
returnmatch.group(1)
raiseValueError("Function name could not be extracted from the code.")
defremove_json_prefix(input_str):
# Check if the input string starts with "json\n"
ifinput_str.startswith("json\n"):
# Remove the prefix and return the remaining string
returninput_str[len("json\n"):]
returninput_str
classExecutorStaticAgent:
def__init__(self,entry):
self.entry=entry
defexecute_static_analysis(self,code):
# Create a temporary file for the script
withtempfile.NamedTemporaryFile(delete=False, suffix=".py", dir="./tmp") astemp_script:
temp_script.write(code.encode('utf-8'))
temp_script_path=temp_script.name
# Execute the script with subprocess.run
result=None
try:
result=subprocess.run(
['bandit', '-r', temp_script_path, '-f', 'json'],
capture_output=True,
text=True,
timeout=6
)
exceptsubprocess.TimeoutExpiredaste:
returnFResult.TIMED_OUT, "function timeout"
except:
# Clean up the temporary file
os.remove(temp_script_path)
ifresult.returncode==1:
returnFResult.ERROR,result.stderr
finally:
# Clean up the temporary file
os.remove(temp_script_path)
ifresultisNone:
returnFResult.ERROR, "none type"
ifresult.returncode==0:
returnFResult.SAFE, "0", ""
ifresult.returncode==1:
bandit_result=json.loads(result.stdout)
print(bandit_result)
cwe_code=bandit_result.get("results", [])[0].get("issue_cwe").get("id")
issue_text=bandit_result.get("results", [])[0].get("issue_text")
cwe_code=f"CWE-{cwe_code}"
returnFResult.ERROR, cwe_code, issue_text
else:
returnFResult.ERROR, result.stderr
defexecute_static_analysis_gpt(self,code):
response=call_chatgpt_analyze_static_security(code)
if ('no vulnerabilities detected'.lower() inresponse.lower()):
returnFResult.SAFE, "No CWE"
else:
returnFResult.ERROR, response