io.codemodder.codemods.SecureRandomCodemod Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-codemods Show documentation
Show all versions of core-codemods Show documentation
Codemods for fixing common errors across many Java projects
package io.codemodder.codemods;
import static io.codemodder.ast.ASTTransforms.addImportIfMissing;
import com.contrastsecurity.sarif.Result;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import io.codemodder.*;
import io.codemodder.javaparser.ChangesResult;
import io.codemodder.providers.sarif.semgrep.SemgrepScan;
import java.security.SecureRandom;
import javax.inject.Inject;
/** Turns {@link java.util.Random} into {@link java.security.SecureRandom}. */
@Codemod(
id = "pixee:java/secure-random",
reviewGuidance = ReviewGuidance.MERGE_WITHOUT_REVIEW,
importance = Importance.MEDIUM,
executionPriority = CodemodExecutionPriority.LOW)
public final class SecureRandomCodemod extends SarifPluginJavaParserChanger {
private static final String DETECTION_RULE =
"""
rules:
- id: secure-random
pattern: new Random()
""";
@Inject
public SecureRandomCodemod(@SemgrepScan(yaml = DETECTION_RULE) final RuleSarif sarif) {
super(sarif, ObjectCreationExpr.class);
}
@Override
public ChangesResult onResultFound(
final CodemodInvocationContext context,
final CompilationUnit cu,
final ObjectCreationExpr objectCreationExpr,
final Result result) {
objectCreationExpr.setType("SecureRandom");
addImportIfMissing(cu, SecureRandom.class.getName());
return ChangesResult.changesApplied;
}
}