aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/main/java/org/sonar/scanner/externalissue/sarif/LocationMapper.java
blob: fe515028b8f13697a08b96eef4291987dc1ef798 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner.externalissue.sarif;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.predicates.AbstractFilePredicate;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.issue.NewIssueLocation;
import org.sonar.api.notifications.AnalysisWarnings;
import org.sonar.api.scanner.ScannerSide;
import org.sonar.sarif.pojo.ArtifactLocation;
import org.sonar.sarif.pojo.Location;
import org.sonar.sarif.pojo.PhysicalLocation;

import static org.sonar.api.utils.Preconditions.checkArgument;

@ScannerSide
public class LocationMapper {
  private static final Logger LOG = LoggerFactory.getLogger(LocationMapper.class);

  private static final int CACHE_SIZE = 500;
  private static final int CACHE_EXPIRY = 60;

  private final SensorContext sensorContext;
  private final RegionMapper regionMapper;
  private final AnalysisWarnings analysisWarnings;

  LoadingCache<String, Optional<InputFile>> inputFileCache = CacheBuilder.newBuilder()
    .maximumSize(CACHE_SIZE)
    .expireAfterAccess(CACHE_EXPIRY, TimeUnit.SECONDS)
    .concurrencyLevel(Runtime.getRuntime().availableProcessors())
    .build(getCacheLoader());

  LocationMapper(SensorContext sensorContext, RegionMapper regionMapper, AnalysisWarnings analysisWarnings) {
    this.sensorContext = sensorContext;
    this.regionMapper = regionMapper;
    this.analysisWarnings = analysisWarnings;
  }

  void fillIssueInProjectLocation(NewIssueLocation newIssueLocation) {
    newIssueLocation
      .on(sensorContext.project());
  }

  boolean fillIssueInFileLocation(NewIssueLocation newIssueLocation, Location location) {
    PhysicalLocation physicalLocation = location.getPhysicalLocation();

    String fileUri = getFileUriOrThrow(location);
    Optional<InputFile> file = findFile(fileUri);
    if (file.isEmpty()) {
      String unresolvedLocationWarning = String.format("Unable to resolve Issue location from SARIF physical location %s. Falling back to the project location.", fileUri);
      analysisWarnings.addUnique(unresolvedLocationWarning);
      LOG.warn(unresolvedLocationWarning);
      return false;
    }
    InputFile inputFile = file.get();
    newIssueLocation.on(inputFile);
    regionMapper.mapRegion(physicalLocation.getRegion(), inputFile).ifPresent(newIssueLocation::at);
    return true;
  }

  private static String getFileUriOrThrow(Location location) {
    PhysicalLocation physicalLocation = location.getPhysicalLocation();
    checkArgument(hasUriFieldPopulated(physicalLocation), "The field location.physicalLocation.artifactLocation.uri is not set.");
    return physicalLocation.getArtifactLocation().getUri();
  }

  private static boolean hasUriFieldPopulated(@Nullable PhysicalLocation location) {
    return Optional.ofNullable(location).map(PhysicalLocation::getArtifactLocation).map(ArtifactLocation::getUri).isPresent();
  }

  private Optional<InputFile> findFile(String filePath) {
    return inputFileCache.getUnchecked(filePath);
  }

  private CacheLoader<String, Optional<InputFile>> getCacheLoader() {
    return new CacheLoader<>() {
      @NotNull
      @Override
      public Optional<InputFile> load(final String filePath) {
        return computeInputFile(filePath);
      }
    };
  }

  private Optional<InputFile> computeInputFile(String key) {
    // we use a custom predicate (which is not optimized) because fileSystem().predicates().is() doesn't handle symlinks correctly
    return Optional.ofNullable(sensorContext.fileSystem().inputFile(new LocationMapper.IsPredicate(getFileFromAbsoluteUriOrPath(key).toPath())));
  }

  private static File getFileFromAbsoluteUriOrPath(String filePath) {
    URI uri = URI.create(filePath);
    if (uri.isAbsolute()) {
      return getFileFromAbsoluteUri(filePath, uri);
    } else {
      return new File(filePath);
    }
  }

  @NotNull
  private static File getFileFromAbsoluteUri(String filePath, URI uri) {
    String path = uri.getPath();
    if (StringUtils.isNotBlank(path)) {
      return new File(path);
    } else {
      throw new IllegalArgumentException("Invalid file scheme URI: " + filePath);
    }
  }

  @VisibleForTesting
  static class IsPredicate extends AbstractFilePredicate {
    private final Path path;

    public IsPredicate(Path path) {
      this.path = path;
    }

    @Override
    public boolean apply(InputFile inputFile) {
      try {
        return Files.isSameFile(path, inputFile.path());
      } catch (IOException e) {
        return false;
      }
    }
  }

}