Skip to content

Commit 0333caa

Browse files
imsahil007terriko
andauthored
feat(checkers): add pre-commit hook for reformatting checkers table (#1290)
* feat(checkers): add pre commit hook for reformatting checkers table * fixes #1250 Co-authored-by: Terri Oda <[email protected]>
1 parent 8e2cd7c commit 0333caa

File tree

4 files changed

+139
-24
lines changed

4 files changed

+139
-24
lines changed

.pre-commit-config.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ repos:
1414
hooks:
1515
- id: pyupgrade
1616
args: ["--py36-plus"]
17-
17+
1818
- repo: https://github.com/pycqa/flake8
1919
rev: 3.9.2
2020
hooks:
2121
- id: flake8
22+
23+
- repo: local
24+
hooks:
25+
- id: format_checkers
26+
language: python
27+
name: format_checkers
28+
entry: python cve_bin_tool/format_checkers.py
29+
files: "^cve_bin_tool/checkers/__init__.py"
30+
types: [python]

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,25 @@ require more information, there is also a [user manual](https://cve-bin-tool.rea
114114
This scanner looks at the strings found in binary files to see if they
115115
match certain vulnerable versions of the following libraries and tools:
116116

117-
| | | | Available checkers | | | |
118-
| -------- | --------- | ---------------| ------------------ | ---------- | ---------- | ------- |
119-
| avahi | bash | bind | binutils | busybox | bzip2 | cups |
120-
| curl | dovecot | expat | ffmpeg | freeradius | gcc | gimp |
121-
| gnutls | glibc | gstreamer | haproxy | hostapd | icecast | icu |
122-
| irssi | kerberos | libarchive | libdb | libgcrypt | libjpeg | libnss |
123-
| libtiff | libvirt | lighttpd | mariadb | memcached | ncurses | nessus |
124-
| netpbm | nginx | node | openafs | openldap | openssh | openssl |
125-
| openswan | openvpn | png | polarssl_fedora | postgresql | python | qt |
126-
| radare2 | rsyslog | samba | sqlite | strongswan | sudo | syslogng|
127-
| systemd | tcpdump | varnish | wireshark | xerces | xml2 | zlib |
128-
117+
<!--CHECKERS TABLE BEGIN-->
118+
| | | | Available checkers | | | |
119+
|--------------- |--------- |-------------- |--------------- |---------- |---------- |------------- |
120+
| accountsservice |avahi |bash |bind |binutils |bolt |bubblewrap |
121+
| busybox |bzip2 |cronie |cryptsetup |cups |curl |dbus |
122+
| dnsmasq |dovecot |dpkg |enscript |expat |ffmpeg |freeradius |
123+
| ftp |gcc |gimp |glibc |gnomeshell |gnupg |gnutls |
124+
| gpgme |gstreamer |gupnp |haproxy |hostapd |hunspell |icecast |
125+
| icu |irssi |kbd |kerberos |kexectools |libarchive |libbpg |
126+
| libdb |libgcrypt |libical |libjpeg_turbo |liblas |libnss |libsndfile |
127+
| libsoup |libssh2 |libtiff |libvirt |libxslt |lighttpd |logrotate |
128+
| lua |mariadb |mdadm |memcached |mtr |mysql |nano |
129+
| ncurses |nessus |netpbm |nginx |node |ntp |open_vm_tools |
130+
| openafs |openjpeg |openldap |openssh |openssl |openswan |openvpn |
131+
| p7zip |pcsc_lite |png |polarssl_fedora |poppler |postgresql |pspp |
132+
| python |qt |radare2 |rsyslog |samba |sqlite |strongswan |
133+
| subversion |sudo |syslogng |systemd |tcpdump |trousers |varnish |
134+
| webkitgtk |wireshark |wpa_supplicant |xerces |xml2 |zlib |zsh |
135+
<!--CHECKERS TABLE END-->
129136

130137
All the checkers can be found in the checkers directory, as can the
131138
[instructions on how to add a new checker](https://github.com/intel/cve-bin-tool/blob/main/cve_bin_tool/checkers/README.md).

cve_bin_tool/format_checkers.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright (C) 2021 Intel Corporation
2+
# SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
"""To reformat checkers table when cve_bin_tool/checkers/__init__.py is updated."""
5+
6+
import os
7+
8+
from cve_bin_tool import checkers
9+
10+
CHECKERS_TABLE_SIZE = 7
11+
12+
13+
def reshape_list(checkers):
14+
"""Reshape the list of checkers to a 2D-List for printing the table"""
15+
return [
16+
checkers[index : (index + CHECKERS_TABLE_SIZE)]
17+
for index in range(0, len(checkers), CHECKERS_TABLE_SIZE)
18+
]
19+
20+
21+
def max_checker_length(checkers):
22+
"""Returns a list of max length of each column"""
23+
checkers[-1].extend([""] * (CHECKERS_TABLE_SIZE - len(checkers[-1])))
24+
25+
size_list = [0] * CHECKERS_TABLE_SIZE
26+
27+
for row in range(CHECKERS_TABLE_SIZE):
28+
for index in range(len(checkers)):
29+
checker = checkers[index][row]
30+
if len(checker) > size_list[row]:
31+
size_list[row] = len(checker)
32+
return size_list
33+
34+
35+
def reformat_checkers(checkers, size_array):
36+
"""Returns a markdown based table string for checkers"""
37+
checkers.insert(0, [""] * CHECKERS_TABLE_SIZE)
38+
checkers[0][CHECKERS_TABLE_SIZE // 2] = "Available checkers"
39+
40+
markdown = "| "
41+
42+
for row in checkers[0]:
43+
markdown += f" {row} |"
44+
markdown += "\n"
45+
46+
markdown += "|"
47+
for index in range(len(checkers[0])):
48+
markdown += f"{'-'*size_array[index]} |"
49+
markdown += "\n"
50+
51+
for row in checkers[1:]:
52+
markdown += "| "
53+
for checker in row:
54+
markdown += f"{checker} |"
55+
markdown += "\n"
56+
57+
return markdown
58+
59+
60+
def update_checker_table(file_path, markdown):
61+
"""Updates README.md and MANUAL.md with the new checker table"""
62+
lines = []
63+
start_index, end_index = None, None
64+
65+
with open(file_path) as f:
66+
for index, line in enumerate(f):
67+
if "CHECKERS TABLE BEGIN" in line:
68+
start_index = index
69+
elif "CHECKERS TABLE END" in line:
70+
end_index = index
71+
lines.append(line)
72+
73+
lines = lines[: start_index + 1] + [markdown] + lines[end_index:]
74+
75+
with open(file_path, "w") as f:
76+
f.writelines(lines)
77+
78+
79+
if __name__ == "__main__":
80+
checkers_array = list(set(checkers.__all__) - {"Checker", "VendorProductPair"})
81+
checkers_array = reshape_list(sorted(checkers_array))
82+
shape_list = max_checker_length(checkers_array)
83+
checkers_markdown = reformat_checkers(checkers_array, shape_list)
84+
update_checker_table(
85+
file_path=os.path.join(os.path.abspath("."), "README.md"),
86+
markdown=checkers_markdown,
87+
)
88+
update_checker_table(
89+
file_path=os.path.join(os.path.abspath("."), "doc", "MANUAL.md"),
90+
markdown=checkers_markdown,
91+
)

doc/MANUAL.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,25 @@ which is useful if you're trying the latest code from
9999
comma-separated list of checkers to disable
100100
-r RUNS, --runs RUNS comma-separated list of checkers to enable
101101

102-
| | | | Available checkers | | | |
103-
| -------- | -------- | ---------- | ------------------ | ---------- | -------- | ------- |
104-
| avahi | bash | bind | binutils | busybox | bzip2 | cups |
105-
| curl | dovecot | expat | ffmpeg | freeradius | gcc | gimp |
106-
| gnutls | glibc | gstreamer | haproxy | hostapd | icecast | icu |
107-
| irssi | kerberos | libarchive | libdb | libgcrypt | libjpeg | libnss |
108-
| libtiff | libvirt | lighttpd | mariadb | memcached | ncurses | nessus |
109-
| netpbm | nginx | node | openafs | openldap | openssh | openssl |
110-
| openswan | openvpn | png | polarssl_fedora | postgresql | python | qt |
111-
| radare2 | rsyslog | samba | sqlite | strongswan | syslogng | systemd |
112-
| tcpdump | varnish | wireshark | xerces | xml2 | zlib | |
102+
<!--CHECKERS TABLE BEGIN-->
103+
| | | | Available checkers | | | |
104+
|--------------- |--------- |-------------- |--------------- |---------- |---------- |------------- |
105+
| accountsservice |avahi |bash |bind |binutils |bolt |bubblewrap |
106+
| busybox |bzip2 |cronie |cryptsetup |cups |curl |dbus |
107+
| dnsmasq |dovecot |dpkg |enscript |expat |ffmpeg |freeradius |
108+
| ftp |gcc |gimp |glibc |gnomeshell |gnupg |gnutls |
109+
| gpgme |gstreamer |gupnp |haproxy |hostapd |hunspell |icecast |
110+
| icu |irssi |kbd |kerberos |kexectools |libarchive |libbpg |
111+
| libdb |libgcrypt |libical |libjpeg_turbo |liblas |libnss |libsndfile |
112+
| libsoup |libssh2 |libtiff |libvirt |libxslt |lighttpd |logrotate |
113+
| lua |mariadb |mdadm |memcached |mtr |mysql |nano |
114+
| ncurses |nessus |netpbm |nginx |node |ntp |open_vm_tools |
115+
| openafs |openjpeg |openldap |openssh |openssl |openswan |openvpn |
116+
| p7zip |pcsc_lite |png |polarssl_fedora |poppler |postgresql |pspp |
117+
| python |qt |radare2 |rsyslog |samba |sqlite |strongswan |
118+
| subversion |sudo |syslogng |systemd |tcpdump |trousers |varnish |
119+
| webkitgtk |wireshark |wpa_supplicant |xerces |xml2 |zlib |zsh |
120+
<!--CHECKERS TABLE END-->
113121

114122
For a quick overview of usage and how it works, you can also see [the readme file](README.md).
115123

0 commit comments

Comments
 (0)