The following sample demonstrates how a WebViewClient is configured to intercept URL loading in a WebView. The shouldOverrideUrlLoading method is implemented to handle navigation requests, which overrides the default behavior of opening links in the default browser.
packageorg.owasp.mastestappimportandroid.content.Contextimportandroid.util.Logimportandroid.webkit.WebResourceRequestimportandroid.webkit.WebViewimportandroid.webkit.WebViewClient// SUMMARY: This sample demonstrates a WebView with a custom WebViewClient that intercepts URL loading without proper validation.classMastgTestWebView(privatevalcontext:Context){funmastgTest(webView:WebView):String{// Configure WebView settingswebView.settings.apply{javaScriptEnabled=true}// FAIL: [MASTG-TEST-0398] Custom WebViewClient intercepts URL loading without proper validationwebView.webViewClient=object:WebViewClient(){overridefunshouldOverrideUrlLoading(view:WebView?,request:WebResourceRequest?):Boolean{valurl=request?.url?.toString()// No URL validation is performed - any URL will be loadedLog.d("MastgTest","Loading URL: $url")returnfalse// Allow the WebView to load the URL}overridefunshouldInterceptRequest(view:WebView?,request:WebResourceRequest?):android.webkit.WebResourceResponse?{valurl=request?.url?.toString()Log.d("MastgTest","Intercepting request: $url")// No validation - allow all requestsreturnsuper.shouldInterceptRequest(view,request)}}// Load a trusted page initiallywebView.loadUrl("https://mas.owasp.org/")return"WebView configured with custom URL handling"}}
The test fails because the WebView can load content from any host the user is directed to. The static rule flags the attack surface: the reported finding points to the setWebViewClient call and the custom WebViewClient it registers, which spans both overridden handlers. Using a WebViewClient is not insecure on its own, so as required by References to WebViewClient URL Loading Handlers we inspect each reported handler to confirm whether it restricts navigation to trusted content.
Reviewing the reported code shows that neither handler performs any validation:
setWebViewClient: The WebView is configured with the custom WebViewClient.
shouldOverrideUrlLoading: The implementation reads only request.getUrl().toString() to log the URL and always returns false. It never calls getHost, getScheme, or getPath, nor compares the URL against any allowlist or denylist, so every URL is allowed to load regardless of its host.
shouldInterceptRequest: The implementation likewise only logs request.getUrl().toString() and falls back to super.shouldInterceptRequest, with no host, scheme, or path inspection.
Because the handlers inspect only the full URL string for logging and make no host-, scheme-, or path-based decision, they impose no restriction on navigation, and the WebView can load content from any host the user is directed to. The runtime counterpart of this conclusion is demonstrated in Runtime Use of WebViewClient URL Loading Handlers with Frida.