Skip to content

Commit

Permalink
Bug 26128: Adapt security slider to WebExtensions version of NoScript
Browse files Browse the repository at this point in the history
(Thanks to Sukhbir Singh for help.)
  • Loading branch information
arthuredelstein committed Jun 7, 2018
1 parent 1b8977f commit 7656b58
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/chrome/content/torbutton.js
Expand Up @@ -11,6 +11,7 @@ let { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
let { showDialog } = Cu.import("resource://torbutton/modules/utils.js", {});
let { getLocale, unescapeTorString } = Cu.import("resource://torbutton/modules/utils.js", {});
let SecurityPrefs = Cu.import("resource://torbutton/modules/security-prefs.js", {});
let NoScriptControl = Cu.import("resource://torbutton/modules/noscript-control.js", {});
let { bindPrefAndInit, observe } = Cu.import("resource://torbutton/modules/utils.js", {});

Cu.importGlobalProperties(["XMLHttpRequest"]);
Expand Down Expand Up @@ -242,6 +243,7 @@ function torbutton_init() {
torbutton_log(3, 'called init()');

SecurityPrefs.initialize();
NoScriptControl.initialize();

if (m_tb_wasinited) {
return;
Expand Down
126 changes: 126 additions & 0 deletions src/modules/noscript-control.js
@@ -0,0 +1,126 @@
// # NoScript settings control (for binding to Security Slider)

// ## Utilities

const { utils: Cu } = Components;
const { LegacyExtensionContext } =
Cu.import("resource://gre/modules/LegacyExtensionsUtils.jsm", {});
const { bindPrefAndInit } =
Cu.import("resource://torbutton/modules/utils.js", {});

// ### NoScript settings

// Minimum and maximum capability states as controlled by NoScript.
const max_caps = ["fetch", "font", "frame", "media", "other", "script", "webgl"];
const min_caps = ["frame", "other"];

// Untrusted capabilities for [Low, Medium, High] safety levels.
const untrusted_caps = [
max_caps, // low safety: neither http nor https
["frame", "font", "other"] // medium: http
min_caps, // high safety: neither http nor https
];

// Default capabilities for [Low, Medium, High] safety levels.
const default_caps = [
max_caps, // low: both http and https
["fetch", "font", "frame", "other", "script", "webgl"], // medium: https only
min_caps, // high: both http and https
];

// __noscriptSettings(safetyLevel)__.
// Produces NoScript settings with policy according to
// the safetyLevel which can be:
// 0 = low
// 1 = medium
// 2 = high
//
// At the lowest safety level, we leave all sites at
// default with maximal capabilities. Essentially no content
// is blocked.
//
// At medium safety, we set all http sites to untrusted,
// and all https sites to default. Scripts are only permitted
// on https sites. Neither type of site is supposed to allow
// media, but both allow fonts (as we used in legacy NoScript).
//
// At high safety, all sites are at default with minimal
// capabilities. Most things are blocked.
let noscriptSettings = safetyLevel => (
{
"type": "NoScript.updateSettings",
"policy": {
"DEFAULT": {
"capabilities": default_caps[safetyLevel],
"temp": false
},
"TRUSTED": {
"capabilities": max_caps,
"temp": false
},
"UNTRUSTED": {
"capabilities": untrusted_caps[safetyLevel],
"temp": false
},
"sites": {
"trusted": [],
"untrusted": [[], ["http:"], []][safetyLevel],
"custom": {},
"temp": []
},
"enforced": true,
"autoAllowTop": false
},
"tabId": -1
});

// ### Communications

// The extension ID for NoScript (WebExtension)
const noscriptID = "{73a6fe31-595d-460b-a920-fcc0f8843232}";

// A mock extension object that can communicate with another extension
// via the WebExtensions sendMessage/onMessage mechanism.
let extensionContext = new LegacyExtensionContext({ id : noscriptID });

// The component that handles WebExtensions' sendMessage.
let messageManager = extensionContext.messenger.messageManagers[0];

// __setNoScriptSettings(settings)__.
// NoScript listens for internal settings with onMessage. We can send
// a new settings JSON object according to NoScript's
// protocol and these are accepted! See the use of
// `browser.runtime.onMessage.addListener(...)` in NoScript's bg/main.js.
let sendNoScriptSettings = settings =>
extensionContext.messenger.sendMessage(messageManager, settings, noscriptID);

// __setNoScriptSafetyLevel(safetyLevel)__.
// Set NoScript settings according to a particular safety level
// (security slider level): 0 = Low, 1 = Med, 2 = High
let setNoScriptSafetyLevel = safetyLevel =>
sendNoScriptSettings(noscriptSettings(safetyLevel));

// ### Slider binding

// __securitySliderToSafetyLevel(sliderState)__.
// Converts the "extensions.torbutton.security_slider" pref value
// to a "safety level" value: 0 = Low, 1 = Med, 2 = High
let securitySliderToSafetyLevel = sliderState => [, 2, 1, 1, 0][sliderState];

// Ensure binding only occurs once.
let initialized = false;

// __initialize()__.
// The main function that binds the NoScript settings to the security
// slider pref state.
var initialize = () => {
if (initialized) {
return;
}
bindPrefAndInit(
"extensions.torbutton.security_slider",
sliderState => setNoScriptSafetyLevel(securitySliderToSafetyLevel(sliderState)));
};

// Export initialize() function for external use.
let EXPORTED_SYMBOLS = ["initialize"];
8 changes: 3 additions & 5 deletions src/modules/security-prefs.js
Expand Up @@ -16,19 +16,17 @@ let log = (level, msg) => logger.log(level, msg);
// __kSecuritySettings__.
// A table of all prefs bound to the security slider, and the value
// for each security setting. Note that 2-m and 3-m are identical,
// corresponding to the old 2-medium-high setting.
// corresponding to the old 2-medium-high setting. We also separately
// bind NoScript settings to the extensions.torbutton.security_slider
// (see noscript-control.js).
const kSecuritySettings = {
// Preference name : [0, 1-high 2-m 3-m 4-low]
"javascript.options.ion" : [, false, false, false, true ],
"javascript.options.baselinejit" : [, false, false, false, true ],
"javascript.options.native_regexp" : [, false, false, false, true ],
"noscript.forbidMedia" : [, true, true, true, false],
"media.webaudio.enabled" : [, false, false, false, true ],
"mathml.disabled" : [, true, true, true, false],
"gfx.font_rendering.opentype_svg.enabled" : [, false, false, false, true ],
"noscript.global" : [, false, false, false, true ],
"noscript.globalHttpsWhitelist" : [, false, true, true, false],
"noscript.forbidFonts" : [, true, false, false, false],
"svg.in-content.enabled" : [, false, true, true, true ],
};

Expand Down

0 comments on commit 7656b58

Please sign in to comment.