Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
51a5eb2
Update the rulesets out-of-band from the extension release channel.
Hainish Apr 15, 2017
52e5eee
Support for an arbitrary number of update channels
Hainish Apr 26, 2017
677679b
Suppress errors in Firefox by specifying text ajax request
Hainish May 13, 2017
1286d2f
Only apply new rulesets if there has been an update on any update cha…
Hainish Jul 25, 2017
e9d12cd
Fix: Use Object.assign rather than de-referencing all_rules object
Hainish Sep 20, 2017
0dadf58
Modularize update_channels.js
Hainish Sep 21, 2017
e302438
JS stylistic cleanup
Hainish Jan 12, 2018
028c9c3
Use store module for storage
Hainish Jan 12, 2018
58fc4d8
Making the signed rulesets infrastructure play well with other module…
Hainish Jan 13, 2018
0fb4ede
Revert back to local storage for updates, using the storage module
Hainish Jan 13, 2018
f6f36ed
Add an option to disable auto-updates in options ui
Hainish Jan 19, 2018
6a88b81
Pass in applyStoredFunc to remove require, which makes node unittests…
Hainish Jan 19, 2018
e1a2d53
Move pako_inflate.min.js into external dependencies path
Hainish Jan 19, 2018
1e73b97
Fixing formatting for eslint test
Hainish Jan 20, 2018
1e4557b
Display version for out-of-band rulesets
Hainish Mar 8, 2018
a85f3fa
Changing signing algorithm to RSA-PSS
Hainish Mar 16, 2018
67eaf90
Adding unit tests for update.js
Hainish Mar 16, 2018
8bfbda0
Change key and update path for EFF (Full)
Hainish Jan 16, 2018
5cc47f9
Avoid base64 encoding the compressed rulesets, which adds to the down…
Hainish Mar 23, 2018
47d2e6c
Protect against downgrade attacks. A sophisticated attacker would
Hainish Mar 23, 2018
09e591b
Fix tests
Hainish Mar 24, 2018
414e3cf
Cleaning up variable names and catch messages
Hainish Mar 30, 2018
8cfaec6
Update periodicity to two days
Hainish Mar 30, 2018
033f82d
Updating periodicity to two days, not two hours
Hainish Apr 3, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions chromium/background-scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
const rules = require('./rules'),
store = require('./store'),
incognito = require('./incognito'),
util = require('./util');
util = require('./util'),
update = require('./update');


let all_rules = new rules.RuleSets();
Expand All @@ -14,14 +15,15 @@ async function initialize() {
await store.initialize();
await store.performMigrations();
await initializeStoredGlobals();
await all_rules.loadFromBrowserStorage(store);
await update.initialize(store, initializeAllRules);
await all_rules.loadFromBrowserStorage(store, update.applyStoredRulesets);
await incognito.onIncognitoDestruction(destroy_caches);
}
initialize();

async function initializeAllRules() {
const r = new rules.RuleSets();
await r.loadFromBrowserStorage(store);
await r.loadFromBrowserStorage(store, update.applyStoredRulesets);
Object.assign(all_rules, r);
}

Expand Down Expand Up @@ -669,6 +671,9 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
import_settings(message.object).then(() => {
sendResponse(true);
});
} else if (message.type == "get_ruleset_timestamps") {
update.getRulesetTimestamps().then(timestamps => sendResponse(timestamps));
return true;
}
});

Expand Down
4 changes: 2 additions & 2 deletions chromium/background-scripts/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ RuleSets.prototype = {
* Load packaged rulesets, and rulesets in browser storage
* @param store object from store.js
*/
loadFromBrowserStorage: async function(store) {
loadFromBrowserStorage: async function(store, applyStoredFunc) {
this.store = store;
this.ruleActiveStates = await this.store.get_promise('ruleActiveStates', {});
this.addFromJson(util.loadExtensionFile('rules/default.rulesets', 'json'));
await applyStoredFunc(this);
this.loadStoredUserRules();
await this.addStoredCustomRulesets();
},
Expand Down
34 changes: 31 additions & 3 deletions chromium/background-scripts/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,34 @@ function initialize() {
});
}

/* Storage promise setters and getters */

function generic_get_promise(key, default_val, storage) {
return new Promise(res => storage.get({[key]: default_val}, data => res(data[key])));
}

function generic_set_promise(key, value, storage) {
return new Promise(res => storage.set({[key]: value}, res));
}

function get_promise(key, default_val) {
return new Promise(res => exports.get({[key]: default_val}, data => res(data[key])));
return generic_get_promise(key, default_val, exports);
}

function set_promise(key, value) {
return new Promise(res => exports.set({[key]: value}, res));
return generic_set_promise(key, value, exports);
}

function local_get_promise(key, default_val) {
return generic_get_promise(key, default_val, chrome.storage.local);
}

function local_set_promise(key, value) {
return generic_set_promise(key, value, chrome.storage.local);
}



async function performMigrations() {
const migration_version = await get_promise('migration_version', 0);

Expand All @@ -56,12 +76,20 @@ async function performMigrations() {
await set_promise('migration_version', 1);
}

const local = {
get: chrome.storage.local.get,
set: chrome.storage.local.set,
get_promise: local_get_promise,
set_promise: local_set_promise
};

function setStorage(store) {
Object.assign(exports, {
get: store.get.bind(store),
set: store.set.bind(store),
get_promise,
set_promise
set_promise,
local
});
}

Expand Down
263 changes: 263 additions & 0 deletions chromium/background-scripts/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
/* global update_channels */
/* global pako */
/* global __dirname */

"use strict";

// Determine if we're in the tests. If so, define some necessary components.
if(typeof(window) == "undefined"){
const fs = require('fs');
const WebCrypto = require("node-webcrypto-ossl"),
atob = require("atob"),
btoa = require("btoa"),
encoding = require('text-encoding');

var window = {
crypto: new WebCrypto(),
atob,
btoa
};

var update_channels = Function(fs.readFileSync(__dirname + '/update_channels.js').toString() + "return update_channels;")(),
pako = Function(fs.readFileSync(__dirname + '/../external/pako-1.0.5/pako_inflate.min.js').toString() + "return pako;")(),
TextDecoder = encoding.TextDecoder,
chrome = require("sinon-chrome");
}

(function(exports) {

const util = require('./util');
let store,
background_callback;

// how often we should check for new rulesets
const periodicity = 172800;

// jwk key loaded from keys.js
let imported_keys = {};
for(let update_channel of update_channels){
imported_keys[update_channel.name] = window.crypto.subtle.importKey(
"jwk",
update_channel.jwk,
{
name: "RSA-PSS",
hash: {name: "SHA-256"},
},
false,
["verify"]
);
}


// Determine the time until we should check for new rulesets
async function timeToNextCheck() {
const last_checked = await store.local.get_promise('last-checked', false);
if(last_checked) {
const current_timestamp = Date.now() / 1000;
const secs_since_last_checked = current_timestamp - last_checked;
return Math.max(0, periodicity - secs_since_last_checked);
} else {
return 0;
}
}

// Check for new rulesets. If found, return the timestamp. If not, return false
async function checkForNewRulesets(update_channel) {
let timestamp_result = await fetch(update_channel.update_path_prefix + "/latest-rulesets-timestamp");
if(timestamp_result.status == 200) {
let rulesets_timestamp = Number(await timestamp_result.text());

if((await store.local.get_promise('rulesets-timestamp: ' + update_channel.name, 0)) < rulesets_timestamp){
return rulesets_timestamp;
}
}
return false;
}

// Retrieve the timestamp for when a stored ruleset bundle was published
async function getRulesetTimestamps(){
let timestamp_promises = [];
for(let update_channel of update_channels){
timestamp_promises.push(new Promise(async resolve => {
let timestamp = await store.local.get_promise('rulesets-stored-timestamp: ' + update_channel.name, 0);
resolve([update_channel.name, timestamp]);
}));
}
let timestamps = await Promise.all(timestamp_promises);
return timestamps;
}

// Download and return new rulesets
async function getNewRulesets(rulesets_timestamp, update_channel) {

store.local.set_promise('rulesets-timestamp: ' + update_channel.name, rulesets_timestamp);

let signature_promise = fetch(update_channel.update_path_prefix + "/rulesets-signature." + rulesets_timestamp + ".sha256");
let rulesets_promise = fetch(update_channel.update_path_prefix + "/default.rulesets." + rulesets_timestamp + ".gz");

let responses = await Promise.all([
signature_promise,
rulesets_promise
]);

let resolutions = await Promise.all([
util.slurp(responses[0]),
util.slurp(responses[1])
]);

return {
signature_array_buffer: resolutions[0],
rulesets_array_buffer: resolutions[1]
};
}

// Returns a promise which verifies that the rulesets have a valid EFF
// signature, and if so, stores them and returns true.
// Otherwise, it throws an exception.
function verifyAndStoreNewRulesets(new_rulesets, rulesets_timestamp, update_channel){
return new Promise((resolve, reject) => {
imported_keys[update_channel.name].then(publicKey => {
window.crypto.subtle.verify(
{
name: "RSA-PSS",
saltLength: 32
},
publicKey,
new_rulesets.signature_array_buffer,
new_rulesets.rulesets_array_buffer
).then(async isvalid => {
if(isvalid) {
util.log(util.NOTE, update_channel.name + ': Downloaded ruleset signature checks out. Storing rulesets.');

const rulesets_gz = util.ArrayBufferToString(new_rulesets.rulesets_array_buffer);
const rulesets_byte_array = pako.inflate(rulesets_gz);
const rulesets = new TextDecoder("utf-8").decode(rulesets_byte_array);
const rulesets_json = JSON.parse(rulesets);

if(rulesets_json.timestamp != rulesets_timestamp){
reject(update_channel.name + ': Downloaded ruleset had an incorrect timestamp. This may be an attempted downgrade attack. Aborting.');
} else {
await store.local.set_promise('rulesets: ' + update_channel.name, window.btoa(rulesets_gz));
resolve(true);
}
} else {
reject(update_channel.name + ': Downloaded ruleset signature is invalid. Aborting.');
}
}).catch(() => {
reject(update_channel.name + ': Downloaded ruleset signature could not be verified. Aborting.');
});
}).catch(() => {
reject(update_channel.name + ': Could not import key. Aborting.');
});
});
}

// Unzip and apply the rulesets we have stored.
async function applyStoredRulesets(rulesets_obj){
let rulesets_promises = [];
for(let update_channel of update_channels){
rulesets_promises.push(new Promise(resolve => {
const key = 'rulesets: ' + update_channel.name
chrome.storage.local.get(key, root => {
if(root[key]){
util.log(util.NOTE, update_channel.name + ': Applying stored rulesets.');

const rulesets_gz = window.atob(root[key]);
const rulesets_byte_array = pako.inflate(rulesets_gz);
const rulesets = new TextDecoder("utf-8").decode(rulesets_byte_array);
const rulesets_json = JSON.parse(rulesets);

resolve(rulesets_json);
} else {
resolve();
}
});
}));
}

const rulesets_jsons = await Promise.all(rulesets_promises);
if(rulesets_jsons.join("").length > 0){
for(let rulesets_json of rulesets_jsons){
rulesets_obj.addFromJson(rulesets_json.rulesets);
}
} else {
rulesets_obj.addFromJson(util.loadExtensionFile('rules/default.rulesets', 'json'));
}
}

// basic workflow for periodic checks
async function performCheck() {
util.log(util.NOTE, 'Checking for new rulesets.');

const current_timestamp = Date.now() / 1000;
store.local.set_promise('last-checked', current_timestamp);

let num_updates = 0;
for(let update_channel of update_channels){
let new_rulesets_timestamp = await checkForNewRulesets(update_channel);
if(new_rulesets_timestamp){
util.log(util.NOTE, update_channel.name + ': A new ruleset bundle has been released. Downloading now.');
let new_rulesets = await getNewRulesets(new_rulesets_timestamp, update_channel);
try{
await verifyAndStoreNewRulesets(new_rulesets, new_rulesets_timestamp, update_channel);
store.local.set_promise('rulesets-stored-timestamp: ' + update_channel.name, new_rulesets_timestamp);
num_updates++;
} catch(err) {
util.log(util.WARN, update_channel.name + ': ' + err);
}
}
}
if(num_updates > 0){
background_callback();
}
};

chrome.storage.onChanged.addListener(async function(changes, areaName) {
if (areaName === 'sync' || areaName === 'local') {
if ('autoUpdateRulesets' in changes) {
if (changes.autoUpdateRulesets.newValue) {
await createTimer();
} else {
destroyTimer();
}
}
}
});

let initialCheck,
subsequentChecks;

async function createTimer(){
const time_to_next_check = await timeToNextCheck();

initialCheck = setTimeout(() => {
performCheck();
subsequentChecks = setInterval(performCheck, periodicity * 1000);
}, time_to_next_check * 1000);
}

function destroyTimer(){
if (initialCheck) {
clearTimeout(initialCheck);
}
if (subsequentChecks) {
clearInterval(subsequentChecks);
}
}

async function initialize(store_param, cb){
store = store_param;
background_callback = cb;

if (await store.get_promise('autoUpdateRulesets', true)) {
await createTimer();
}
}

Object.assign(exports, {
applyStoredRulesets,
initialize,
getRulesetTimestamps
});

})(typeof exports == 'undefined' ? require.scopes.update = {} : exports);
13 changes: 13 additions & 0 deletions chromium/background-scripts/update_channels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* exported update_channels */

"use strict";

var update_channels = [{
name: "EFF (Full)",
jwk: {
kty: "RSA",
e: "AQAB",
n: "1cwvFQu3Kw-Pz8bcEFuV5zx0ZheDsc4Tva7Qv6BL90_sDLqCW79Y543nDkPtNVfFH_89pt2kSPp_IcS5XnYiw6zBQeFuILFw5JpvZt14K0s4e025Q9CXfhYKIBKT9PnqihwAacjMa6rQb7RTu7XxVvqxRb3b0vx2CR40LSlYZ8H_KpeaUwq2oz-fyrI6LFTeYvbO3ZuLKeK5xV1a32xeTVMFkIj3LxnQalxq-DRHfj7LRRoTnbRDW4uoDc8aVpLFliuO79jUKbobz4slpiWJ4wjKR_O6OK13HbZUiOSxi8Bms-UqBPOyzbMVpmA7lv_zWdaLu1IVlVXQyLVbbrqI6llRqfHdcJoEl-eC48AofuB-relQtjTEK_hyBf7sPwrbqAarjRjlyEx6Qy5gTXyxM9attfNAeupYR6jm8LKm6TFpfWkyDxUmj_f5pJMBWNTomV74f8iQ2M18_KWMUDCOf80tR0t21Q1iCWdvA3K_KJn05tTLyumlwwlQijMqRkYuao-CX9L3DJIaB3VPYPTSIPUr7oi16agsuamOyiOtlZiRpEvoNg2ksJMZtwnj5xhBQydkdhMW2ZpHDzcLuZlhJYZL_l3_7wuzRM7vpyA9obP92CpZRFJErGZmFxJC93I4U9-0B0wg-sbyMKGJ5j1BWTnibCklDXtWzXtuiz18EgE"
},
update_path_prefix: "https://www.https-rulesets.org/v1/"
}];
Loading