-
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.
add regex injection fixer with codeql for first example
- Loading branch information
Showing
13 changed files
with
103,589 additions
and
1 deletion.
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
54 changes: 54 additions & 0 deletions
54
core-codemods/src/main/java/io/codemodder/codemods/codeql/CodeQLRegexInjectionCodemod.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,54 @@ | ||
package io.codemodder.codemods.codeql; | ||
|
||
import com.contrastsecurity.sarif.Result; | ||
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.Remediator; | ||
import io.codemodder.remediation.regexinjection.RegexInjectionRemediator; | ||
import java.util.Optional; | ||
import javax.inject.Inject; | ||
|
||
/** A codemod for automatically fixing Regex Injections from CodeQL. */ | ||
@Codemod( | ||
id = "codeql:java/regex-injection", | ||
reviewGuidance = ReviewGuidance.MERGE_AFTER_CURSORY_REVIEW, | ||
importance = Importance.HIGH, | ||
executionPriority = CodemodExecutionPriority.HIGH) | ||
public final class CodeQLRegexInjectionCodemod extends CodeQLRemediationCodemod { | ||
|
||
private final Remediator<Result> remediator; | ||
|
||
@Inject | ||
public CodeQLRegexInjectionCodemod( | ||
@ProvidedCodeQLScan(ruleId = "java/regex-injection") final RuleSarif sarif) { | ||
super(GenericRemediationMetadata.REGEX_INJECTION.reporter(), sarif); | ||
this.remediator = new RegexInjectionRemediator<>(); | ||
} | ||
|
||
@Override | ||
public DetectorRule detectorRule() { | ||
return new DetectorRule( | ||
"regex-injectiom", | ||
"Regular expression injection", | ||
"https://codeql.github.com/codeql-query-help/java/java-regex-injection/"); | ||
} | ||
|
||
@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 -> Optional.of(r.getLocations().get(0).getPhysicalLocation().getRegion().getEndLine()), | ||
r -> | ||
Optional.of( | ||
r.getLocations().get(0).getPhysicalLocation().getRegion().getStartColumn())); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...codemods/src/test/java/io/codemodder/codemods/codeql/CodeQLRegexInjectionCodemodTest.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,28 @@ | ||
package io.codemodder.codemods.codeql; | ||
|
||
import io.codemodder.testutils.CodemodTestMixin; | ||
import io.codemodder.testutils.Metadata; | ||
import org.junit.jupiter.api.Nested; | ||
|
||
final class CodeQLRegexInjectionCodemodTest { | ||
|
||
@Nested | ||
@Metadata( | ||
codemodType = CodeQLRegexInjectionCodemod.class, | ||
testResourceDir = "codeql-regex-injection/bannedwordlist", | ||
renameTestFile = "app/src/main/java/org/apache/roller/weblogger/util/Bannedwordslist.java", | ||
expectingFixesAtLines = 438, | ||
doRetransformTest = false, | ||
dependencies = {}) | ||
class BannedWordlistTest implements CodemodTestMixin {} | ||
|
||
@Nested | ||
@Metadata( | ||
codemodType = CodeQLRegexInjectionCodemod.class, | ||
testResourceDir = "codeql-regex-injection/regexutil", | ||
renameTestFile = "app/src/main/java/org/apache/roller/util/RegexUtil.java", | ||
expectingFixesAtLines = {71, 66, 49}, | ||
doRetransformTest = false, | ||
dependencies = {}) | ||
class RegexUtilTest implements CodemodTestMixin {} | ||
} |
Oops, something went wrong.