From b35c3e44ad1d5def93757a30e5d54dda081dd6a4 Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 18 Oct 2021 17:54:28 +0530 Subject: [PATCH] guard against QuotaExceeded DOMExceptions when writing to localStorage --- www/common/LessLoader.js | 6 +++++- www/common/outer/local-store.js | 26 +++++++++++++++++--------- www/common/sframe-common-outer.js | 18 +++++++++++++++--- www/secureiframe/main.js | 12 ++++++++++-- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/www/common/LessLoader.js b/www/common/LessLoader.js index a10936644..b4ca8fd73 100644 --- a/www/common/LessLoader.js +++ b/www/common/LessLoader.js @@ -37,7 +37,11 @@ define([ var cachePut = function (k, v, cb) { if (window.cryptpadCache) { return void window.cryptpadCache.put(k, v, cb); } setTimeout(function () { - localStorage['LESS_CACHE|' + key + '|' + k] = v; + try { + localStorage['LESS_CACHE|' + key + '|' + k] = v; + } catch (err) { + console.error(err); + } if (cb) { cb(); } }); }; diff --git a/www/common/outer/local-store.js b/www/common/outer/local-store.js index 5d6f2e0ff..6f5f128ba 100644 --- a/www/common/outer/local-store.js +++ b/www/common/outer/local-store.js @@ -8,6 +8,14 @@ define([ ], function (Constants, Hash, Cache, localForage, AppConfig, Util) { var LocalStore = {}; + var safeSet = function (key, val) { + try { + localStorage.setItem(key, val); + } catch (err) { + console.error(err); + } + }; + LocalStore.setThumbnail = function (key, value, cb) { localForage.setItem(key, value, cb); }; @@ -21,7 +29,7 @@ define([ LocalStore.setFSHash = function (hash) { var sHash = Hash.serializeHash(hash); - localStorage[Constants.fileHashKey] = sHash; + safeSet(Constants.fileHashKey, sHash); }; LocalStore.getFSHash = function () { var hash = localStorage[Constants.fileHashKey]; @@ -33,7 +41,7 @@ define([ if (hash) { var sHash = Hash.serializeHash(hash); - if (sHash !== hash) { localStorage[Constants.fileHashKey] = sHash; } + if (sHash !== hash) { safeSet(Constants.fileHashKey, sHash); } } return hash; @@ -49,7 +57,7 @@ define([ if (hash) { var sHash = Hash.serializeHash(hash); - if (sHash !== hash) { localStorage[Constants.userHashKey] = sHash; } + if (sHash !== hash) { safeSet(Constants.userHashKey, sHash); } } return hash; @@ -57,7 +65,7 @@ define([ LocalStore.setUserHash = function (hash) { var sHash = Hash.serializeHash(hash); - localStorage[Constants.userHashKey] = sHash; + safeSet(Constants.userHashKey, sHash); }; LocalStore.getBlockHash = function () { @@ -65,7 +73,7 @@ define([ }; LocalStore.setBlockHash = function (hash) { - localStorage[Constants.blockHashKey] = hash; + safeSet(Constants.blockHashKey, hash); }; LocalStore.getAccountName = function () { @@ -87,7 +95,7 @@ define([ }; LocalStore.setDriveRedirectPreference = function (bool) { - localStorage.setItem(Constants.redirectToDriveKey, Boolean(bool)); + safeSet(Constants.redirectToDriveKey, Boolean(bool)); }; LocalStore.getPremium = function () { @@ -96,15 +104,15 @@ define([ } catch (err) { return; } }; LocalStore.setPremium = function (bool) { - localStorage.setItem(Constants.isPremiumKey, Boolean(bool)); + safeSet(Constants.isPremiumKey, Boolean(bool)); }; LocalStore.login = function (hash, name, cb) { if (!hash) { throw new Error('expected a user hash'); } if (!name) { throw new Error('expected a user name'); } hash = Hash.serializeHash(hash); - localStorage.setItem(Constants.userHashKey, hash); - localStorage.setItem(Constants.userNameKey, name); + safeSet(Constants.userHashKey, hash); + safeSet(Constants.userNameKey, name); if (cb) { cb(); } }; var logoutHandlers = []; diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index 4ec56f4a0..fed4e9e75 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -251,7 +251,11 @@ define([ sframeChan.on('EV_CACHE_PUT', function (x) { Object.keys(x).forEach(function (k) { - localStorage['CRYPTPAD_CACHE|' + k] = x[k]; + try { + localStorage['CRYPTPAD_CACHE|' + k] = x[k]; + } catch (err) { + console.error(err); + } }); }); sframeChan.on('EV_LOCALSTORE_PUT', function (x) { @@ -260,7 +264,11 @@ define([ delete localStorage['CRYPTPAD_STORE|' + k]; return; } - localStorage['CRYPTPAD_STORE|' + k] = x[k]; + try { + localStorage['CRYPTPAD_STORE|' + k] = x[k]; + } catch (err) { + console.error(err); + } }); }); @@ -1859,7 +1867,11 @@ define([ if (isChrome && getChromeVersion() === 68) { sframeChan.whenReg('EV_CHROME_68', function () { sframeChan.event("EV_CHROME_68"); - localStorage.CryptPad_chrome68 = "1"; + try { + localStorage.CryptPad_chrome68 = "1"; + } catch (err) { + console.error(err); + } }); } } diff --git a/www/secureiframe/main.js b/www/secureiframe/main.js index be026194e..e1a95dfe8 100644 --- a/www/secureiframe/main.js +++ b/www/secureiframe/main.js @@ -125,7 +125,11 @@ define([ sframeChan.on('EV_CACHE_PUT', function (x) { Object.keys(x).forEach(function (k) { - localStorage['CRYPTPAD_CACHE|' + k] = x[k]; + try { + localStorage['CRYPTPAD_CACHE|' + k] = x[k]; + } catch (err) { + console.error(err); + } }); }); sframeChan.on('EV_LOCALSTORE_PUT', function (x) { @@ -134,7 +138,11 @@ define([ delete localStorage['CRYPTPAD_STORE|' + k]; return; } - localStorage['CRYPTPAD_STORE|' + k] = x[k]; + try { + localStorage['CRYPTPAD_STORE|' + k] = x[k]; + } catch (err) { + console.error(err); + } }); });