From 08b05b9fdaa36f605e9f13b4583ed1d5d0d6afb9 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 28 Jul 2018 13:19:35 +0200 Subject: [PATCH] Validate the Preferences when fetching them from storage When updating Preferences using the `set` method, the input is carefully validated. However, no validation is (currently) done when a `BasePreferences` instance is created, which probably isn't that great. Hence this patch that simply ignores, to not unnecessarily break loading of the viewer itself, any invalid Preferences. --- web/preferences.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/web/preferences.js b/web/preferences.js index 6d257d55d..a5fa67388 100644 --- a/web/preferences.js +++ b/web/preferences.js @@ -60,9 +60,19 @@ class BasePreferences { this.prefs = Object.assign(Object.create(null), defaults); return this._readFromStorage(defaults); - }).then((prefObj) => { - if (prefObj) { - this.prefs = prefObj; + }).then((prefs) => { + if (!prefs) { + return; + } + for (let name in prefs) { + const defaultValue = this.defaults[name], prefValue = prefs[name]; + // Ignore preferences not present in, or whose types don't match, + // the default values. + if (defaultValue === undefined || + typeof prefValue !== typeof defaultValue) { + continue; + } + this.prefs[name] = prefValue; } }); }