3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.issue;
22 import java.util.HashMap;
23 import java.util.LinkedList;
24 import java.util.List;
26 import java.util.regex.Pattern;
27 import org.apache.commons.codec.digest.DigestUtils;
28 import org.sonar.ce.task.projectanalysis.component.Component;
29 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
30 import org.sonar.ce.task.projectanalysis.source.SourceLinesRepository;
31 import org.sonar.core.issue.DefaultIssue;
32 import org.sonar.core.util.CloseableIterator;
33 import org.sonar.db.protobuf.DbCommons;
34 import org.sonar.db.protobuf.DbIssues;
35 import org.sonar.server.issue.TaintChecker;
37 import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
38 import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
41 * This visitor will update the locations field of issues, by filling the hashes for all locations.
42 * It only applies to issues that are taint vulnerabilities or security hotspots, and that are new or were changed.
43 * For performance reasons, it will read each source code file once and feed the lines to all locations in that file.
45 public class ComputeLocationHashesVisitor extends IssueVisitor {
46 private static final Pattern MATCH_ALL_WHITESPACES = Pattern.compile("\\s");
47 private final List<DefaultIssue> issues = new LinkedList<>();
48 private final SourceLinesRepository sourceLinesRepository;
49 private final TreeRootHolder treeRootHolder;
50 private final TaintChecker taintChecker;
52 public ComputeLocationHashesVisitor(TaintChecker taintChecker, SourceLinesRepository sourceLinesRepository, TreeRootHolder treeRootHolder) {
53 this.taintChecker = taintChecker;
54 this.sourceLinesRepository = sourceLinesRepository;
55 this.treeRootHolder = treeRootHolder;
59 public void beforeComponent(Component component) {
64 public void onIssue(Component component, DefaultIssue issue) {
65 if (shouldComputeLocation(issue)) {
70 private boolean shouldComputeLocation(DefaultIssue issue) {
71 return (taintChecker.isTaintVulnerability(issue) || SECURITY_HOTSPOT.equals(issue.type()))
72 && !issue.isFromExternalRuleEngine()
73 && (issue.isNew() || issue.locationsChanged());
77 public void beforeCaching(Component component) {
78 Map<Component, List<Location>> locationsByComponent = new HashMap<>();
79 List<LocationToSet> locationsToSet = new LinkedList<>();
81 for (DefaultIssue issue : issues) {
82 DbIssues.Locations locations = issue.getLocations();
83 if (locations == null) {
87 DbIssues.Locations.Builder primaryLocationBuilder = locations.toBuilder();
88 boolean hasTextRange = addLocations(component, issue, locationsByComponent, primaryLocationBuilder);
90 // If any location was added (because it had a text range), we'll need to update the issue at the end with the new object containing the hashes
92 locationsToSet.add(new LocationToSet(issue, primaryLocationBuilder));
96 // Feed lines to locations, component by component
97 locationsByComponent.forEach(this::updateLocationsInComponent);
99 // Finalize by setting hashes
100 locationsByComponent.values().forEach(list -> list.forEach(Location::afterAllLines));
102 // set new locations to issues
103 locationsToSet.forEach(LocationToSet::set);
108 private boolean addLocations(Component component, DefaultIssue issue, Map<Component, List<Location>> locationsByComponent, DbIssues.Locations.Builder primaryLocationBuilder) {
109 boolean hasPrimaryLocation = addPrimaryLocation(component, locationsByComponent, primaryLocationBuilder);
110 boolean hasSecondaryLocations = addSecondaryLocations(issue, locationsByComponent, primaryLocationBuilder);
111 return hasPrimaryLocation || hasSecondaryLocations;
114 private static boolean addPrimaryLocation(Component component, Map<Component, List<Location>> locationsByComponent, DbIssues.Locations.Builder primaryLocationBuilder) {
115 if (!primaryLocationBuilder.hasTextRange()) {
118 PrimaryLocation primaryLocation = new PrimaryLocation(primaryLocationBuilder);
119 locationsByComponent.computeIfAbsent(component, c -> new LinkedList<>()).add(primaryLocation);
123 private boolean addSecondaryLocations(DefaultIssue issue, Map<Component, List<Location>> locationsByComponent, DbIssues.Locations.Builder primaryLocationBuilder) {
124 if (SECURITY_HOTSPOT.equals(issue.type())) {
128 List<DbIssues.Location.Builder> locationBuilders = primaryLocationBuilder.getFlowBuilderList().stream()
129 .flatMap(flowBuilder -> flowBuilder.getLocationBuilderList().stream())
130 .filter(DbIssues.Location.Builder::hasTextRange)
133 locationBuilders.forEach(locationBuilder -> addSecondaryLocation(locationBuilder, issue, locationsByComponent));
135 return !locationBuilders.isEmpty();
138 private void addSecondaryLocation(DbIssues.Location.Builder locationBuilder, DefaultIssue issue, Map<Component, List<Location>> locationsByComponent) {
139 String componentUuid = defaultIfEmpty(locationBuilder.getComponentId(), issue.componentUuid());
140 Component locationComponent = treeRootHolder.getComponentByUuid(componentUuid);
141 locationsByComponent.computeIfAbsent(locationComponent, c -> new LinkedList<>()).add(new SecondaryLocation(locationBuilder));
144 private void updateLocationsInComponent(Component component, List<Location> locations) {
145 try (CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(component)) {
147 while (linesIterator.hasNext()) {
148 String line = linesIterator.next();
149 for (Location location : locations) {
150 location.processLine(lineNumber, line);
157 private static class LocationToSet {
158 private final DefaultIssue issue;
159 private final DbIssues.Locations.Builder locationsBuilder;
161 public LocationToSet(DefaultIssue issue, DbIssues.Locations.Builder locationsBuilder) {
163 this.locationsBuilder = locationsBuilder;
167 issue.setLocations(locationsBuilder.build());
171 private static class PrimaryLocation extends Location {
172 private final DbIssues.Locations.Builder locationsBuilder;
174 public PrimaryLocation(DbIssues.Locations.Builder locationsBuilder) {
175 this.locationsBuilder = locationsBuilder;
179 DbCommons.TextRange getTextRange() {
180 return locationsBuilder.getTextRange();
184 void setHash(String hash) {
185 locationsBuilder.setChecksum(hash);
189 private static class SecondaryLocation extends Location {
190 private final DbIssues.Location.Builder locationBuilder;
192 public SecondaryLocation(DbIssues.Location.Builder locationBuilder) {
193 this.locationBuilder = locationBuilder;
197 DbCommons.TextRange getTextRange() {
198 return locationBuilder.getTextRange();
202 void setHash(String hash) {
203 locationBuilder.setChecksum(hash);
207 private abstract static class Location {
208 private final StringBuilder hashBuilder = new StringBuilder();
210 abstract DbCommons.TextRange getTextRange();
212 abstract void setHash(String hash);
214 public void processLine(int lineNumber, String line) {
215 DbCommons.TextRange textRange = getTextRange();
216 if (lineNumber > textRange.getEndLine() || lineNumber < textRange.getStartLine()) {
220 if (lineNumber == textRange.getStartLine() && lineNumber == textRange.getEndLine()) {
221 hashBuilder.append(line, textRange.getStartOffset(), textRange.getEndOffset());
222 } else if (lineNumber == textRange.getStartLine()) {
223 hashBuilder.append(line, textRange.getStartOffset(), line.length());
224 } else if (lineNumber < textRange.getEndLine()) {
225 hashBuilder.append(line);
227 hashBuilder.append(line, 0, textRange.getEndOffset());
231 void afterAllLines() {
232 String issueContentWithoutWhitespaces = MATCH_ALL_WHITESPACES.matcher(hashBuilder.toString()).replaceAll("");
233 String hash = DigestUtils.md5Hex(issueContentWithoutWhitespaces);