cve-json-to-text.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/env python3
  2. # SPDX-FileCopyrightText: OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. # CVE results conversion script: JSON format to text
  6. # Derived from cve-report.py from Oniro (MIT, by Huawei Inc)
  7. import sys
  8. import getopt
  9. infile = "in.json"
  10. outfile = "out.txt"
  11. def show_syntax_and_exit(code):
  12. """
  13. Show the program syntax and exit with an errror
  14. Arguments:
  15. code: the error code to return
  16. """
  17. print("Syntax: %s [-h] [-i inputJSONfile][-o outputfile]" % sys.argv[0])
  18. sys.exit(code)
  19. def exit_error(code, message):
  20. """
  21. Show the error message and exit with an errror
  22. Arguments:
  23. code: the error code to return
  24. message: the message to show
  25. """
  26. print("Error: %s" % message)
  27. sys.exit(code)
  28. def parse_args(argv):
  29. """
  30. Parse the program arguments, put options in global variables
  31. Arguments:
  32. argv: program arguments
  33. """
  34. global infile, outfile
  35. try:
  36. opts, args = getopt.getopt(
  37. argv, "hi:o:", ["help", "input", "output"]
  38. )
  39. except getopt.GetoptError:
  40. show_syntax_and_exit(1)
  41. for opt, arg in opts:
  42. if opt in ("-h"):
  43. show_syntax_and_exit(0)
  44. elif opt in ("-i"):
  45. infile = arg
  46. elif opt in ("-o"):
  47. outfile = arg
  48. def load_json(filename):
  49. """
  50. Load the JSON file, return the resulting dictionary
  51. Arguments:
  52. filename: the file to open
  53. Returns:
  54. Parsed file as a dictionary
  55. """
  56. import json
  57. out = {}
  58. try:
  59. with open(filename, "r") as f:
  60. out = json.load(f)
  61. except FileNotFoundError:
  62. exit_error(1, "Input file (%s) not found" % (filename))
  63. except json.decoder.JSONDecodeError as error:
  64. exit_error(1, "Malformed JSON file: %s" % str(error))
  65. return out
  66. def process_data(filename, data):
  67. """
  68. Write the resulting CSV with one line for each package
  69. Arguments:
  70. filename: the file to write to
  71. data: dictionary from parsing the JSON file
  72. Returns:
  73. None
  74. """
  75. if not "version" in data or data["version"] != "1":
  76. exit_error(1, "Unrecognized format version number")
  77. if not "package" in data:
  78. exit_error(1, "Mandatory 'package' key not found")
  79. lines = ""
  80. total_issue_count = 0
  81. for package in data["package"]:
  82. package_info = ""
  83. keys_in_package = {"name", "layer", "version", "issue"}
  84. if keys_in_package - package.keys():
  85. exit_error(
  86. 1,
  87. "Missing a mandatory key in package: %s"
  88. % (keys_in_package - package.keys()),
  89. )
  90. package_info += "LAYER: %s\n" % package["layer"]
  91. package_info += "PACKAGE NAME: %s\n" % package["name"]
  92. package_info += "PACKAGE VERSION: %s\n" % package["version"]
  93. for issue in package["issue"]:
  94. keys_in_issue = {"id", "status", "detail"}
  95. if keys_in_issue - issue.keys():
  96. print("Warning: Missing keys %s in 'issue' for the package '%s'"
  97. % (keys_in_issue - issue.keys(), package["name"]))
  98. lines += package_info
  99. lines += "CVE: %s\n" % issue["id"]
  100. lines += "CVE STATUS: %s\n" % issue["status"]
  101. lines += "CVE DETAIL: %s\n" % issue["detail"]
  102. if "description" in issue:
  103. lines += "CVE DESCRIPTION: %s\n" % issue["description"]
  104. if "summary" in issue:
  105. lines += "CVE SUMMARY: %s\n" % issue["summary"]
  106. if "scorev2" in issue:
  107. lines += "CVSS v2 BASE SCORE: %s\n" % issue["scorev2"]
  108. if "scorev3" in issue:
  109. lines += "CVSS v3 BASE SCORE: %s\n" % issue["scorev3"]
  110. if "scorev4" in issue:
  111. lines += "CVSS v4 BASE SCORE: %s\n" % issue["scorev4"]
  112. if "vector" in issue:
  113. lines += "VECTOR: %s\n" % issue["vector"]
  114. if "vectorString" in issue:
  115. lines += "VECTORSTRING: %s\n" % issue["vectorString"]
  116. lines += "MORE INFORMATION: https://nvd.nist.gov/vuln/detail/%s\n" % issue["id"]
  117. lines += "\n"
  118. with open(filename, "w") as f:
  119. f.write(lines)
  120. def main(argv):
  121. parse_args(argv)
  122. data = load_json(infile)
  123. process_data(outfile, data)
  124. if __name__ == "__main__":
  125. main(sys.argv[1:])