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.

HotspotRuleDescription.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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. package org.sonar.server.rule;
  21. import java.util.Optional;
  22. import javax.annotation.CheckForNull;
  23. import javax.annotation.Nullable;
  24. import org.sonar.db.rule.RuleDefinitionDto;
  25. import org.sonar.db.rule.RuleForIndexingDto;
  26. import static java.util.Optional.ofNullable;
  27. /***
  28. * Temporary rule split before we have a better
  29. * solution to specify security hotspot.
  30. */
  31. public class HotspotRuleDescription {
  32. private static final HotspotRuleDescription NO_DESCRIPTION = new HotspotRuleDescription(null, null, null);
  33. @CheckForNull
  34. private final String risk;
  35. @CheckForNull
  36. private final String vulnerable;
  37. @CheckForNull
  38. private final String fixIt;
  39. private HotspotRuleDescription(@Nullable String risk, @Nullable String vulnerable, @Nullable String fixIt) {
  40. this.risk = risk;
  41. this.vulnerable = vulnerable;
  42. this.fixIt = fixIt;
  43. }
  44. public static HotspotRuleDescription from(RuleDefinitionDto dto) {
  45. String description = dto.isCustomRule() ? RuleDescriptionFormatter.getDescriptionAsHtml(dto) : dto.getDescription();
  46. return from(description);
  47. }
  48. public static HotspotRuleDescription from(RuleForIndexingDto dto) {
  49. return from(dto.getDescription());
  50. }
  51. private static HotspotRuleDescription from(@Nullable String description) {
  52. if (description == null) {
  53. return NO_DESCRIPTION;
  54. }
  55. String[] split = extractSection("", description);
  56. description = split[0];
  57. String ruleDescriptionSection = split[1];
  58. split = extractSection("<h2>Exceptions</h2>", description);
  59. description = split[0];
  60. String exceptions = split[1];
  61. split = extractSection("<h2>Ask Yourself Whether</h2>", description);
  62. description = split[0];
  63. String askSection = split[1];
  64. split = extractSection("<h2>Sensitive Code Example</h2>", description);
  65. description = split[0];
  66. String sensitiveSection = split[1];
  67. split = extractSection("<h2>Noncompliant Code Example</h2>", description);
  68. description = split[0];
  69. String noncompliantSection = split[1];
  70. split = extractSection("<h2>Recommended Secure Coding Practices</h2>", description);
  71. description = split[0];
  72. String recommendedSection = split[1];
  73. split = extractSection("<h2>Compliant Solution</h2>", description);
  74. description = split[0];
  75. String compliantSection = split[1];
  76. split = extractSection("<h2>See</h2>", description);
  77. description = split[0];
  78. String seeSection = split[1];
  79. return new HotspotRuleDescription(
  80. trimToNull(ruleDescriptionSection + exceptions + description),
  81. trimToNull(askSection + sensitiveSection + noncompliantSection),
  82. trimToNull(recommendedSection + compliantSection + seeSection));
  83. }
  84. private static String trimToNull(String input) {
  85. return input.isEmpty() ? null : input;
  86. }
  87. private static String[] extractSection(String beginning, String description) {
  88. String endSection = "<h2>";
  89. int beginningIndex = description.indexOf(beginning);
  90. if (beginningIndex != -1) {
  91. int endIndex = description.indexOf(endSection, beginningIndex + beginning.length());
  92. if (endIndex == -1) {
  93. endIndex = description.length();
  94. }
  95. return new String[]{
  96. description.substring(0, beginningIndex) + description.substring(endIndex),
  97. description.substring(beginningIndex, endIndex)
  98. };
  99. } else {
  100. return new String[]{description, ""};
  101. }
  102. }
  103. public Optional<String> getRisk() {
  104. return ofNullable(risk);
  105. }
  106. public Optional<String> getVulnerable() {
  107. return ofNullable(vulnerable);
  108. }
  109. public Optional<String> getFixIt() {
  110. return ofNullable(fixIt);
  111. }
  112. public boolean isComplete() {
  113. return risk != null && vulnerable != null && fixIt != null;
  114. }
  115. @Override
  116. public String toString() {
  117. return "HotspotRuleDescription{" +
  118. "risk='" + risk + '\'' +
  119. ", vulnerable='" + vulnerable + '\'' +
  120. ", fixIt='" + fixIt + '\'' +
  121. '}';
  122. }
  123. }