|
| 1 | +/** |
| 2 | + * Definitions for reasoning about untrusted data used in APIs defined outside the |
| 3 | + * database. |
| 4 | + */ |
| 5 | + |
| 6 | +import csharp |
| 7 | +import semmle.code.csharp.dataflow.flowsources.Remote |
| 8 | +import semmle.code.csharp.dataflow.TaintTracking |
| 9 | +import semmle.code.csharp.frameworks.System |
| 10 | +import semmle.code.csharp.dataflow.FlowSummary |
| 11 | + |
| 12 | +/** |
| 13 | + * A callable that is considered a "safe" external API from a security perspective. |
| 14 | + */ |
| 15 | +abstract class SafeExternalAPICallable extends Callable { } |
| 16 | + |
| 17 | +private class SummarizedCallableSafe extends SafeExternalAPICallable { |
| 18 | + SummarizedCallableSafe() { this instanceof SummarizedCallable } |
| 19 | +} |
| 20 | + |
| 21 | +/** The default set of "safe" external APIs. */ |
| 22 | +private class DefaultSafeExternalAPICallable extends SafeExternalAPICallable { |
| 23 | + DefaultSafeExternalAPICallable() { |
| 24 | + this instanceof EqualsMethod or |
| 25 | + this instanceof IEquatableEqualsMethod or |
| 26 | + this = any(SystemObjectClass s).getEqualsMethod() or |
| 27 | + this = any(SystemObjectClass s).getReferenceEqualsMethod() or |
| 28 | + this = any(SystemObjectClass s).getStaticEqualsMethod() or |
| 29 | + this = any(SystemObjectClass s).getGetTypeMethod() or |
| 30 | + this = any(SystemStringClass s).getEqualsMethod() or |
| 31 | + this = any(SystemStringClass s).getEqualsOperator() or |
| 32 | + this = any(SystemStringClass s).getIsNullOrEmptyMethod() or |
| 33 | + this = any(SystemStringClass s).getIsNullOrWhiteSpaceMethod() or |
| 34 | + this.getName().regexpMatch("Count|get_Count|get_Length") |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** A node representing data being passed to an external API. */ |
| 39 | +class ExternalAPIDataNode extends DataFlow::Node { |
| 40 | + Call call; |
| 41 | + int i; |
| 42 | + |
| 43 | + ExternalAPIDataNode() { |
| 44 | + ( |
| 45 | + // Argument to call |
| 46 | + this.asExpr() = call.getArgument(i) |
| 47 | + or |
| 48 | + // Qualifier to call |
| 49 | + this.asExpr() = call.getChild(i) and |
| 50 | + i = -1 and |
| 51 | + // extension methods are covered above |
| 52 | + not call.getTarget().(Method).isExtensionMethod() |
| 53 | + ) and |
| 54 | + // Defined outside the source archive |
| 55 | + not call.getTarget().fromSource() and |
| 56 | + // Not a call to a method which is overridden in source |
| 57 | + not exists(Virtualizable m | |
| 58 | + m.overridesOrImplementsOrEquals(call.getTarget().getUnboundDeclaration()) and |
| 59 | + m.fromSource() |
| 60 | + ) and |
| 61 | + // Not a call to a known safe external API |
| 62 | + not call.getTarget().getUnboundDeclaration() instanceof SafeExternalAPICallable |
| 63 | + } |
| 64 | + |
| 65 | + /** Gets the called API callable. */ |
| 66 | + Callable getCallable() { result = call.getTarget().getUnboundDeclaration() } |
| 67 | + |
| 68 | + /** Gets the index which is passed untrusted data (where -1 indicates the qualifier). */ |
| 69 | + int getIndex() { result = i } |
| 70 | + |
| 71 | + /** Gets the description of the callable being called. */ |
| 72 | + string getCallableDescription() { result = getCallable().getQualifiedName() } |
| 73 | +} |
| 74 | + |
| 75 | +/** A configuration for tracking flow from `RemoteFlowSource`s to `ExternalAPIDataNode`s. */ |
| 76 | +class UntrustedDataToExternalAPIConfig extends TaintTracking::Configuration { |
| 77 | + UntrustedDataToExternalAPIConfig() { this = "UntrustedDataToExternalAPIConfig" } |
| 78 | + |
| 79 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 80 | + |
| 81 | + override predicate isSink(DataFlow::Node sink) { sink instanceof ExternalAPIDataNode } |
| 82 | +} |
| 83 | + |
| 84 | +/** A node representing untrusted data being passed to an external API. */ |
| 85 | +class UntrustedExternalAPIDataNode extends ExternalAPIDataNode { |
| 86 | + private UntrustedDataToExternalAPIConfig c; |
| 87 | + |
| 88 | + UntrustedExternalAPIDataNode() { c.hasFlow(_, this) } |
| 89 | + |
| 90 | + /** Gets a source of untrusted data which is passed to this external API data node. */ |
| 91 | + DataFlow::Node getAnUntrustedSource() { c.hasFlow(result, this) } |
| 92 | +} |
| 93 | + |
| 94 | +private newtype TExternalAPI = |
| 95 | + TExternalAPIParameter(Callable m, int index) { |
| 96 | + exists(UntrustedExternalAPIDataNode n | |
| 97 | + m = n.getCallable().getUnboundDeclaration() and |
| 98 | + index = n.getIndex() |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | +/** An external API which is used with untrusted data. */ |
| 103 | +class ExternalAPIUsedWithUntrustedData extends TExternalAPI { |
| 104 | + /** Gets a possibly untrusted use of this external API. */ |
| 105 | + UntrustedExternalAPIDataNode getUntrustedDataNode() { |
| 106 | + this = TExternalAPIParameter(result.getCallable().getUnboundDeclaration(), result.getIndex()) |
| 107 | + } |
| 108 | + |
| 109 | + /** Gets the number of untrusted sources used with this external API. */ |
| 110 | + int getNumberOfUntrustedSources() { |
| 111 | + result = count(getUntrustedDataNode().getAnUntrustedSource()) |
| 112 | + } |
| 113 | + |
| 114 | + /** Gets a textual representation of this element. */ |
| 115 | + string toString() { |
| 116 | + exists(Callable m, int index, string indexString | |
| 117 | + if index = -1 then indexString = "qualifier" else indexString = "param " + index |
| 118 | + | |
| 119 | + this = TExternalAPIParameter(m, index) and |
| 120 | + result = |
| 121 | + m.getDeclaringType().getQualifiedName() + "." + m.toStringWithTypes() + " [" + indexString + |
| 122 | + "]" |
| 123 | + ) |
| 124 | + } |
| 125 | +} |
0 commit comments