Skip to content

Commit

Permalink
Merge pull request #153 from Microsoft/albulank/ierestricted
Browse files Browse the repository at this point in the history
Only calling window.localStorage and window.sessionStorage after safe checks
  • Loading branch information
AlexBulankou committed Mar 15, 2016
2 parents 5699acb + 83edd56 commit 7066f79
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Global.props
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
-->
<SemanticVersionMajor>0</SemanticVersionMajor>
<SemanticVersionMinor>22</SemanticVersionMinor>
<SemanticVersionPatch>7</SemanticVersionPatch>
<SemanticVersionPatch>8</SemanticVersionPatch>

<!--
Date when Semantic Version was changed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class SessionContextTests extends TestClass {
this.testCase({
name: "ai_session local storage has correct structure",
test: () => {
if (window.localStorage) {
if (Microsoft.ApplicationInsights.Util.canUseLocalStorage()) {
// setup
var actualCookieName: string;
var actualCookieValue: string;
Expand Down Expand Up @@ -569,7 +569,7 @@ class SessionContextTests extends TestClass {
}

private resetStorage() {
if (window.localStorage) {
if (Microsoft.ApplicationInsights.Util.canUseLocalStorage()) {
window.localStorage.clear();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class DataLossAnalyzerTests extends TestClass {
}

public testInialize() {
sessionStorage.clear();
if (Microsoft.ApplicationInsights.Util.canUseSessionStorage()) {
sessionStorage.clear();
}
}

public registerTests() {
Expand Down
4 changes: 3 additions & 1 deletion JavaScript/JavaScriptSDK.Tests/CheckinTests/Sender.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class SenderTests extends TestClass {
private requests;

public testInitialize() {
sessionStorage.clear();
if (Microsoft.ApplicationInsights.Util.canUseSessionStorage()) {
sessionStorage.clear();
}
this.requests = [];
this.xhr = sinon.useFakeXMLHttpRequest();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class AppInsightsTests extends TestClass {
this.clock.reset();
Microsoft.ApplicationInsights.Util.setCookie('ai_session', "");
Microsoft.ApplicationInsights.Util.setCookie('ai_user', "");
if (window.localStorage) {
if (Microsoft.ApplicationInsights.Util.canUseLocalStorage()) {
window.localStorage.clear();
}
}

public testCleanup() {
Microsoft.ApplicationInsights.Util.setCookie('ai_session', "");
Microsoft.ApplicationInsights.Util.setCookie('ai_user', "");
if (window.localStorage) {
if (Microsoft.ApplicationInsights.Util.canUseLocalStorage()) {
window.localStorage.clear();
}
}
Expand Down
2 changes: 1 addition & 1 deletion JavaScript/JavaScriptSDK/AppInsights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Microsoft.ApplicationInsights {

"use strict";

export var Version = "0.22.7";
export var Version = "0.22.8";

export interface IConfig {
instrumentationKey: string;
Expand Down
20 changes: 9 additions & 11 deletions JavaScript/JavaScriptSDK/DataLossAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@

static reset() {
if (DataLossAnalyzer.isEnabled()) {
sessionStorage.setItem(DataLossAnalyzer.ITEMS_QUEUED_KEY, "0");
Util.setSessionStorage(DataLossAnalyzer.ITEMS_QUEUED_KEY, "0");
}
}

private static isEnabled(): boolean {
return DataLossAnalyzer.enabled &&
DataLossAnalyzer.appInsights != null &&
window.sessionStorage != null &&
window.sessionStorage.getItem != null &&
window.sessionStorage.setItem != null;
Util.canUseSessionStorage()
}

static getIssuesReported(): number {
var result =
(!DataLossAnalyzer.isEnabled() || isNaN(+sessionStorage.getItem(DataLossAnalyzer.ISSUES_REPORTED_KEY))) ?
(!DataLossAnalyzer.isEnabled() || isNaN(+Util.getSessionStorage(DataLossAnalyzer.ISSUES_REPORTED_KEY))) ?
0 :
+sessionStorage.getItem(DataLossAnalyzer.ISSUES_REPORTED_KEY);
+Util.getSessionStorage(DataLossAnalyzer.ISSUES_REPORTED_KEY);

return result;
}
Expand All @@ -37,7 +35,7 @@
if (DataLossAnalyzer.isEnabled()) {
var itemsQueued: number = DataLossAnalyzer.getNumberOfLostItems();
++itemsQueued;
sessionStorage.setItem(DataLossAnalyzer.ITEMS_QUEUED_KEY, itemsQueued.toString());
Util.setSessionStorage(DataLossAnalyzer.ITEMS_QUEUED_KEY, itemsQueued.toString());
}
} catch (e) { }
}
Expand All @@ -48,7 +46,7 @@
var itemsQueued: number = DataLossAnalyzer.getNumberOfLostItems();
itemsQueued -= countOfItemsSentSuccessfully;
if (itemsQueued < 0) itemsQueued = 0;
sessionStorage.setItem(DataLossAnalyzer.ITEMS_QUEUED_KEY, itemsQueued.toString());
Util.setSessionStorage(DataLossAnalyzer.ITEMS_QUEUED_KEY, itemsQueued.toString());
}
} catch (e) { }
}
Expand All @@ -57,9 +55,9 @@
var result: number = 0;
try {
if (DataLossAnalyzer.isEnabled()) {
result = isNaN(+sessionStorage.getItem(DataLossAnalyzer.ITEMS_QUEUED_KEY)) ?
result = isNaN(+Util.getSessionStorage(DataLossAnalyzer.ITEMS_QUEUED_KEY)) ?
0 :
+sessionStorage.getItem(DataLossAnalyzer.ITEMS_QUEUED_KEY);
+Util.getSessionStorage(DataLossAnalyzer.ITEMS_QUEUED_KEY);
}
} catch (e) {
result = 0;
Expand All @@ -82,7 +80,7 @@

var issuesReported: number = DataLossAnalyzer.getIssuesReported();
++issuesReported;
sessionStorage.setItem(DataLossAnalyzer.ISSUES_REPORTED_KEY, issuesReported.toString());
Util.setSessionStorage(DataLossAnalyzer.ISSUES_REPORTED_KEY, issuesReported.toString());
}
} catch (e) {
_InternalLogging.throwInternalNonUserActionable(LoggingSeverity.CRITICAL,
Expand Down
19 changes: 14 additions & 5 deletions JavaScript/JavaScriptSDK/Util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/// <reference path="./logging.ts" />
module Microsoft.ApplicationInsights {

/**
* Type of storage to differentiate between local storage and session storage
*/
enum StorageType {
LocalStorage,
SessionStorage
}

export class Util {
private static document: any = typeof document !== "undefined" ? document : {};
public static NotSpecified = "not_specified";
Expand All @@ -10,22 +18,23 @@ module Microsoft.ApplicationInsights {
* @return {Storage} - Returns the storage object if available else returns null
*/
private static _getLocalStorageObject(): Storage {
return Util._getVerifiedStorageObject(window.localStorage);
return Util._getVerifiedStorageObject(StorageType.LocalStorage);
}

/**
* Tests storage object (localStorage or sessionStorage) to verify that it is usable
* More details here: https://mathiasbynens.be/notes/localstorage-pattern
* @param storageRef
* @param storageType Type of storage
* @return {Storage} Returns storage object verified that it is usable
*/
private static _getVerifiedStorageObject(storageRef: Storage): Storage {
private static _getVerifiedStorageObject(storageType: StorageType): Storage {
var storage: Storage = null;
var fail: boolean;
var uid;
try {
uid = new Date;
(storage = storageRef).setItem(uid, uid);
storage = storageType === StorageType.LocalStorage ? window.localStorage : window.sessionStorage;
storage.setItem(uid, uid);
fail = storage.getItem(uid) != uid;
storage.removeItem(uid);
if (fail) {
Expand Down Expand Up @@ -124,7 +133,7 @@ module Microsoft.ApplicationInsights {
* @return {Storage} - Returns the storage object if available else returns null
*/
private static _getSessionStorageObject(): Storage {
return Util._getVerifiedStorageObject(window.sessionStorage);
return Util._getVerifiedStorageObject(StorageType.SessionStorage);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "applicationinsights-js",
"main": "dist/ai.0.js",
"version": "0.22.7",
"version": "0.22.8",
"homepage": "https://github.com/Microsoft/ApplicationInsights-JS",
"authors": [
"Max Shekhovtsov <mashekho@microsoft.com>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "applicationinsights-js",
"version": "0.22.7",
"version": "0.22.8",
"description": "[Application Insights](https://azure.microsoft.com/services/application-insights/) tells you about your app's performance and usage. By adding a few lines of code to your web pages, you get data about how many users you have, which pages are most popular, how fast pages load, whether they throw exceptions, and more. And you can add code to track more detailed user activity.",
"main": "dist/ai.0.js",
"scripts": {
Expand Down

0 comments on commit 7066f79

Please sign in to comment.