Skip to content

Instantly share code, notes, and snippets.

@mrhungnq95
Forked from scottyab/SaferWebViewClient.java
Created November 10, 2022 08:08
Show Gist options
  • Select an option

  • Save mrhungnq95/f55fa3d931b0673adca1d74483e37f37 to your computer and use it in GitHub Desktop.

Select an option

Save mrhungnq95/f55fa3d931b0673adca1d74483e37f37 to your computer and use it in GitHub Desktop.

Revisions

  1. @scottyab scottyab created this gist May 14, 2014.
    48 changes: 48 additions & 0 deletions SaferWebViewClient.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    /**
    * Implements whitelisting on host name
    */
    public class SaferWebViewClient extends WebViewClient {

    private String[] hostsWhitelist;

    public SaferWebViewClient(String hostsWhitelsit){
    super();
    this.hostsWhitelist = hostsWhitelist;
    }

    @Override
    public WebResourceResponse shouldInterceptRequest(final WebView view, String url) {
    if (isValidHost(url)) {
    return super.shouldInterceptRequest(view, url);
    } else {
    return getWebResourceResponseFromString();
    }
    }

    private WebResourceResponse getWebResourceResponseFromString() {
    return getUtf8EncodedWebResourceResponse(new StringBufferInputStream("alert('!NO!')"));
    }

    private WebResourceResponse getUtf8EncodedWebResourceResponse(InputStream data) {
    return new WebResourceResponse("text/css", "UTF-8", data);
    }


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return isValidHost(url);
    }

    private boolean isValidHost(String url){
    if (!TextUtils.isEmpty(url)) {
    final String host = Uri.parse(url).getHost();
    for (String whitelistedHost: hostsWhitelist){
    if (whitelistedHost.equalsIgnoreCase(host)){
    return true;
    }
    }
    }
    return false;
    }

    }
    20 changes: 20 additions & 0 deletions WebviewUtil.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@

    public class Util {
    public static void disableRiskySettings(WebView webView){

    //javascript could be a vector to exploit your applications
    webView.getSettings().setJavaScriptEnabled(false);

    //default is off, but just in case. plugins could be a vector to exploit your applications process
    webView.getSettings().setPluginState(WebSettings.PluginState.OFF);

    //Should an attacker somehow find themselves in a position to inject script into a WebView, then they could exploit the opportunity to access local resources. This can be somewhat prevented by disabling local file system access. It is enabled by default. The Android WebSettings class can be used to disable local file system access via the public method setAllowFileAccess.
    //This restricts the WebView to loading local resources from file:///android_asset (assets) and file:///android_res (resources).
    webView.getSettings().setAllowFileAccess(false);

    //disable Geolocation API
    webView.getSettings().setGeolocationEnabled(false);

    }

    }