mirror of
https://github.com/github/codeql.git
synced 2026-02-28 21:03:50 +01:00
140 lines
4.4 KiB
Plaintext
140 lines
4.4 KiB
Plaintext
/** Provides classes and predicates related to handling APIs for the VS Code extension. */
|
|
|
|
private import java
|
|
private import semmle.code.java.dataflow.DataFlow
|
|
private import semmle.code.java.dataflow.ExternalFlow
|
|
private import semmle.code.java.dataflow.FlowSources
|
|
private import semmle.code.java.dataflow.FlowSummary
|
|
private import semmle.code.java.dataflow.internal.DataFlowPrivate
|
|
private import semmle.code.java.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
|
|
private import semmle.code.java.dataflow.TaintTracking
|
|
private import semmle.code.java.dataflow.internal.ModelExclusions
|
|
|
|
/** Holds if the given callable/method is not worth supporting. */
|
|
private predicate isUninteresting(Callable c) {
|
|
c.getDeclaringType() instanceof TestLibrary or
|
|
c.(Constructor).isParameterless() or
|
|
c.getDeclaringType() instanceof AnonymousClass
|
|
}
|
|
|
|
/**
|
|
* A callable method from either the Standard Library, a 3rd party library or from the source.
|
|
*/
|
|
class CallableMethod extends Callable {
|
|
CallableMethod() { not isUninteresting(this) }
|
|
|
|
/**
|
|
* Gets information about the external API in the form expected by the MaD modeling framework.
|
|
*/
|
|
string getApiName() {
|
|
result =
|
|
this.getDeclaringType().getPackage() + "." + this.getDeclaringType().nestedName() + "#" +
|
|
this.getName() + paramsString(this)
|
|
}
|
|
|
|
private string getJarName() {
|
|
result = this.getCompilationUnit().getParentContainer*().(JarFile).getBaseName()
|
|
}
|
|
|
|
private string getJarVersion() {
|
|
result = this.getCompilationUnit().getParentContainer*().(JarFile).getSpecificationVersion()
|
|
}
|
|
|
|
/**
|
|
* Gets the jar file containing this API. Normalizes the Java Runtime to "rt.jar" despite the presence of modules.
|
|
*/
|
|
string jarContainer() {
|
|
result = this.getJarName()
|
|
or
|
|
not exists(this.getJarName()) and result = "rt.jar"
|
|
}
|
|
|
|
/**
|
|
* Gets the version of the JAR file containing this API. Empty if no version is found in the JAR.
|
|
*/
|
|
string jarVersion() {
|
|
result = this.getJarVersion()
|
|
or
|
|
not exists(this.getJarVersion()) and result = ""
|
|
}
|
|
|
|
/** Gets a node that is an input to a call to this API. */
|
|
private DataFlow::Node getAnInput() {
|
|
exists(Call call | call.getCallee().getSourceDeclaration() = this |
|
|
result.asExpr().(Argument).getCall() = call or
|
|
result.(ArgumentNode).getCall().asCall() = call
|
|
)
|
|
}
|
|
|
|
/** Gets a node that is an output from a call to this API. */
|
|
private DataFlow::Node getAnOutput() {
|
|
exists(Call call | call.getCallee().getSourceDeclaration() = this |
|
|
result.asExpr() = call or
|
|
result.(DataFlow::PostUpdateNode).getPreUpdateNode().(ArgumentNode).getCall().asCall() = call
|
|
)
|
|
}
|
|
|
|
/** Holds if this API has a supported summary. */
|
|
pragma[nomagic]
|
|
predicate hasSummary() {
|
|
this = any(SummarizedCallable sc).asCallable() or
|
|
TaintTracking::localAdditionalTaintStep(this.getAnInput(), _)
|
|
}
|
|
|
|
pragma[nomagic]
|
|
predicate isSource() {
|
|
this.getAnOutput() instanceof RemoteFlowSource or sourceNode(this.getAnOutput(), _)
|
|
}
|
|
|
|
/** Holds if this API is a known sink. */
|
|
pragma[nomagic]
|
|
predicate isSink() { sinkNode(this.getAnInput(), _) }
|
|
|
|
/** Holds if this API is a known neutral. */
|
|
pragma[nomagic]
|
|
predicate isNeutral() {
|
|
exists(
|
|
string namespace, string type, string name, string signature
|
|
|
|
|
neutralModel(namespace, type, name, signature, _, _) and
|
|
this = interpretElement(namespace, type, false, name, signature, "")
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Holds if this API is supported by existing CodeQL libraries, that is, it is either a
|
|
* recognized source, sink or neutral or it has a flow summary.
|
|
*/
|
|
predicate isSupported() {
|
|
this.hasSummary() or this.isSource() or this.isSink() or this.isNeutral()
|
|
}
|
|
}
|
|
|
|
boolean isSupported(CallableMethod method) {
|
|
method.isSupported() and result = true
|
|
or
|
|
not method.isSupported() and result = false
|
|
}
|
|
|
|
string supportedType(CallableMethod method) {
|
|
method.isSink() and result = "sink"
|
|
or
|
|
method.isSource() and result = "source"
|
|
or
|
|
method.hasSummary() and result = "summary"
|
|
or
|
|
method.isNeutral() and result = "neutral"
|
|
or
|
|
not method.isSupported() and result = ""
|
|
}
|
|
|
|
string methodClassification(Call method) {
|
|
isInTestFile(method.getLocation().getFile()) and result = "test"
|
|
or
|
|
method.getFile() instanceof GeneratedFile and result = "generated"
|
|
or
|
|
not isInTestFile(method.getLocation().getFile()) and
|
|
not method.getFile() instanceof GeneratedFile and
|
|
result = "source"
|
|
}
|