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

io.codemodder.remediation.ssrf.SSRFRemediator Maven / Gradle / Ivy

There is a newer version: 0.98.6
Show newest version
package io.codemodder.remediation.ssrf;

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

public final class SSRFRemediator implements Remediator {

  private final SearcherStrategyRemediator searchStrategyRemediator;

  public SSRFRemediator() {
    this.searchStrategyRemediator =
        new SearcherStrategyRemediator.Builder()
            .withSearcherStrategyPair(
                new FixCandidateSearcher.Builder()
                    .withMatcher(
                        n ->
                            Optional.>empty()
                                .or(
                                    () ->
                                        Optional.of(n)
                                            .map(
                                                m ->
                                                    m instanceof ObjectCreationExpr
                                                        ? (ObjectCreationExpr) m
                                                        : null)
                                            .filter(m -> "URL".equals(m.getTypeAsString()))
                                            .filter(m -> m.getArguments().isNonEmpty())
                                            .map(Either::right))
                                .or(
                                    () ->
                                        Optional.of(n)
                                            .map(
                                                m ->
                                                    m instanceof MethodCallExpr
                                                        ? (MethodCallExpr) m
                                                        : null)
                                            .filter(Node::hasScope)
                                            .filter(m -> "exchange".equals(m.getNameAsString()))
                                            .map(Either::left))
                                .isPresent())
                    .build(),
                new SSRFFixStrategy())
            .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 - 2025 Weber Informatics LLC | Privacy Policy