-
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.
/close #work Could not find any examples of sonar findings for SSRF vulnerabilities using `Url` or `HTTPUrlConnection`. This codemod only covers vulnerabilities found using the `RestTemplate` class from spring.
- Loading branch information
1 parent
9196cb2
commit 6fc81f2
Showing
8 changed files
with
593 additions
and
26 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
58 changes: 58 additions & 0 deletions
58
core-codemods/src/main/java/io/codemodder/codemods/SonarSSRFCodemod.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,58 @@ | ||
package io.codemodder.codemods; | ||
|
||
import com.github.javaparser.ast.CompilationUnit; | ||
import io.codemodder.*; | ||
import io.codemodder.codemods.remediators.ssrf.SSRFRemediator; | ||
import io.codemodder.codetf.DetectorRule; | ||
import io.codemodder.providers.sonar.ProvidedSonarScan; | ||
import io.codemodder.providers.sonar.RuleIssue; | ||
import io.codemodder.providers.sonar.SonarRemediatingJavaParserChanger; | ||
import io.codemodder.remediation.GenericRemediationMetadata; | ||
import io.codemodder.sonar.model.Issue; | ||
import io.codemodder.sonar.model.SonarFinding; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import javax.inject.Inject; | ||
|
||
/** Fixes SSRF issues found by sonar rule javasecurity:S5144. */ | ||
@Codemod( | ||
id = "sonar:java/ssrf-s5144", | ||
reviewGuidance = ReviewGuidance.MERGE_WITHOUT_REVIEW, | ||
executionPriority = CodemodExecutionPriority.HIGH, | ||
importance = Importance.HIGH) | ||
public final class SonarSSRFCodemod extends SonarRemediatingJavaParserChanger { | ||
|
||
private final SSRFRemediator remediator; | ||
private final RuleIssue issues; | ||
|
||
@Inject | ||
public SonarSSRFCodemod( | ||
@ProvidedSonarScan(ruleId = "javasecurity:S5144") final RuleIssue issues) { | ||
super(GenericRemediationMetadata.SSRF.reporter(), issues); | ||
this.issues = Objects.requireNonNull(issues); | ||
this.remediator = SSRFRemediator.DEFAULT; | ||
} | ||
|
||
@Override | ||
public DetectorRule detectorRule() { | ||
return new DetectorRule( | ||
"javasecurity:S5144", | ||
"Server-side requests should not be vulnerable to forging attacks", | ||
"https://rules.sonarsource.com/java/RSPEC-5144/"); | ||
} | ||
|
||
@Override | ||
public CodemodFileScanningResult visit( | ||
final CodemodInvocationContext context, final CompilationUnit cu) { | ||
List<Issue> issuesForFile = issues.getResultsByPath(context.path()); | ||
return remediator.remediateAll( | ||
cu, | ||
context.path().toString(), | ||
detectorRule(), | ||
issuesForFile, | ||
SonarFinding::getKey, | ||
i -> i.getTextRange() != null ? i.getTextRange().getStartLine() : i.getLine(), | ||
i -> i.getTextRange() != null ? i.getTextRange().getEndLine() : null, | ||
i -> i.getTextRange() != null ? i.getTextRange().getStartOffset() : null); | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
core-codemods/src/test/java/io/codemodder/codemods/SonarSSRFCodemodTest.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,19 @@ | ||
package io.codemodder.codemods; | ||
|
||
import io.codemodder.DependencyGAV; | ||
import io.codemodder.testutils.CodemodTestMixin; | ||
import io.codemodder.testutils.Metadata; | ||
import org.junit.jupiter.api.Nested; | ||
|
||
final class SonarSSRFCodemodTest { | ||
|
||
@Nested | ||
@Metadata( | ||
codemodType = SonarSSRFCodemod.class, | ||
testResourceDir = "sonar-ssrf-s5144/resttemplate", | ||
renameTestFile = | ||
"src/main/java/org/owasp/webgoat/lessons/passwordreset/ResetLinkAssignmentForgotPassword.java", | ||
expectingFixesAtLines = {104}, | ||
dependencies = DependencyGAV.JAVA_SECURITY_TOOLKIT_GAV) | ||
class RestTemplateTest implements CodemodTestMixin {} | ||
} |
Oops, something went wrong.