|
| 1 | +/** Provides classes and predicates related to handling APIs from external libraries. */ |
| 2 | + |
| 3 | +private import csharp |
| 4 | +private import semmle.code.csharp.dispatch.Dispatch |
| 5 | +private import semmle.code.csharp.dataflow.DataFlow |
| 6 | +private import semmle.code.csharp.dataflow.ExternalFlow |
| 7 | +private import semmle.code.csharp.dataflow.FlowSummary |
| 8 | +private import semmle.code.csharp.dataflow.internal.DataFlowImplCommon as DataFlowImplCommon |
| 9 | +private import semmle.code.csharp.dataflow.internal.DataFlowPrivate |
| 10 | +private import semmle.code.csharp.dataflow.internal.DataFlowDispatch as DataFlowDispatch |
| 11 | +private import semmle.code.csharp.dataflow.TaintTracking |
| 12 | +private import semmle.code.csharp.dataflow.internal.TaintTrackingPrivate |
| 13 | +private import semmle.code.csharp.security.dataflow.flowsources.Remote |
| 14 | + |
| 15 | +/** |
| 16 | + * A test library. |
| 17 | + */ |
| 18 | +class TestLibrary extends RefType { |
| 19 | + TestLibrary() { |
| 20 | + this.getNamespace() |
| 21 | + .getName() |
| 22 | + .matches(["NUnit.Framework%", "Xunit%", "Microsoft.VisualStudio.TestTools.UnitTesting%"]) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * An external API from either the C# Standard Library or a 3rd party library. |
| 28 | + */ |
| 29 | +class ExternalApi extends DataFlowDispatch::DataFlowCallable { |
| 30 | + ExternalApi() { this.fromLibrary() } |
| 31 | + |
| 32 | + /** |
| 33 | + * Gets the unbound type, name and parameter types of this API. |
| 34 | + */ |
| 35 | + private string getSignature() { |
| 36 | + result = |
| 37 | + this.getDeclaringType().getUnboundDeclaration() + "." + this.getName() + "(" + |
| 38 | + parameterQualifiedTypeNamesToString(this) + ")" |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Gets the namespace of this API. |
| 43 | + */ |
| 44 | + private string getNamespace() { this.getDeclaringType().hasQualifiedName(result, _) } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the assembly file name containing this API. |
| 48 | + */ |
| 49 | + private string getAssembly() { result = this.getFile().getBaseName() } |
| 50 | + |
| 51 | + /** |
| 52 | + * Gets the assembly file name and namespace of this API. |
| 53 | + */ |
| 54 | + string getInfoPrefix() { result = this.getAssembly() + "#" + this.getNamespace() } |
| 55 | + |
| 56 | + /** |
| 57 | + * Gets the assembly file name, namespace and signature of this API. |
| 58 | + */ |
| 59 | + string getInfo() { result = this.getInfoPrefix() + "#" + this.getSignature() } |
| 60 | + |
| 61 | + /** Gets a call to this API callable. */ |
| 62 | + DispatchCall getACall() { |
| 63 | + this = result.getADynamicTarget().getUnboundDeclaration() |
| 64 | + or |
| 65 | + this = result.getAStaticTarget().getUnboundDeclaration() |
| 66 | + } |
| 67 | + |
| 68 | + /** Gets a node that is an input to a call to this API. */ |
| 69 | + private ArgumentNode getAnInput() { |
| 70 | + result.getCall().(DataFlowDispatch::NonDelegateDataFlowCall).getDispatchCall() = this.getACall() |
| 71 | + } |
| 72 | + |
| 73 | + /** Gets a node that is an output from a call to this API. */ |
| 74 | + private DataFlow::Node getAnOutput() { |
| 75 | + exists(DataFlowDispatch::NonDelegateDataFlowCall call, DataFlowImplCommon::ReturnKindExt ret | |
| 76 | + result = ret.getAnOutNode(call) |
| 77 | + | |
| 78 | + this.getACall() = call.getDispatchCall() |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + /** Holds if this API has a supported summary. */ |
| 83 | + predicate hasSummary() { |
| 84 | + this instanceof SummarizedCallable or |
| 85 | + defaultAdditionalTaintStep(this.getAnInput(), _) |
| 86 | + } |
| 87 | + |
| 88 | + /** Holds if this API is is a constructor without parameters. */ |
| 89 | + private predicate isParameterlessConstructor() { |
| 90 | + this instanceof Constructor and this.getNumberOfParameters() = 0 |
| 91 | + } |
| 92 | + |
| 93 | + /** Holds if this API is part of a common testing library or framework. */ |
| 94 | + private predicate isTestLibrary() { this.getDeclaringType() instanceof TestLibrary } |
| 95 | + |
| 96 | + /** Holds if this API is not worth supporting. */ |
| 97 | + predicate isUninteresting() { this.isTestLibrary() or this.isParameterlessConstructor() } |
| 98 | + |
| 99 | + /** Holds if this API is a known source. */ |
| 100 | + predicate isSource() { |
| 101 | + this.getAnOutput() instanceof RemoteFlowSource or sourceNode(this.getAnOutput(), _) |
| 102 | + } |
| 103 | + |
| 104 | + /** Holds if this API is a known sink. */ |
| 105 | + predicate isSink() { sinkNode(this.getAnInput(), _) } |
| 106 | + |
| 107 | + /** 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. */ |
| 108 | + predicate isSupported() { this.hasSummary() or this.isSource() or this.isSink() } |
| 109 | +} |
0 commit comments