Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle blocked access to local/session storage #414

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/src/PublicRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ export type KeyguardError = {
CORE: 'Core',
// used for internal keyguard Errors.
KEYGUARD: 'Keyguard',
// used for errors caused by the browser and its configuration
BROWSER: 'Browser',
// used for the remaining Errors which are not assigned an own type just yet.
UNCLASSIFIED: 'Unclassified',
},
Expand All @@ -246,5 +248,7 @@ export type KeyguardError = {
KEY_NOT_FOUND: 'keyId not found',
// network name does not exist
INVALID_NETWORK_CONFIG: 'Invalid network config',
// when the browser prevents access to LocalStorage or SessionStorage (because of privacy settings)
NO_STORAGE_ACCESS: 'Cannot access browser storage because of privacy settings',
},
};
14 changes: 12 additions & 2 deletions src/components/TabWidthSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ class TabWidthSelector extends Nimiq.Observable {
this.$el = TabWidthSelector._createElement($el);

// Load last used width from localStorage.
this._tabWidth = localStorage.getItem(TabWidthSelector.LOCALSTORAGE_KEY) || TabWidthSelector.DEFAULT_TAB_WIDTH;
try {
/** @type {string} */
this._tabWidth = localStorage.getItem(TabWidthSelector.LOCALSTORAGE_KEY) || '';
} catch (error) {
// Ignore
}
this._tabWidth = this._tabWidth || TabWidthSelector.DEFAULT_TAB_WIDTH;
this._updateClasses();

this.$width2Button = /** @type {HTMLButtonElement} */ (this.$el.querySelector('button[data-width="2"]'));
Expand Down Expand Up @@ -67,7 +73,11 @@ class TabWidthSelector extends Nimiq.Observable {
_updateWidth(width) {
this._tabWidth = width;
this._updateClasses();
localStorage.setItem(TabWidthSelector.LOCALSTORAGE_KEY, this._tabWidth);
try {
localStorage.setItem(TabWidthSelector.LOCALSTORAGE_KEY, this._tabWidth);
} catch (error) {
// Ignore
}
this.fire(TabWidthSelector.Events.INPUT, this._tabWidth);
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/ErrorConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const ErrorConstants = {
CORE: 'Core',
// used for other internal keyguard Errors.
KEYGUARD: 'Keyguard',
// used for errors caused by the browser and its configuration
BROWSER: 'Browser',
// used for the remaining Errors which are not assigned an own type just yet.
UNCLASSIFIED: 'Unclassified',
},
Expand All @@ -23,6 +25,8 @@ const ErrorConstants = {
KEY_NOT_FOUND: 'keyId not found',
// network name does not exist
INVALID_NETWORK_CONFIG: 'Invalid network config',
// when the browser prevents access to LocalStorage or SessionStorage (because of privacy settings)
NO_STORAGE_ACCESS: 'Cannot access browser storage because of privacy settings',
},
};

Expand Down
9 changes: 9 additions & 0 deletions src/lib/Errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ Errors.KeyguardError = class extends Errors.BaseError {
}
};

Errors.BrowserError = class extends Errors.BaseError {
/**
* @param {string|Error} [messageOrError]
*/
constructor(messageOrError) {
super(ErrorConstants.Types.BROWSER, messageOrError);
}
};

Errors.UnclassifiedError = class extends Errors.BaseError {
/**
* @param {string|Error} [messageOrError]
Expand Down
20 changes: 14 additions & 6 deletions src/lib/RpcServer.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,15 @@ class RpcServer { // eslint-disable-line no-unused-vars
// Check for a stored request referenced by a URL 'id' parameter
const searchParams = new URLSearchParams(window.location.search);
if (searchParams.has(UrlRpcEncoder.URL_SEARCHPARAM_NAME)) {
const storedRequest = window.sessionStorage.getItem(
`request-${searchParams.get(UrlRpcEncoder.URL_SEARCHPARAM_NAME)}`,
);
if (storedRequest) {
return this._receive(JsonUtils.parse(storedRequest), false);
try {
const storedRequest = window.sessionStorage.getItem(
`request-${searchParams.get(UrlRpcEncoder.URL_SEARCHPARAM_NAME)}`,
);
if (storedRequest) {
return this._receive(JsonUtils.parse(storedRequest), false);
}
} catch (error) {
// Ignore SessionStorage access error
}
}

Expand Down Expand Up @@ -292,7 +296,11 @@ class RpcServer { // eslint-disable-line no-unused-vars
console.debug('RpcServer ACCEPT', state.data);

if (persistMessage) {
sessionStorage.setItem(`request-${state.data.id}`, JsonUtils.stringify(state.toRequestObject()));
try {
sessionStorage.setItem(`request-${state.data.id}`, JsonUtils.stringify(state.toRequestObject()));
} catch (error) {
// Ignore SessionStorage access error
}
}

// Call method
Expand Down
8 changes: 8 additions & 0 deletions src/request/import/ImportApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* global ImportFile */
/* global ImportWords */
/* global Errors */
/* global ErrorConstants */

/** @extends {TopLevelApi<KeyguardRequest.ImportRequest>} */
class ImportApi extends TopLevelApi {
Expand All @@ -14,6 +15,13 @@ class ImportApi extends TopLevelApi {
throw new Errors.InvalidRequestError('request is required');
}

try {
sessionStorage.setItem('_test', 'write-access');
sessionStorage.removeItem('_test');
} catch (e) {
throw new Errors.BrowserError(ErrorConstants.Messages.NO_STORAGE_ACCESS);
}

const parsedRequest = {};
parsedRequest.appName = this.parseAppName(request.appName);
parsedRequest.requestedKeyPaths = this.parsePathsArray(request.requestedKeyPaths, ' requestedKeyPaths');
Expand Down