MASTG-DEMO-0097: Sensitive Data and Functionality Exposed Through WebView JavaScript Bridges
Download MASTG-DEMO-0097 APK Open MASTG-DEMO-0097 Folder Build MASTG-DEMO-0097 APK
Sample¶
The following demo demonstrates a WebView component rendering a "Contact Support" form. A native bridge class, SupportBridge, is registered via addJavascriptInterface() and exposes three methods to JavaScript: one that reads the user's name, email, and JWT from SharedPreferences and returns them as JSON, one that submits a support message, and one that writes a preference value back to SharedPreferences. JavaScript is explicitly enabled on the WebView, which means any JavaScript running in the page (including injected scripts) can call these bridge methods directly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
Some payloads that could be executed by an attacker who can run JavaScript in this page include:
<img src=x onerror='document.getElementById("resultText").innerText = MASBridge.getUserProfileJson()'><img src=x onerror='MASBridge.updateSupportPreference("malicious value")'>
Steps¶
Let's run our semgrep rule against the reversed Java code.
| ../../../../rules/mastg-android-webview-bridges.yml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
| run.sh | |
|---|---|
1 | |
Observation¶
The rule detected the SupportBridge class with three methods annotated with @JavascriptInterface, as well as the addJavascriptInterface() call that registers the bridge on a WebView with JavaScript enabled.
| output.txt | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
Evaluation¶
After reviewing the decompiled code at the locations reported in the output, we can conclude that the test fails because:
- JavaScript is enabled on the
WebViewand a native bridge (SupportBridge) is registered viaaddJavascriptInterface()(line 85). - There are three sensitive methods annotated with
@JavascriptInterfacethat are reachable from JavaScript running in the page, without any origin restrictions or other mitigations:getUserProfileJson()(line 37) reads the user's name, email, and a JWT fromSharedPreferencesand returns them to JavaScript as a JSON string, exposing PII and a credential.submitSupportMessage(email, message)(line 58) allows JavaScript to trigger a support-message submission on behalf of the user, affecting integrity.updateSupportPreference(value)(line 71) allows JavaScript to write arbitrary values toSharedPreferences, affecting app integrity.
Two remediation paths exist for this issue:
- One is to prevent attacker controlled input from being rendered as executable HTML or JavaScript in the
WebView. This is intentionally left vulnerable in the demo to simulate an XSS style condition, but in a real app the JavaScript execution primitive could come from many other causes, not only obvious reflected input. - The other is to redesign the native bridge by removing the legacy
addJavascriptInterface()model and using a message based bridge with explicit origin restrictions, such asWebViewCompat.addWebMessageListener()with a narrowallowedOriginRulesallowlist and sender validation. In this demo, that bridge redesign alone would not visibly stop the exploit because the injected script executes in the same trusted main page origin, so it would still satisfy the origin policy. Even so, it remains an important hardening measure, and it becomes effective when attacker controlled JavaScript runs from a different origin or untrusted frame, which is the broader class of risk associated with legacy JavaScript bridges.