Skip to content

Commit 560bb05

Browse files
authored
fix: Uncaught session storage security error handling (#358)
* change custom localStorage to ampLocalStorage to remove confusion * Add error handling for unavailable sessionStorage * Add docs to option
1 parent ff56fe4 commit 560bb05

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/metadata-storage.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import Base64 from './base64';
77
import baseCookie from './base-cookie';
88
import Constants from './constants';
99
import getLocation from './get-location';
10-
import localStorage from './localstorage';
10+
import ampLocalStorage from './localstorage';
1111
import topDomain from './top-domain';
12+
import utils from './utils';
1213

1314
const storageOptionExists = {
1415
[Constants.STORAGE_COOKIES]: true,
@@ -94,7 +95,7 @@ class MetadataStorage {
9495
}
9596
break;
9697
case Constants.STORAGE_LOCAL:
97-
localStorage.setItem(this.storageKey, value);
98+
ampLocalStorage.setItem(this.storageKey, value);
9899
break;
99100
case Constants.STORAGE_COOKIES:
100101
baseCookie.set(this.getCookieStorageKey(), value, {
@@ -113,10 +114,14 @@ class MetadataStorage {
113114
str = baseCookie.get(this.getCookieStorageKey() + '=');
114115
}
115116
if (!str) {
116-
str = localStorage.getItem(this.storageKey);
117+
str = ampLocalStorage.getItem(this.storageKey);
117118
}
118119
if (!str) {
119-
str = window.sessionStorage && window.sessionStorage.getItem(this.storageKey);
120+
try {
121+
str = window.sessionStorage && window.sessionStorage.getItem(this.storageKey);
122+
} catch (e) {
123+
utils.log.info(`window.sessionStorage unavailable. Reason: "${e}"`);
124+
}
120125
}
121126

122127
if (!str) {

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ if (BUILD_COMPAT_REACT_NATIVE) {
4444
* @property {boolean} [saveParamsReferrerOncePerSession=`true`] - If `true`, then includeGclid, includeFbclid, includeReferrer, and includeUtm will only track their respective properties once per session. New values that come in during the middle of the user's session will be ignored. Set to false to always capture new values.
4545
* @property {boolean} [secureCookie=`false`] - If `true`, the amplitude cookie will be set with the Secure flag.
4646
* @property {number} [sessionTimeout=`30*60*1000` (30 min)] - The time between logged events before a new session starts in milliseconds.
47+
* @property {string[]} [storage=`''`] - Sets storage strategy. Options are 'cookies', 'localStorage', 'sessionStorage', or `none`. Will override `disableCookies` option
4748
* @property {Object} [trackingOptions=`{ city: true, country: true, carrier: true, device_manufacturer: true, device_model: true, dma: true, ip_address: true, language: true, os_name: true, os_version: true, platform: true, region: true, version_name: true}`] - Type of data associated with a user.
4849
* @property {boolean} [unsetParamsReferrerOnNewSession=`false`] - If `false`, the existing `referrer` and `utm_parameter` values will be carried through each new session. If set to `true`, the `referrer` and `utm_parameter` user properties, which include `referrer`, `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, and `utm_content`, will be set to `null` upon instantiating a new session. Note: This only works if `includeReferrer` or `includeUtm` is set to `true`.
4950
* @property {string} [unsentKey=`amplitude_unsent`] - localStorage key that stores unsent events.

0 commit comments

Comments
 (0)