Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
remove location.{assign,reload,replace} references from HTML5 standar…
Browse files Browse the repository at this point in the history
…d, since they're read only and can't be modifided anyway
  • Loading branch information
pes10k committed Dec 30, 2017
1 parent 6886838 commit a768e81
Show file tree
Hide file tree
Showing 34 changed files with 791 additions and 363 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Expand Up @@ -2,5 +2,5 @@ node_modules/
add-on/lib/third_party/
add-on/lib/standards.js
add-on/config/js/third_party/
add-on/config/js/vue_compiled_templates
add-on/config/js/vue_compiled_templates/
test.config.js
2 changes: 1 addition & 1 deletion .eslintrc.json
Expand Up @@ -13,7 +13,7 @@
"no-console": "off",
"no-unused-vars": ["error", {"varsIgnorePattern": "ignore"}],
"semi": ["error", "always"],
"indent": ["error", 4],
"indent": ["error", 4, {"SwitchCase": 1}],
"eol-last": "error",
"quotes": ["error", "double", {"allowTemplateLiterals": true}],
"comma-dangle": ["error", "only-multiline"],
Expand Down
11 changes: 6 additions & 5 deletions add-on/background_scripts/background.js
Expand Up @@ -3,7 +3,7 @@
"use strict";
const {preferencesLib, constants, messagesLib} = window.WEB_API_MANAGER;
const {cookieEncodingLib, proxyBlockLib, httpHeadersLib} = window.WEB_API_MANAGER;
const {standardsLib, browserLib} = window.WEB_API_MANAGER;
const {standardsLib, browserLib, enums} = window.WEB_API_MANAGER;
const rootObject = browserLib.getRootObject();

preferencesLib.load(loadedPrefs => {
Expand Down Expand Up @@ -122,10 +122,11 @@
value: encodedOptions,
});

// If there are no standards to block on this domain, then there is
// no need to modify the CSP headers, since no script will be injected
// into the page.
if (standardIdsToBlock.length === 0) {
// If there are no standards to block on this domain, and we're not
// in "passive" logging mode, then there is no need to modify the CSP
// headers, since no script will be injected into the page.
if (standardIdsToBlock.length === 0 &&
shouldLog !== enums.ShouldLogVal.PASSIVE) {
return;
}

Expand Down
12 changes: 6 additions & 6 deletions add-on/background_scripts/messages.js
Expand Up @@ -52,12 +52,12 @@
rootObject.webNavigation.getAllFrames({tabId: tabs[0].id})
.then(frameResults => {
const frameHosts = frameResults.map(frame => {
if (frame.errorOccurred === true) {
return false;
}
if (frame.errorOccurred === true) {
return false;
}

return window.URI.parse(frame.url).host;
})
return window.URI.parse(frame.url).host;
})
.filter(url => !!url);

const uniqueHosts = Array.from(new Set(frameHosts));
Expand All @@ -69,7 +69,7 @@
rootObject.runtime.sendMessage(["getPreferencesAndFramesResponse", data]);
});
});
};
}

// Sent from the popup / browser action, that the user wants to view
// the blocking report for the currently active tab.
Expand Down
5 changes: 4 additions & 1 deletion add-on/config/js/vue_components/logging-settings.vue.js
@@ -1,13 +1,16 @@
(function () {
"use strict";

const {stateLib} = window.WEB_API_MANAGER;
const {stateLib, enums} = window.WEB_API_MANAGER;
const Vue = window.Vue;

Vue.component("logging-settings", {
props: ["dataShouldLog"],
render: window.WEB_API_MANAGER.vueComponents["logging-settings"].render,
staticRenderFns: window.WEB_API_MANAGER.vueComponents["logging-settings"].staticRenderFns,
computed: {
enums: () => enums,
},
methods: {
shouldLogChanged: function () {
const state = this.$root.$data;
Expand Down
7 changes: 5 additions & 2 deletions add-on/config/js/vue_components/web-api-standards.vue.js
Expand Up @@ -2,18 +2,21 @@
"use strict";

const standardsDefaults = window.WEB_API_MANAGER.defaults;
const {standardsLib, categoriesLib, stateLib} = window.WEB_API_MANAGER;
const {standardsLib, categoriesLib, stateLib, enums} = window.WEB_API_MANAGER;
const Vue = window.Vue;

Vue.component("web-api-standards", {
props: ["dataCurrentStandardIds", "dataSelectedPattern"],
props: ["dataCurrentStandardIds", "dataSelectedPattern", "dataShouldLog"],
render: window.WEB_API_MANAGER.vueComponents["web-api-standards"].render,
staticRenderFns: window.WEB_API_MANAGER.vueComponents["web-api-standards"].staticRenderFns,
computed: {
sortedCategoryIds: () => {
const categoryIds = categoriesLib.allCategoryIds();
return categoryIds.sort(categoriesLib.sortCategoriesById);
},
isPassiveMode: function () {
return this.dataShouldLog === enums.ShouldLogVal.PASSIVE;
},
},
methods: {
getCategoryLib: () => categoriesLib,
Expand Down
9 changes: 6 additions & 3 deletions add-on/content_scripts/instrument.js
Expand Up @@ -6,7 +6,7 @@

const consoleLog = window.console.log;
const cookies2 = window.Cookies.noConflict();
const {constants, cookieEncodingLib} = window.WEB_API_MANAGER;
const {constants, cookieEncodingLib, enums} = window.WEB_API_MANAGER;
const {browserLib, proxyBlockLib} = window.WEB_API_MANAGER;
const standardsCookieName = constants.cookieName;

Expand Down Expand Up @@ -51,8 +51,11 @@
const [standardIdsToBlock, shouldLog, randNonce] = decodedCookieValues;

// If there are no standards to block on this domain, then don't
// insert any script into the page.
if (standardIdsToBlock.length === 0) {
// insert any script into the page *unless* we're in passive
// logging mode, in which case we want to log everything despite logging
// settings.
if (standardIdsToBlock.length === 0 &&
shouldLog !== enums.ShouldLogVal.PASSIVE) {
return;
}

Expand Down
8 changes: 0 additions & 8 deletions add-on/lib/blockrules.js
Expand Up @@ -122,14 +122,6 @@
return testPatternWithHost(matchPattern, hostName);
};

/**
* A shorthand, reg-ex like rule for matching domains.
*
* @see https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Match_patterns
*
* @typedef {string} MatchPattern
*/

/**
* Creates a new block rule object, specifying which standards to block
* on which domains.
Expand Down
51 changes: 19 additions & 32 deletions add-on/lib/cookieencoding.js
@@ -1,8 +1,7 @@
(function () {
"use strict";
const {packingLib, standardsLib, constants} = window.WEB_API_MANAGER;
const shouldLogKey = constants.shouldLogKey;
const allStandardIdsWithShouldLogOption = standardsLib.allStandardIds().concat([shouldLogKey]);
const {packingLib, standardsLib} = window.WEB_API_MANAGER;
const allStandardIds = standardsLib.allStandardIds();

/**
* Creates a cookie-safe encoding of standard ids to block, and
Expand All @@ -19,8 +18,8 @@
*
* @param {array} standardIdsToBlock
* An array of strings, each a standard that should be blocked.
* @param {boolean} shouldLog
* Whether logging should be enabled.
* @param {ShouldLogVal} shouldLog
* Whether whether and how logging should be enabled.
* @param {string} randNonce
* A unique, unguessable identifier, used so that the injected content
* script can communicate with the content script, using an unguessable
Expand All @@ -31,55 +30,43 @@
* A cookie safe string encoding the above values.
*/
const toCookieValue = (standardIdsToBlock, shouldLog, randNonce) => {
const standardIdsToBlockWithShouldLogKey = shouldLog
? standardIdsToBlock.concat(shouldLogKey)
: standardIdsToBlock;
const cookieValueParts = [];

const packedValues = packingLib.pack(
allStandardIdsWithShouldLogOption,
standardIdsToBlockWithShouldLogKey
allStandardIds,
standardIdsToBlock
);

// Next, append the rand nonce to the end of the cookie string. Since
// "@" is guaranteed to never be a character in a base64 encoded
// string, we can use it as a separator between the packed cookie
// values and the nonce.
const packedCookieValue = packedValues + "@" + randNonce;
cookieValueParts.push(packedValues);
cookieValueParts.push(shouldLog);
cookieValueParts.push(randNonce);

const packedCookieValue = cookieValueParts.join("@");

// Last, replace "=" with "-" in the base64 string, to avoid
// silly ambiguities in the cookie value.
return packedCookieValue.replace(/=/g, "-");
};

/**
* Takes a encoded string (created from the `toCookieValue` function
* Takes a encoded string (created from the ` ` function
* in this module) and returns to values, one an array of
* standard names, and two, a boolean flag of whether the logging option
* is enabled.
*
* @param {string} data
* A string created from `toCookieValue`
*
* @return {[array, bool, string]}
* An array of length three. The first value in the returned array is
* @return {[array, ShouldLogVal, string]}
* An array of length three. The first value in the returned array is
* an array of strings of standard ids (representing standards to
* block). The second value is a boolean describing whether to log
* blocking behavior. The third value is a random nonce.
* block). The second value is a ShouldLogVal string, describing the
* logging settings. The third value is a random nonce.
*/
const fromCookieValue = data => {
const [packedValues, randNonce] = data.replace(/-/g, "=").split("@");
const unpackedValues = packingLib.unpack(allStandardIdsWithShouldLogOption, packedValues);

let shouldLog;
const [packedValues, shouldLog, randNonce] = data.replace(/-/g, "=").split("@");
const unpackedValues = packingLib.unpack(allStandardIds, packedValues);
const standardIdsToBlock = unpackedValues;
const indexOfShouldLog = unpackedValues.indexOf(shouldLogKey);

if (indexOfShouldLog === -1) {
shouldLog = false;
} else {
shouldLog = true;
standardIdsToBlock.splice(indexOfShouldLog, 1);
}

return [standardIdsToBlock, shouldLog, randNonce];
};
Expand Down
55 changes: 49 additions & 6 deletions add-on/lib/init.js
Expand Up @@ -13,17 +13,27 @@
global.window = {};
}

/**
* @enum {ShouldLogVal} string
* Enum style value that stores all possible setting for the "should log"
* value.
*
* NONE indicates that no logging should occur, STANDARD that selected
* features should be blocked and loged, and PASSIVE means that nothing
* should be blocked, but everything should be logged.
*/
const ShouldLogVal = Object.freeze({
NONE: "0",
STANDARD: "1",
PASSIVE: "2",
});

window.WEB_API_MANAGER = {
constants: {
// The name of the cookie that will be used to push domain
// configuration information into pages.
cookieName: "_wamtcstandards",

// The value in the packed array of options thats used to
// include the shouldLog option in the in bitfield encoded in
// the above cookie.
shouldLogKey: "shouldLogKey",

// The homepage of the extension.
homepage: "https://github.com/snyderp/web-api-manager",

Expand All @@ -32,7 +42,40 @@
defaultPattern: "(default)",

// Version of schema used when serializing preferences to storage.
schemaVersion: 2,
schemaVersion: 3,
},
enums: {
utils: {
/**
* Enum helper function that throws if the given value is
* not a valid value in the enumeration.
*
* @param {object} enumValues
* An enumerated set of valid values, likely defined in
* window.WEB_API_MANAGER.enums.
* @param {*} value
* A value that is expected to be one of the valid values
* in the enumeration. Implementaton side, this function
* checks that the give value is the value of one of the
* keys in the provided enumValues object.
*
* @throws
* Throws if the given value is not an expected enumerated
* value.
*
* @return {undefined}
*/
assertValidEnum: (enumValues, value) => {
const validValues = new Set(Object.values(enumValues));
if (!validValues.has(value)) {
throw `${value} is not a valid value for enum ${JSON.stringify(enumValues)}.`;
}
},
},
ShouldLogVal,
},
};

Object.freeze(window.WEB_API_MANAGER.constants);
Object.freeze(window.WEB_API_MANAGER.enums);
}());
32 changes: 31 additions & 1 deletion add-on/lib/migration.js
Expand Up @@ -5,7 +5,7 @@
(function () {
"use strict";

const {constants, standardsLib} = window.WEB_API_MANAGER;
const {constants, enums, standardsLib} = window.WEB_API_MANAGER;
const currentVersion = constants.schemaVersion;

/**
Expand Down Expand Up @@ -40,6 +40,12 @@
return 1;
}

// Small sanity check to make sure the data doesn't advertise
// some version of the schema we don't understand.
if (data.webApiManager.schema > currentVersion) {
return false;
}

return data.webApiManager.schema;
} catch (ignore) {
// If we received something other than an object, or the
Expand Down Expand Up @@ -85,6 +91,29 @@
return Object.freeze(migratedData);
};

/**
* Creates a new object, with the settings stored in the given v-2
* preferences data, but in the v-3 format.
*
* @param {object} data
* Persistent data loaded from the storage API in the extension.
*
* @return {object}
* A new read only object, describing the same preferences, but in the
* schema 3 format.
*/
const twoToThree = data => {
const migratedData = JSON.parse(JSON.stringify(data));

migratedData.webApiManager.shouldLog = (data.webApiManager.shouldLog === true)
? enums.ShouldLogVal.STANDARD
: enums.ShouldLogVal.NONE;

migratedData.webApiManager.schema = 3;

return Object.freeze(migratedData);
};

/**
* Apply any needed migrations to bring the structure of the given
* stored preferences data to the current version.
Expand All @@ -110,6 +139,7 @@

const migrations = [
oneToTwo,
twoToThree,
];

let currentMigratedVersion = foundDataVersion;
Expand Down

0 comments on commit a768e81

Please sign in to comment.