aboutsummaryrefslogtreecommitdiffstats
path: root/lib/script_entries
diff options
context:
space:
mode:
authorNik Nyby <nikolas@gnu.org>2015-01-17 17:12:36 -0500
committerNik Nyby <nikolas@gnu.org>2015-01-17 17:12:36 -0500
commitada88090ead2c3b9d0804794c5f20f9b24d1c2b1 (patch)
tree2838a7eee6c5d74094216acebd86915e0ea1de42 /lib/script_entries
downloadlibrejsxul-ada88090ead2c3b9d0804794c5f20f9b24d1c2b1.tar.lz
librejsxul-ada88090ead2c3b9d0804794c5f20f9b24d1c2b1.tar.xz
librejsxul-ada88090ead2c3b9d0804794c5f20f9b24d1c2b1.zip
Import to new git repository
The old repository was using almost 100mb of space because of all the unnecessary files in the history. So I've imported the code to a new git repository. Unfortunately the history isn't viewable from this repository anymore. To see what happened with LibreJS before 2015, see the old Bazaar repo here: http://bzr.savannah.gnu.org/lh/librejs/
Diffstat (limited to 'lib/script_entries')
-rw-r--r--lib/script_entries/accepted_scripts.js67
-rw-r--r--lib/script_entries/all_scripts.js108
-rw-r--r--lib/script_entries/crypto.js60
-rw-r--r--lib/script_entries/dryrun_scripts.js76
-rw-r--r--lib/script_entries/free_libraries.js65
-rw-r--r--lib/script_entries/removed_scripts.js71
-rw-r--r--lib/script_entries/scripts_cache.js189
7 files changed, 636 insertions, 0 deletions
diff --git a/lib/script_entries/accepted_scripts.js b/lib/script_entries/accepted_scripts.js
new file mode 100644
index 0000000..1a8ac9a
--- /dev/null
+++ b/lib/script_entries/accepted_scripts.js
@@ -0,0 +1,67 @@
+/**
+ * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
+ * *
+ * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+var allScripts = require('script_entries/all_scripts').allScripts;
+
+var AcceptedScripts = function() {
+ this.scripts = {};
+ this.truncateJsData = allScripts.truncateJsData;
+ this.getScripts = allScripts.getScripts;
+ this.isFound = allScripts.isFound;
+ this.returnWhenFound = allScripts.returnWhenFound;
+ this.getOrInitScripts = allScripts.getOrInitScripts;
+ this.setHash = allScripts.setHash;
+};
+
+AcceptedScripts.prototype.clearScripts = function (url) {
+ this.scripts[url] = [];
+};
+
+/**
+ * addAScript
+ * adds a single script to the scripts array.
+ * @param {string} url - the url of the page where it is loaded.
+ * @param {object} scriptObj - Additional data regarding this script,
+ * including: inline: boolean,
+ * contents: string,
+ * removalReason: string.
+ */
+AcceptedScripts.prototype.addAScript = function (url, scriptObj) {
+ var exists;
+
+ if (this.scripts[url] === undefined) {
+ this.clearScripts(url);
+ }
+
+ // check if content is actually js code.
+ if (scriptObj.inline === true) {
+ this.setHash(scriptObj);
+ this.truncateJsData(scriptObj);
+ }
+ exists = this.isFound(url, scriptObj);
+ if (!exists) {
+ this.scripts[url].push(scriptObj);
+ return true;
+ } else {
+ return false;
+ }
+};
+
+exports.acceptedScripts = new AcceptedScripts();
diff --git a/lib/script_entries/all_scripts.js b/lib/script_entries/all_scripts.js
new file mode 100644
index 0000000..c08b4ac
--- /dev/null
+++ b/lib/script_entries/all_scripts.js
@@ -0,0 +1,108 @@
+/**
+ * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
+ * *
+ * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+var crypto = require('script_entries/crypto');
+
+var AllScripts = function() {
+ this.scripts = {};
+};
+
+AllScripts.prototype.truncateJsData = function (scriptObj) {
+ if (scriptObj.contents === undefined) {
+ console.debug('this is an inline script', scriptObj.value);
+ console.debug('this is an src', scriptObj.url);
+ }
+ if (scriptObj.contents.length > 1000) {
+ scriptObj.contents = scriptObj.contents.substring(0, 1000);
+ scriptObj.contents += '…';
+ }
+};
+
+AllScripts.prototype.setHash = function (scriptObj) {
+ scriptObj.hash = crypto.sha1Encrypt(scriptObj.contents);
+ return scriptObj.hash;
+};
+
+AllScripts.prototype.getScripts = function (url) {
+ if (!this.scripts[url]) {
+ return false;
+ } else {
+ return this.scripts[url];
+ }
+};
+
+AllScripts.prototype.reverseArray = function (url) {
+ this.scripts[url].reverse();
+};
+
+AllScripts.prototype.getOrInitScripts = function (url) {
+ if (this.scripts[url] === undefined) {
+ this.scripts[url] = [];
+ }
+ return this.scripts[url];
+};
+
+AllScripts.prototype.returnWhenFound = function(url, data) {
+ var pageScripts = this.getOrInitScripts(url),
+ i = 0,
+ le = pageScripts.length;
+
+ // check that entry doesn't exist.
+ if (data.inline === false) {
+ for (; i < le; i++) {
+ if (pageScripts[i].contents === data.url) {
+ return pageScripts[i];
+ }
+ }
+ } else if (data.inline === true) {
+ for (; i < le; i++) {
+ if (pageScripts[i].hash === crypto.sha1Encrypt(data.contents)) {
+ return pageScripts[i];
+ }
+ }
+ }
+
+ return false;
+};
+
+AllScripts.prototype.isFound = function(url, data) {
+ var pageScripts = this.getOrInitScripts(url),
+ i = 0,
+ le = pageScripts.length;
+
+ // check that entry doesn't exist.
+ if (data.inline === false) {
+ for (; i < le; i++) {
+ if (pageScripts[i].url === data.url) {
+ return true;
+ }
+ }
+ } else if (data.inline === true) {
+ for (; i < le; i++) {
+ if (pageScripts[i].hash === crypto.sha1Encrypt(data.contents)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+};
+
+exports.allScripts = new AllScripts();
diff --git a/lib/script_entries/crypto.js b/lib/script_entries/crypto.js
new file mode 100644
index 0000000..e8844b1
--- /dev/null
+++ b/lib/script_entries/crypto.js
@@ -0,0 +1,60 @@
+/**
+ * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
+ * *
+ * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+var {Cc, Ci, Cu, Cm, Cr} = require("chrome");
+
+var CryptoString = function() {
+ this.cryptoHash = Cc["@mozilla.org/security/hash;1"]
+ .createInstance(Ci.nsICryptoHash);
+ this.converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
+ createInstance(Ci.nsIScriptableUnicodeConverter);
+ this.hashAlgorithm = null;
+};
+
+CryptoString.prototype.init = function(hashAlgorithm, charset) {
+ this.converter.charset = charset;
+ this.hashAlgorithm = hashAlgorithm;
+ this.cryptoHash.init(this.cryptoHash[this.hashAlgorithm]);
+
+};
+
+CryptoString.prototype.encryptString = function(str) {
+ var result = {};
+ var data = this.converter.convertToByteArray(str, result);
+ this.cryptoHash.update(data, data.length);
+ var hash = this.cryptoHash.finish(false);
+ return [this.toHexString(hash.charCodeAt(i)) for (i in hash)].join("");
+};
+
+CryptoString.prototype.toHexString = function(charCode) {
+ return ("0" + charCode.toString(16)).slice(-2);
+};
+
+var cryptoString = new CryptoString();
+
+exports.sha1Encrypt = function(str) {
+ cryptoString.init('SHA1', 'UTF-8');
+ return cryptoString.encryptString(str);
+};
+
+exports.sha256Encrypt = function(str) {
+ cryptoString.init('SHA256', 'UTF-8');
+ return cryptoString.encryptString(str);
+};
diff --git a/lib/script_entries/dryrun_scripts.js b/lib/script_entries/dryrun_scripts.js
new file mode 100644
index 0000000..d32263b
--- /dev/null
+++ b/lib/script_entries/dryrun_scripts.js
@@ -0,0 +1,76 @@
+/**
+ * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
+ * *
+ * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+var allScripts = require('script_entries/all_scripts').allScripts;
+const urlHandler = require('url_handler/url_handler');
+
+exports.dryRunScripts = {
+ scripts: {},
+
+ truncateJsData: allScripts.truncateJsData,
+
+ getScripts: allScripts.getScripts,
+
+ isFound: allScripts.isFound,
+
+ returnWhenFound: allScripts.returnWhenFound,
+
+ getOrInitScripts: allScripts.getOrInitScripts,
+
+ reverseArray: allScripts.reverseArray,
+
+ setHash: allScripts.setHash,
+
+ clearScripts: function (url) {
+ this.scripts[url] = [];
+ },
+
+ /**
+ * addAScript
+ * adds a single script to the scripts array.
+ * @param {string} url - the url of the page where it is loaded.
+ * @param {object} scriptObj - Additional data regarding this script,
+ * including: inline: boolean,
+ * contents: string,
+ * removalReason: string.
+ */
+ addAScript: function (url, scriptObj, absoluteUrl) {
+ var exists;
+
+ if (this.scripts[url] === undefined) {
+ this.clearScripts(url);
+ }
+ if (scriptObj.inline === true) {
+ this.setHash(scriptObj);
+ this.truncateJsData(scriptObj);
+ } else if (absoluteUrl !== undefined &&
+ scriptObj.inline === false) {
+ scriptObj.contents = urlHandler.resolve(absoluteUrl, scriptObj.contents);
+ }
+ exists = this.isFound(url, scriptObj);
+
+ if (!exists) {
+ this.scripts[url].push(scriptObj);
+ return true;
+ } else {
+ return false;
+ }
+ }
+};
diff --git a/lib/script_entries/free_libraries.js b/lib/script_entries/free_libraries.js
new file mode 100644
index 0000000..ee97eea
--- /dev/null
+++ b/lib/script_entries/free_libraries.js
@@ -0,0 +1,65 @@
+/**
+ * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
+ * *
+ * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+// THIS MODULE IS DEPRECATED IN FAVOR OF THE NEW WHITELISTING MODULE (LibreJS 6.0)
+
+var relationChecker = require("js_checker/relation_checker").relationChecker;
+var checkTypes = require("js_checker/constant_types").checkTypes;
+var scriptsCached = require("./scripts_cache").scriptsCached;
+
+
+// find the json database path.
+var dbContents = require("sdk/self").data.load("script_libraries/script-libraries.json");
+
+const AUTHOR_REASON = "This script has been tagged as free software by LibreJS authors.";
+
+var freeLibraries = JSON.parse(dbContents); /* a database of the free libraries recognized by default */
+
+/*
+ * List of free libraries and their SHA256 hash.
+ * This is used to recognize the most common free libraries.
+ */
+
+var init = function () {
+
+ // relationChecker, which roughly checks if variables are window
+ // variables or not, is useless in this case. Use the same
+ // object for all entries.
+ var rc = relationChecker();
+ var library, hash;
+ var freeObj = { "type": 4, "reason": AUTHOR_REASON};
+ console.debug("Building init");
+ for (hash in freeLibraries) {
+ library = freeLibraries[hash];
+
+ // assign empty relationChecker object.
+ library.relationChecker = rc;
+
+ // make them free and nontrivial.
+ library.result = freeObj;
+
+ scriptsCached.addObjectEntry(hash, library);
+ }
+};
+
+//init();
+
+exports.init = init;
+exports.AUTHOR_REASON = AUTHOR_REASON;
diff --git a/lib/script_entries/removed_scripts.js b/lib/script_entries/removed_scripts.js
new file mode 100644
index 0000000..8f65c4d
--- /dev/null
+++ b/lib/script_entries/removed_scripts.js
@@ -0,0 +1,71 @@
+/**
+ * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
+ * *
+ * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+var allScripts = require('script_entries/all_scripts').allScripts;
+const urlHandler = require('url_handler/url_handler');
+
+var RemovedScripts = function() {
+ this.scripts = {};
+ this.truncateJsData = allScripts.truncateJsData;
+ this.getScripts = allScripts.getScripts;
+ this.isFound = allScripts.isFound;
+ this.returnWhenFound = allScripts.returnWhenFound;
+ this.getOrInitScripts = allScripts.getOrInitScripts;
+ this.reverseArray = allScripts.reverseArray;
+ this.setHash = allScripts.setHash;
+};
+
+RemovedScripts.prototype.clearScripts = function (url) {
+ this.scripts[url] = [];
+};
+
+/**
+ * addAScript
+ * adds a single script to the scripts array.
+ * @param {string} url - the url of the page where it is loaded.
+ * @param {object} scriptObj - Additional data regarding this script,
+ * including: inline: boolean,
+ * contents: string,
+ * removalReason: string.
+ */
+RemovedScripts.prototype.addAScript = function (url, scriptObj, absoluteUrl) {
+ var exists;
+
+ if (this.scripts[url] === undefined) {
+ this.clearScripts(url);
+ }
+ if (scriptObj.inline === true) {
+ this.setHash(scriptObj);
+ this.truncateJsData(scriptObj);
+ } else if (absoluteUrl !== undefined &&
+ scriptObj.inline === false) {
+ scriptObj.contents = urlHandler.resolve(absoluteUrl, scriptObj.contents);
+ }
+ exists = this.isFound(url, scriptObj);
+
+ if (!exists) {
+ this.scripts[url].push(scriptObj);
+ return true;
+ } else {
+ return false;
+ }
+};
+
+exports.removedScripts = new RemovedScripts();
diff --git a/lib/script_entries/scripts_cache.js b/lib/script_entries/scripts_cache.js
new file mode 100644
index 0000000..ca477cc
--- /dev/null
+++ b/lib/script_entries/scripts_cache.js
@@ -0,0 +1,189 @@
+/**
+ * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
+ * *
+ * Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+var relationCheckerObj = require("js_checker/relation_checker").relationChecker;
+
+// import free_libraries to populate the cache hash map.
+var free_libraries = require("script_entries/free_libraries");
+
+var crypto = require('script_entries/crypto');
+const checkTypes = require("js_checker/constant_types").checkTypes;
+
+// cachedResults contains objects with result/relationChecker for
+// scripts entries indexed by SHA1sum
+var cachedResults = {};
+
+/**
+ * ScriptsCached keeps a cache of whitelisted scripts in the browser
+ * session.
+ */
+var ScriptsCached = function() {
+};
+
+ScriptsCached.prototype.getHash = function(scriptText) {
+ require('ui/notification').createNotification(scriptText.substring(0,100));
+
+ return crypto.sha1Encrypt(scriptText);
+};
+
+/**
+ * resetCache
+ * Resets the full cache and re-initialize
+ * the free libraries list.
+ */
+ScriptsCached.prototype.resetCache = function () {
+ cachedResults = {};
+ free_libraries.init();
+};
+
+/**
+ *
+ * addEntry
+ *
+ * Adds a script entry to the cache by providing the results
+ * and the actual script text.
+ *
+ */
+ScriptsCached.prototype.addEntry = function(
+ scriptText, result, relationChecker, allowTrivial, url) {
+ console.debug("result addEntry is", JSON.stringify(result));
+ cachedResults[this.getHash(scriptText)] = {
+ 'result': result,
+ 'relationChecker': relationCheckerObj(),
+ 'allowTrivial': allowTrivial,
+ 'url': url
+ };
+};
+
+/**
+ *
+ * addEntry
+ *
+ * Adds a script entry to the cache by providing the results
+ * using the script's hash.
+ *
+ */
+ScriptsCached.prototype.addEntryByHash = function(
+ hash, result, relationChecker, allowTrivial, url) {
+ cachedResults[hash] = {
+ 'result': result,
+ 'relationChecker': relationCheckerObj(),
+ 'allowTrivial': allowTrivial,
+ 'url': url || ''
+ };
+};
+
+/**
+ * removeEntryByHash
+ *
+ * Remove an entry from the cache using hash key.
+ */
+ScriptsCached.prototype.removeEntryByHash = function(hash) {
+ delete cachedResults[hash];
+};
+
+/**
+ * addEntryIfNotCached
+ *
+ * Checks first if entry is cached before attempting to cache result.
+ */
+ScriptsCached.prototype.addEntryIfNotCached = function(
+ scriptText, result, relationChecker, allowTrivial, url) {
+ // save a bit of computing by getting hash once.
+ var hash = this.getHash(scriptText);
+ console.debug('hash is then', hash);
+ if (!this.isCached(scriptText, hash)) {
+ cachedResults[hash] = {
+ 'result': result,
+ 'relationChecker': relationCheckerObj(),
+ 'allowTrivial': allowTrivial,
+ 'url': url || ''
+ };
+ }
+ return hash;
+};
+
+/**
+ *
+ * addObjectEntry
+ *
+ * Adds a script entry by providing an object.
+ * Used to provide free library hashes from free_libraries.js
+ *
+ */
+ScriptsCached.prototype.addObjectEntry = function(hash, script) {
+ cachedResults[hash] = script;
+};
+
+ScriptsCached.prototype.isCached = function(scriptText, hash) {
+ var scriptHash;
+ console.debug("Is CACHED start?");
+ try {
+ if (typeof hash === 'string') {
+ scriptHash = hash;
+ } else {
+ scriptHash = this.getHash(scriptText);
+ }
+ if (typeof scriptHash === 'string') {
+ let cachedResult = cachedResults[scriptHash];
+ if (cachedResult) {
+ // exact copy of file has already been cached.
+ console.debug('scriptHash is', cachedResult);
+ if (cachedResult.relationChecker == "[rl]") {
+ cachedResult.relationChecker = {}; //relationCheckerObj();
+ }
+ console.debug("Is Cached ENd TRUE");
+ return cachedResult;
+ }
+ }
+ return false;
+ } catch (e) {
+ console.debug("an error", scriptHash, e, e.linenumber, e.filename);
+ }
+};
+
+/**
+ * Writes allowed scripts to the cache.
+ * nonfree/nontrivial scripts are not added to the cache.
+ */
+ScriptsCached.prototype.getCacheForWriting = function() {
+ var formattedResults = {};
+ for (let item in cachedResults) {
+ let type = cachedResults[item].result.type;
+ if (type != checkTypes.NONTRIVIAL &&
+ type != checkTypes.TRIVIAL_DEFINES_FUNCTION
+ ) {
+ formattedResults[item] = cachedResults[item];
+ }
+ }
+ return formattedResults;
+};
+
+/**
+ * Import data from database into cachedResults.
+ * Calling this function replaces the current cache if it exists.
+ */
+ScriptsCached.prototype.bulkImportCache = function(data) {
+ cachedResults = data;
+ console.debug("Imported data. Number of keys ISSS ",
+ Object.keys(cachedResults).length);
+ console.debug("It looks like ", JSON.stringify(cachedResults));
+};
+
+exports.scriptsCached = new ScriptsCached();