Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 20220cdb authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add validators for all Settings.Secure settings that are backed up"

parents 57ebb60d 964943ab
Loading
Loading
Loading
Loading
+500 −13

File changed.

Preview size limit exceeded, changes collapsed.

+80 −0
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import android.net.Uri;

import com.android.internal.util.ArrayUtils;

import java.util.Locale;

/**
 * This class provides both interface for validation and common validators
 * used to ensure Settings have meaningful values.
@@ -50,6 +52,18 @@ public class SettingsValidators {
        }
    };

    public static final Validator ANY_INTEGER_VALIDATOR = new Validator() {
        @Override
        public boolean validate(String value) {
            try {
                Integer.parseInt(value);
                return true;
            } catch (NumberFormatException e) {
                return false;
            }
        }
    };

    public static final Validator URI_VALIDATOR = new Validator() {
        @Override
        public boolean validate(String value) {
@@ -80,6 +94,9 @@ public class SettingsValidators {
            // and underscores ('_'). However, individual package name parts may only
            // start with letters.
            // (https://developer.android.com/guide/topics/manifest/manifest-element.html#package)
            if (value == null) {
                return false;
            }
            String[] subparts = value.split(".");
            boolean isValidPackageName = true;
            for (String subpart : subparts) {
@@ -106,10 +123,29 @@ public class SettingsValidators {

        @Override
        public boolean validate(String value) {
            if (value == null) {
                return false;
            }
            return value.length() <= MAX_IPV6_LENGTH;
        }
    };

    public static final Validator LOCALE_VALIDATOR = new Validator() {
        @Override
        public boolean validate(String value) {
            if (value == null) {
                return false;
            }
            Locale[] validLocales = Locale.getAvailableLocales();
            for (Locale locale : validLocales) {
                if (value.equals(locale.toString())) {
                    return true;
                }
            }
            return false;
        }
    };

    public interface Validator {
        boolean validate(String value);
    }
@@ -166,4 +202,48 @@ public class SettingsValidators {
            }
        }
    }

    public static final class ComponentNameListValidator implements Validator {
        private final String mSeparator;

        public ComponentNameListValidator(String separator) {
            mSeparator = separator;
        }

        @Override
        public boolean validate(String value) {
            if (value == null) {
                return false;
            }
            String[] elements = value.split(mSeparator);
            for (String element : elements) {
                if (!COMPONENT_NAME_VALIDATOR.validate(element)) {
                    return false;
                }
            }
            return true;
        }
    }

    public static final class PackageNameListValidator implements Validator {
        private final String mSeparator;

        public PackageNameListValidator(String separator) {
            mSeparator = separator;
        }

        @Override
        public boolean validate(String value) {
            if (value == null) {
                return false;
            }
            String[] elements = value.split(mSeparator);
            for (String element : elements) {
                if (!PACKAGE_NAME_VALIDATOR.validate(element)) {
                    return false;
                }
            }
            return true;
        }
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -534,7 +534,9 @@ public class SettingsBackupTest {
                 Settings.Secure.VOICE_RECOGNITION_SERVICE,
                 Settings.Secure.INSTANT_APPS_ENABLED,
                 Settings.Secure.BACKUP_MANAGER_CONSTANTS,
                 Settings.Secure.KEYGUARD_SLICE_URI);
                 Settings.Secure.KEYGUARD_SLICE_URI,
                 Settings.Secure.PARENTAL_CONTROL_ENABLED,
                 Settings.Secure.PARENTAL_CONTROL_REDIRECT_URL);

    @Test
    public void systemSettingsBackedUpOrBlacklisted() {
+24 −4
Original line number Diff line number Diff line
@@ -36,20 +36,28 @@ public class SettingsValidatorsTest {

    @Test
    public void ensureAllBackedUpSystemSettingsHaveValidators() {
        String offenders = getOffenders(Settings.System.SETTINGS_TO_BACKUP,
                Settings.System.VALIDATORS);
        String offenders = getOffenders(concat(Settings.System.SETTINGS_TO_BACKUP,
                Settings.System.LEGACY_RESTORE_SETTINGS), Settings.System.VALIDATORS);

        failIfOffendersPresent(offenders, "Settings.System");
    }

    @Test
    public void ensureAllBackedUpGlobalSettingsHaveValidators() {
        String offenders = getOffenders(Settings.Global.SETTINGS_TO_BACKUP,
                Settings.Global.VALIDATORS);
        String offenders = getOffenders(concat(Settings.Global.SETTINGS_TO_BACKUP,
                Settings.Global.LEGACY_RESTORE_SETTINGS), Settings.Global.VALIDATORS);

        failIfOffendersPresent(offenders, "Settings.Global");
    }

    @Test
    public void ensureAllBackedUpSecureSettingsHaveValidators() {
        String offenders = getOffenders(concat(Settings.Secure.SETTINGS_TO_BACKUP,
                Settings.Secure.LEGACY_RESTORE_SETTINGS), Settings.Secure.VALIDATORS);

        failIfOffendersPresent(offenders, "Settings.Secure");
    }

    private void failIfOffendersPresent(String offenders, String settingsType) {
        if (offenders.length() > 0) {
            fail("All " + settingsType + " settings that are backed up have to have a non-null"
@@ -66,4 +74,16 @@ public class SettingsValidatorsTest {
        }
        return offenders.toString();
    }

    private String[] concat(String[] first, String[] second) {
        if (second == null || second.length == 0) {
            return first;
        }
        final int firstLen = first.length;
        final int secondLen = second.length;
        String[] both = new String[firstLen + secondLen];
        System.arraycopy(first, 0, both, 0, firstLen);
        System.arraycopy(second, 0, both, firstLen, secondLen);
        return both;
    }
}
+24 −4
Original line number Diff line number Diff line
@@ -302,6 +302,7 @@ public class SettingsProvider extends ContentProvider {
        // fail to boot if there're any backed up settings that don't have a non-null validator
        ensureAllBackedUpSystemSettingsHaveValidators();
        ensureAllBackedUpGlobalSettingsHaveValidators();
        ensureAllBackedUpSecureSettingsHaveValidators();

        synchronized (mLock) {
            mUserManager = UserManager.get(getContext());
@@ -321,19 +322,26 @@ public class SettingsProvider extends ContentProvider {
    }

    private void ensureAllBackedUpSystemSettingsHaveValidators() {
        String offenders = getOffenders(Settings.System.SETTINGS_TO_BACKUP,
                Settings.System.VALIDATORS);
        String offenders = getOffenders(concat(Settings.System.SETTINGS_TO_BACKUP,
                Settings.System.LEGACY_RESTORE_SETTINGS), Settings.System.VALIDATORS);

        failToBootIfOffendersPresent(offenders, "Settings.System");
    }

    private void ensureAllBackedUpGlobalSettingsHaveValidators() {
        String offenders = getOffenders(Settings.Global.SETTINGS_TO_BACKUP,
                Settings.Global.VALIDATORS);
        String offenders = getOffenders(concat(Settings.Global.SETTINGS_TO_BACKUP,
                Settings.Global.LEGACY_RESTORE_SETTINGS), Settings.Global.VALIDATORS);

        failToBootIfOffendersPresent(offenders, "Settings.Global");
    }

    private void ensureAllBackedUpSecureSettingsHaveValidators() {
        String offenders = getOffenders(concat(Settings.Secure.SETTINGS_TO_BACKUP,
                Settings.Secure.LEGACY_RESTORE_SETTINGS), Settings.Secure.VALIDATORS);

        failToBootIfOffendersPresent(offenders, "Settings.Secure");
    }

    private void failToBootIfOffendersPresent(String offenders, String settingsType) {
        if (offenders.length() > 0) {
            throw new RuntimeException("All " + settingsType + " settings that are backed up"
@@ -352,6 +360,18 @@ public class SettingsProvider extends ContentProvider {
        return offenders.toString();
    }

    private final String[] concat(String[] first, String[] second) {
        if (second == null || second.length == 0) {
            return first;
        }
        final int firstLen = first.length;
        final int secondLen = second.length;
        String[] both = new String[firstLen + secondLen];
        System.arraycopy(first, 0, both, 0, firstLen);
        System.arraycopy(second, 0, both, firstLen, secondLen);
        return both;
    }

    @Override
    public Bundle call(String method, String name, Bundle args) {
        final int requestingUserId = getRequestingUserId(args);