I'm trying to add a "tab namespace" to all local storage keys so that I can achieve a uniqueness of local storage for sites between multiple tabs.
In a simple HTML5 page, let's say a web page has the following script:
Storage.prototype._setItem = Storage.prototype.setItem;
Storage.prototype.setItem = function(key, value) {
console.log("intercepted set local storage " + key + " = " + value);
this._setItem(key, value);
}
Storage.prototype._getItem = Storage.prototype.getItem;
Storage.prototype.getItem = function(key) {
console.log("intercepted get local storage " + key);
return this._getItem(key);
}
This gives the user a chance to intercept all gets/sets to the local storage.
Now let's say you have an extension doing the same thing:
var actualCode = `Storage.prototype._setItem = Storage.prototype.setItem;
Storage.prototype.setItem = function(key, value) {
console.log("intercepted set local storage " + key + " = " + value);
this._setItem(key, value);
}
Storage.prototype._getItem = Storage.prototype.getItem;
Storage.prototype.getItem = function(key) {
console.log("intercepted get local storage " + key);
return this._getItem(key);
}
console.log("local storage get/set injector completed");
`;
var script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.remove();
You will get a stack overflow when you have this extension enabled while loading the page with the script above:
Uncaught RangeError: Maximum call stack size exceeded
at Storage.getItem [as _getItem] (<anonymous>:48:37)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
at Storage.getItem [as _getItem] (<anonymous>:50:17)
Is there some way to detect that someone has already done an intercepted local storage and prevent the issue?