Skip to content

Commit c54e93f

Browse files
authored
Merge pull request github#13705 from atorralba/atorralba/java/android-unsafe-fetch-apply
Java: Add support for Kotlin's `apply` to java/android/unsafe-android-wevbiew-fetch
2 parents 8e85f4d + ce60036 commit c54e93f

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

java/ql/lib/ext/android.webkit.model.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ extensions:
33
pack: codeql/java-all
44
extensible: sourceModel
55
data:
6-
- ["android.webkit", "WebView", False, "getOriginalUrl", "()", "", "ReturnValue", "remote", "manual"]
7-
- ["android.webkit", "WebView", False, "getUrl", "()", "", "ReturnValue", "remote", "manual"]
6+
- ["android.webkit", "WebView", True, "getOriginalUrl", "()", "", "ReturnValue", "remote", "manual"]
7+
- ["android.webkit", "WebView", True, "getUrl", "()", "", "ReturnValue", "remote", "manual"]
88
- addsTo:
99
pack: codeql/java-all
1010
extensible: sinkModel
1111
data:
1212
# Models representing methods susceptible to XSS attacks.
13-
- ["android.webkit", "WebView", False, "evaluateJavascript", "", "", "Argument[0]", "js-injection", "manual"]
14-
- ["android.webkit", "WebView", False, "loadData", "", "", "Argument[0]", "html-injection", "manual"]
15-
- ["android.webkit", "WebView", False, "loadDataWithBaseURL", "", "", "Argument[1]", "html-injection", "manual"]
13+
- ["android.webkit", "WebView", True, "evaluateJavascript", "", "", "Argument[0]", "js-injection", "manual"]
14+
- ["android.webkit", "WebView", True, "loadData", "", "", "Argument[0]", "html-injection", "manual"]
15+
- ["android.webkit", "WebView", True, "loadDataWithBaseURL", "", "", "Argument[1]", "html-injection", "manual"]

java/ql/lib/semmle/code/java/security/UnsafeAndroidAccess.qll

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java
66
private import semmle.code.java.dataflow.DataFlow
77
private import semmle.code.java.frameworks.android.WebView
8+
private import semmle.code.java.frameworks.kotlin.Kotlin
89

910
/**
1011
* A sink that represents a method that fetches a web resource in Android.
@@ -62,10 +63,26 @@ private class WebViewRef extends Element {
6263
t.isOwnInstanceAccess() or t.getInstanceAccess().isEnclosingInstanceAccess(this)
6364
)
6465
or
65-
result = DataFlow::exprNode(this.(Variable).getAnAccess())
66+
exists(Variable v | result.asExpr() = v.getAnAccess() |
67+
v = this
68+
or
69+
applyReceiverVariable(this, v)
70+
)
6671
}
6772
}
6873

74+
/**
75+
* Holds if `p` is the lambda parameter that holds the receiver of an `apply` expression in Kotlin,
76+
* and `v` is the variable of the receiver in the outer scope.
77+
*/
78+
private predicate applyReceiverVariable(Parameter p, Variable v) {
79+
exists(LambdaExpr lambda, KotlinApply apply |
80+
p.getCallable() = lambda.asMethod() and
81+
lambda = apply.getLambdaArg() and
82+
v = apply.getReceiver().(VarAccess).getVariable()
83+
)
84+
}
85+
6986
/**
7087
* Holds if a `WebViewLoadUrlMethod` is called on an access of `webview`
7188
* with `urlArg` as its first argument.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The query "Unsafe resource fetching in Android WebView" (`java/android/unsafe-android-webview-fetch`) now recognizes WebViews where `setJavascriptEnabled`, `setAllowFileAccess`, `setAllowUniversalAccessFromFileURLs`, and/or `setAllowFileAccessFromFileURLs` are set inside the function block of the Kotlin `apply` function.

java/ql/test/query-tests/security/CWE-749/UnsafeActivityKt.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ import android.webkit.WebViewClient
99
class UnsafeActivityKt : Activity() {
1010
override fun onCreate(savedInstanceState : Bundle) {
1111

12+
val src : String = intent.extras.getString("url")
13+
1214
val wv = findViewById<WebView>(-1)
1315
// Implicit not-nulls happening here
1416
wv.settings.setJavaScriptEnabled(true)
1517
wv.settings.setAllowFileAccessFromFileURLs(true)
1618

17-
val thisUrl : String = intent.extras.getString("url")
18-
wv.loadUrl(thisUrl) // $ hasUnsafeAndroidAccess
19+
wv.loadUrl(src) // $ hasUnsafeAndroidAccess
20+
21+
val wv2 = findViewById<WebView>(-1)
22+
wv2.apply {
23+
settings.setJavaScriptEnabled(true)
24+
}
25+
wv2.loadUrl(src) // $ hasUnsafeAndroidAccess
1926
}
2027
}

0 commit comments

Comments
 (0)