You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

update-cwes.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /* eslint-disable no-console */
  21. /**
  22. * Execute this script by passing the path to the CWE XML definition file.
  23. *
  24. * You can download the full CWE database in XML format here: https://cwe.mitre.org/data/downloads.html
  25. * Make sure to unzip the downloaded file first before passing it to this script.
  26. *
  27. * Usage:
  28. * node scripts/update-cwes.js PATH
  29. * or:
  30. * yarn update-cwes PATH
  31. *
  32. * Example:
  33. * node scripts/update-cwes.js ~/Downloads/cwec_v4.6.xml
  34. * or:
  35. * yarn update-cwes ~/Downloads/cwec_v4.6.xml
  36. */
  37. const fs = require('fs');
  38. const chalk = require('chalk');
  39. const jsdom = require('jsdom');
  40. const { trim } = require('lodash');
  41. const path = require('path');
  42. const STANDARDS_JSON_FILE = path.join(
  43. __dirname,
  44. '..',
  45. 'src',
  46. 'main',
  47. 'js',
  48. 'helpers',
  49. 'standards.json'
  50. );
  51. const xmlContent = readXMLContent(process.argv[2]);
  52. const newCWEs = getCWEs(xmlContent);
  53. writeToStandardsJson(newCWEs);
  54. function readXMLContent(xmlPath) {
  55. if (fs.existsSync(xmlPath)) {
  56. try {
  57. fs.accessSync(xmlPath, fs.constants.R_OK);
  58. return fs.readFileSync(xmlPath).toString();
  59. } catch (e) {
  60. console.error(chalk.red(`No read access for XML file '${xmlPath}'`));
  61. throw e;
  62. }
  63. } else {
  64. console.error(chalk.red(`Cannot find XML file '${xmlPath}'`));
  65. throw Error('');
  66. }
  67. }
  68. function getCWEs(xml) {
  69. const document = new jsdom.JSDOM(xml);
  70. const weaknesses = document.window.document.querySelectorAll('Weaknesses Weakness');
  71. const cwes = {
  72. unknown: {
  73. title: 'No CWE associated'
  74. }
  75. };
  76. weaknesses.forEach(weakness => {
  77. const id = weakness.getAttribute('ID');
  78. const title = weakness.getAttribute('Name');
  79. let description = '';
  80. if (!id) {
  81. return;
  82. }
  83. if (!title) {
  84. console.log(chalk.yellow(`No Name attribute found for CWE '${id}'. Skipping.`));
  85. return;
  86. }
  87. const descriptionEl = weakness.querySelector('Description');
  88. if (descriptionEl) {
  89. description = trim(descriptionEl.textContent);
  90. }
  91. cwes[id] = { title, description };
  92. });
  93. return cwes;
  94. }
  95. function writeToStandardsJson(cwes) {
  96. try {
  97. fs.accessSync(STANDARDS_JSON_FILE, fs.constants.W_OK);
  98. } catch (e) {
  99. console.error(chalk.red(`No write access for standards.json ('${STANDARDS_JSON_FILE}') file`));
  100. throw e;
  101. }
  102. try {
  103. const json = JSON.parse(fs.readFileSync(STANDARDS_JSON_FILE).toString());
  104. json.cwe = cwes;
  105. fs.writeFileSync(STANDARDS_JSON_FILE, JSON.stringify(json, undefined, 2));
  106. } catch (e) {
  107. console.error(
  108. chalk.red(`Failed to write data to standards.json ('${STANDARDS_JSON_FILE}') file`)
  109. );
  110. throw e;
  111. }
  112. }