Skip to content

Commit

Permalink
add regex injection fixer with codeql for first example
Browse files Browse the repository at this point in the history
  • Loading branch information
nahsra committed Nov 14, 2024
1 parent 3ee3ad9 commit a117920
Show file tree
Hide file tree
Showing 13 changed files with 103,589 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static List<Class<? extends CodeChanger>> asList() {
CodeQLMavenSecureURLCodemod.class,
CodeQLOutputResourceLeakCodemod.class,
CodeQLPredictableSeedCodemod.class,
CodeQLRegexInjectionCodemod.class,
CodeQLSQLInjectionCodemod.class,
CodeQLSSRFCodemod.class,
CodeQLStackTraceExposureCodemod.class,
Expand Down
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()));
}
}
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 {}
}
Loading

0 comments on commit a117920

Please sign in to comment.