|
| 1 | +/** |
| 2 | + * Definitions for reasoning about untrusted data used in APIs defined outside the |
| 3 | + * database. |
| 4 | + */ |
| 5 | + |
| 6 | +import java |
| 7 | +import semmle.code.java.dataflow.FlowSources |
| 8 | +import semmle.code.java.dataflow.TaintTracking |
| 9 | + |
| 10 | +module ExternalAPIs { |
| 11 | + /** |
| 12 | + * A `Method` which is considered a "safe" external API from a security perspective. |
| 13 | + */ |
| 14 | + abstract class SafeExternalAPIMethod extends Method { } |
| 15 | + |
| 16 | + /** The default set of "safe" external APIs. */ |
| 17 | + class DefaultSafeExternalAPIMethod extends SafeExternalAPIMethod { |
| 18 | + DefaultSafeExternalAPIMethod() { |
| 19 | + this instanceof EqualsMethod |
| 20 | + or |
| 21 | + getName().regexpMatch("size|length|compareTo|getClass|lastIndexOf") |
| 22 | + or |
| 23 | + this.getDeclaringType().hasQualifiedName("org.apache.commons.lang3", "Validate") |
| 24 | + or |
| 25 | + getQualifiedName() = "Objects.equals" |
| 26 | + or |
| 27 | + getDeclaringType().getQualifiedName() = "java.lang.String" and getName() = "equals" |
| 28 | + or |
| 29 | + getDeclaringType().hasQualifiedName("com.google.common.base", "Preconditions") |
| 30 | + or |
| 31 | + getDeclaringType().getPackage().getName().matches("org.junit%") |
| 32 | + or |
| 33 | + getDeclaringType().hasQualifiedName("com.google.common.base", "Strings") and |
| 34 | + getName() = "isNullOrEmpty" |
| 35 | + or |
| 36 | + getDeclaringType().hasQualifiedName("org.apache.commons.lang3", "StringUtils") and |
| 37 | + getName() = "isNotEmpty" |
| 38 | + or |
| 39 | + getDeclaringType().hasQualifiedName("java.lang", "Character") and |
| 40 | + getName() = "isDigit" |
| 41 | + or |
| 42 | + getDeclaringType().hasQualifiedName("java.lang", "String") and |
| 43 | + getName().regexpMatch("equalsIgnoreCase|regionMatches") |
| 44 | + or |
| 45 | + getDeclaringType().hasQualifiedName("java.lang", "Boolean") and |
| 46 | + getName() = "parseBoolean" |
| 47 | + or |
| 48 | + getDeclaringType().hasQualifiedName("org.apache.commons.io", "IOUtils") and |
| 49 | + getName() = "closeQuietly" |
| 50 | + or |
| 51 | + getDeclaringType().hasQualifiedName("org.springframework.util", "StringUtils") and |
| 52 | + getName().regexpMatch("hasText|isEmpty") |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** A node representing data being passed to an external API. */ |
| 57 | + class ExternalAPIDataNode extends DataFlow::Node { |
| 58 | + Call call; |
| 59 | + int i; |
| 60 | + |
| 61 | + ExternalAPIDataNode() { |
| 62 | + ( |
| 63 | + // Argument to call to a method |
| 64 | + this.asExpr() = call.getArgument(i) |
| 65 | + or |
| 66 | + // Qualifier to call to a method which returns non trivial value |
| 67 | + this.asExpr() = call.getQualifier() and |
| 68 | + i = -1 and |
| 69 | + not call.getCallee().getReturnType() instanceof VoidType and |
| 70 | + not call.getCallee().getReturnType() instanceof BooleanType |
| 71 | + ) and |
| 72 | + // Defined outside the source archive |
| 73 | + not call.getCallee().fromSource() and |
| 74 | + // Not a call to an method which is overridden in source |
| 75 | + not exists(Method m | |
| 76 | + m.getASourceOverriddenMethod() = call.getCallee().getSourceDeclaration() and |
| 77 | + m.fromSource() |
| 78 | + ) and |
| 79 | + // Not already modelled as a taint step |
| 80 | + not exists(DataFlow::Node next | TaintTracking::localTaintStep(this, next)) and |
| 81 | + // Not a call to a known safe external API |
| 82 | + not call.getCallee() instanceof SafeExternalAPIMethod |
| 83 | + } |
| 84 | + |
| 85 | + /** Gets the called API `Method`. */ |
| 86 | + Method getMethod() { result = call.getCallee() } |
| 87 | + |
| 88 | + /** Gets the index which is passed untrusted data (where -1 indicates the qualifier). */ |
| 89 | + int getIndex() { result = i } |
| 90 | + |
| 91 | + /** Gets the description of the method being called. */ |
| 92 | + string getMethodDescription() { |
| 93 | + result = |
| 94 | + getMethod().getDeclaringType().getPackage() + "." + getMethod().getQualifiedName() |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + class UntrustedDataToExternalAPIConfig extends TaintTracking::Configuration { |
| 99 | + UntrustedDataToExternalAPIConfig() { this = "UntrustedDataToExternalAPIConfig" } |
| 100 | + |
| 101 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 102 | + |
| 103 | + override predicate isSink(DataFlow::Node sink) { sink instanceof ExternalAPIDataNode } |
| 104 | + } |
| 105 | + |
| 106 | + /** A node representing untrusted data being passed to an external API. */ |
| 107 | + class UntrustedExternalAPIDataNode extends ExternalAPIDataNode { |
| 108 | + UntrustedExternalAPIDataNode() { any(UntrustedDataToExternalAPIConfig c).hasFlow(_, this) } |
| 109 | + |
| 110 | + /** Gets a source of untrusted data which is passed to this external API data node. */ |
| 111 | + DataFlow::Node getAnUntrustedSource() { |
| 112 | + any(UntrustedDataToExternalAPIConfig c).hasFlow(result, this) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private newtype TExternalAPI = |
| 117 | + TExternalAPIParameter(Method m, int index) { |
| 118 | + exists(UntrustedExternalAPIDataNode n | |
| 119 | + m = n.getMethod() and |
| 120 | + index = n.getIndex() |
| 121 | + ) |
| 122 | + } |
| 123 | + |
| 124 | + /** An external API which is used with untrusted data. */ |
| 125 | + class ExternalAPIUsedWithUntrustedData extends TExternalAPI { |
| 126 | + /** Gets a possibly untrusted use of this external API. */ |
| 127 | + UntrustedExternalAPIDataNode getUntrustedDataNode() { |
| 128 | + this = TExternalAPIParameter(result.getMethod(), result.getIndex()) |
| 129 | + } |
| 130 | + |
| 131 | + /** Gets the number of untrusted sources used with this external API. */ |
| 132 | + int getNumberOfUntrustedSources() { |
| 133 | + result = count(getUntrustedDataNode().getAnUntrustedSource()) |
| 134 | + } |
| 135 | + |
| 136 | + string toString() { |
| 137 | + exists(Method m, int index, string indexString | |
| 138 | + if index = -1 then indexString = "qualifier" else indexString = "param " + index |
| 139 | + | |
| 140 | + this = TExternalAPIParameter(m, index) and |
| 141 | + result = |
| 142 | + m.getDeclaringType().(RefType).getQualifiedName() + "." + m.getSignature() + " [" + indexString + "]" |
| 143 | + ) |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments