Skip to content

Commit

Permalink
Validate the Preferences when fetching them from storage
Browse files Browse the repository at this point in the history
When updating Preferences using the `set` method, the input is carefully validated. However, no validation is (currently) done when the Preferences are initially read, which probably isn't that great; hence this patch that simply ignores invalid Preferences.
  • Loading branch information
Snuffleupagus committed Jul 25, 2018
1 parent 34957ec commit b9c4171
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions web/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,30 @@ class BasePreferences {
enumerable: true,
configurable: false,
});
return this._initPrefs();
});
}

/**
* @private
*/
_initPrefs() {
const defaults = this.defaults;
this.prefs = Object.assign(Object.create(null), defaults);

this.prefs = Object.assign(Object.create(null), defaults);
return this._readFromStorage(defaults);
}).then((prefObj) => {
if (prefObj) {
this.prefs = prefObj;
return this._readFromStorage(defaults).then((prefObj) => {
if (!prefObj) {
return;
}
for (let name in prefObj) {
const defaultValue = defaults[name], prefValue = prefObj[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;
}
});
}
Expand Down Expand Up @@ -106,11 +124,7 @@ class BasePreferences {
*/
reload() {
return this._initializedPromise.then(() => {
return this._readFromStorage(this.defaults);
}).then((prefObj) => {
if (prefObj) {
this.prefs = prefObj;
}
return this._initPrefs();
});
}

Expand Down

0 comments on commit b9c4171

Please sign in to comment.