|
| 1 | +/** Provides classes and predicates related to handling APIs for the VS Code extension. */ |
| 2 | + |
| 3 | +private import csharp |
| 4 | +private import semmle.code.csharp.dataflow.FlowSummary |
| 5 | +private import semmle.code.csharp.dataflow.internal.DataFlowPrivate |
| 6 | +private import semmle.code.csharp.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl |
| 7 | +private import semmle.code.csharp.frameworks.Test |
| 8 | +private import Telemetry.TestLibrary |
| 9 | + |
| 10 | +/** Holds if the given callable is not worth supporting. */ |
| 11 | +private predicate isUninteresting(Callable c) { |
| 12 | + c.getDeclaringType() instanceof TestLibrary or |
| 13 | + c.(Constructor).isParameterless() or |
| 14 | + c.getDeclaringType() instanceof AnonymousClass |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * A callable method or accessor from either the C# Standard Library, a 3rd party library, or from the source. |
| 19 | + */ |
| 20 | +class Endpoint extends Callable { |
| 21 | + Endpoint() { |
| 22 | + [this.(Modifiable), this.(Accessor).getDeclaration()].isEffectivelyPublic() and |
| 23 | + not isUninteresting(this) and |
| 24 | + this.isUnboundDeclaration() |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Gets the namespace of this endpoint. |
| 29 | + */ |
| 30 | + bindingset[this] |
| 31 | + string getNamespace() { this.getDeclaringType().hasQualifiedName(result, _) } |
| 32 | + |
| 33 | + /** |
| 34 | + * Gets the unbound type name of this endpoint. |
| 35 | + */ |
| 36 | + bindingset[this] |
| 37 | + string getTypeName() { result = nestedName(this.getDeclaringType().getUnboundDeclaration()) } |
| 38 | + |
| 39 | + /** |
| 40 | + * Gets the parameter types of this endpoint. |
| 41 | + */ |
| 42 | + bindingset[this] |
| 43 | + string getParameterTypes() { result = parameterQualifiedTypeNamesToString(this) } |
| 44 | + |
| 45 | + private string getDllName() { result = this.getLocation().(Assembly).getName() } |
| 46 | + |
| 47 | + private string getDllVersion() { result = this.getLocation().(Assembly).getVersion().toString() } |
| 48 | + |
| 49 | + string dllName() { |
| 50 | + result = this.getDllName() |
| 51 | + or |
| 52 | + not exists(this.getDllName()) and result = this.getFile().getBaseName() |
| 53 | + } |
| 54 | + |
| 55 | + string dllVersion() { |
| 56 | + result = this.getDllVersion() |
| 57 | + or |
| 58 | + not exists(this.getDllVersion()) and result = "" |
| 59 | + } |
| 60 | + |
| 61 | + /** Holds if this API has a supported summary. */ |
| 62 | + pragma[nomagic] |
| 63 | + predicate hasSummary() { this instanceof SummarizedCallable } |
| 64 | + |
| 65 | + /** Holds if this API is a known source. */ |
| 66 | + pragma[nomagic] |
| 67 | + abstract predicate isSource(); |
| 68 | + |
| 69 | + /** Holds if this API is a known sink. */ |
| 70 | + pragma[nomagic] |
| 71 | + abstract predicate isSink(); |
| 72 | + |
| 73 | + /** Holds if this API is a known neutral. */ |
| 74 | + pragma[nomagic] |
| 75 | + predicate isNeutral() { this instanceof FlowSummaryImpl::Public::NeutralCallable } |
| 76 | + |
| 77 | + /** |
| 78 | + * Holds if this API is supported by existing CodeQL libraries, that is, it is either a |
| 79 | + * recognized source, sink or neutral or it has a flow summary. |
| 80 | + */ |
| 81 | + predicate isSupported() { |
| 82 | + this.hasSummary() or this.isSource() or this.isSink() or this.isNeutral() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +boolean isSupported(Endpoint endpoint) { |
| 87 | + if endpoint.isSupported() then result = true else result = false |
| 88 | +} |
| 89 | + |
| 90 | +string supportedType(Endpoint endpoint) { |
| 91 | + endpoint.isSink() and result = "sink" |
| 92 | + or |
| 93 | + endpoint.isSource() and result = "source" |
| 94 | + or |
| 95 | + endpoint.hasSummary() and result = "summary" |
| 96 | + or |
| 97 | + endpoint.isNeutral() and result = "neutral" |
| 98 | + or |
| 99 | + not endpoint.isSupported() and result = "" |
| 100 | +} |
| 101 | + |
| 102 | +string methodClassification(Call method) { |
| 103 | + method.getFile() instanceof TestFile and result = "test" |
| 104 | + or |
| 105 | + not method.getFile() instanceof TestFile and |
| 106 | + result = "source" |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Gets the nested name of the type `t`. |
| 111 | + * |
| 112 | + * If the type is not a nested type, the result is the same as `getName()`. |
| 113 | + * Otherwise the name of the nested type is prefixed with a `+` and appended to |
| 114 | + * the name of the enclosing type, which might be a nested type as well. |
| 115 | + */ |
| 116 | +private string nestedName(Type t) { |
| 117 | + not exists(t.getDeclaringType().getUnboundDeclaration()) and |
| 118 | + result = t.getName() |
| 119 | + or |
| 120 | + nestedName(t.getDeclaringType().getUnboundDeclaration()) + "+" + t.getName() = result |
| 121 | +} |
0 commit comments