3 * Copyright (C) 2009-2022 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;
40 * This visitor will update the locations field of issues, by filling the hashes for all locations.
41 * It only applies to issues that are taint vulnerabilities and that are new or were changed.
42 * For performance reasons, it will read each source code file once and feed the lines to all locations in that file.
44 public class ComputeLocationHashesVisitor extends IssueVisitor {
45 private static final Pattern MATCH_ALL_WHITESPACES = Pattern.compile("\\s");
46 private final List<DefaultIssue> issues = new LinkedList<>();
47 private final SourceLinesRepository sourceLinesRepository;
48 private final TreeRootHolder treeRootHolder;
49 private final TaintChecker taintChecker;
51 public ComputeLocationHashesVisitor(TaintChecker taintChecker, SourceLinesRepository sourceLinesRepository, TreeRootHolder treeRootHolder) {
52 this.taintChecker = taintChecker;
53 this.sourceLinesRepository = sourceLinesRepository;
54 this.treeRootHolder = treeRootHolder;
58 public void beforeComponent(Component component) {
63 public void onIssue(Component component, DefaultIssue issue) {
64 if (taintChecker.isTaintVulnerability(issue) && !issue.isFromExternalRuleEngine() && (issue.isNew() || issue.locationsChanged())) {
70 public void afterComponent(Component component) {
71 Map<Component, List<Location>> locationsByComponent = new HashMap<>();
72 List<LocationToSet> locationsToSet = new LinkedList<>();
74 for (DefaultIssue issue : issues) {
75 if (issue.getLocations() == null) {
79 DbIssues.Locations.Builder primaryLocationBuilder = ((DbIssues.Locations) issue.getLocations()).toBuilder();
80 boolean hasTextRange = addLocations(component, issue, locationsByComponent, primaryLocationBuilder);
82 // 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
84 locationsToSet.add(new LocationToSet(issue, primaryLocationBuilder));
88 // Feed lines to locations, component by component
89 locationsByComponent.forEach(this::updateLocationsInComponent);
91 // Finalize by setting hashes
92 locationsByComponent.values().forEach(list -> list.forEach(Location::afterAllLines));
94 // set new locations to issues
95 locationsToSet.forEach(LocationToSet::set);
100 private boolean addLocations(Component component, DefaultIssue issue, Map<Component, List<Location>> locationsByComponent, DbIssues.Locations.Builder primaryLocationBuilder) {
101 boolean hasTextRange = false;
103 // Add primary location
104 if (primaryLocationBuilder.hasTextRange()) {
106 PrimaryLocation primaryLocation = new PrimaryLocation(primaryLocationBuilder);
107 locationsByComponent.computeIfAbsent(component, c -> new LinkedList<>()).add(primaryLocation);
110 // Add secondary locations
111 for (DbIssues.Flow.Builder flowBuilder : primaryLocationBuilder.getFlowBuilderList()) {
112 for (DbIssues.Location.Builder locationBuilder : flowBuilder.getLocationBuilderList()) {
113 if (locationBuilder.hasTextRange()) {
115 var componentUuid = defaultIfEmpty(locationBuilder.getComponentId(), issue.componentUuid());
116 Component locationComponent = treeRootHolder.getComponentByUuid(componentUuid);
117 locationsByComponent.computeIfAbsent(locationComponent, c -> new LinkedList<>()).add(new SecondaryLocation(locationBuilder));
125 private void updateLocationsInComponent(Component component, List<Location> locations) {
126 try (CloseableIterator<String> linesIterator = sourceLinesRepository.readLines(component)) {
128 while (linesIterator.hasNext()) {
129 String line = linesIterator.next();
130 for (Location location : locations) {
131 location.processLine(lineNumber, line);
138 private static class LocationToSet {
139 private final DefaultIssue issue;
140 private final DbIssues.Locations.Builder locationsBuilder;
142 public LocationToSet(DefaultIssue issue, DbIssues.Locations.Builder locationsBuilder) {
144 this.locationsBuilder = locationsBuilder;
148 issue.setLocations(locationsBuilder.build());
152 private static class PrimaryLocation extends Location {
153 private final DbIssues.Locations.Builder locationsBuilder;
155 public PrimaryLocation(DbIssues.Locations.Builder locationsBuilder) {
156 this.locationsBuilder = locationsBuilder;
160 DbCommons.TextRange getTextRange() {
161 return locationsBuilder.getTextRange();
165 void setHash(String hash) {
166 locationsBuilder.setChecksum(hash);
170 private static class SecondaryLocation extends Location {
171 private final DbIssues.Location.Builder locationBuilder;
173 public SecondaryLocation(DbIssues.Location.Builder locationBuilder) {
174 this.locationBuilder = locationBuilder;
178 DbCommons.TextRange getTextRange() {
179 return locationBuilder.getTextRange();
183 void setHash(String hash) {
184 locationBuilder.setChecksum(hash);
188 private abstract static class Location {
189 private final StringBuilder hashBuilder = new StringBuilder();
191 abstract DbCommons.TextRange getTextRange();
193 abstract void setHash(String hash);
195 public void processLine(int lineNumber, String line) {
196 DbCommons.TextRange textRange = getTextRange();
197 if (lineNumber > textRange.getEndLine() || lineNumber < textRange.getStartLine()) {
201 if (lineNumber == textRange.getStartLine() && lineNumber == textRange.getEndLine()) {
202 hashBuilder.append(line, textRange.getStartOffset(), textRange.getEndOffset());
203 } else if (lineNumber == textRange.getStartLine()) {
204 hashBuilder.append(line, textRange.getStartOffset(), line.length());
205 } else if (lineNumber < textRange.getEndLine()) {
206 hashBuilder.append(line);
208 hashBuilder.append(line, 0, textRange.getEndOffset());
212 void afterAllLines() {
213 String issueContentWithoutWhitespaces = MATCH_ALL_WHITESPACES.matcher(hashBuilder.toString()).replaceAll("");
214 String hash = DigestUtils.md5Hex(issueContentWithoutWhitespaces);