1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-23 18:02:15 -05:00

Added input validation for folder settings

This commit is contained in:
cketti 2011-10-08 21:30:45 +02:00
parent f21e14afc7
commit 1ab7dbae54
4 changed files with 120 additions and 47 deletions

View File

@ -88,15 +88,6 @@ public class LocalStore extends Store implements Serializable {
HEADERS_TO_SAVE.add("User-Agent");
}
public static final Set<String> FOLDER_SETTINGS_KEYS = new HashSet<String>();
static {
FOLDER_SETTINGS_KEYS.add("displayMode");
FOLDER_SETTINGS_KEYS.add("syncMode");
FOLDER_SETTINGS_KEYS.add("pushMode");
FOLDER_SETTINGS_KEYS.add("inTopGroup");
FOLDER_SETTINGS_KEYS.add("integrate");
}
/*
* a String containing the columns getMessages expects to work with
* in the correct order.

View File

@ -0,0 +1,44 @@
package com.fsck.k9.preferences;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import android.content.SharedPreferences;
import com.fsck.k9.mail.Folder.FolderClass;
import com.fsck.k9.preferences.Settings.*;
public class FolderSettings {
public static final Map<String, SettingsDescription> SETTINGS;
static {
Map<String, SettingsDescription> s = new LinkedHashMap<String, SettingsDescription>();
s.put("displayMode", new EnumSetting(FolderClass.class, FolderClass.NO_CLASS));
s.put("syncMode", new EnumSetting(FolderClass.class, FolderClass.INHERITED));
s.put("pushMode", new EnumSetting(FolderClass.class, FolderClass.INHERITED));
s.put("inTopGroup", new BooleanSetting(false));
s.put("integrate", new BooleanSetting(false));
SETTINGS = Collections.unmodifiableMap(s);
}
public static Map<String, String> validate(Map<String, String> importedSettings,
boolean useDefaultValues) {
return Settings.validate(SETTINGS, importedSettings, useDefaultValues);
}
public static Map<String, String> getFolderSettings(SharedPreferences storage, String uuid,
String folderName) {
Map<String, String> result = new HashMap<String, String>();
String prefix = uuid + "." + folderName + ".";
for (String key : SETTINGS.keySet()) {
String value = storage.getString(prefix + key, null);
if (value != null) {
result.put(key, value);
}
}
return result;
}
}

View File

@ -28,7 +28,6 @@ import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Store;
import com.fsck.k9.mail.ServerSettings;
import com.fsck.k9.mail.Transport;
import com.fsck.k9.mail.store.LocalStore;
import com.fsck.k9.preferences.Settings.InvalidSettingValueException;
import com.fsck.k9.preferences.Settings.SettingsDescription;
@ -304,7 +303,7 @@ public class StorageExporter {
continue;
}
if (LocalStore.FOLDER_SETTINGS_KEYS.contains(thirdPart)) {
if (FolderSettings.SETTINGS.containsKey(thirdPart)) {
// This is a folder key. Save folder name for later...
folders.add(secondPart);
// ... but don't write it now.
@ -429,25 +428,42 @@ public class StorageExporter {
serializer.startTag(null, FOLDER_ELEMENT);
serializer.attribute(null, NAME_ATTRIBUTE, folder);
// Write folder settings
for (Map.Entry<String, Object> entry : prefs.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().toString();
String valueString = entry.getValue().toString();
String[] comps = key.split("\\.");
if (comps.length >= 3) {
String keyUuid = comps[0];
String folderName = comps[1];
String folderKey = comps[2];
if (!keyUuid.equals(accountUuid) || !folderName.equals(folder)
|| !LocalStore.FOLDER_SETTINGS_KEYS.contains(folderKey)) {
continue;
}
} else {
if (comps.length < 3) {
// Skip non-folder config entries
continue;
}
writeKeyValue(serializer, comps[2], value);
String keyUuid = comps[0];
String folderName = comps[1];
String folderKey = comps[2];
if (!keyUuid.equals(accountUuid) || !folderName.equals(folder)) {
// Skip entries that belong to another folder
continue;
}
SettingsDescription setting = FolderSettings.SETTINGS.get(folderKey);
if (setting != null) {
// Only write settings that have an entry in FolderSettings.SETTINGS
try {
Object value = setting.fromString(valueString);
String outputValue = setting.toPrettyString(value);
writeKeyValue(serializer, folderKey, outputValue);
} catch (InvalidSettingValueException e) {
Log.w(K9.LOG_TAG, "Folder setting \"" + folderKey +
"\" has invalid value \"" + valueString +
"\" in preference storage. This shouldn't happen!");
}
}
}
serializer.endTag(null, FOLDER_ELEMENT);
}

View File

@ -311,8 +311,6 @@ public class StorageImporter {
//TODO: validate account name
//TODO: validate server settings
//TODO: validate identity settings
//TODO: validate folder settings
String accountName = account.name;
@ -360,12 +358,7 @@ public class StorageImporter {
// Write folder settings
if (account.folders != null) {
for (ImportedFolder folder : account.folders) {
String folderKeyPrefix = uuid + "." + folder.name + ".";
for (Map.Entry<String, String> setting : folder.settings.settings.entrySet()) {
String key = folderKeyPrefix + setting.getKey();
String value = setting.getValue();
editor.putString(key, value);
}
importFolder(editor, uuid, folder, mergeImportedAccount, prefs);
}
}
@ -375,6 +368,33 @@ public class StorageImporter {
return new AccountDescriptionPair(original, imported);
}
private static void importFolder(SharedPreferences.Editor editor, String uuid,
ImportedFolder folder, boolean overwrite, Preferences prefs) {
// Validate folder settings
Map<String, String> validatedFolderSettings =
FolderSettings.validate(folder.settings.settings, !overwrite);
// Merge folder settings if necessary
Map<String, String> writeFolderSettings;
if (overwrite) {
writeFolderSettings = FolderSettings.getFolderSettings(prefs.getPreferences(),
uuid, folder.name);
writeFolderSettings.putAll(validatedFolderSettings);
} else {
writeFolderSettings = new HashMap<String, String>(validatedFolderSettings);
}
// Write folder settings
String folderKeyPrefix = uuid + "." + folder.name + ".";
for (Map.Entry<String, String> setting : writeFolderSettings.entrySet()) {
String key = folderKeyPrefix + setting.getKey();
String value = setting.getValue();
Log.v(K9.LOG_TAG, "Writing " + key + "=" + value);
editor.putString(key, value);
}
}
private static void importIdentities(SharedPreferences.Editor editor, String uuid,
ImportedAccount account, boolean overwrite, Account existingAccount,
Preferences prefs) throws InvalidSettingValueException {
@ -440,25 +460,27 @@ public class StorageImporter {
editor.putString(accountKeyPrefix + Account.IDENTITY_DESCRIPTION_KEY + identitySuffix,
identityDescription);
// Validate identity settings
Map<String, String> validatedSettings = IdentitySettings.validate(
identity.settings.settings, !mergeSettings);
if (identity.settings != null) {
// Validate identity settings
Map<String, String> validatedSettings = IdentitySettings.validate(
identity.settings.settings, !mergeSettings);
// Merge identity settings if necessary
Map<String, String> writeSettings;
if (mergeSettings) {
writeSettings = new HashMap<String, String>(IdentitySettings.getIdentitySettings(
prefs.getPreferences(), uuid, writeIdentityIndex));
writeSettings.putAll(validatedSettings);
} else {
writeSettings = new HashMap<String, String>(validatedSettings);
}
// Merge identity settings if necessary
Map<String, String> writeSettings;
if (mergeSettings) {
writeSettings = new HashMap<String, String>(IdentitySettings.getIdentitySettings(
prefs.getPreferences(), uuid, writeIdentityIndex));
writeSettings.putAll(validatedSettings);
} else {
writeSettings = new HashMap<String, String>(validatedSettings);
}
// Write identity settings
for (Map.Entry<String, String> setting : writeSettings.entrySet()) {
String key = accountKeyPrefix + setting.getKey() + identitySuffix;
String value = setting.getValue();
editor.putString(key, value);
// Write identity settings
for (Map.Entry<String, String> setting : writeSettings.entrySet()) {
String key = accountKeyPrefix + setting.getKey() + identitySuffix;
String value = setting.getValue();
editor.putString(key, value);
}
}
}
}