|
| 1 | +/** Definitions for the keyboard cache query */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.dataflow.DataFlow |
| 5 | +import semmle.code.java.security.SensitiveActions |
| 6 | +import semmle.code.xml.AndroidManifest |
| 7 | + |
| 8 | +/** An Android Layout XML file. */ |
| 9 | +private class AndroidLayoutXmlFile extends XmlFile { |
| 10 | + AndroidLayoutXmlFile() { this.getRelativePath().matches("%/res/layout/%.xml") } |
| 11 | +} |
| 12 | + |
| 13 | +/** A component declared in an Android layout file. */ |
| 14 | +class AndroidLayoutXmlElement extends XmlElement { |
| 15 | + AndroidXmlAttribute id; |
| 16 | + |
| 17 | + AndroidLayoutXmlElement() { |
| 18 | + this.getFile() instanceof AndroidLayoutXmlFile and |
| 19 | + id = this.getAttribute("id") |
| 20 | + } |
| 21 | + |
| 22 | + /** Gets the ID of this component. */ |
| 23 | + string getId() { result = id.getValue() } |
| 24 | + |
| 25 | + /** Gets the class of this component. */ |
| 26 | + Class getClass() { |
| 27 | + this.getName() = "view" and |
| 28 | + this.getAttribute("class").getValue() = result.getQualifiedName() |
| 29 | + or |
| 30 | + this.getName() = result.getQualifiedName() |
| 31 | + or |
| 32 | + result.hasQualifiedName(["android.widget", "android.view"], this.getName()) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** An XML element that represents an editable text field. */ |
| 37 | +class AndroidEditableXmlElement extends AndroidLayoutXmlElement { |
| 38 | + AndroidEditableXmlElement() { |
| 39 | + this.getClass().getASourceSupertype*().hasQualifiedName("android.widget", "EditText") |
| 40 | + } |
| 41 | + |
| 42 | + /** Gets the input type of this field, if any. */ |
| 43 | + string getInputType() { result = this.getAttribute("inputType").(AndroidXmlAttribute).getValue() } |
| 44 | +} |
| 45 | + |
| 46 | +/** A `findViewById` or `requireViewById` method on `Activity` or `View`. */ |
| 47 | +private class FindViewMethod extends Method { |
| 48 | + FindViewMethod() { |
| 49 | + this.hasQualifiedName("android.view", "View", ["findViewById", "requireViewById"]) |
| 50 | + or |
| 51 | + exists(Method m | |
| 52 | + m.hasQualifiedName("android.app", "Activity", ["findViewById", "requireViewById"]) and |
| 53 | + this = m.getAnOverride*() |
| 54 | + ) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** Gets a use of the view that has the given id. */ |
| 59 | +private MethodAccess getAUseOfViewWithId(string id) { |
| 60 | + exists(string name, NestedClass r_id, Field id_field | |
| 61 | + id = "@+id/" + name and |
| 62 | + result.getMethod() instanceof FindViewMethod and |
| 63 | + r_id.getEnclosingType().hasName("R") and |
| 64 | + r_id.hasName("id") and |
| 65 | + id_field.getDeclaringType() = r_id and |
| 66 | + id_field.hasName(name) |
| 67 | + | |
| 68 | + DataFlow::localExprFlow(id_field.getAnAccess(), result.getArgument(0)) |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +/** Gets the argument of a use of `setInputType` called on the view with the given id. */ |
| 73 | +private Argument setInputTypeForId(string id) { |
| 74 | + exists(MethodAccess setInputType | |
| 75 | + setInputType.getMethod().hasQualifiedName("android.widget", "TextView", "setInputType") and |
| 76 | + DataFlow::localExprFlow(getAUseOfViewWithId(id), setInputType.getQualifier()) and |
| 77 | + result = setInputType.getArgument(0) |
| 78 | + ) |
| 79 | +} |
| 80 | + |
| 81 | +/** Holds if the given field is a constant flag indicating that an input with this type will not be cached. */ |
| 82 | +private predicate inputTypeFieldNotCached(Field f) { |
| 83 | + f.getDeclaringType().hasQualifiedName("android.text", "InputType") and |
| 84 | + ( |
| 85 | + not f.getName().matches("%TEXT%") |
| 86 | + or |
| 87 | + f.getName().matches("%PASSWORD%") |
| 88 | + or |
| 89 | + f.hasName("TYPE_TEXT_FLAG_NO_SUGGESTIONS") |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +/** Configuration that finds uses of `setInputType` for non cached fields. */ |
| 94 | +private class GoodInputTypeConf extends DataFlow::Configuration { |
| 95 | + GoodInputTypeConf() { this = "GoodInputTypeConf" } |
| 96 | + |
| 97 | + override predicate isSource(DataFlow::Node node) { |
| 98 | + inputTypeFieldNotCached(node.asExpr().(FieldAccess).getField()) |
| 99 | + } |
| 100 | + |
| 101 | + override predicate isSink(DataFlow::Node node) { node.asExpr() = setInputTypeForId(_) } |
| 102 | + |
| 103 | + override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 104 | + exists(OrBitwiseExpr bitOr | |
| 105 | + node1.asExpr() = bitOr.getAChildExpr() and |
| 106 | + node2.asExpr() = bitOr |
| 107 | + ) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** Gets a regex indicating that an input field may contain sensitive data. */ |
| 112 | +private string getInputSensitiveInfoRegex() { |
| 113 | + result = |
| 114 | + [ |
| 115 | + getCommonSensitiveInfoRegex(), |
| 116 | + "(?i).*(bank|credit|debit|(pass(wd|word|code|phrase))|security).*" |
| 117 | + ] |
| 118 | +} |
| 119 | + |
| 120 | +/** Holds if input using the given input type (as written in XML) is not stored in the keyboard cache. */ |
| 121 | +bindingset[ty] |
| 122 | +private predicate inputTypeNotCached(string ty) { |
| 123 | + not ty.matches("%text%") |
| 124 | + or |
| 125 | + ty.regexpMatch("(?i).*(nosuggestions|password).*") |
| 126 | +} |
| 127 | + |
| 128 | +/** Gets an input field whose contents may be sensitive and may be stored in the keyboard cache. */ |
| 129 | +AndroidEditableXmlElement getASensitiveCachedInput() { |
| 130 | + result.getId().regexpMatch(getInputSensitiveInfoRegex()) and |
| 131 | + ( |
| 132 | + not inputTypeNotCached(result.getInputType()) and |
| 133 | + not exists(GoodInputTypeConf conf, DataFlow::Node src, DataFlow::Node sink | |
| 134 | + conf.hasFlow(src, sink) and |
| 135 | + sink.asExpr() = setInputTypeForId(result.getId()) |
| 136 | + ) |
| 137 | + ) |
| 138 | +} |
0 commit comments