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

io.codemodder.remediation.jndiinjection.JNDIInjectionRemediator Maven / Gradle / Ivy

package io.codemodder.remediation.jndiinjection;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.NameExpr;
import io.codemodder.CodemodFileScanningResult;
import io.codemodder.codetf.DetectorRule;
import io.codemodder.remediation.*;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Function;

public class JNDIInjectionRemediator implements Remediator {

  private final SearcherStrategyRemediator searchStrategyRemediator;

  /** Remediator with the default strategy. */
  public JNDIInjectionRemediator() {
    this(new ReplaceLimitedLookupStrategy());
  }

  /**
   * A remediator with a chosen strategy
   *
   * @param strategy
   */
  public JNDIInjectionRemediator(final RemediationStrategy strategy) {
    this.searchStrategyRemediator =
        new SearcherStrategyRemediator.Builder()
            .withSearcherStrategyPair(
                new FixCandidateSearcher.Builder()
                    .withMatcher(
                        n ->
                            Optional.of(n)
                                .map(MethodOrConstructor::new)
                                .filter(mce -> mce.isMethodCallWithName("lookup"))
                                .filter(mce -> mce.asNode().hasScope())
                                .filter(mce -> mce.getArguments().size() == 1)
                                .filter(mce -> mce.getArguments().get(0) instanceof NameExpr)
                                .isPresent())
                    .build(),
                strategy)
            .build();
  }

  @Override
  public CodemodFileScanningResult remediateAll(
      CompilationUnit cu,
      String path,
      DetectorRule detectorRule,
      Collection findingsForPath,
      Function findingIdExtractor,
      Function findingStartLineExtractor,
      Function> findingEndLineExtractor,
      Function> findingColumnExtractor) {
    return searchStrategyRemediator.remediateAll(
        cu,
        path,
        detectorRule,
        findingsForPath,
        findingIdExtractor,
        findingStartLineExtractor,
        findingEndLineExtractor,
        findingColumnExtractor);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy