1 // # NoScript settings control (for binding to Security Slider)
3 /* jshint esversion:6 */
7 const { utils: Cu } = Components;
8 const { LegacyExtensionContext } =
9 Cu.import("resource://gre/modules/LegacyExtensionsUtils.jsm", {});
10 const { bindPrefAndInit } =
11 Cu.import("resource://torbutton/modules/utils.js", {});
13 // ## NoScript settings
15 // Minimum and maximum capability states as controlled by NoScript.
16 const max_caps = ["fetch", "font", "frame", "media", "other", "script", "webgl"];
17 const min_caps = ["frame", "other"];
19 // Untrusted capabilities for [Standard, Safer, Safest] safety levels.
20 const untrusted_caps = [
21 max_caps, // standard safety: neither http nor https
22 ["frame", "font", "other"], // safer: http
23 min_caps, // safest: neither http nor https
26 // Default capabilities for [Standard, Safer, Safest] safety levels.
27 const default_caps = [
28 max_caps, // standard: both http and https
29 ["fetch", "font", "frame", "other", "script", "webgl"], // safer: https only
30 min_caps, // safest: both http and https
33 // __noscriptSettings(safetyLevel)__.
34 // Produces NoScript settings with policy according to
35 // the safetyLevel which can be:
36 // 0 = Standard, 1 = Safer, 2 = Safest
38 // At the "Standard" safety level, we leave all sites at
39 // default with maximal capabilities. Essentially no content
42 // At "Safer", we set all http sites to untrusted,
43 // and all https sites to default. Scripts are only permitted
44 // on https sites. Neither type of site is supposed to allow
45 // media, but both allow fonts (as we used in legacy NoScript).
47 // At "Safest", all sites are at default with minimal
48 // capabilities. Most things are blocked.
49 let noscriptSettings = safetyLevel => (
51 "type": "NoScript.updateSettings", // backwards compatibility
52 "_messageName": "updateSettings",
55 "capabilities": default_caps[safetyLevel],
59 "capabilities": max_caps,
63 "capabilities": untrusted_caps[safetyLevel],
68 "untrusted": [[], ["http:"], []][safetyLevel],
80 // The extension ID for NoScript (WebExtension)
81 const noscriptID = "{73a6fe31-595d-460b-a920-fcc0f8843232}";
83 // A mock extension object that can communicate with another extension
84 // via the WebExtensions sendMessage/onMessage mechanism.
85 let extensionContext = new LegacyExtensionContext({ id : noscriptID });
87 // The component that handles WebExtensions' sendMessage.
88 let messageManager = extensionContext.messenger.messageManagers[0];
90 // __setNoScriptSettings(settings)__.
91 // NoScript listens for internal settings with onMessage. We can send
92 // a new settings JSON object according to NoScript's
93 // protocol and these are accepted! See the use of
94 // `browser.runtime.onMessage.addListener(...)` in NoScript's bg/main.js.
95 let sendNoScriptSettings = settings =>
96 extensionContext.messenger.sendMessage(messageManager, settings, noscriptID);
98 // __setNoScriptSafetyLevel(safetyLevel)__.
99 // Set NoScript settings according to a particular safety level
100 // (security slider level): 0 = Standard, 1 = Safer, 2 = Safest
101 let setNoScriptSafetyLevel = safetyLevel =>
102 sendNoScriptSettings(noscriptSettings(safetyLevel));
106 // __securitySliderToSafetyLevel(sliderState)__.
107 // Converts the "extensions.torbutton.security_slider" pref value
108 // to a "safety level" value: 0 = Standard, 1 = Safer, 2 = Safest
109 let securitySliderToSafetyLevel = sliderState => [undefined, 2, 1, 1, 0][sliderState];
111 // Ensure binding only occurs once.
112 let initialized = false;
115 // The main function that binds the NoScript settings to the security
116 // slider pref state.
117 var initialize = () => {
123 "extensions.torbutton.security_slider",
124 sliderState => setNoScriptSafetyLevel(securitySliderToSafetyLevel(sliderState)));
127 // Export initialize() function for external use.
128 let EXPORTED_SYMBOLS = ["initialize"];