Skip to content

Commit 6cb26d5

Browse files
Merge pull request #10241 from joefarebrother/android-webview-dubugging
Java: Add query for WebView debugging enabled
2 parents 22946b1 + af41f2b commit 6cb26d5

File tree

17 files changed

+143
-1
lines changed

17 files changed

+143
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/** Definitions for the Android Webview Debugging Enabled query */
2+
3+
import java
4+
import semmle.code.java.dataflow.DataFlow
5+
import semmle.code.java.controlflow.Guards
6+
import semmle.code.java.security.SecurityTests
7+
8+
/** Holds if `ex` looks like a check that this is a debug build. */
9+
private predicate isDebugCheck(Expr ex) {
10+
exists(Expr subex, string debug |
11+
debug.toLowerCase().matches(["%debug%", "%test%"]) and
12+
subex.getParent*() = ex
13+
|
14+
subex.(VarAccess).getVariable().getName() = debug
15+
or
16+
subex.(MethodAccess).getMethod().hasName("getProperty") and
17+
subex.(MethodAccess).getAnArgument().(CompileTimeConstantExpr).getStringValue() = debug
18+
)
19+
}
20+
21+
/** A configuration to find instances of `setWebContentDebuggingEnabled` called with `true` values. */
22+
class WebviewDebugEnabledConfig extends DataFlow::Configuration {
23+
WebviewDebugEnabledConfig() { this = "WebviewDebugEnabledConfig" }
24+
25+
override predicate isSource(DataFlow::Node node) {
26+
node.asExpr().(BooleanLiteral).getBooleanValue() = true
27+
}
28+
29+
override predicate isSink(DataFlow::Node node) {
30+
exists(MethodAccess ma |
31+
ma.getMethod().hasQualifiedName("android.webkit", "WebView", "setWebContentsDebuggingEnabled") and
32+
node.asExpr() = ma.getArgument(0)
33+
)
34+
}
35+
36+
override predicate isBarrier(DataFlow::Node node) {
37+
exists(Guard debug | isDebugCheck(debug) and debug.controls(node.asExpr().getBasicBlock(), _))
38+
or
39+
node.getEnclosingCallable().getDeclaringType() instanceof NonSecurityTestClass
40+
}
41+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// BAD - debugging is always enabled
2+
WebView.setWebContentsDebuggingEnabled(true);
3+
4+
// GOOD - debugging is only enabled when this is a debug build, as indicated by the debuggable flag being set.
5+
if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) {
6+
WebView.setWebContentsDebuggingEnabled(true);
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>The <code>WebView.setWebContentsDebuggingEnabled</code> method enables or disables the contents of any <code>WebView</code> in the application to be debugged.</p>
8+
9+
<p>You should only enable debugging features during development. When you create a production build, you should disable it. If you enable debugging features, this can make your code vulnerable by adding entry points, or leaking sensitive information.</p>
10+
</overview>
11+
<recommendation>
12+
<p>Ensure that debugging features are not enabled in production builds, such as by guarding calls to <code>WebView.setWebContentsDebuggingEnabled(true)</code> by a flag that is only enabled in debug builds. </p>
13+
14+
</recommendation>
15+
<example>
16+
17+
<p>In the first (bad) example, WebView debugging is always enabled.
18+
whereas the GOOD case only enables it if the <code>android:debuggable</code> attribute is set to <code>true</code>.</p>
19+
20+
<sample src="WebviewDebuggingEnabled.java" />
21+
22+
</example>
23+
<references>
24+
25+
<li>
26+
Android Developers:
27+
<a href="https://developer.android.com/reference/android/webkit/WebView.html#setWebContentsDebuggingEnabled(boolean)">setWebContentsDebuggingEnabled</a>.
28+
</li>
29+
30+
<li>
31+
Android Developers:
32+
<a href="https://developer.chrome.com/docs/devtools/remote-debugging/webviews/">Remote debugging WebViews</a>.
33+
</li>
34+
35+
</references>
36+
</qhelp>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @name Android Webview debugging enabled
3+
* @description Enabling Webview debugging in production builds can expose entry points or leak sensitive information.
4+
* @kind path-problem
5+
* @problem.severity warning
6+
* @security-severity 7.2
7+
* @id java/android/webview-debugging-enabled
8+
* @tags security
9+
* external/cwe/cwe-489
10+
* @precision high
11+
*/
12+
13+
import java
14+
import semmle.code.java.security.WebviewDubuggingEnabledQuery
15+
import DataFlow::PathGraph
16+
17+
from WebviewDebugEnabledConfig conf, DataFlow::PathNode source, DataFlow::PathNode sink
18+
where conf.hasFlowPath(source, sink)
19+
select sink, source, sink, "Webview debugging is enabled."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: newQuery
3+
---
4+
* Added a new query, `java/android/webview-debugging-enabled`, to detect instances of WebView debugging being enabled in production builds.

0 commit comments

Comments
 (0)