From a07cd25e90490bb0c9aee237e499102cd80e7bf9 Mon Sep 17 00:00:00 2001 From: Angel Garbarino Date: Wed, 28 Jun 2023 12:58:23 -0600 Subject: [PATCH 1/3] initial fix --- ui/app/lib/local-storage.js | 35 ++++++++++++++++++++++++++++++++-- ui/app/routes/vault/cluster.js | 4 ++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/ui/app/lib/local-storage.js b/ui/app/lib/local-storage.js index e3be3447b6b4..b44ffc2ae43d 100644 --- a/ui/app/lib/local-storage.js +++ b/ui/app/lib/local-storage.js @@ -4,9 +4,27 @@ */ export default { + isLocalStorageSupported() { + try { + const key = `__storage__test`; + window.localStorage.setItem(key, null); + window.localStorage.removeItem(key); + return true; + } catch (e) { + // modify the e object so we can customize the error message. + // e.message is readOnly. + e.errors = [`This is likely due to your browser's cookie settings.`]; + throw e; + } + }, + getItem(key) { - var item = window.localStorage.getItem(key); - return item && JSON.parse(item); + try { + const item = window.localStorage.getItem(key); + return item && JSON.parse(item); + } catch (e) { + return e; + } }, setItem(key, val) { @@ -31,3 +49,16 @@ export default { }); }, }; + +function isSupported(getStorage) { + try { + const key = '__some_random_key_you_are_not_going_to_use__'; + getStorage().setItem(key, key); + getStorage().removeItem(key); + return true; + } catch (e) { + return false; + } +} + +isSupported(() => localStorage); // => true | false diff --git a/ui/app/routes/vault/cluster.js b/ui/app/routes/vault/cluster.js index 303bcc0b92fa..3e52da9820e7 100644 --- a/ui/app/routes/vault/cluster.js +++ b/ui/app/routes/vault/cluster.js @@ -10,6 +10,7 @@ import Route from '@ember/routing/route'; import { task, timeout } from 'ember-concurrency'; import Ember from 'ember'; import getStorage from '../../lib/token-storage'; +import localStorage from 'vault/lib/local-storage'; import ClusterRoute from 'vault/mixins/cluster-route'; import ModelBoundaryRoute from 'vault/mixins/model-boundary-route'; @@ -87,6 +88,9 @@ export default Route.extend(ModelBoundaryRoute, ClusterRoute, { }, model(params) { + // if a user's browser settings block localStorage they will be unable to use Vault. The method will throw the error and the rest of the application will not load. + localStorage.isLocalStorageSupported(); + const id = this.getClusterId(params); return this.store.findRecord('cluster', id); }, From 2169b4082eac67a703db0478620cb0b1751924fb Mon Sep 17 00:00:00 2001 From: Angel Garbarino Date: Wed, 28 Jun 2023 13:07:45 -0600 Subject: [PATCH 2/3] changelog --- changelog/21503.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog/21503.txt diff --git a/changelog/21503.txt b/changelog/21503.txt new file mode 100644 index 000000000000..a61b22ba8a7d --- /dev/null +++ b/changelog/21503.txt @@ -0,0 +1,3 @@ +```release-note:bug +ui: Surface DOMException error when browser settings prevent localStorage. +``` From 2a139860b23e8b1f062334b68c3e74b08a8eb161 Mon Sep 17 00:00:00 2001 From: Angel Garbarino Date: Wed, 28 Jun 2023 13:15:07 -0600 Subject: [PATCH 3/3] clean up --- ui/app/lib/local-storage.js | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/ui/app/lib/local-storage.js b/ui/app/lib/local-storage.js index b44ffc2ae43d..b7aeb70b55a3 100644 --- a/ui/app/lib/local-storage.js +++ b/ui/app/lib/local-storage.js @@ -19,12 +19,8 @@ export default { }, getItem(key) { - try { - const item = window.localStorage.getItem(key); - return item && JSON.parse(item); - } catch (e) { - return e; - } + const item = window.localStorage.getItem(key); + return item && JSON.parse(item); }, setItem(key, val) { @@ -49,16 +45,3 @@ export default { }); }, }; - -function isSupported(getStorage) { - try { - const key = '__some_random_key_you_are_not_going_to_use__'; - getStorage().setItem(key, key); - getStorage().removeItem(key); - return true; - } catch (e) { - return false; - } -} - -isSupported(() => localStorage); // => true | false