From 9fa802afe2922317f23b703090d7117a0cb86452 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 18 Oct 2011 01:30:41 +0200 Subject: [PATCH] Validate file format version and content version of settings file --- .../fsck/k9/preferences/SettingsImporter.java | 56 ++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/src/com/fsck/k9/preferences/SettingsImporter.java b/src/com/fsck/k9/preferences/SettingsImporter.java index 05cdecc39..3c1b36475 100644 --- a/src/com/fsck/k9/preferences/SettingsImporter.java +++ b/src/com/fsck/k9/preferences/SettingsImporter.java @@ -664,11 +664,17 @@ public class SettingsImporter { private static Imported parseRoot(XmlPullParser xpp, boolean globalSettings, List accountUuids, boolean overview) - throws XmlPullParserException, IOException { + throws XmlPullParserException, IOException, SettingsImportExportException { Imported result = new Imported(); - //TODO: check version attribute + String fileFormatVersionString = xpp.getAttributeValue(null, + SettingsExporter.FILE_FORMAT_ATTRIBUTE); + validateFileFormatVersion(fileFormatVersionString); + + String contentVersionString = xpp.getAttributeValue(null, + SettingsExporter.VERSION_ATTRIBUTE); + validateContentVersion(contentVersionString); int eventType = xpp.next(); while (!(eventType == XmlPullParser.END_TAG && @@ -709,6 +715,52 @@ public class SettingsImporter { return result; } + private static int validateFileFormatVersion(String versionString) + throws SettingsImportExportException { + + if (versionString == null) { + throw new SettingsImportExportException("Missing file format version"); + } + + int version; + try { + version = Integer.parseInt(versionString); + } catch (NumberFormatException e) { + throw new SettingsImportExportException("Invalid file format version: " + + versionString); + } + + if (version != SettingsExporter.FILE_FORMAT_VERSION) { + throw new SettingsImportExportException("Unsupported file format version: " + + versionString); + } + + return version; + } + + private static int validateContentVersion(String versionString) + throws SettingsImportExportException { + + if (versionString == null) { + throw new SettingsImportExportException("Missing content version"); + } + + int version; + try { + version = Integer.parseInt(versionString); + } catch (NumberFormatException e) { + throw new SettingsImportExportException("Invalid content version: " + + versionString); + } + + if (version != Settings.VERSION) { + throw new SettingsImportExportException("Unsupported content version: " + + versionString); + } + + return version; + } + private static ImportedSettings parseSettings(XmlPullParser xpp, String endTag) throws XmlPullParserException, IOException {