1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
var Settings = {
async import(data) {
// figure out whether it's just a whitelist, a legacy backup or a "Quantum" export
try {
let json = JSON.parse(data);
if (json.whitelist) {
return await this.importLegacy(json);
}
if (json.trusted) {
return await this.importPolicy(json);
}
if (json.policy) {
return await this.importSettings(json);
}
} catch (e) {
return await this.importLists(data);
}
},
async importLegacy(json) {
await include("/legacy/Legacy.js");
if (await Legacy.import(json)) {
try {
ns.policy = Legacy.migratePolicy();
await ns.savePolicy();
await Legacy.persist();
return true;
} catch (e) {
error(e, "Importing legacy settings");
Legacy.migrated = Legacy.undo;
}
}
return false;
},
async importLists(data) {
await include("/legacy/Legacy.js");
try {
let [trusted, untrusted] = Legacy.extractLists(data.split("[UNTRUSTED]"));
let policy = ns.policy;
for (let site of trusted) {
policy.set(site, policy.TRUSTED);
}
for (let site of untrusted) {
policy.set(site, policy.UNTRUSTED, true);
}
await ns.savePolicy();
} catch (e) {
error(e, "Importing white/black lists %s", data);
return false;
}
return true;
},
async importPolicy(json) {
try {
ns.policy = new Policy(json);
await ns.savePolicy();
return true;
} catch (e) {
error(e, "Importing policy %o", json);
}
},
async importSettings(json) {
try {
await this.update(json);
return true;
} catch (e) {
error(e, "Importing settings %o", json);
}
return false;
},
async update(settings) {
let {
policy,
xssUserChoices,
tabId,
unrestrictedTab,
reloadAffected,
isTorBrowser,
} = settings;
let oldDebug = ns.local.debug;
let reloadOptionsUI = false;
if (isTorBrowser) {
// Tor Browser-specific settings
ns.defaults.local.isTorBrowser = true; // prevents reset from forgetting
ns.defaults.sync.cascadeRestrictions = true; // we want this to be the default even on reset
if (!this.gotTorBrowserInit) {
// First initialization message from the Tor Browser
this.gotTorBrowserInit = true;
if (ns.sync.overrideTorBrowserPolicy) {
// If the user chose to override Tor Browser's policy we skip
// copying the Security Level preset on startup (only).
// Manually changing the security level works as usual.
ns.local.isTorBrowser = true;
await ns.save(ns.local);
return;
}
} else {
reloadOptionsUI = true;
}
let torBrowserSettings = {
local: {
isTorBrowser: true,
},
sync: {
cascadeRestrictions: true,
xssScanRequestBody: false,
xssBlockUnscannedPOST: true,
}
}
for (let [storage, prefs] of Object.entries(torBrowserSettings)) {
settings[storage] = Object.assign(settings[storage] || {}, prefs);
}
}
if (settings.sync === null) {
// overriden defaults when user manually resets options
// we want the reset options to stick (otherwise it gets very confusing)
ns.defaults.sync.overrideTorBrowserPolicy = true;
reloadOptionsUI = true;
}
await Promise.all(["local", "sync"].map(
async storage => (settings[storage] || // changed or...
settings[storage] === null // ... needs reset to default
) && await ns.save(settings[storage]
? Object.assign(ns[storage], settings[storage]) : ns[storage] = Object.assign({}, ns.defaults[storage]))
));
if (ns.local.debug !== oldDebug) {
await include("/lib/log.js");
if (oldDebug) debug = () => {};
}
if (policy) {
ns.policy = new Policy(policy);
await ns.savePolicy();
}
if (typeof unrestrictedTab === "boolean") {
ns.unrestrictedTabs[unrestrictedTab ? "add" : "delete"](tabId);
}
if (reloadAffected) {
browser.tabs.reload(tabId);
}
if (xssUserChoices) await XSS.saveUserChoices(xssUserChoices);
if (ns.sync.xss) {
XSS.start();
} else {
XSS.stop();
}
if (reloadOptionsUI) await this.reloadOptionsUI();
},
export() {
return JSON.stringify({
policy: ns.policy.dry(),
local: ns.local,
sync: ns.sync,
xssUserChoices: XSS.getUserChoices(),
}, null, 2);
},
async enforceTabRestrictions(tabId, unrestricted = ns.unrestrictedTabs.has(tabId)) {
await ChildPolicies.storeTabInfo(tabId, unrestricted && {unrestricted: true});
return unrestricted;
},
async reloadOptionsUI() {
try {
for (let t of await browser.tabs.query({url: browser.extension.getURL(
browser.runtime.getManifest().options_ui.page) })
) {
browser.tabs.reload(t.id);
};
} catch (e) {
error(e);
}
}
}
|