-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcodeqlclient.py
More file actions
415 lines (364 loc) · 13.1 KB
/
Copy pathcodeqlclient.py
File metadata and controls
415 lines (364 loc) · 13.1 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import time
import re
import os
import json
import subprocess
import threading
import uuid
from pathlib import Path
class CodeQLQueryServer:
def __init__(self, codeql_path="codeql"):
self.codeql_path = codeql_path
self.proc = None
self.reader_thread = None
self.pending = {}
self.running = True
self.id_counter = 1
self.progress_id = 0
self.progress_callbacks = {}
def start(self):
self.proc = subprocess.Popen(
[
self.codeql_path,
"execute",
"query-server2",
"--debug",
"--tuple-counting",
"--threads=0",
"--evaluator-log-level",
"5",
"-v",
"--log-to-stderr",
],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1, # line-buffered
)
self.reader_thread = threading.Thread(
target=self._read_loop, daemon=True
)
self.reader_thread.start()
self.stderr_thread = threading.Thread(
target=self._stderr_loop, daemon=True
)
self.stderr_thread.start()
def _stderr_loop(self):
while self.running:
line = self.proc.stderr.readline()
# For debugging
# if line:
# print("[CodeQL stderr]", line.strip())
def _read_loop(self):
print("[*] Read loop started")
while self.running:
line = self.proc.stdout.readline()
if not line:
print("[*] Read loop: EOF or closed stdout")
break
print(f"[stdout] {line.strip()}")
if line.startswith("Content-Length:"):
try:
length = int(line.strip().split(":")[1])
blank = self.proc.stdout.readline()
content = self.proc.stdout.read(length)
print(f"[raw response body] {content.strip()}")
message = json.loads(content)
self._handle_message(message)
except Exception as e:
print(f"[!] Failed to parse message: {e}")
def _handle_message(self, message):
print(f"\n[←] Received response:\n{json.dumps(message, indent=2)}\n")
if message.get("method") == "ql/progressUpdated":
params = message.get("params", {})
progress_id = params.get("id")
callback = self.progress_callbacks.get(progress_id)
if callback:
callback(params)
else:
print(
f"[ql-progress:{progress_id}] step={params.get('step')} / {params.get('maxStep')}"
)
return
if "method" in message and message["method"] == "evaluation/progress":
progress_id = message["params"].get("progressId")
msg = message["params"].get("message")
callback = self.progress_callbacks.get(progress_id)
if callback:
callback(msg)
else:
print(f"[progress:{progress_id}] {msg}")
return
if "id" in message and "result" in message:
request_id = message["id"]
if request_id in self.pending:
callback, progress_id = self.pending[request_id]
callback(message["result"])
del self.pending[request_id]
if progress_id in self.progress_callbacks:
del self.progress_callbacks[progress_id]
elif "id" in message and "error" in message:
print(
f"[!] Error response to request {message['id']}:\n{json.dumps(message['error'], indent=2)}"
)
def _send(self, payload):
if not self.proc or not self.proc.stdin:
print("[!] Tried to send but process not running.")
return
data = json.dumps(payload)
content = f"Content-Length: {len(data)}\r\n\r\n{data}"
print(f"\n[→] Sending request:\n{json.dumps(payload, indent=2)}\n")
self.proc.stdin.write(content)
self.proc.stdin.flush()
def send_request(self, method, params, callback, progress_callback=None):
req_id = self.id_counter
self.id_counter += 1
if isinstance(params, dict) and "progressId" in params:
self.progress_callbacks[params["progressId"]] = progress_callback
payload = {
"jsonrpc": "2.0",
"id": req_id,
"method": method,
"params": params,
}
self.pending[req_id] = (
callback,
params.get("progressId") if isinstance(params, dict) else None,
)
self._send(payload)
def stop(self):
self.running = False
if self.proc:
self.proc.terminate()
def find_class_identifier_position(self, filepath, class_name):
"""
Find the 1-based position of the class name identifier in a QL file.
Returns: (start_line, start_col, end_line, end_col)
"""
path = Path(filepath)
lines = path.read_text().splitlines()
for i, line in enumerate(lines):
match = re.search(rf"\bclass\s+{re.escape(class_name)}\b", line)
if match:
start_line = i + 1
start_col = (
match.start(0) + line[match.start(0) :].find(class_name) + 1
)
end_col = start_col + len(class_name)
return start_line, start_col, start_line, end_col
raise ValueError(
f"Class name '{class_name}' not found in file: {filepath}"
)
def find_predicate_identifier_position(self, filepath, predicate_name):
"""
Find the 1-based position of a predicate name in a QL file.
Supports: predicate name(...), name(...) (inside class), etc.
Returns: (start_line, start_col, end_line, end_col)
"""
path = Path(filepath)
lines = path.read_text().splitlines()
for i, line in enumerate(lines):
match = re.search(rf"\b{re.escape(predicate_name)}\s*\(", line)
if match:
start_line = i + 1
start_col = match.start() + 1
end_col = start_col + len(predicate_name)
return start_line, start_col, start_line, end_col
raise ValueError(
f"Predicate name '{predicate_name}' not found in file: {filepath}"
)
def register_databases(
self, db_paths, callback=None, progress_callback=None
):
resolved = [str(Path(p).resolve()) for p in db_paths]
progress_id = self.progress_id
self.progress_id += 1
params = {"body": {"databases": resolved}, "progressId": progress_id}
print(
f"[DEBUG] Sending evaluation/registerDatabases with progressId={progress_id}"
)
self.send_request(
"evaluation/registerDatabases",
params,
callback or (lambda r: print("[registerDatabases] done:", r)),
progress_callback=progress_callback,
)
def deregister_databases(
self, db_paths, callback=None, progress_callback=None
):
resolved = [str(Path(p).resolve()) for p in db_paths]
progress_id = self.progress_id
self.progress_id += 1
params = {"body": {"databases": resolved}, "progressId": progress_id}
print(
f"[DEBUG] Sending evaluation/deregisterDatabases with progressId={progress_id}"
)
self.send_request(
"evaluation/deregisterDatabases",
params,
callback or (lambda r: print("[registerDatabases] done:", r)),
progress_callback=progress_callback,
)
def evaluate_queries(
self,
query_path,
db_path,
output_path,
callback=None,
progress_callback=None,
):
db = str(Path(db_path).resolve())
query_path = str(Path(query_path).resolve())
output_path = str(Path(output_path).resolve())
progress_id = self.progress_id
self.progress_id += 1
params = {
"body": {
"db": db,
"queryPath": query_path,
"outputPath": output_path,
"target": {"query": {}},
"additionalPacks": [""],
"externalInputs": {},
"singletonExternalInputs": {},
},
"progressId": progress_id,
}
def on_done(result):
print("[evaluateQueries] done:", result)
if result.get("resultType") != 0:
raise RuntimeError(
f"CodeQL evaluation failed: {result.get('message', 'Unknown error')}"
)
print(
f"[DEBUG] Sending evaluation/runQuery with progressId={progress_id}"
)
self.send_request(
"evaluation/runQuery",
params,
callback or on_done,
progress_callback=progress_callback,
)
def evaluate_and_wait(self, query_path, db_path, output_path):
progress_id = self.progress_id
progress_cb, done = self.wait_for_progress_done(progress_id)
self.evaluate_queries(
query_path, db_path, output_path, progress_callback=progress_cb
)
done.wait()
print("[evaluate_and_wait] Query completed.")
def quick_evaluate_and_wait(
self,
query_path,
db_path,
output_path,
start_line,
start_col,
end_line,
end_col,
):
progress_id = self.progress_id
progress_cb, done = self.wait_for_progress_done(progress_id)
self.quick_evaluate(
query_path,
db_path,
output_path,
start_line,
start_col,
end_line,
end_col,
progress_callback=progress_cb,
)
done.wait()
print("[evaluate_and_wait] Query completed.")
def quick_evaluate(
self,
file_path,
db_path,
output_path,
start_line,
start_col,
end_line,
end_col,
callback=None,
progress_callback=None,
):
progress_id = self.progress_id
self.progress_id += 1
params = {
"body": {
"db": str(Path(db_path).resolve()),
"queryPath": str(Path(file_path).resolve()),
"outputPath": str(Path(output_path).resolve()),
"target": {
"quickEval": {
"quickEvalPos": {
"fileName": str(Path(file_path).resolve()),
"line": start_line,
"column": start_col,
"endLine": end_line,
"endColumn": end_col,
}
}
},
"additionalPacks": [],
"externalInputs": {},
"singletonExternalInputs": {},
},
"progressId": progress_id,
}
def on_done(result):
print("[quickEvaluate] done:", result)
if result.get("resultType") != 0:
raise RuntimeError(
f"CodeQL evaluation failed: {result.get('message', 'Unknown error')}"
)
print(
f"[DEBUG] Sending evaluation/evaluateQueries (quickEval) with progressId={progress_id}"
)
self.send_request(
"evaluation/runQuery",
params,
callback or on_done,
progress_callback=progress_callback,
)
def decode_bqrs(self, bqrs_path, output_format="json"):
bqrs_path = str(Path(bqrs_path).resolve())
if not os.path.exists(bqrs_path):
raise FileNotFoundError(f"BQRS file not found: {bqrs_path}")
result = subprocess.run(
[
self.codeql_path,
"bqrs",
"decode",
"--format",
output_format,
bqrs_path,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
if result.returncode != 0:
raise RuntimeError(
f"Failed to decode BQRS: {result.stderr.strip()}"
)
return result.stdout
def wait_for_progress_done(self, expected_progress_id):
event = threading.Event()
def progress_callback(message):
if (
isinstance(message, dict)
and message.get("id") == expected_progress_id
and message.get("step") == message.get("maxStep")
):
event.set()
return progress_callback, event
def wait_for_completion_callback(self):
done = threading.Event()
result_holder = {}
def callback(result):
result_holder["result"] = result
done.set()
return callback, done, result_holder