How to enable Safe Search in my Android browser
Asked Answered
G

4

13

Requirement

I have requirement in my browser to enable/disable safe search while browsing.

On Google Safe Search Page

Block explicit results on Google using SafeSearch

You can filter explicit search results on Google, like pornography, with the SafeSearch setting. SafeSearch isn’t 100% accurate. But it can help you avoid explicit and inappropriate search results on your phone, tablet, or computer.

As you know when safe search is turned on (like google chrome), then user does not see results of many threat types, infected pages, social engineering pages, pornography etc. and block explicit images, videos, and websites from Google Search results

This is available on Google platforms like chrome, google app etc. So I got this task but can I enable it in my browser?

Resources Found

I got hints from safe search api, but I don't know if it is helpful for me, because if I use this, I can do two things,

  • Either I need to call api every-time user open a website (Lookup api)
  • Or I can download all url data. (Update api)

Problem

  1. How can I filter Google search results in my WebView? Is there some way provided by search engines like www.google.com , www.bing.com etc.
  2. If above is not possible, is only way to call safe browsing lookup api and perform blocking/ warning when malware, infection found?

Can any friend give me any hints, if it is possible?

Gaikwar answered 13/9, 2018 at 5:24 Comment(7)
Could you please tell me what is the minimum and maximum OS version you are using?Lubricate
@AndyDeveloper min=16 & max=28Gaikwar
AFAIK, it is not possible with below 27 OS version. But you can enable it from 27 version. For below 27 version you have to use Safe search api.Lubricate
@AndyDeveloper How can I enable it for 27? Perhaps I'll have some hints from that.Gaikwar
@Khemraj are you using webview for your browser right?Lubricate
Yes @AndyDeveloper, I am using webviewGaikwar
@Khemraj see my answer and I mentioned the developer link. I hope this will help you out.Lubricate
L
8

If you look into the developer site it is clearly mentioned that

If your app targets Android 7.1 (API level 25) or lower, you can opt your WebView objects out of checking URLs against Google Safe Browsing's list of unsafe websites by adding the following element to your app’s manifest file:

<manifest>
<application>
    <meta-data android:name="android.webkit.WebView.EnableSafeBrowsing"
               android:value="false" />
    ...
 </application>
</manifest>

For Android 8.0 it is clearly mentioned that

While the default value of EnableSafeBrowsing is true, there are occasional cases when you may want to only enable Safe Browsing conditionally or disable it. Android 8.0 (API level 26) and higher support using setSafeBrowsingEnabled(). Apps compiling at lower API levels cannot use setSafeBrowsingEnabled() and should change the value of EnableSafeBrowsing in the manifest to false to disable the feature for all instances of WebView.

If you target Android 8.1 (API level 27) or higher, you can define programmatically how your app responds to a known threat:

  • You can control whether your app reports known threats to Safe Browsing.
  • You can have your app automatically perform a particular action—such as going back to safety—each time it encounters a URL that's classified as a known threat.

Please look into the below sample code, it show how you can instruct your app's instances of WebView to always go back to safety after encountering a known threat:

MyWebActivity.java

private WebView mSuperSafeWebView;
private boolean mSafeBrowsingIsInitialized;

@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);

  mSuperSafeWebView = new WebView(this);
  mSuperSafeWebView.setWebViewClient(new MyWebViewClient());
  mSafeBrowsingIsInitialized = false;

  mSuperSafeWebView.startSafeBrowsing(this, new ValueCallback<Boolean>() {
    @Override
    public void onReceiveValue(Boolean success) {
        mSafeBrowsingIsInitialized = true;
        if (!success) {
            Log.e("MY_APP_TAG", "Unable to initialize Safe Browsing!");
          }
      }
   });
}

For enable or disable Safe Browsing. Use the following method.

mSuperSafeWebView.getSettings().setSafeBrowsingEnabled(true);

MyWebViewClient.java

public class MyWebViewClient extends WebViewClient {
   // Automatically go "back to safety" when attempting to load a website that
   // Google has identified as a known threat. An instance of WebView calls
   // this method only after Safe Browsing is initialized, so there's no
   // conditional logic needed here.
   @Override
   public void onSafeBrowsingHit(WebView view, WebResourceRequest request,
        int threatType, SafeBrowsingResponse callback) {
    // The "true" argument indicates that your app reports incidents like
    // this one to Safe Browsing.
    callback.backToSafety(true);
    Toast.makeText(view.getContext(), "Unsafe web page blocked.",
            Toast.LENGTH_LONG).show();
   }
}

Please take look If you want to know about the WebView security version by version.

Lubricate answered 20/9, 2018 at 6:22 Comment(11)
Does onSafeBrowsingHit() method is invoked when there is some infected website?Gaikwar
Yes. As you see MyWebViewClient.java, look into the comment it is clearly mentioned that "Automatically go "back to safety" when attempting to load a website that Google has identified as a known threat".Lubricate
But this will work for the individual pages, not safe search. Safe search feature filters the results of google.Gaikwar
Also, this feature mean, I have to implement safe search for <27 version, while I get this enabled default from version 27.Gaikwar
You can use this method to enable or disable the safe browsing "webView.getSettings().setSafeBrowsingEnabled(true);"Lubricate
No, no, I said I have to implement it for pre 27 versions. Right?Gaikwar
Yes you are right. You have to implement Safe search API for pre 27 versions.Lubricate
Have you any idea about problem 1?Gaikwar
Sorry, I have no idea about that. I will do some R&D on that and will let you know. :-)Lubricate
@Khemraj Please look into this, it will give you some idea about webview security.Lubricate
@AndyDeveloper Have you successfully implemented the Safe Browsing in Webview? The docs copied contain one fail. startSafeBrowsing() is a static method. I'm not able to get onSafeBrowsingHit() hitting on malicious URLs.Thickskinned
S
2

Add on to @Andy Developer answer, you can perform an Android version checking, if it is less than API 27, you could use the Safe Browsing API.

To ensure your browser the best user experience, I would suggest you to implement the Update API and setup a local database. This is the common approach for browsers you can find in Play Store nowadays.

From the Documentation:

Update API (v4)

The Update API lets your client applications download encrypted versions of the Safe Browsing lists for local, client-side checks of URLs.

The Update API is designed for clients that require high frequency, low-latency verdicts. Several web browsers and software platforms use this API to protect large sets of users.

If you are concerned about the privacy of the queried URLs or the latency induced by a network request, use the Update API.

By doing this you can offer this features for both lower end user and high end user.

Note: WebView have implemented Safe Browsing API in native, so it is just the same. If you want to implement Update API only instead of the answer proposed by @Andy Developer, it is still recommendable as it provides uniformity.

Spermatium answered 20/9, 2018 at 6:53 Comment(4)
thanks for answer, but does this tell anything else mentioned in question.Gaikwar
Yes, it is a suggestion on how you implement the Safe Search Browsing across the Android version you stated before. Of course no step by step coding here but giving you a guide/direction on how to apply in your appSpermatium
You are right Angus, by the way it is very typical to fetch data and write logics on data fetched from update api, so I'll not think to implement it. You can check documentation, Google says it is hard to implement. Also I was not stuck to choose among Lookup/Update api, I am stuck in problem 1.Gaikwar
what do you mean by filter here? Filter malicious website or ?Spermatium
S
1

I think Safe Browsing and Safe Search are two different topic. First one, is to ensure we access sites that are secured and not a malware. For example, converting an http request to https. Refer: https://developers.google.com/safe-browsing/ Second one, is to ensure that user don't access explicit content like pornography.

For Safe Browsing, we can use the API as mentioned above by enabling EnableSafeBrowsing in manifest.

But, I am not sure that will help in dealing with the Safe search case.

Swann answered 27/2, 2021 at 7:44 Comment(0)
P
0

Definitely we can use the Googles Safe Browsing API for this requirement, the purpose of this APIs is just for that.

So, the scenario can be one like this:
  • we could have an URL Checker engine in background
  • it uses a queue which contains the URLs to be checked and uses Googles Safe Browsing API to check
  • the results could be call-backed to the main thread to safe the browsing data

Good luck )

Phthalocyanine answered 20/9, 2018 at 6:7 Comment(1)
Can you please show an example, how can I filter google results inside webview. I also have this scenario in my mind, but I don't have knowledge how to execute it.Gaikwar

© 2022 - 2024 — McMap. All rights reserved.