From ec7e69adc01309e5970bcdd9bde398c3284bcbdc Mon Sep 17 00:00:00 2001 From: hc-github-team-secure-vault-core <82990506+hc-github-team-secure-vault-core@users.noreply.github.com> Date: Fri, 7 Jul 2023 14:01:33 -0400 Subject: [PATCH] backport of commit 9c8a7422ade1b46f413274c5eb6d5306c9e3e563 (#21665) Co-authored-by: Angel Garbarino --- changelog/21503.txt | 3 +++ ui/app/lib/local-storage.js | 16 +++++++++++++++- ui/app/routes/vault/cluster.js | 4 ++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 changelog/21503.txt diff --git a/changelog/21503.txt b/changelog/21503.txt new file mode 100644 index 000000000..a61b22ba8 --- /dev/null +++ b/changelog/21503.txt @@ -0,0 +1,3 @@ +```release-note:bug +ui: Surface DOMException error when browser settings prevent localStorage. +``` diff --git a/ui/app/lib/local-storage.js b/ui/app/lib/local-storage.js index e3be3447b..b7aeb70b5 100644 --- a/ui/app/lib/local-storage.js +++ b/ui/app/lib/local-storage.js @@ -4,8 +4,22 @@ */ 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); + const item = window.localStorage.getItem(key); return item && JSON.parse(item); }, diff --git a/ui/app/routes/vault/cluster.js b/ui/app/routes/vault/cluster.js index 303bcc0b9..3e52da982 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); },