|
| 1 | +/** Provides classes and predicates related to handling APIs for the VS Code extension. */ |
| 2 | + |
| 3 | +private import java |
| 4 | +private import semmle.code.java.dataflow.DataFlow |
| 5 | +private import semmle.code.java.dataflow.ExternalFlow |
| 6 | +private import semmle.code.java.dataflow.FlowSources |
| 7 | +private import semmle.code.java.dataflow.FlowSummary |
| 8 | +private import semmle.code.java.dataflow.internal.DataFlowPrivate |
| 9 | +private import semmle.code.java.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl |
| 10 | +private import semmle.code.java.dataflow.TaintTracking |
| 11 | +private import semmle.code.java.dataflow.internal.ModelExclusions |
| 12 | + |
| 13 | +/** Holds if the given callable/method is not worth supporting. */ |
| 14 | +private predicate isUninteresting(Callable c) { |
| 15 | + c.getDeclaringType() instanceof TestLibrary or |
| 16 | + c.(Constructor).isParameterless() or |
| 17 | + c.getDeclaringType() instanceof AnonymousClass |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * A callable method from either the Standard Library, a 3rd party library or from the source. |
| 22 | + */ |
| 23 | +class CallableMethod extends Callable { |
| 24 | + CallableMethod() { not isUninteresting(this) } |
| 25 | + |
| 26 | + /** |
| 27 | + * Gets information about the external API in the form expected by the MaD modeling framework. |
| 28 | + */ |
| 29 | + string getApiName() { |
| 30 | + result = |
| 31 | + this.getDeclaringType().getPackage() + "." + this.getDeclaringType().nestedName() + "#" + |
| 32 | + this.getName() + paramsString(this) |
| 33 | + } |
| 34 | + |
| 35 | + private string getJarName() { |
| 36 | + result = this.getCompilationUnit().getParentContainer*().(JarFile).getBaseName() |
| 37 | + } |
| 38 | + |
| 39 | + private string getJarVersion() { |
| 40 | + result = this.getCompilationUnit().getParentContainer*().(JarFile).getSpecificationVersion() |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Gets the jar file containing this API. Normalizes the Java Runtime to "rt.jar" despite the presence of modules. |
| 45 | + */ |
| 46 | + string jarContainer() { |
| 47 | + result = this.getJarName() |
| 48 | + or |
| 49 | + not exists(this.getJarName()) and result = "rt.jar" |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Gets the version of the JAR file containing this API. Empty if no version is found in the JAR. |
| 54 | + */ |
| 55 | + string jarVersion() { |
| 56 | + result = this.getJarVersion() |
| 57 | + or |
| 58 | + not exists(this.getJarVersion()) and result = "" |
| 59 | + } |
| 60 | + |
| 61 | + /** Gets a node that is an input to a call to this API. */ |
| 62 | + private DataFlow::Node getAnInput() { |
| 63 | + exists(Call call | call.getCallee().getSourceDeclaration() = this | |
| 64 | + result.asExpr().(Argument).getCall() = call or |
| 65 | + result.(ArgumentNode).getCall().asCall() = call |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + /** Gets a node that is an output from a call to this API. */ |
| 70 | + private DataFlow::Node getAnOutput() { |
| 71 | + exists(Call call | call.getCallee().getSourceDeclaration() = this | |
| 72 | + result.asExpr() = call or |
| 73 | + result.(DataFlow::PostUpdateNode).getPreUpdateNode().(ArgumentNode).getCall().asCall() = call |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + /** Holds if this API has a supported summary. */ |
| 78 | + pragma[nomagic] |
| 79 | + predicate hasSummary() { |
| 80 | + this = any(SummarizedCallable sc).asCallable() or |
| 81 | + TaintTracking::localAdditionalTaintStep(this.getAnInput(), _) |
| 82 | + } |
| 83 | + |
| 84 | + pragma[nomagic] |
| 85 | + predicate isSource() { |
| 86 | + this.getAnOutput() instanceof RemoteFlowSource or sourceNode(this.getAnOutput(), _) |
| 87 | + } |
| 88 | + |
| 89 | + /** Holds if this API is a known sink. */ |
| 90 | + pragma[nomagic] |
| 91 | + predicate isSink() { sinkNode(this.getAnInput(), _) } |
| 92 | + |
| 93 | + /** Holds if this API is a known neutral. */ |
| 94 | + pragma[nomagic] |
| 95 | + predicate isNeutral() { |
| 96 | + exists( |
| 97 | + string namespace, string type, string name, string signature, string kind, string provenance |
| 98 | + | |
| 99 | + neutralModel(namespace, type, name, signature, kind, provenance) and |
| 100 | + this = interpretElement(namespace, type, false, name, signature, "") |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Holds if this API is supported by existing CodeQL libraries, that is, it is either a |
| 106 | + * recognized source, sink or neutral or it has a flow summary. |
| 107 | + */ |
| 108 | + predicate isSupported() { |
| 109 | + this.hasSummary() or this.isSource() or this.isSink() or this.isNeutral() |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +boolean isSupported(CallableMethod method) { |
| 114 | + method.isSupported() and result = true |
| 115 | + or |
| 116 | + not method.isSupported() and result = false |
| 117 | +} |
| 118 | + |
| 119 | +string supportedType(CallableMethod method) { |
| 120 | + method.isSink() and result = "sink" |
| 121 | + or |
| 122 | + method.isSource() and result = "source" |
| 123 | + or |
| 124 | + method.hasSummary() and result = "summary" |
| 125 | + or |
| 126 | + method.isNeutral() and result = "neutral" |
| 127 | + or |
| 128 | + not method.isSupported() and result = "" |
| 129 | +} |
| 130 | + |
| 131 | +string methodClassification(Call method) { |
| 132 | + isInTestFile(method.getLocation().getFile()) and result = "test" |
| 133 | + or |
| 134 | + method.getFile() instanceof GeneratedFile and result = "generated" |
| 135 | + or |
| 136 | + not isInTestFile(method.getLocation().getFile()) and |
| 137 | + not method.getFile() instanceof GeneratedFile and |
| 138 | + result = "source" |
| 139 | +} |
0 commit comments