|
| 1 | +/** |
| 2 | + * @id java/incorrect-url-verification |
| 3 | + * @name Incorrect URL verification |
| 4 | + * @description Apps that rely on URL parsing to verify that a given URL is pointing to a trusted server are susceptible to wrong ways of URL parsing and verification. |
| 5 | + * @kind problem |
| 6 | + * @tags security |
| 7 | + * external/cwe-939 |
| 8 | + */ |
| 9 | + |
| 10 | +import java |
| 11 | + |
| 12 | +/** |
| 13 | + * The Java class `android.R.string` specific to Android applications, which contains references to application specific resources defined in /res/values/strings.xml. |
| 14 | + * For example, <resources>...<string name="host">example.com</string>...</resources> in the application com.example.android.web can be referred as R.string.host with the type com.example.android.web.R$string |
| 15 | + */ |
| 16 | +class AndroidRString extends RefType { |
| 17 | + AndroidRString() { this.hasQualifiedName(_, "R$string") } |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * The Java class `android.net.Uri` and `java.net.URL`. |
| 22 | + */ |
| 23 | +class Uri extends RefType { |
| 24 | + Uri() { |
| 25 | + hasQualifiedName("android.net", "Uri") or |
| 26 | + hasQualifiedName("java.net", "URL") |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * The method `getHost()` declared in `android.net.Uri` and `java.net.URL`. |
| 32 | + */ |
| 33 | +class UriGetHostMethod extends Method { |
| 34 | + UriGetHostMethod() { |
| 35 | + getDeclaringType() instanceof Uri and |
| 36 | + hasName("getHost") and |
| 37 | + getNumberOfParameters() = 0 |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * The method access with incorrect string comparision |
| 43 | + */ |
| 44 | +class HostVerificationMethodAccess extends MethodAccess { |
| 45 | + HostVerificationMethodAccess() { |
| 46 | + ( |
| 47 | + this.getMethod().hasName("endsWith") or |
| 48 | + this.getMethod().hasName("contains") or |
| 49 | + this.getMethod().hasName("indexOf") |
| 50 | + ) and |
| 51 | + this.getMethod().getNumberOfParameters() = 1 and |
| 52 | + ( |
| 53 | + this.getArgument(0).(StringLiteral).getRepresentedString().charAt(0) != "." //string constant comparison e.g. uri.getHost().endsWith("example.com") |
| 54 | + or |
| 55 | + this |
| 56 | + .getArgument(0) |
| 57 | + .(AddExpr) |
| 58 | + .getLeftOperand() |
| 59 | + .(VarAccess) |
| 60 | + .getVariable() |
| 61 | + .getAnAssignedValue() |
| 62 | + .(StringLiteral) |
| 63 | + .getRepresentedString() |
| 64 | + .charAt(0) != "." //var1+var2, check var1 starts with "." e.g. String domainName = "example"; Uri.parse(url).getHost().endsWith(domainName+".com") |
| 65 | + or |
| 66 | + this |
| 67 | + .getArgument(0) |
| 68 | + .(AddExpr) |
| 69 | + .getLeftOperand() |
| 70 | + .(StringLiteral) |
| 71 | + .getRepresentedString() |
| 72 | + .charAt(0) != "." //"."+var2, check string constant "." e.g. String domainName = "example.com"; Uri.parse(url).getHost().endsWith("www."+domainName) |
| 73 | + or |
| 74 | + exists(MethodAccess ma, Method m, Field f | |
| 75 | + this.getArgument(0) = ma and |
| 76 | + ma.getMethod() = m and |
| 77 | + m.hasName("getString") and |
| 78 | + m.getDeclaringType().getQualifiedName() = "android.content.res.Resources" and |
| 79 | + ma.getArgument(0).(FieldRead).getField() = f and |
| 80 | + f.getDeclaringType() instanceof AndroidRString |
| 81 | + ) //Check resource properties in /res/values/strings.xml in Android mobile applications using res.getString(R.string.key) |
| 82 | + or |
| 83 | + this |
| 84 | + .getArgument(0) |
| 85 | + .(VarAccess) |
| 86 | + .getVariable() |
| 87 | + .getAnAssignedValue() |
| 88 | + .(StringLiteral) |
| 89 | + .getRepresentedString() |
| 90 | + .charAt(0) != "." //check variable starts with "." e.g. String domainName = "example.com"; Uri.parse(url).getHost().endsWith(domainName) |
| 91 | + ) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +from UriGetHostMethod um, MethodAccess uma, HostVerificationMethodAccess hma |
| 96 | +where hma.getQualifier() = uma and uma.getMethod() = um |
| 97 | +select hma, "Method has potentially $@ ", hma.getArgument(0), "improper URL verification" |
0 commit comments