Skip to content

Commit

Permalink
Add several Sonar hotspot mappings (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
nahsra authored Dec 18, 2024
1 parent 394739f commit b841ee3
Show file tree
Hide file tree
Showing 122 changed files with 1,183 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
python-version: '3.11'

- name: Install Semgrep
run: python3 -m pip install semgrep
run: python3 -m pip install semgrep==1.84.1

- name: Run Check task
uses: gradle/gradle-build-action@842c587ad8aa4c68eeba24c396e15af4c2e9f30a # v2.9.0
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
python-version: '3.11'

- name: Install Semgrep
run: python3 -m pip install semgrep
run: python3 -m pip install semgrep==1.84.1

- uses: actions/setup-java@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Follow these instructions if you intend to modify and build this project from so
[here](https://semgrep.dev/docs/getting-started/#installing-and-running-semgrep-locally)
for instructions. It can usually be done via `pip`:
```shell
pip install semgrep
pip install semgrep==1.84.1
```

If your python library paths contain your home directory as a root folder (i.e.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ protected void verifyNoFailedFiles(final CodeTFReport report) {
.map(CodeTFResult::getFailedFiles)
.flatMap(Collection::stream)
.toList();
assertThat(failedFiles.size(), is(0));
if (!failedFiles.isEmpty()) {
System.out.println("Failed files during scan:");
failedFiles.forEach(System.err::println);
}
int size = failedFiles.size();
assertThat(size, is(0));
}

protected void verifyStandardCodemodResults(final List<CodeTFChangesetEntry> fileChanges) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.codemodder.codemods.semgrep.SemgrepServletResponseWriterXSSCodemod;
import io.codemodder.codemods.semgrep.SemgrepWeakRandomCodemod;
import io.codemodder.codemods.semgrep.SemgrepXXECodemod;
import io.codemodder.codemods.sonar.*;
import java.util.List;

/**
Expand Down Expand Up @@ -89,12 +90,15 @@ public static List<Class<? extends CodeChanger>> asList() {
SemgrepOverlyPermissiveFilePermissionsCodemod.class,
SimplifyRestControllerAnnotationsCodemod.class,
SubstituteReplaceAllCodemod.class,
SonarCookieMissingSecureFlagCodemod.class,
SonarJNDIInjectionCodemod.class,
SonarObjectDeserializationCodemod.class,
SonarRemoveUnthrowableExceptionCodemod.class,
SonarSQLInjectionCodemod.class,
SonarSSRFCodemod.class,
SonarUnsafeReflectionRemediationCodemod.class,
SonarWeakHashingAlgorithmCodemod.class,
SonarWeakRandomCodemod.class,
SonarXXECodemod.class,
SQLParameterizerCodemod.class,
SSRFCodemod.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import java.util.Set;
import java.util.regex.Matcher;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.Range;
import com.github.javaparser.ast.CompilationUnit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.StringLiteralExpr;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.EnumDeclaration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.comments.Comment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.EnclosedExpr;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import static io.codemodder.ast.ASTTransforms.addImportIfMissing;
import static io.codemodder.ast.ASTTransforms.removeImportIfUnused;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import io.codemodder.*;
import io.codemodder.codetf.DetectorRule;
import io.codemodder.providers.sonar.ProvidedSonarScan;
import io.codemodder.providers.sonar.RuleHotspot;
import io.codemodder.providers.sonar.SonarRemediatingJavaParserChanger;
import io.codemodder.remediation.GenericRemediationMetadata;
import io.codemodder.remediation.Remediator;
import io.codemodder.remediation.missingsecureflag.MissingSecureFlagRemediator;
import io.codemodder.sonar.model.Hotspot;
import io.codemodder.sonar.model.SonarFinding;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.inject.Inject;

@Codemod(
id = "sonar:java/cookie-missing-secure-flag-2092",
reviewGuidance = ReviewGuidance.MERGE_AFTER_CURSORY_REVIEW,
importance = Importance.HIGH,
executionPriority = CodemodExecutionPriority.HIGH)
public final class SonarCookieMissingSecureFlagCodemod extends SonarRemediatingJavaParserChanger {

private final Remediator<Hotspot> remediationStrategy;
private final RuleHotspot issues;

@Inject
public SonarCookieMissingSecureFlagCodemod(
@ProvidedSonarScan(ruleId = "java:S2092") final RuleHotspot hotspots) {
super(GenericRemediationMetadata.MISSING_SECURE_FLAG.reporter(), hotspots);
this.issues = Objects.requireNonNull(hotspots);
this.remediationStrategy = new MissingSecureFlagRemediator<>();
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S2092",
"Make sure creating this cookie without the \"secure\" flag is safe here.",
"https://rules.sonarsource.com/java/type/Security%20Hotspot/RSPEC-2092/");
}

@Override
public CodemodFileScanningResult visit(
final CodemodInvocationContext context, final CompilationUnit cu) {
List<Hotspot> issuesForFile = issues.getResultsByPath(context.path());
return remediationStrategy.remediateAll(
cu,
context.path().toString(),
detectorRule(),
issuesForFile,
SonarFinding::getKey,
i -> i.getTextRange() != null ? i.getTextRange().getStartLine() : i.getLine(),
i ->
i.getTextRange() != null
? Optional.of(i.getTextRange().getEndLine())
: Optional.empty(),
i -> Optional.empty());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import io.codemodder.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import io.codemodder.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.Expression;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import io.codemodder.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.codemodder.codemods;
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import io.codemodder.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.codemodder.codemods.sonar;

import com.github.javaparser.ast.CompilationUnit;
import io.codemodder.*;
import io.codemodder.codetf.DetectorRule;
import io.codemodder.providers.sonar.ProvidedSonarScan;
import io.codemodder.providers.sonar.RuleHotspot;
import io.codemodder.providers.sonar.SonarRemediatingJavaParserChanger;
import io.codemodder.remediation.GenericRemediationMetadata;
import io.codemodder.remediation.Remediator;
import io.codemodder.remediation.weakcrypto.WeakCryptoAlgorithmRemediator;
import io.codemodder.sonar.model.Hotspot;
import io.codemodder.sonar.model.SonarFinding;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.inject.Inject;

@Codemod(
id = "sonar:java/weak-hash-4790",
reviewGuidance = ReviewGuidance.MERGE_AFTER_CURSORY_REVIEW,
importance = Importance.HIGH,
executionPriority = CodemodExecutionPriority.HIGH)
public final class SonarWeakHashingAlgorithmCodemod extends SonarRemediatingJavaParserChanger {

private final Remediator<Hotspot> remediationStrategy;
private final RuleHotspot issues;

@Inject
public SonarWeakHashingAlgorithmCodemod(
@ProvidedSonarScan(ruleId = "java:S4790") final RuleHotspot hotspots) {
super(GenericRemediationMetadata.WEAK_CRYPTO_ALGORITHM.reporter(), hotspots);
this.issues = Objects.requireNonNull(hotspots);
this.remediationStrategy = new WeakCryptoAlgorithmRemediator<>();
}

@Override
public DetectorRule detectorRule() {
return new DetectorRule(
"java:S4790",
"Using weak hashing algorithms is security-sensitive",
"https://rules.sonarsource.com/java/type/Security%20Hotspot/RSPEC-4790/");
}

@Override
public CodemodFileScanningResult visit(
final CodemodInvocationContext context, final CompilationUnit cu) {
List<Hotspot> issuesForFile = issues.getResultsByPath(context.path());
return remediationStrategy.remediateAll(
cu,
context.path().toString(),
detectorRule(),
issuesForFile,
SonarFinding::getKey,
i -> i.getTextRange() != null ? i.getTextRange().getStartLine() : i.getLine(),
i ->
i.getTextRange() != null
? Optional.of(i.getTextRange().getEndLine())
: Optional.empty(),
i -> Optional.empty());
}
}
Loading

0 comments on commit b841ee3

Please sign in to comment.