master
Hanno Böck Use isort-style import sorting

Hanno Böck authored 4 months ago

1) import difflib
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

2) import glob
3) import json
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

4) import re
Hanno Böck Use isort-style import sorting

Hanno Böck authored 4 months ago

5) import sys
6) import unittest
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

7) 
8) 
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

9) def versioncompare(safe_version, find_version):
10)     safe_version_tup = [int(x) for x in safe_version.split(".")]
11)     find_version_tup = [int(x) for x in find_version.split(".")]
12)     return find_version_tup < safe_version_tup
13) 
14) 
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

15) class TestJsonLint(unittest.TestCase):
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

16)     @unittest.skipIf(
17)         sys.version_info < (3, 6, 0), "json.dumps force-sorts on python 3.5"
18)     )
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

19)     def test_json_lint(self):
20)         valid = True
21)         for f in glob.glob("freewvsdb/*.json"):
Hanno Böck fix new pylint warnings

Hanno Böck authored 2 years ago

22)             fp = open(f, encoding="ascii")
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

23)             orig = fp.read()
24)             fp.close()
25)             tmp = json.loads(orig)
Hanno Böck add newline at end of json...

Hanno Böck authored 1 year ago

26)             new = json.dumps(tmp, indent=2) + "\n"
Hanno Böck avoid some pylint warnings

Hanno Böck authored 4 years ago

27)             if orig != new:
Hanno Böck fix f-strings in tests

Hanno Böck authored 2 years ago

28)                 print(f"json {f} not valid")
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

29)                 sys.stdout.writelines(difflib.unified_diff(orig, new))
30)                 valid = False
Hanno Böck avoid some pylint warnings

Hanno Böck authored 4 years ago

31)         self.assertTrue(valid)
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

32) 
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

33)     def test_json_values(self):
34)         jconfig = []
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

35)         for cfile in glob.glob("freewvsdb/*.json"):
Hanno Böck fix new pylint warnings

Hanno Böck authored 2 years ago

36)             with open(cfile, encoding="ascii") as json_file:
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

37)                 jconfig += json.load(json_file)
38) 
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

39)         mkeys = {"name", "url", "safe", "vuln", "detection"}
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

40)         for item in jconfig:
41)             # check for all mandatory keys
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

42)             self.assertEqual(
43)                 mkeys.intersection(item.keys()),
44)                 mkeys,
45)                 msg=f"Missing key in {item['name']}",
46)             )
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

47) 
48)             # check we have at least one detection
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

49)             self.assertTrue(
50)                 len(item["detection"]) >= 1, msg=f"No detection in {item['name']}"
51)             )
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

52) 
53)             # vuln needs to be CVE or HTTPS URL
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

54)             self.assertTrue(
55)                 re.match("^CVE-[0-9]*-[0-9]*$", item["vuln"])
56)                 or item["vuln"].startswith("https://"),
57)                 msg=f"{item['name']}: Invalid vuln {item['vuln']}",
58)             )
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

59) 
Hanno Böck avoid bad data in the safe...

Hanno Böck authored 1 year ago

60)             # make sure safe is a version
61)             if item["safe"] != "":
62)                 # we have a theoretical reDoS here, but
63)                 # this is no external data, therefore ok
64)                 self.assertTrue(
Hanno Böck black-style quotes

Hanno Böck authored 4 months ago

65)                     re.match(r"^([0-9]+\.)*[0-9]+$", item["safe"]),  # noqa: DUO138
Hanno Böck avoid bad data in the safe...

Hanno Böck authored 1 year ago

66)                     msg=f"{item['name']}: Invalid safe version {item['safe']}",
67)                 )
68) 
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

69)             # make sure old_safe is properly sorted
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

70)             if "old_safe" in item:
71)                 old_safe = item["old_safe"].split(",")
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

72)                 for i in range(1, len(old_safe)):
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

73)                     self.assertTrue(
74)                         versioncompare(old_safe[i - 1], old_safe[i]),
75)                         msg=f"{item['name']}: Invalid old_safe"
76)                         " ordering {item['old_safe']}",
77)                     )
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

78) 
Hanno Böck test sanity of safe vs late...

Hanno Böck authored 4 years ago

79)             # make sure latest is not outdated
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

80)             if "latest" in item and item["safe"] != "":
81)                 self.assertTrue(
82)                     not versioncompare(item["safe"], item["latest"]),
83)                     msg=f"{item['name']}: Safe version "
84)                     "{item['safe']} newer than latest"
85)                     " {item['latest']}",
86)                 )
Hanno Böck test sanity of safe vs late...

Hanno Böck authored 4 years ago

87) 
Hanno Böck check old_safe for consiste...

Hanno Böck authored 4 years ago

88)             # subdir needs to be integer
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

89)             for det in item["detection"]:
90)                 self.assertTrue(
91)                     isinstance(det["subdir"], int),
92)                     msg=f"{item['name']}: subdir not int",
93)                 )
Hanno Böck add sanity checks for json...

Hanno Böck authored 4 years ago

94) 
Hanno Böck add codingstyle and json li...

Hanno Böck authored 4 years ago

95) 
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

96) if __name__ == "__main__":