-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathdatasets.py
More file actions
166 lines (142 loc) · 7.52 KB
/
Copy pathdatasets.py
File metadata and controls
166 lines (142 loc) · 7.52 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
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
import normalize as norm
import pandas as pd
import numpy as np
class Devign:
def __init__(self, df):
self.df = df
def prepare(self):
""" Prepare Devign dataset. """
self.df = self.df.rename(columns={'sha_id': 'fix_commit'})
class SAP:
def __init__(self, df):
self.df = df
def prepare(self):
""" Prepare SAP dataset. """
self.df = self.df.rename(columns={'sha': 'fix_commit', 'cve_id': 'vuln_id', 'commits': 'chain'})
del self.df['type']
def normalize(self):
print("Normalizing SAP ...")
self.df['chain'] = self.df['chain'].apply(lambda x: norm.split_commits(x))
self.df['chain'] = self.df['chain'].apply(lambda x: norm.commit(x))
print(f"Entries: {len(self.df)}")
self.df = self.df.dropna(subset=['chain'])
print(f"Entries (After duplicates): {len(self.df)}")
self.df['chain_len'] = self.df['chain'].apply(lambda x: len(x))
self.df['commit_href'] = self.df['chain'].apply(lambda x: len(x))
del self.df['refs']
del self.df['code_refs']
print(self.df.keys())
class SECBENCH:
def __init__(self, df):
self.df = df
def prepare(self):
""" Prepare SECBENCH dataset. """
self.df = self.df.rename(columns={'sha': 'fix_commit',
'cve_id': 'vuln_id'})
self.df['cwe_id'] = self.df['cwe_id'].apply(lambda x: norm.to_set(x))
self.df = self.df[['owner', 'project', 'fix_commit', 'vuln_id', 'cwe_id', 'score', 'dataset']]
def normalize(self):
print("Normalizing Secbench ...")
self.df['chain'] = self.df.apply(lambda x: norm.chain(x['owner'], x['project'], x['fix_commit']), axis=1)
self.df['chain'] = self.df['chain'].apply(lambda x: norm.commit(x))
print(f"Entries: {len(self.df)}")
self.df = self.df.dropna(subset=['chain'])
print(f"Entries (After duplicates): {len(self.df)}")
self.df['chain_len'] = self.df['chain'].apply(lambda x: len(x))
self.df['project'] = self.df.apply(lambda x: norm.project_from_meta(x['owner'], x['project']), axis=1)
del self.df['owner']
class BIGVUL:
def __init__(self, df):
self.df = df
def prepare(self):
""" Prepare BIG-VUL dataset. """
self.df = self.df.rename(columns={'commit_id': 'fix_commit',
'cve_id': 'vuln_id',
'publish_date': 'published_date',
'commits': 'chain'})
self.df['cwe_id'] = self.df['cwe_id'].apply(lambda x: norm.to_set(x))
self.df = self.df[['vuln_id', 'cwe_id', 'score', 'chain', 'fix_commit', 'dataset', 'summary', 'published_date', 'project']]
def normalize(self):
print("Normalizing Big-Vul ...")
self.df['chain'] = self.df['chain'].apply(lambda x: norm.split_commits(x))
# for each vuln_id
for vuln_id in self.df['vuln_id'].unique():
# if nan vuln_id, vuln belongs to the Chrome project;
# Chrome only contains single patches; this was verified earlier
if not pd.notna(vuln_id):
self.df.at[rows.index.values[0], 'commit'] = next(iter(self.df['chain'].iloc[rows.index].values[0]))
self.df.at[rows.index.values[0], 'patch'] = 'SINGLE'
continue
# get all the commits for each vuln
rows = self.df[self.df['vuln_id'] == vuln_id]
# if multi commit patch (n_commits > 1)
if len(rows) > 1:
chain = [list(commit)[0] for commit in rows['chain']]
count = 0
for idx, row in rows.iterrows():
self.df.at[idx, 'chain'] = set(chain)
self.df.at[idx, 'commit'] = chain[count]
self.df.at[idx, 'patch'] = 'MULTI'
count+=1
# if single commit patch (n_commits == 1)
else:
self.df.at[rows.index.values[0], 'commit'] = next(iter(self.df['chain'].iloc[rows.index].values[0]))
self.df.at[rows.index.values[0], 'patch'] = 'SINGLE'
self.df['chain'] = self.df['chain'].apply(lambda x: norm.commit(x))
print(f"Entries: {len(self.df)}")
self.df = self.df.dropna(subset=['chain'])
print(f"Entries (After duplicates): {len(self.df)}")
self.df['chain_len'] = self.df['chain'].apply(lambda x: len(x))
self.df['project'] = self.df['chain'].apply(lambda x: norm.project_from_chain(x))
class NVD:
def __init__(self, df):
self.df = df
def prepare(self):
""" Prepare NVD dataset."""
self.df = self.df.rename(columns={'cve_id': 'vuln_id', 'cwes':'cwe_id', 'commits': 'chain', 'description': 'summary', 'impact': 'score'})
self.df = self.df[['vuln_id', 'cwe_id', 'score', 'chain', 'dataset', 'summary', 'published_date']]
def normalize(self):
print("Normalizing NVD ...")
self.df['chain'] = self.df['chain'].apply(lambda x: norm.split_commits(x))
self.df['chain'] = self.df['chain'].apply(lambda x: norm.commit(x))
print(f"Entries: {len(self.df)}")
self.df = self.df.dropna(subset=['chain'])
print(f"Entries (After duplicates): {len(self.df)}")
self.df['chain_len'] = self.df['chain'].apply(lambda x: len(x))
self.df['project'] = self.df['chain'].apply(lambda x: norm.project_from_chain(x))
self.df['published_date'] = self.df['published_date'].apply(lambda x: norm.date(x))
class OSV:
def __init__(self, df):
self.df = df
def prepare(self):
""" Prepare OSV dataset. """
self.df = self.df.rename(columns={'commits': 'chain'})
self.df['summary'] = self.df.apply(lambda x: norm.join(x['summary'], x['details']), axis=1)
self.df = self.df[['vuln_id', 'cwe_id', 'score', 'chain', 'dataset', 'summary', 'published_date']]
def normalize(self):
print("Normalizing OSV ...")
self.df['chain'] = self.df['chain'].apply(lambda x: norm.split_commits(x))
self.df['chain'] = self.df['chain'].apply(lambda x: norm.commit(x))
print(f"Entries: {len(self.df)}")
self.df = self.df.dropna(subset=['chain'])
print(f"Entries (After duplicates): {len(self.df)}")
self.df['chain_len'] = self.df['chain'].apply(lambda x: len(x))
self.df['project'] = self.df['chain'].apply(lambda x: norm.project_from_chain(x))
self.df['published_date'] = self.df['published_date'].apply(lambda x: norm.date(x))
class CVEDetails:
def __init__(self, df):
self.df = df
def prepare(self):
""" Prepare CVE Details dataset. """
self.df = self.df.rename(columns={'cve_id': 'vuln_id', 'publish_date': 'published_date', 'commits': 'chain'})
self.df['cwe_id'] = self.df['cwe_id'].apply(lambda x: norm.to_set(x))
self.df = self.df[['vuln_id', 'cwe_id', 'score', 'chain', 'summary', 'dataset', 'published_date']]
def normalize(self):
print("Normalizing CVE Details ...")
self.df['chain'] = self.df.apply(lambda x: norm.split_commits(x['chain']), axis=1)
self.df['chain'] = self.df['chain'].apply(lambda x: norm.commit(x))
print(f"Entries: {len(self.df)}")
self.df = self.df.dropna(subset=['chain'])
print(f"Entries (After duplicates): {len(self.df)}")
self.df['chain_len'] = self.df['chain'].apply(lambda x: len(x))
self.df['project'] = self.df['chain'].apply(lambda x: norm.project_from_chain(x))