Skip to content

Commit 7c859be

Browse files
Review comments resolved
1 parent 601a93c commit 7c859be

5 files changed

Lines changed: 75 additions & 97 deletions

File tree

devassist-lib/src/main/java/com/checkmarx/intellij/devassist/ignore/IgnoreManager.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ public void updateLineNumbersForIgnoredEntriesByProblematicLine(ScanResult<?> fu
772772
// Iterate through all ignore entries using normal for-each
773773
for (Map.Entry<String, IgnoreEntry> mapEntry : ignoreFileManager.getIgnoreData().entrySet()) {
774774
IgnoreEntry ignoreEntry = mapEntry.getValue();
775-
if (!"ASCA".equalsIgnoreCase(String.valueOf(ignoreEntry.getType()))) {
775+
if (!ScanEngine.ASCA.toString().equalsIgnoreCase(String.valueOf(ignoreEntry.getType()))) {
776776
continue; // Only process ASCA entries
777777
}
778778
// Remove file references that are not present in the scan result
@@ -783,8 +783,8 @@ public void updateLineNumbersForIgnoredEntriesByProblematicLine(ScanResult<?> fu
783783
String ignoredProblematicLine = fileRef.getProblematicLine();
784784
// Find a matching vulnerability by problematicLine (null-safe)
785785
VulnerabilityWithLine match = vulnerabilitiesWithLine.stream()
786-
.filter(vwl -> Objects.equals(vwl.problematicLine, ignoredProblematicLine))
787-
.findFirst().orElse(null);
786+
.filter(vwl -> Objects.equals(vwl.problematicLine, ignoredProblematicLine))
787+
.findFirst().orElse(null);
788788
if (match != null && match.line > 0 && fileRef.getLine() != match.line) {
789789
fileRef.setLine(match.line);
790790
hasChanges = true;
@@ -840,7 +840,7 @@ public void removeIgnoreEntriesForFileIfEmpty(String filePath) {
840840
boolean removed = false;
841841
for (Map.Entry<String, IgnoreEntry> mapEntry : ignoreFileManager.getIgnoreData().entrySet()) {
842842
IgnoreEntry ignoreEntry = mapEntry.getValue();
843-
if (!"ASCA".equalsIgnoreCase(String.valueOf(ignoreEntry.getType()))) {
843+
if (!ScanEngine.ASCA.toString().equalsIgnoreCase(String.valueOf(ignoreEntry.getType()))) {
844844
continue;
845845
}
846846
// Remove file references for this file
@@ -862,4 +862,38 @@ public void removeIgnoreEntriesForFileIfEmpty(String filePath) {
862862
}
863863
}
864864

865+
public boolean isIgnored(ScanIssue issue, List<IgnoreEntry> ignoreEntries, String filePath) {
866+
String normalizedPath = ignoreFileManager.normalizePath(filePath);
867+
boolean isAsca = issue.getScanEngine() == ScanEngine.ASCA;
868+
// For ASCA, check problematicLine for all vulnerabilities
869+
if (isAsca && issue.getVulnerabilities() != null && !issue.getVulnerabilities().isEmpty()) {
870+
for (Vulnerability vuln : issue.getVulnerabilities()) {
871+
String issueProblematicLine = vuln.getProblematicLine();
872+
for (IgnoreEntry entry : ignoreEntries) {
873+
for (IgnoreEntry.FileReference ref : entry.getFiles()) {
874+
boolean pathMatch = ref.isActive() && ref.getPath().equals(normalizedPath);
875+
boolean problematicLineMatch = (issueProblematicLine == null && ref.getProblematicLine() == null)
876+
|| (issueProblematicLine != null && issueProblematicLine.equals(ref.getProblematicLine()));
877+
if (pathMatch && problematicLineMatch) {
878+
return true;
879+
}
880+
}
881+
}
882+
}
883+
return false;
884+
}
885+
// Default: match by path and line
886+
int issueLine = issue.getLocations() != null && !issue.getLocations().isEmpty()
887+
? issue.getLocations().get(0).getLine()
888+
: -1;
889+
for (IgnoreEntry entry : ignoreEntries) {
890+
for (IgnoreEntry.FileReference ref : entry.getFiles()) {
891+
if (ref.isActive() && ref.getPath().equals(normalizedPath) && ref.getLine() == issueLine) {
892+
return true;
893+
}
894+
}
895+
}
896+
return false;
897+
}
898+
865899
}

devassist-lib/src/main/java/com/checkmarx/intellij/devassist/scanners/asca/AscaScanResultAdaptor.java

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import com.checkmarx.ast.asca.ScanResult;
55
import com.checkmarx.intellij.common.utils.SeverityLevel;
66
import com.checkmarx.intellij.common.utils.Utils;
7-
import com.checkmarx.intellij.devassist.ignore.IgnoreEntry;
8-
import com.checkmarx.intellij.devassist.ignore.IgnoreFileManager;
97
import com.checkmarx.intellij.devassist.model.Location;
108
import com.checkmarx.intellij.devassist.model.ScanIssue;
119
import com.checkmarx.intellij.devassist.model.Vulnerability;
@@ -14,10 +12,7 @@
1412
import com.checkmarx.intellij.devassist.utils.ScanEngine;
1513
import com.intellij.openapi.diagnostic.Logger;
1614
import org.jetbrains.annotations.NotNull;
17-
import com.intellij.openapi.project.Project;
1815

19-
import java.nio.file.Path;
20-
import java.nio.file.Paths;
2116
import java.util.*;
2217
import java.util.stream.Collectors;
2318

@@ -32,8 +27,6 @@ public class AscaScanResultAdaptor implements com.checkmarx.intellij.devassist.c
3227
private final ScanResult ascaScanResult;
3328
private final String filePath;
3429
private final List<ScanIssue> scanIssues;
35-
private String workspaceRootPath = "";
36-
private final Project project;
3730

3831

3932
/**
@@ -43,12 +36,9 @@ public class AscaScanResultAdaptor implements com.checkmarx.intellij.devassist.c
4336
* @param ascaScanResult the ASCA scan results to be wrapped by this adapter
4437
* @param filePath the path of the file being scanned (needed for UI display)
4538
*/
46-
public AscaScanResultAdaptor(ScanResult ascaScanResult, String filePath, Project project) {
39+
public AscaScanResultAdaptor(ScanResult ascaScanResult, String filePath) {
4740
this.ascaScanResult = ascaScanResult;
4841
this.filePath = filePath;
49-
this.project = project;
50-
String basePath = project.getBasePath();
51-
this.workspaceRootPath = basePath;
5242
this.scanIssues = buildIssues();
5343
}
5444

@@ -237,51 +227,4 @@ private String getUniqueId(ScanDetail scanIssue) {
237227
return ScanEngine.ASCA.name();
238228
}
239229

240-
public boolean isIgnored(ScanIssue issue, List<IgnoreEntry> ignoreEntries, String filePath) {
241-
String normalizedPath = normalizePath(filePath);
242-
boolean isAsca = issue.getScanEngine() == ScanEngine.ASCA;
243-
// For ASCA, check problematicLine for all vulnerabilities
244-
if (isAsca && issue.getVulnerabilities() != null && !issue.getVulnerabilities().isEmpty()) {
245-
for (Vulnerability vuln : issue.getVulnerabilities()) {
246-
String issueProblematicLine = vuln.getProblematicLine();
247-
for (IgnoreEntry entry : ignoreEntries) {
248-
for (IgnoreEntry.FileReference ref : entry.getFiles()) {
249-
boolean pathMatch = ref.isActive() && ref.getPath().equals(normalizedPath);
250-
boolean problematicLineMatch = (issueProblematicLine == null && ref.getProblematicLine() == null)
251-
|| (issueProblematicLine != null && issueProblematicLine.equals(ref.getProblematicLine()));
252-
if (pathMatch && problematicLineMatch) {
253-
return true;
254-
}
255-
}
256-
}
257-
}
258-
return false;
259-
}
260-
// Default: match by path and line
261-
int issueLine = issue.getLocations() != null && !issue.getLocations().isEmpty()
262-
? issue.getLocations().get(0).getLine()
263-
: -1;
264-
for (IgnoreEntry entry : ignoreEntries) {
265-
for (IgnoreEntry.FileReference ref : entry.getFiles()) {
266-
if (ref.isActive() && ref.getPath().equals(normalizedPath) && ref.getLine() == issueLine) {
267-
return true;
268-
}
269-
}
270-
}
271-
return false;
272-
}
273-
274-
/**
275-
* normalizes the given file path to be relative to the project's workspace root.
276-
*
277-
* @param filePath
278-
* @return
279-
*/
280-
private String normalizePath(String filePath) {
281-
return Path.of(workspaceRootPath)
282-
.relativize(Paths.get(filePath))
283-
.toString()
284-
.replace("\\", "/");
285-
}
286-
287230
}

devassist-lib/src/main/java/com/checkmarx/intellij/devassist/scanners/asca/AscaScannerService.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.checkmarx.intellij.devassist.basescanner.BaseScannerService;
99
import com.checkmarx.intellij.devassist.configuration.ScannerConfig;
1010
import com.checkmarx.intellij.devassist.ignore.IgnoreEntry;
11+
import com.checkmarx.intellij.devassist.ignore.IgnoreFileManager;
1112
import com.checkmarx.intellij.devassist.ignore.IgnoreManager;
1213
import com.checkmarx.intellij.devassist.telemetry.TelemetryService;
1314
import com.checkmarx.intellij.devassist.utils.DevAssistConstants;
@@ -39,6 +40,7 @@
3940
public class AscaScannerService extends BaseScannerService<ScanResult> {
4041
private static final Logger LOGGER = Utils.getLogger(AscaScannerService.class);
4142
private static final String ASCA_DIR = "CxASCA";
43+
private static final Object SCAN_LOCK = new Object();
4244

4345
/**
4446
* Creates an ASCA scanner service with the default ASCA realtime configuration.
@@ -147,19 +149,20 @@ public com.checkmarx.intellij.devassist.common.ScanResult<ScanResult> scan(@NotN
147149
int issueCount = ascaResult.getScanDetails() != null ? ascaResult.getScanDetails().size() : 0;
148150
LOGGER.debug("ASCA scanner: scan completed - " + uri + " (" + issueCount + " issues found)");
149151

150-
AscaScanResultAdaptor scanResultAdaptor = new AscaScanResultAdaptor(ascaResult, uri, psiFile.getProject());
152+
AscaScanResultAdaptor scanResultAdaptor = new AscaScanResultAdaptor(ascaResult, uri);
151153

152154
// Filter out ignored issues based on problematicLine
153-
com.checkmarx.intellij.devassist.ignore.IgnoreFileManager ignoreFileManager = com.checkmarx.intellij.devassist.ignore.IgnoreFileManager.getInstance(psiFile.getProject());
155+
IgnoreManager ignoreManager = new IgnoreManager(psiFile.getProject());
156+
IgnoreFileManager ignoreFileManager = IgnoreFileManager.getInstance(psiFile.getProject());
154157
List<IgnoreEntry> ignoreEntries = ignoreFileManager.getAllIgnoreEntries();
155158
List<com.checkmarx.intellij.devassist.model.ScanIssue> filteredIssues = new ArrayList<>();
156159
for (com.checkmarx.intellij.devassist.model.ScanIssue issue : scanResultAdaptor.getIssues()) {
157-
if (!scanResultAdaptor.isIgnored(issue, ignoreEntries, uri)) {
160+
if (!ignoreManager.isIgnored(issue, ignoreEntries, uri)) {
158161
filteredIssues.add(issue);
159162
}
160163
}
161164
// Return a new adaptor with only non-ignored issues
162-
AscaScanResultAdaptor filteredAdaptor = new AscaScanResultAdaptor(ascaResult, uri, psiFile.getProject()) {
165+
AscaScanResultAdaptor filteredAdaptor = new AscaScanResultAdaptor(ascaResult, uri) {
163166
@Override
164167
public List<com.checkmarx.intellij.devassist.model.ScanIssue> getIssues() {
165168
return filteredIssues;
@@ -201,24 +204,25 @@ private ScanResult runAscaScan(PsiFile file, Project project, boolean ascLatestV
201204
return null;
202205
}
203206

204-
String tempFilePath = saveTempFile(file.getName(), fileContent);
205-
if (tempFilePath == null) {
206-
LOGGER.warn("Failed to create temporary file for ASCA scan.");
207-
return null;
208-
}
209-
210-
try {
211-
LOGGER.info(Strings.join("Starting ASCA scan on file: ", virtualFile.getPath()));
212-
ScanResult scanResult = scanAscaFile(tempFilePath, ascLatestVersion, agent, DevAssistUtils.getIgnoreFilePath(project));
213-
// Update line numbers for ignored ASCA issues if any exist
214-
updateIgnoredFileDataOnLatestResult(tempFilePath, project, uri, agent, ascLatestVersion);
215-
handleScanResult(file, scanResult);
216-
return scanResult;
217-
} catch (Exception e) {
218-
LOGGER.warn("Error during ASCA scan:", e);
219-
return null;
220-
} finally {
221-
deleteFile(tempFilePath);
207+
synchronized (SCAN_LOCK) {
208+
String tempFilePath = saveTempFile(file.getName(), fileContent);
209+
if (tempFilePath == null) {
210+
LOGGER.warn("Failed to create temporary file for ASCA scan.");
211+
return null;
212+
}
213+
try {
214+
LOGGER.info(Strings.join("Starting ASCA scan on file: ", virtualFile.getPath()));
215+
ScanResult scanResult = scanAscaFile(tempFilePath, ascLatestVersion, agent, DevAssistUtils.getIgnoreFilePath(project));
216+
// Update line numbers for ignored ASCA issues if any exist
217+
updateIgnoredFileDataOnLatestResult(tempFilePath, project, uri, agent, ascLatestVersion);
218+
handleScanResult(file, scanResult);
219+
return scanResult;
220+
} catch (Exception e) {
221+
LOGGER.warn("Error during ASCA scan:", e);
222+
return null;
223+
} finally {
224+
deleteFile(tempFilePath);
225+
}
222226
}
223227
}
224228

@@ -460,7 +464,7 @@ private void updateIgnoredFileDataOnLatestResult(String tempFilePath, Project pr
460464
LOGGER.debug("ASCA: Performing full scan to update line numbers for ignored issues");
461465
ScanResult fullScanResult = scanAscaFile(tempFilePath, ascLatestVersion, agent, "");
462466
if (fullScanResult.getScanDetails() != null && !fullScanResult.getScanDetails().isEmpty()) {
463-
AscaScanResultAdaptor fullScanResultAdaptor = new AscaScanResultAdaptor(fullScanResult, filePath, project);
467+
AscaScanResultAdaptor fullScanResultAdaptor = new AscaScanResultAdaptor(fullScanResult, filePath);
464468
ignoreManager.updateLineNumbersForIgnoredEntriesByProblematicLine(fullScanResultAdaptor, filePath);
465469
}else{
466470
ignoreManager.removeIgnoreEntriesForFileIfEmpty(filePath);

devassist-lib/src/test/java/com/checkmarx/intellij/devassist/test/scanners/asca/AscaScanResultAdaptorTest.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.checkmarx.intellij.devassist.scanners.asca.AscaScanResultAdaptor;
77
import com.checkmarx.intellij.devassist.utils.DevAssistConstants;
88
import com.checkmarx.intellij.devassist.utils.ScanEngine;
9-
import com.intellij.openapi.project.Project;
109
import org.junit.jupiter.api.DisplayName;
1110
import org.junit.jupiter.api.Test;
1211

@@ -20,8 +19,6 @@
2019

2120
class AscaScanResultAdaptorTest {
2221

23-
private final Project project = mock(Project.class);
24-
2522
private ScanResult mockResult(List<ScanDetail> details) {
2623
ScanResult result = mock(ScanResult.class);
2724
when(result.getScanDetails()).thenReturn(details);
@@ -47,18 +44,18 @@ private ScanDetail mockDetail(int line,
4744
@DisplayName("getResults returns original ScanResult reference")
4845
void getResultsReturnsOriginal() {
4946
ScanResult scanResult = mockResult(Collections.emptyList());
50-
AscaScanResultAdaptor adaptor = new AscaScanResultAdaptor(scanResult, "/repo/Main.java", project);
47+
AscaScanResultAdaptor adaptor = new AscaScanResultAdaptor(scanResult, "/repo/Main.java");
5148
assertSame(scanResult, adaptor.getResults());
5249
}
5350

5451
@Test
5552
@DisplayName("getIssues returns empty list when results or details are null")
5653
void getIssuesHandlesNullInputs() {
57-
AscaScanResultAdaptor nullAdaptor = new AscaScanResultAdaptor(null, "/repo/Main.java", project);
54+
AscaScanResultAdaptor nullAdaptor = new AscaScanResultAdaptor(null, "/repo/Main.java");
5855
assertTrue(nullAdaptor.getIssues().isEmpty());
5956

6057
AscaScanResultAdaptor emptyAdaptor =
61-
new AscaScanResultAdaptor(mockResult(null), "/repo/Main.java", project);
58+
new AscaScanResultAdaptor(mockResult(null), "/repo/Main.java");
6259
assertTrue(emptyAdaptor.getIssues().isEmpty());
6360
}
6461

@@ -74,7 +71,7 @@ void getIssuesConvertsSingleDetail() {
7471
);
7572

7673
AscaScanResultAdaptor adaptor =
77-
new AscaScanResultAdaptor(mockResult(List.of(detail)), "/repo/Main.java", project);
74+
new AscaScanResultAdaptor(mockResult(List.of(detail)), "/repo/Main.java");
7875

7976
List<ScanIssue> issues = adaptor.getIssues();
8077
assertEquals(1, issues.size());
@@ -99,7 +96,7 @@ void getIssuesGroupsMultipleDetailsPerLine() {
9996
ScanDetail low = mockDetail(20, "Low", "LowRule", "low-desc", "low-fix");
10097

10198
AscaScanResultAdaptor adaptor =
102-
new AscaScanResultAdaptor(mockResult(Arrays.asList(low, critical)), "/repo/Main.java", project);
99+
new AscaScanResultAdaptor(mockResult(Arrays.asList(low, critical)), "/repo/Main.java");
103100

104101
List<ScanIssue> issues = adaptor.getIssues();
105102
assertEquals(1, issues.size(), "Same line entries should be grouped");
@@ -122,7 +119,7 @@ void getIssuesGroupsMultipleDetailsPerLine() {
122119
void getIssuesSkipsNullEntries() {
123120
ScanDetail valid = mockDetail(5, "Medium", "ValidRule", "desc", "remedy");
124121
AscaScanResultAdaptor adaptor =
125-
new AscaScanResultAdaptor(mockResult(Arrays.asList(null, valid)), "/repo/Main.java", project);
122+
new AscaScanResultAdaptor(mockResult(Arrays.asList(null, valid)), "/repo/Main.java");
126123

127124
List<ScanIssue> issues = adaptor.getIssues();
128125
assertEquals(1, issues.size());
@@ -134,7 +131,7 @@ void getIssuesSkipsNullEntries() {
134131
void mapSeverityTreatsInfoAsLow() {
135132
ScanDetail infoDetail = mockDetail(7, "info", "InfoRule", "desc", "remedy");
136133
AscaScanResultAdaptor adaptor =
137-
new AscaScanResultAdaptor(mockResult(List.of(infoDetail)), "/repo/Main.java", project);
134+
new AscaScanResultAdaptor(mockResult(List.of(infoDetail)), "/repo/Main.java");
138135

139136
ScanIssue issue = adaptor.getIssues().get(0);
140137
assertEquals("Low", issue.getSeverity());

plugin-checkmarx-ast/src/main/java/com/checkmarx/intellij/ast/window/CxToolWindowPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ public CxToolWindowPanel(@NotNull Project project) {
133133
// Establish message bus connection before subscribing
134134
ApplicationManager.getApplication().getMessageBus()
135135
.connect(this)
136-
.subscribe(SettingsListener.SETTINGS_APPLIED, r::run);
136+
.subscribe(SettingsListener.SETTINGS_APPLIED, (SettingsListener) r::run);
137137
ApplicationManager.getApplication().getMessageBus().connect(this)
138-
.subscribe(FilterBaseAction.FILTER_CHANGED, this::changeFilter);
138+
.subscribe(FilterBaseAction.FILTER_CHANGED, (FilterBaseAction.FilterChanged) this::changeFilter);
139139

140140
r.run();
141141
}

0 commit comments

Comments
 (0)