Debian Codegraph @

src/js/contentscripts/clobberlocalstorage.js

raw
1 /*
2 * This file is part of Privacy Badger <https://privacybadger.org/>
3 * Copyright (C) 2014 Electronic Frontier Foundation
4 *
5 * Privacy Badger is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Privacy Badger is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Privacy Badger. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 (function () {
19
20 // don't inject into non-HTML documents (such as XML documents)
21 // but do inject into XHTML documents
22 if (document instanceof HTMLDocument === false && (
23 document instanceof XMLDocument === false ||
24 document.createElement('div') instanceof HTMLDivElement === false
25 )) {
26 return;
27 }
28
29 // don't bother asking to run when trivially in first-party context
30 if (window.top == window) {
31 return;
32 }
33
34 // TODO race condition; fix waiting on https://crbug.com/478183
35 chrome.runtime.sendMessage({
36 type: "checkClobberingEnabled",
37 frameUrl: window.FRAME_URL
38 }, function (blocked) {
39 if (blocked) {
40 // https://stackoverflow.com/questions/49092423/how-to-break-on-localstorage-changes
41 var code =
42 '('+ function() {
43
44 /*
45 * If localStorage is inaccessible, such as when "Block third-party cookies"
46 * in enabled in Chrome or when `dom.storage.enabled` is set to `false` in
47 * Firefox, do not go any further.
48 */
49 try {
50 // No localStorage raises an Exception in Chromium-based browsers, while
51 // it's equal to `null` in Firefox.
52 if (null === localStorage) {
53 throw false;
54 }
55 } catch (ex) {
56 return;
57 }
58
59 let lsProxy = new Proxy(localStorage, {
60 set: function (/*ls, prop, value*/) {
61 return true;
62 },
63 get: function (ls, prop) {
64 if (typeof ls[prop] == 'function') {
65 let fn = function () {};
66 if (prop == 'getItem' || prop == 'key') {
67 fn = function () { return null; };
68 }
69 return fn.bind(ls);
70 } else {
71 if (prop == 'length') {
72 return 0;
73 } else if (prop == '__proto__') {
74 return lsProxy;
75 }
76 return;
77 }
78 }
79 });
80
81 Object.defineProperty(window, 'localStorage', {
82 configurable: true,
83 enumerable: true,
84 value: lsProxy
85 });
86
87 } +')()';
88
89 window.injectScript(code);
90 }
91 return true;
92 });
93
94 }());