Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Fixes for WebRTC IP leak issue #8565

Merged
merged 4 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions android/brave_java_resources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ brave_java_resources = [
"java/res/layout/brave_sync_done.xml",
"java/res/layout/brave_sync_layout.xml",
"java/res/layout/brave_toolbar.xml",
"java/res/layout/brave_webrtc_policy_preference.xml",
"java/res/layout/bre_bottom_banner.xml",
"java/res/layout/crypto_widget_layout.xml",
"java/res/layout/ddg_offer_layout.xml",
Expand Down Expand Up @@ -774,6 +775,7 @@ brave_java_resources = [
"java/res/xml/brave_rewards_preferences.xml",
"java/res/xml/brave_stats_preferences.xml",
"java/res/xml/brave_theme_preferences.xml",
"java/res/xml/brave_webrtc_policy_preferences.xml",
"java/res/xml/clear_browsing_data_preferences_tab.xml",
"java/res/xml/closing_all_tabs_closes_brave_preference.xml",
"java/res/xml/desktop_mode_preferences.xml",
Expand Down
2 changes: 2 additions & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/settings/BraveSettingsLauncherImpl.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/BraveStandardSearchEnginePreference.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/BraveStatsPreferences.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/BraveWebrtcPolicyPreference.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/BraveWebrtcPolicyPreferencesFragment.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/ClosingAllTabsClosesBravePreference.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/SearchEngineTabModelSelectorObserver.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/developer/BraveQAPreferences.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ public int getENSResolveMethod() {
return BravePrefServiceBridgeJni.get().getENSResolveMethod();
}

public void setWebrtcPolicy(int policy) {
BravePrefServiceBridgeJni.get().setWebrtcPolicy(policy);
}

public int getWebrtcPolicy() {
return BravePrefServiceBridgeJni.get().getWebrtcPolicy();
}

@NativeMethods
interface Natives {
void setHTTPSEEnabled(boolean enabled);
Expand Down Expand Up @@ -275,5 +283,8 @@ interface Natives {
void setENSResolveMethod(int method);
int getUnstoppableDomainsResolveMethod();
int getENSResolveMethod();

void setWebrtcPolicy(int policy);
int getWebrtcPolicy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import org.chromium.chrome.browser.preferences.Pref;
import org.chromium.chrome.browser.privacy.settings.PrivacySettings;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.settings.BraveWebrtcPolicyPreference;
import org.chromium.chrome.browser.settings.ChromeManagedPreferenceDelegate;
import org.chromium.components.browser_ui.settings.ChromeBaseCheckBoxPreference;
import org.chromium.components.browser_ui.settings.ChromeBasePreference;
import org.chromium.components.browser_ui.settings.ChromeSwitchPreference;
import org.chromium.components.browser_ui.settings.SettingsUtils;
import org.chromium.components.prefs.PrefService;
Expand All @@ -43,6 +45,7 @@ public class BravePrivacySettings extends PrivacySettings {
private static final String PREF_SOCIAL_BLOCKING_TWITTER = "social_blocking_twitter";
private static final String PREF_SOCIAL_BLOCKING_LINKEDIN = "social_blocking_linkedin";
private static final String PREF_DO_NOT_TRACK = "do_not_track";
private static final String PREF_WEBRTC_POLICY = "webrtc_policy";

private final PrefService mPrefServiceBridge = UserPrefs.get(Profile.getLastUsedRegularProfile());
private final ChromeManagedPreferenceDelegate mManagedPreferenceDelegate =
Expand All @@ -61,6 +64,7 @@ public class BravePrivacySettings extends PrivacySettings {
private ChromeSwitchPreference mSocialBlockingFacebook;
private ChromeSwitchPreference mSocialBlockingTwitter;
private ChromeSwitchPreference mSocialBlockingLinkedin;
private ChromeBasePreference mWebrtcPolicy;

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Expand Down Expand Up @@ -114,6 +118,8 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
mSocialBlockingLinkedin = (ChromeSwitchPreference) findPreference(PREF_SOCIAL_BLOCKING_LINKEDIN);
mSocialBlockingLinkedin.setOnPreferenceChangeListener(this);

mWebrtcPolicy = (ChromeBasePreference) findPreference(PREF_WEBRTC_POLICY);

updatePreferences();
}

Expand Down Expand Up @@ -184,6 +190,9 @@ private void updatePreferences() {
mAdBlockPref.setOrder(++order);
mFingerprintingProtectionPref.setOrder(++order);
mSearchSuggestions.setOrder(++order);
mWebrtcPolicy.setOrder(++order);
mWebrtcPolicy.setSummary(
webrtcPolicyToString(BravePrefServiceBridge.getInstance().getWebrtcPolicy()));
mAutocompleteTopSites
.setChecked(UserPrefs.get(Profile.getLastUsedRegularProfile()).getBoolean(BravePref.TOP_SITE_SUGGESTIONS_ENABLED));
mAutocompleteTopSites.setOrder(++order);
Expand All @@ -209,4 +218,23 @@ private ChromeManagedPreferenceDelegate createManagedPreferenceDelegate() {
return false;
};
}

private String webrtcPolicyToString(@BraveWebrtcPolicyPreference.WebrtcPolicy int policy) {
switch (policy) {
case BraveWebrtcPolicyPreference.WebrtcPolicy.DEFAULT:
return getActivity().getResources().getString(
R.string.settings_webrtc_policy_default);
case BraveWebrtcPolicyPreference.WebrtcPolicy.DEFAULT_PUBLIC_AND_PRIVATE_INTERFACES:
return getActivity().getResources().getString(
R.string.settings_webrtc_policy_default_public_and_private_interfaces);
case BraveWebrtcPolicyPreference.WebrtcPolicy.DEFAULT_PUBLIC_INTERFACE_ONLY:
return getActivity().getResources().getString(
R.string.settings_webrtc_policy_default_public_interface_only);
case BraveWebrtcPolicyPreference.WebrtcPolicy.DISABLE_NON_PROXIED_UDP:
return getActivity().getResources().getString(
R.string.settings_webrtc_policy_disable_non_proxied_udp);
}
assert false : "Setting is out of range!";
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser.settings;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RadioGroup;

import androidx.annotation.IntDef;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;

import org.chromium.chrome.R;
import org.chromium.components.browser_ui.widget.RadioButtonWithDescription;
import org.chromium.components.browser_ui.widget.RadioButtonWithDescriptionLayout;

import java.util.ArrayList;
import java.util.Collections;

public class BraveWebrtcPolicyPreference
extends Preference implements RadioGroup.OnCheckedChangeListener {
@IntDef({WebrtcPolicy.DEFAULT, WebrtcPolicy.DEFAULT_PUBLIC_AND_PRIVATE_INTERFACES,
WebrtcPolicy.DEFAULT_PUBLIC_INTERFACE_ONLY, WebrtcPolicy.DISABLE_NON_PROXIED_UDP})
public @interface WebrtcPolicy {
int DEFAULT = 0;
int DEFAULT_PUBLIC_AND_PRIVATE_INTERFACES = 1;
int DEFAULT_PUBLIC_INTERFACE_ONLY = 2;
int DISABLE_NON_PROXIED_UDP = 3;

int NUM_ENTRIES = 4;
}

private @WebrtcPolicy int mSetting;
private RadioButtonWithDescription mSettingRadioButton;
private RadioButtonWithDescriptionLayout mGroup;
private ArrayList<RadioButtonWithDescription> mButtons;

public BraveWebrtcPolicyPreference(Context context, AttributeSet attrs) {
super(context, attrs);

setLayoutResource(R.layout.brave_webrtc_policy_preference);

mButtons = new ArrayList<>(Collections.nCopies(WebrtcPolicy.NUM_ENTRIES, null));
}

public void initialize(@WebrtcPolicy int policy) {
mSetting = policy;
}

@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);

mGroup = (RadioButtonWithDescriptionLayout) holder.findViewById(R.id.radio_button_layout);
mGroup.setOnCheckedChangeListener(this);

mButtons.set(WebrtcPolicy.DEFAULT,
(RadioButtonWithDescription) holder.findViewById(R.id.webrtc_policy_default));
mButtons.set(WebrtcPolicy.DEFAULT_PUBLIC_AND_PRIVATE_INTERFACES,
(RadioButtonWithDescription) holder.findViewById(
R.id.webrtc_policy_default_public_and_private_interfaces));
mButtons.set(WebrtcPolicy.DEFAULT_PUBLIC_INTERFACE_ONLY,
(RadioButtonWithDescription) holder.findViewById(
R.id.webrtc_policy_default_public_interface_only));
mButtons.set(WebrtcPolicy.DISABLE_NON_PROXIED_UDP,
(RadioButtonWithDescription) holder.findViewById(
R.id.webrtc_policy_disable_non_proxied_udp));

mSettingRadioButton = mButtons.get(mSetting);
mSettingRadioButton.setChecked(true);
}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for (int i = 0; i < WebrtcPolicy.NUM_ENTRIES; i++) {
if (mButtons.get(i).isChecked()) {
mSetting = i;
mSettingRadioButton = mButtons.get(i);
break;
}
}
assert mSetting >= 0 && mSetting < WebrtcPolicy.NUM_ENTRIES : "No matching setting found.";

callChangeListener(mSetting);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser.settings;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;

import org.chromium.chrome.R;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.components.browser_ui.settings.SettingsUtils;
import org.chromium.ui.UiUtils;

/**
* Fragment to manage webrtc policy settings.
*/
public class BraveWebrtcPolicyPreferencesFragment extends BravePreferenceFragment {
static final String PREF_WEBRTC_POLICY = "webrtc_policy";

@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, String rootKey) {
SettingsUtils.addPreferencesFromResource(this, R.xml.brave_webrtc_policy_preferences);
getActivity().setTitle(R.string.settings_webrtc_policy_label);

BraveWebrtcPolicyPreference webrtcPolicyPreference =
(BraveWebrtcPolicyPreference) findPreference(PREF_WEBRTC_POLICY);
webrtcPolicyPreference.initialize(BravePrefServiceBridge.getInstance().getWebrtcPolicy());

webrtcPolicyPreference.setOnPreferenceChangeListener((preference, newValue) -> {
BravePrefServiceBridge.getInstance().setWebrtcPolicy((int) newValue);
return true;
});
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1) {
UiUtils.setNavigationBarIconColor(getActivity().getWindow().getDecorView(),
getResources().getBoolean(R.bool.window_light_navigation_bar));
}

setDivider(null);
}
}
54 changes: 54 additions & 0 deletions android/java/res/layout/brave_webrtc_policy_preference.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2021 The Brave Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="false">

<org.chromium.components.browser_ui.widget.RadioButtonWithDescriptionLayout
android:id="@+id/radio_button_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<org.chromium.components.browser_ui.widget.RadioButtonWithDescription
android:id="@+id/webrtc_policy_default"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:primaryText="@string/settings_webrtc_policy_default" />

<org.chromium.components.browser_ui.widget.RadioButtonWithDescription
android:id="@+id/webrtc_policy_default_public_and_private_interfaces"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/min_touch_target_size"
android:paddingTop="8dp"
android:paddingBottom="8dp"
app:primaryText="@string/settings_webrtc_policy_default_public_and_private_interfaces" />

<org.chromium.components.browser_ui.widget.RadioButtonWithDescription
android:id="@+id/webrtc_policy_default_public_interface_only"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/min_touch_target_size"
android:paddingTop="8dp"
android:paddingBottom="8dp"
app:primaryText="@string/settings_webrtc_policy_default_public_interface_only" />

<org.chromium.components.browser_ui.widget.RadioButtonWithDescription
android:id="@+id/webrtc_policy_disable_non_proxied_udp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/min_touch_target_size"
android:paddingTop="8dp"
android:paddingBottom="8dp"
app:primaryText="@string/settings_webrtc_policy_disable_non_proxied_udp" />

</org.chromium.components.browser_ui.widget.RadioButtonWithDescriptionLayout>

</LinearLayout>
5 changes: 5 additions & 0 deletions android/java/res/xml/brave_privacy_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
android:key="autocomplete_brave_suggested_sites"
android:title="@string/autocomplete_brave_suggested_sites_title"
android:persistent="false"/>
<org.chromium.components.browser_ui.settings.ChromeBasePreference
android:fragment="org.chromium.chrome.browser.settings.BraveWebrtcPolicyPreferencesFragment"
android:key="webrtc_policy"
android:title="@string/settings_webrtc_policy_label"
android:persistent="false"/>
<PreferenceCategory
android:key="brave_shields_social_blocking"
android:title="@string/brave_shields_social_blocking_title">
Expand Down
9 changes: 9 additions & 0 deletions android/java/res/xml/brave_webrtc_policy_preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2021 The Brave Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<org.chromium.chrome.browser.settings.BraveWebrtcPolicyPreference
android:key="webrtc_policy" />
</PreferenceScreen>
Loading