All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.codemodder.codemods.RandomizeSeedCodemod Maven / Gradle / Ivy

There is a newer version: 0.97.3
Show newest version
package io.codemodder.codemods;

import com.contrastsecurity.sarif.Result;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import io.codemodder.*;
import io.codemodder.javaparser.ChangesResult;
import io.codemodder.providers.sarif.semgrep.SemgrepScan;
import javax.inject.Inject;

/** Turns hardcoded seeds for PRNGs to be more random. */
@Codemod(
    id = "pixee:java/make-prng-seed-unpredictable",
    importance = Importance.LOW,
    reviewGuidance = ReviewGuidance.MERGE_WITHOUT_REVIEW)
public final class RandomizeSeedCodemod extends SarifPluginJavaParserChanger {

  @Inject
  public RandomizeSeedCodemod(
      @SemgrepScan(ruleId = "make-prng-seed-unpredictable") final RuleSarif sarif) {
    super(sarif, MethodCallExpr.class);
  }

  @Override
  public ChangesResult onResultFound(
      final CodemodInvocationContext context,
      final CompilationUnit cu,
      final MethodCallExpr setSeedCall,
      final Result result) {
    MethodCallExpr safeExpression =
        new MethodCallExpr(new NameExpr(System.class.getSimpleName()), "currentTimeMillis");
    NodeList arguments = setSeedCall.getArguments();
    arguments.set(0, safeExpression);
    return ChangesResult.changesApplied;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy