Skip to content

Commit a9d8d95

Browse files
authored
Expose Contrast results to providers (#357)
Adds Contrast results so a provider can pick it up and act on it. Also adds some utility methods that may be useful for codemod development.
1 parent 380a8a9 commit a9d8d95

File tree

20 files changed

+60
-23
lines changed

20 files changed

+60
-23
lines changed

core-codemods/src/main/java/io/codemodder/codemods/DefectDojoSqlInjectionCodemod.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
import java.util.Objects;
1616
import javax.inject.Inject;
1717

18-
/** This codemod knows how to translate */
18+
/**
19+
* This codemod knows how to fix SQL injection findings that come through DefectDojo for supported
20+
* vendors.
21+
*/
1922
@Codemod(
2023
id = "defectdojo:java/sql-injection",
2124
reviewGuidance = ReviewGuidance.MERGE_AFTER_REVIEW,

core-codemods/src/main/java/io/codemodder/codemods/SQLParameterizer.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ static boolean isParameterizationCandidate(final MethodCallExpr methodCallExpr)
7676

7777
/** Returns true if this is a fixable JDBC method name. */
7878
public static boolean isSupportedJdbcMethodCall(final MethodCallExpr methodCall) {
79-
return methodCall.getNameAsString().equals("executeQuery")
80-
|| methodCall.getNameAsString().equals("execute")
81-
|| methodCall.getNameAsString().equals("executeLargeUpdate")
82-
|| methodCall.getNameAsString().equals("executeUpdate");
79+
return fixableJdbcMethodNames.contains(methodCall.getNameAsString());
8380
}
8481

82+
/** Returns a set of fixable JDBC method names. */
83+
public static Set<String> fixableJdbcMethodNames() {
84+
return fixableJdbcMethodNames;
85+
}
86+
87+
private static final Set<String> fixableJdbcMethodNames =
88+
Set.of("executeQuery", "execute", "executeLargeUpdate", "executeUpdate");
89+
8590
/**
8691
* Tries to find the source of an expression if it can be uniquely defined, otherwise, returns
8792
* self.

core-codemods/src/test/java/io/codemodder/codemods/AddMissingI18nCodemodTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ private CodemodLoader createLoader(final Class<? extends CodeChanger> codemodTyp
255255
Map.of(),
256256
List.of(),
257257
null,
258+
null,
258259
null);
259260
}
260261

core-codemods/src/test/java/io/codemodder/codemods/JSPScriptletXSSCodemodTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void it_fixes_jsp(
5151
Map.of(),
5252
List.of(),
5353
null,
54+
null,
5455
null);
5556
CodemodIdPair codemod = codemodInvoker.getCodemods().get(0);
5657
CodemodExecutor executor =

core-codemods/src/test/java/io/codemodder/codemods/VerbTamperingCodemodTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ void it_removes_verb_tampering(
6161
Map.of(),
6262
List.of(),
6363
null,
64+
null,
6465
null);
6566

6667
CodemodExecutor executor =

framework/codemodder-base/src/main/java/io/codemodder/CLI.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ final class CLI implements Callable<Integer> {
133133
"a path to a file containing the result of a call to the Sonar Web API Hotspots endpoint")
134134
private Path sonarHotspotsJsonFilePath;
135135

136+
@CommandLine.Option(
137+
names = {"--contrast-vulnerabilities-xml"},
138+
description =
139+
"a path to a file containing the result of a call to the Contrast Assess XML export API")
140+
private Path contrastVulnerabilitiesXmlFilePath;
141+
136142
@CommandLine.Option(
137143
names = {"--list"},
138144
description = "print codemod(s) metadata, then exit",
@@ -388,7 +394,8 @@ public Integer call() throws IOException {
388394
pathSarifMap,
389395
codemodParameters,
390396
sonarIssuesJsonFilePath,
391-
defectDojoFindingsJsonFilePath);
397+
defectDojoFindingsJsonFilePath,
398+
contrastVulnerabilitiesXmlFilePath);
392399
List<CodemodIdPair> codemods = loader.getCodemods();
393400

394401
log.debug("sarif files: {}", sarifFiles.size());

framework/codemodder-base/src/main/java/io/codemodder/CodemodLoader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public CodemodLoader(
2929
final Map<String, List<RuleSarif>> ruleSarifByTool,
3030
final List<ParameterArgument> codemodParameters,
3131
final Path sonarIssuesJsonFile,
32-
final Path defectDojoFindingsJsonFile) {
32+
final Path defectDojoFindingsJsonFile,
33+
final Path contrastVulnerabilitiesXmlFilePath) {
3334

3435
// get all the providers ready for dependency injection & codemod instantiation
3536
final List<CodemodProvider> providers =
@@ -100,7 +101,8 @@ public CodemodLoader(
100101
orderedCodemodTypes,
101102
allWantedSarifs,
102103
sonarIssuesJsonFile,
103-
defectDojoFindingsJsonFile);
104+
defectDojoFindingsJsonFile,
105+
contrastVulnerabilitiesXmlFilePath);
104106
allModules.addAll(modules);
105107
}
106108

framework/codemodder-base/src/main/java/io/codemodder/CodemodProvider.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ public interface CodemodProvider {
1515
* Return a set of Guice modules that allow dependency injection
1616
*
1717
* @param repository the repository root
18-
* @param codemodTypes the codemod types that are being run
19-
* @param sarifs the SARIF output of tools that are being run
2018
* @param includedFiles the files that qualify for inclusion based on the patterns provided
2119
* @param pathIncludes the path includes provided to the CLI (which could inform the providers on
2220
* their own analysis)
2321
* @param pathExcludes the path excludes provided to the CLI (which could inform the providers on
2422
* their own analysis)
23+
* @param codemodTypes the codemod types that are being run
24+
* @param sarifs the SARIF output of tools that are being run
2525
* @param sonarIssuesJsonPath the path to a Sonar issues JSON file retrieved from their web API --
2626
* may be null
27+
* @param contrastFindingsJsonPath the path to a Contrast findings JSON file retrieved from their
28+
* web API -- may be null
2729
* @return a set of modules that perform dependency injection
2830
*/
2931
Set<AbstractModule> getModules(
@@ -34,13 +36,14 @@ Set<AbstractModule> getModules(
3436
List<Class<? extends CodeChanger>> codemodTypes,
3537
List<RuleSarif> sarifs,
3638
Path sonarIssuesJsonPath,
37-
Path defectDojoFindingsJsonPath);
39+
Path defectDojoFindingsJsonPath,
40+
Path contrastFindingsJsonPath);
3841

3942
/**
4043
* Tools this provider is interested in processing the SARIF output of. Codemodder CLI will look
4144
* for the SARIF outputted by tools in this list in the repository root and then provide the
42-
* results to {@link #getModules(Path, List, List, List, List, List, Path, Path)} as a {@link
43-
* List} of {@link RuleSarif}s.
45+
* results to {@link #getModules(Path, List, List, List, List, List, Path, Path, Path)} as a
46+
* {@link List} of {@link RuleSarif}s.
4447
*
4548
* <p>By default, this returns an empty list.
4649
*

framework/codemodder-base/src/test/java/io/codemodder/CodemodLoaderTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ private CodemodLoader createLoader(final Class<? extends CodeChanger> codemodTyp
410410
Map.of(),
411411
List.of(),
412412
null,
413+
null,
413414
null);
414415
}
415416

@@ -425,6 +426,7 @@ private CodemodLoader createLoader(
425426
Map.of(),
426427
List.of(),
427428
null,
429+
null,
428430
null);
429431
}
430432

@@ -443,6 +445,7 @@ private CodemodLoader createLoader(
443445
Map.of(),
444446
params,
445447
null,
448+
null,
446449
null);
447450
}
448451
}

framework/codemodder-testutils/src/main/java/io/codemodder/testutils/CodemodTestMixin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ private void verifyCodemod(
136136
map,
137137
List.of(),
138138
Files.exists(sonarJson) ? sonarJson : null,
139-
Files.exists(defectDojo) ? defectDojo : null);
139+
Files.exists(defectDojo) ? defectDojo : null,
140+
null);
140141

141142
List<CodemodIdPair> codemods = loader.getCodemods();
142143
assertThat(codemods.size(), equalTo(1));
@@ -231,6 +232,7 @@ private void verifyCodemod(
231232
map,
232233
List.of(),
233234
null,
235+
null,
234236
null);
235237
CodemodIdPair codemod2 = loader2.getCodemods().get(0);
236238
CodemodExecutor executor2 =

0 commit comments

Comments
 (0)