(scan_id, detection, package, cursor)
| 102 | |
| 103 | |
| 104 | def add_detection(scan_id, detection, package, cursor): |
| 105 | # TODO: add severity to table cols |
| 106 | type_id = get_detection_type_id(detection["type"], cursor) |
| 107 | signature = xxhash.xxh64(f"{package}#{detection['signature']}").hexdigest() |
| 108 | |
| 109 | extra = zlib.compress(rapidjson.dumps(detection.get("extra", {})).encode()) |
| 110 | |
| 111 | try: |
| 112 | cursor.execute(""" |
| 113 | INSERT INTO detections (scan, type, signature, message, score, extra) VALUES (?, ?, ?, ?, ?, ?) |
| 114 | """, (scan_id, type_id, signature, detection["message"], detection.get("score", 0), extra)) |
| 115 | except sqlite3.IntegrityError: |
| 116 | print(f"Warning detection already exists: {detection}") |
| 117 | return |
| 118 | |
| 119 | detection_id = cursor.lastrowid |
| 120 | |
| 121 | for tag in detection.get("tags", []): |
| 122 | tag_id = get_tag_id(tag, cursor) |
| 123 | try: |
| 124 | cursor.execute(""" |
| 125 | INSERT INTO tags (detection, tag) VALUES (?, ?) |
| 126 | """, (detection_id, tag_id)) |
| 127 | except sqlite3.IntegrityError: |
| 128 | pass |
| 129 | |
| 130 | |
| 131 | def add_scan(scan: dict, cursor): |
no test coverage detected