|
| 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.ExternalFlow |
| 5 | +private import semmle.code.java.dataflow.FlowSummary |
| 6 | +private import semmle.code.java.dataflow.TaintTracking |
| 7 | +private import semmle.code.java.dataflow.internal.ModelExclusions |
| 8 | + |
| 9 | +/** Holds if the given callable/method is not worth supporting. */ |
| 10 | +private predicate isUninteresting(Callable c) { |
| 11 | + c.getDeclaringType() instanceof TestLibrary or |
| 12 | + c.(Constructor).isParameterless() or |
| 13 | + c.getDeclaringType() instanceof AnonymousClass |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * A callable method from either the Standard Library, a 3rd party library or from the source. |
| 18 | + */ |
| 19 | +class Endpoint extends Callable { |
| 20 | + Endpoint() { not isUninteresting(this) } |
| 21 | + |
| 22 | + /** |
| 23 | + * Gets the package name of this endpoint. |
| 24 | + */ |
| 25 | + string getPackageName() { result = this.getDeclaringType().getPackage().getName() } |
| 26 | + |
| 27 | + /** |
| 28 | + * Gets the type name of this endpoint. |
| 29 | + */ |
| 30 | + string getTypeName() { result = this.getDeclaringType().nestedName() } |
| 31 | + |
| 32 | + /** |
| 33 | + * Gets the parameter types of this endpoint. |
| 34 | + */ |
| 35 | + string getParameterTypes() { result = paramsString(this) } |
| 36 | + |
| 37 | + private string getJarName() { |
| 38 | + result = this.getCompilationUnit().getParentContainer*().(JarFile).getBaseName() |
| 39 | + } |
| 40 | + |
| 41 | + private string getJarVersion() { |
| 42 | + result = this.getCompilationUnit().getParentContainer*().(JarFile).getSpecificationVersion() |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Gets the jar file containing this API. Normalizes the Java Runtime to "rt.jar" despite the presence of modules. |
| 47 | + */ |
| 48 | + string jarContainer() { |
| 49 | + result = this.getJarName() |
| 50 | + or |
| 51 | + not exists(this.getJarName()) and result = "rt.jar" |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Gets the version of the JAR file containing this API. Empty if no version is found in the JAR. |
| 56 | + */ |
| 57 | + string jarVersion() { |
| 58 | + result = this.getJarVersion() |
| 59 | + or |
| 60 | + not exists(this.getJarVersion()) and result = "" |
| 61 | + } |
| 62 | + |
| 63 | + /** Holds if this API has a supported summary. */ |
| 64 | + pragma[nomagic] |
| 65 | + predicate hasSummary() { this = any(SummarizedCallable sc).asCallable() } |
| 66 | + |
| 67 | + /** Holds if this API is a known source. */ |
| 68 | + pragma[nomagic] |
| 69 | + abstract predicate isSource(); |
| 70 | + |
| 71 | + /** Holds if this API is a known sink. */ |
| 72 | + pragma[nomagic] |
| 73 | + abstract predicate isSink(); |
| 74 | + |
| 75 | + /** Holds if this API is a known neutral. */ |
| 76 | + pragma[nomagic] |
| 77 | + predicate isNeutral() { |
| 78 | + exists(string namespace, string type, string name, string signature | |
| 79 | + neutralModel(namespace, type, name, signature, _, _) and |
| 80 | + this = interpretElement(namespace, type, false, name, signature, "") |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Holds if this API is supported by existing CodeQL libraries, that is, it is either a |
| 86 | + * recognized source, sink or neutral or it has a flow summary. |
| 87 | + */ |
| 88 | + predicate isSupported() { |
| 89 | + this.hasSummary() or this.isSource() or this.isSink() or this.isNeutral() |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +boolean isSupported(Endpoint endpoint) { |
| 94 | + endpoint.isSupported() and result = true |
| 95 | + or |
| 96 | + not endpoint.isSupported() and result = false |
| 97 | +} |
| 98 | + |
| 99 | +string supportedType(Endpoint endpoint) { |
| 100 | + endpoint.isSink() and result = "sink" |
| 101 | + or |
| 102 | + endpoint.isSource() and result = "source" |
| 103 | + or |
| 104 | + endpoint.hasSummary() and result = "summary" |
| 105 | + or |
| 106 | + endpoint.isNeutral() and result = "neutral" |
| 107 | + or |
| 108 | + not endpoint.isSupported() and result = "" |
| 109 | +} |
| 110 | + |
| 111 | +string usageClassification(Call usage) { |
| 112 | + isInTestFile(usage.getLocation().getFile()) and result = "test" |
| 113 | + or |
| 114 | + usage.getFile() instanceof GeneratedFile and result = "generated" |
| 115 | + or |
| 116 | + not isInTestFile(usage.getLocation().getFile()) and |
| 117 | + not usage.getFile() instanceof GeneratedFile and |
| 118 | + result = "source" |
| 119 | +} |
0 commit comments