|
| 1 | +/** Provides classes and predicates related to handling APIs from external libraries. */ |
| 2 | + |
| 3 | +private import java |
| 4 | +private import semmle.code.java.dataflow.DataFlow |
| 5 | +private import semmle.code.java.dataflow.DataFlow |
| 6 | +private import semmle.code.java.dataflow.ExternalFlow |
| 7 | +private import semmle.code.java.dataflow.FlowSources |
| 8 | +private import semmle.code.java.dataflow.FlowSummary |
| 9 | +private import semmle.code.java.dataflow.internal.DataFlowPrivate |
| 10 | +private import semmle.code.java.dataflow.TaintTracking |
| 11 | + |
| 12 | +/** |
| 13 | + * An external API from either the Java Standard Library or a 3rd party library. |
| 14 | + */ |
| 15 | +class ExternalAPI extends Callable { |
| 16 | + ExternalAPI() { not this.fromSource() } |
| 17 | + |
| 18 | + /** Holds if this API is not worth supporting */ |
| 19 | + predicate isUninteresting() { isTestLibrary() or isParameterlessConstructor() } |
| 20 | + |
| 21 | + /** Holds if this API is is a constructor without parameters */ |
| 22 | + predicate isParameterlessConstructor() { |
| 23 | + this instanceof Constructor and this.getNumberOfParameters() = 0 |
| 24 | + } |
| 25 | + |
| 26 | + /** Holds if this API is part of a common testing library or framework */ |
| 27 | + private predicate isTestLibrary() { getDeclaringType() instanceof TestLibrary } |
| 28 | + |
| 29 | + /** |
| 30 | + * Gets information about the external API in the form expected by the CSV modeling framework. |
| 31 | + */ |
| 32 | + string getApiName() { |
| 33 | + result = |
| 34 | + this.getDeclaringType().getPackage() + "." + this.getDeclaringType().getSourceDeclaration() + |
| 35 | + "#" + this.getName() + paramsString(this) |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Gets the jar file containing this API. Normalizes the Java Runtime to "rt.jar" despite the presence of modules. |
| 40 | + */ |
| 41 | + string jarContainer() { result = containerAsJar(this.getCompilationUnit().getParentContainer*()) } |
| 42 | + |
| 43 | + private string containerAsJar(Container container) { |
| 44 | + if container instanceof JarFile then result = container.getBaseName() else result = "rt.jar" |
| 45 | + } |
| 46 | + |
| 47 | + /** Gets a node that is an input to a call to this API. */ |
| 48 | + private DataFlow::Node getAnInput() { |
| 49 | + exists(Call call | call.getCallee().getSourceDeclaration() = this | |
| 50 | + result.asExpr().(Argument).getCall() = call or |
| 51 | + result.(ArgumentNode).getCall() = call |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + /** Gets a node that is an output from a call to this API. */ |
| 56 | + private DataFlow::Node getAnOutput() { |
| 57 | + exists(Call call | call.getCallee().getSourceDeclaration() = this | |
| 58 | + result.asExpr() = call or |
| 59 | + result.(DataFlow::PostUpdateNode).getPreUpdateNode().(ArgumentNode).getCall() = call |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + /** Holds if this API has a supported summary. */ |
| 64 | + predicate hasSummary() { |
| 65 | + this instanceof SummarizedCallable or |
| 66 | + TaintTracking::localAdditionalTaintStep(this.getAnInput(), _) |
| 67 | + } |
| 68 | + |
| 69 | + /** Holds if this API is a known source. */ |
| 70 | + predicate isSource() { |
| 71 | + this.getAnOutput() instanceof RemoteFlowSource or sourceNode(this.getAnOutput(), _) |
| 72 | + } |
| 73 | + |
| 74 | + /** Holds if this API is a known sink. */ |
| 75 | + predicate isSink() { sinkNode(this.getAnInput(), _) } |
| 76 | + |
| 77 | + /** Holds if this API is supported by existing CodeQL libraries, that is, it is either a recognized source or sink or has a flow summary. */ |
| 78 | + predicate isSupported() { hasSummary() or isSource() or isSink() } |
| 79 | +} |
| 80 | + |
| 81 | +private class TestLibrary extends RefType { |
| 82 | + TestLibrary() { |
| 83 | + getPackage() |
| 84 | + .getName() |
| 85 | + .matches([ |
| 86 | + "org.junit%", "junit.%", "org.mockito%", "org.assertj%", |
| 87 | + "com.github.tomakehurst.wiremock%", "org.hamcrest%", "org.springframework.test.%", |
| 88 | + "org.springframework.mock.%", "org.springframework.boot.test.%", "reactor.test%", |
| 89 | + "org.xmlunit%", "org.testcontainers.%", "org.opentest4j%", "org.mockserver%", |
| 90 | + "org.powermock%", "org.skyscreamer.jsonassert%", "org.rnorth.visibleassertions", |
| 91 | + "org.openqa.selenium%", "com.gargoylesoftware.htmlunit%", |
| 92 | + "org.jboss.arquillian.testng%", "org.testng%" |
| 93 | + ]) |
| 94 | + } |
| 95 | +} |
0 commit comments