harden WebView settings

This commit is contained in:
B. Petersen
2021-01-29 00:08:34 +01:00
committed by bjoern
parent 74cdd85815
commit f593f27d98
2 changed files with 37 additions and 3 deletions
@@ -3,6 +3,7 @@ package org.thoughtcrime.securesms;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import androidx.appcompat.app.AlertDialog;
@@ -37,11 +38,23 @@ public class FullMsgActivity extends WebViewActivity
loadRemoteContent = Prefs.getAlwaysLoadRemoteContent(this);
webView.getSettings().setBlockNetworkLoads(!loadRemoteContent);
// setBuiltInZoomControls() adds pinch-to-zoom as well as two ugly buttons;
// the latter are hidden with setDisplayZoomControls() again.
// setBuiltInZoomControls() adds pinch-to-zoom as well as two on-screen zoom control buttons.
// The latter are a bit annoying, however, they are deprecated anyway,
// and also Android docs recommend to disable them with setDisplayZoomControls().
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
// disable useless and unwanted features:
// - JavaScript and Plugins are disabled by default, however,
// doing it explicitly here protects against changed base classes or bugs
// - Content- and File-access is enabled by default and disabled here
// - the other setAllow*() functions are related to enabled JavaScript only
// - "safe browsing" comes with privacy issues and already disabled in the base class
webView.getSettings().setJavaScriptEnabled(false);
webView.getSettings().setPluginState(WebSettings.PluginState.OFF);
webView.getSettings().setAllowContentAccess(false);
webView.getSettings().setAllowFileAccess(false);
getSupportActionBar().setTitle(getString(R.string.chat_input_placeholder));
dcContext = DcHelper.getContext(this);
@@ -3,6 +3,7 @@ package org.thoughtcrime.securesms;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
@@ -41,18 +42,38 @@ public class WebViewActivity extends PassphraseRequiredActionBarActivity
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
// `shouldOverrideUrlLoading()` is called when the user clicks a URL,
// returning `true` means, the URL is passed to `loadUrl()`, `false` aborts loading.
// the method is not called for POST request nor for on-page-links.
//
// nb: from API 24, `shouldOverrideUrlLoading(String)` is deprecated and
// `shouldOverrideUrlLoading(WebResourceRequest)` shall be used.
// the new one has the same functionality, and the old one still exist,
// so, to support all systems, for now, using the old one seems to be the simplest way.
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null) {
if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("mailto:")) {
openOnlineUrl(url);
// URL opened externally, returning `true` causes the WebView to abort loading
return true;
}
}
return false;
// by returning `true`, we also abort loading other URLs in our WebView;
// eg. that might be weird or internal protocols.
// if we come over really useful things, we should allow that explicitly.
return true;
}
});
webView.setFindListener(this);
// disable "safe browsing" as this has privacy issues,
// eg. at least false positives are sent to the "Safe Browsing Lookup API".
// as all URLs opened in the WebView are local anyway,
// "safe browsing" will never be able to report issues, so it can be disabled.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
webView.getSettings().setSafeBrowsingEnabled(false);
}
}
@Override