-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds several mappings for CodeQL rules, as well as adds a new integration test for a more recent version of CodeQL+WebGoat 2023.8.
- Loading branch information
Showing
55 changed files
with
13,326 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
core-codemods/src/intTest/java/io/codemodder/integration/WebGoat20238Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.codemodder.integration; | ||
|
||
import static org.hamcrest.CoreMatchers.*; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.codemodder.codemods.DefaultCodemods; | ||
import io.codemodder.codetf.CodeTFChangesetEntry; | ||
import io.codemodder.codetf.CodeTFReport; | ||
import io.codemodder.codetf.CodeTFResult; | ||
import java.io.FileReader; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class WebGoat20238Test extends GitRepositoryTest { | ||
|
||
WebGoat20238Test() { | ||
super("https://github.com/WebGoat/WebGoat", "main", "f9b810c5ee2d6731eb5e37172af20d276a7dfb98"); | ||
} | ||
|
||
@Test | ||
void it_remediates_webgoat_2023_8() throws Exception { | ||
|
||
DefaultCodemods.main( | ||
new String[] { | ||
"--output", | ||
outputFile.getPath(), | ||
"--sarif", | ||
"src/test/resources/webgoat_v2023.8_from_ghas_06_2024.sarif", | ||
"--dont-exit", | ||
"--path-include=" + testPathIncludes, | ||
"--path-exclude=" + testPathExcludes, | ||
repoDir.getPath() | ||
}); | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
var report = objectMapper.readValue(new FileReader(outputFile), CodeTFReport.class); | ||
|
||
verifyNoFailedFiles(report); | ||
|
||
List<CodeTFChangesetEntry> fileChanges = | ||
report.getResults().stream() | ||
.map(CodeTFResult::getChangeset) | ||
.flatMap(Collection::stream) | ||
.toList(); | ||
|
||
assertThat(fileChanges.size(), is(50)); | ||
|
||
verifyStandardCodemodResults(fileChanges); | ||
|
||
// count the changes associated with missing-jwt-signature-check from codeql | ||
List<CodeTFResult> jwtResults = | ||
report.getResults().stream() | ||
.filter(result -> "codeql:java/missing-jwt-signature-check".equals(result.getCodemod())) | ||
.toList(); | ||
assertThat(jwtResults.size(), equalTo(1)); | ||
|
||
// this file is also only changed by including the codeql results | ||
CodeTFChangesetEntry jwtChange = | ||
fileChanges.stream() | ||
.filter(change -> change.getPath().endsWith("JWTRefreshEndpoint.java")) | ||
.findFirst() | ||
.orElseThrow(); | ||
|
||
assertThat(jwtChange.getChanges().size(), equalTo(2)); | ||
assertThat(jwtChange.getChanges().get(0).getLineNumber(), equalTo(113)); | ||
assertThat(jwtChange.getChanges().get(1).getLineNumber(), equalTo(140)); | ||
|
||
verifyCodemodsHitWithChangesetCount(report, "codeql:java/insecure-randomness", 0); | ||
verifyCodemodsHitWithChangesetCount(report, "codeql:java/ssrf", 1); | ||
verifyCodemodsHitWithChangesetCount(report, "codeql:java/xxe", 1); | ||
verifyCodemodsHitWithChangesetCount(report, "codeql:java/sql-injection", 6); | ||
verifyCodemodsHitWithChangesetCount(report, "codeql:java/insecure-cookie", 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
.../java/io/codemodder/codemods/codeql/CodeQLDeserializationOfUserControlledDataCodemod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.codemodder.codemods.codeql; | ||
|
||
import com.github.javaparser.ast.CompilationUnit; | ||
import io.codemodder.*; | ||
import io.codemodder.codetf.DetectorRule; | ||
import io.codemodder.providers.sarif.codeql.ProvidedCodeQLScan; | ||
import io.codemodder.remediation.GenericRemediationMetadata; | ||
import io.codemodder.remediation.javadeserialization.JavaDeserializationRemediator; | ||
import javax.inject.Inject; | ||
|
||
/** A codemod for automatically fixing untrusted deserialization from CodeQL. */ | ||
@Codemod( | ||
id = "codeql:java/unsafe-deserialization", | ||
reviewGuidance = ReviewGuidance.MERGE_WITHOUT_REVIEW, | ||
importance = Importance.HIGH, | ||
executionPriority = CodemodExecutionPriority.HIGH) | ||
public final class CodeQLDeserializationOfUserControlledDataCodemod | ||
extends CodeQLRemediationCodemod { | ||
|
||
private final JavaDeserializationRemediator remediator; | ||
|
||
@Inject | ||
public CodeQLDeserializationOfUserControlledDataCodemod( | ||
@ProvidedCodeQLScan(ruleId = "java/unsafe-deserialization") final RuleSarif sarif) { | ||
super(GenericRemediationMetadata.DESERIALIZATION.reporter(), sarif); | ||
this.remediator = JavaDeserializationRemediator.DEFAULT; | ||
} | ||
|
||
@Override | ||
public DetectorRule detectorRule() { | ||
return new DetectorRule( | ||
"unsafe-deserialization", | ||
"Deserialization of user-controlled data", | ||
"https://codeql.github.com/codeql-query-help/java/java-unsafe-deserialization/"); | ||
} | ||
|
||
@Override | ||
public CodemodFileScanningResult visit( | ||
final CodemodInvocationContext context, final CompilationUnit cu) { | ||
return remediator.remediateAll( | ||
cu, | ||
context.path().toString(), | ||
detectorRule(), | ||
ruleSarif.getResultsByLocationPath(context.path()), | ||
SarifFindingKeyUtil::buildFindingId, | ||
r -> r.getLocations().get(0).getPhysicalLocation().getRegion().getStartLine(), | ||
r -> r.getLocations().get(0).getPhysicalLocation().getRegion().getEndLine(), | ||
r -> r.getLocations().get(0).getPhysicalLocation().getRegion().getStartColumn()); | ||
} | ||
} |
Oops, something went wrong.