Private DNS: Extract host verification code

Since Private DNS host may now be set by the admin via the
DevicePolicyManager, extract the verification code to the
ConnectivityManager so it can be used both from Settings and framework
code.

Bug: 112982691
Test: See cts tests.
Change-Id: Ic091e4714f8d3f9f28042bc825cf57c991504f27
This commit is contained in:
Eran Messeri
2018-09-20 15:15:57 +01:00
parent d69be7df8d
commit 7ba739ba14

View File

@@ -27,6 +27,7 @@ import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.net.NetworkUtils;
import android.provider.Settings; import android.provider.Settings;
import android.system.Os; import android.system.Os;
import android.text.Editable; import android.text.Editable;
@@ -72,8 +73,6 @@ public class PrivateDnsModeDialogPreference extends CustomDialogPreferenceCompat
PRIVATE_DNS_MAP.put(PRIVATE_DNS_MODE_PROVIDER_HOSTNAME, R.id.private_dns_mode_provider); PRIVATE_DNS_MAP.put(PRIVATE_DNS_MODE_PROVIDER_HOSTNAME, R.id.private_dns_mode_provider);
} }
private static final int[] ADDRESS_FAMILIES = new int[] {AF_INET, AF_INET6};
@VisibleForTesting @VisibleForTesting
static final String MODE_KEY = Settings.Global.PRIVATE_DNS_MODE; static final String MODE_KEY = Settings.Global.PRIVATE_DNS_MODE;
@VisibleForTesting @VisibleForTesting
@@ -203,23 +202,6 @@ public class PrivateDnsModeDialogPreference extends CustomDialogPreferenceCompat
updateDialogInfo(); updateDialogInfo();
} }
private boolean isWeaklyValidatedHostname(String hostname) {
// TODO(b/34953048): Use a validation method that permits more accurate,
// but still inexpensive, checking of likely valid DNS hostnames.
final String WEAK_HOSTNAME_REGEX = "^[a-zA-Z0-9_.-]+$";
if (!hostname.matches(WEAK_HOSTNAME_REGEX)) {
return false;
}
for (int address_family : ADDRESS_FAMILIES) {
if (Os.inet_pton(address_family, hostname) != null) {
return false;
}
}
return true;
}
private Button getSaveButton() { private Button getSaveButton() {
final AlertDialog dialog = (AlertDialog) getDialog(); final AlertDialog dialog = (AlertDialog) getDialog();
if (dialog == null) { if (dialog == null) {
@@ -236,7 +218,7 @@ public class PrivateDnsModeDialogPreference extends CustomDialogPreferenceCompat
final Button saveButton = getSaveButton(); final Button saveButton = getSaveButton();
if (saveButton != null) { if (saveButton != null) {
saveButton.setEnabled(modeProvider saveButton.setEnabled(modeProvider
? isWeaklyValidatedHostname(mEditText.getText().toString()) ? NetworkUtils.isWeaklyValidatedHostname(mEditText.getText().toString())
: true); : true);
} }
} }