diff options
author | Ruben Rodriguez <ruben@gnu.org> | 2014-10-20 02:24:51 +0200 |
---|---|---|
committer | Ruben Rodriguez <ruben@gnu.org> | 2014-10-20 02:24:51 +0200 |
commit | 6e7918b6ccb69876d339a320091fdee811445395 (patch) | |
tree | 31cb88ee438d652fddefca1193f70289a8b3dcc8 /data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer | |
parent | 60e5b13c35d4d3ba21bb03b026750a0a414f6c77 (diff) |
Generalize data directory
Diffstat (limited to 'data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer')
5 files changed, 784 insertions, 0 deletions
diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/allowed_referrers.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/allowed_referrers.js new file mode 100644 index 0000000..4f524a7 --- /dev/null +++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/allowed_referrers.js @@ -0,0 +1,75 @@ +/** + * 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 prefChange = require("addon_management/prefchange"); + +/** + * Contains a list of pages that are allowed + * to execute JavaScript regardless of whether it is + * nonfree and nontrivial. + */ +exports.allowedReferrers = { + + allowed: {}, + + addPage: function (url) { + this.allowed[url] = 1; + }, + + urlInAllowedReferrers: function (url) { + + if (this.allowed[url] === 1) { + return true; + } + // check if whitelisted. + return this.urlInWhitelist(url); + + }, + + urlInWhitelist: function (url) { + var whitelist = prefChange.getWhitelist(); + var i = 0, le = whitelist.length; + + for (; i < le; i++) { + + if (whitelist[i].test(url)) { + + return true; + + } + + } + + }, + + clearSinglePageEntry: function (url) { + + var index = this.allowed[url]; + + if (this.allowed[url] === 1) { + delete this.allowed[url]; + } + + }, + + clearAllEntries: function () { + this.allowed = {}; + } +};
\ No newline at end of file diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/caching.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/caching.js new file mode 100644 index 0000000..1463b1c --- /dev/null +++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/caching.js @@ -0,0 +1,30 @@ +/** + * 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"); + +const nsICacheService = Ci.nsICacheService; +const cacheService = Cc["@mozilla.org/network/cache-service;1"].getService(nsICacheService); + +exports.clearAllCache = function () { + cacheService.evictEntries(Ci.nsICache.STORE_ON_DISK); + cacheService.evictEntries(Ci.nsICache.STORE_IN_MEMORY); +}; + diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/http_request_observer.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/http_request_observer.js new file mode 100644 index 0000000..09e66e8 --- /dev/null +++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/http_request_observer.js @@ -0,0 +1,161 @@ +/** + * 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 observerService = Cc["@mozilla.org/observer-service;1"] + .getService(Ci.nsIObserverService); + +// these are our target mime types for response interception. +var targetMimeTypes = /.*(javascript|ecmascript|html).*/i; +//var targetMimeTypes = /.*(html).*/i; + +// ensure xhr won't create an infinite loop +// with html content. +var urlTester = require("html_script_finder/url_seen_tester").urlSeenTester; + +var streamLoader = require("http_observer/stream_loader").streamLoader; + +var httpRequestObserver = { + + observe: function(request, aTopic, aData) { + + console.debug('atopic is', aTopic); + + var url, newListener, status; + + if (aTopic == "http-on-examine-response" || + aTopic == "http-on-examine-cached-response" || + aTopic == "http-on-examine-merged-response") { + + request.QueryInterface(Ci.nsIHttpChannel); + + if (request.URI.scheme != 'chrome' && + (request.responseStatus < 300 || + request.responseStatus > 399) && + (targetMimeTypes.test(request.contentType) || + request.contentType == undefined) && + (!urlTester.isWhitelisted(request.URI.spec) && + !urlTester.isWhitelisted(request.originalURI.spec))) { + + newListener = new TracingListener(); + request.QueryInterface(Ci.nsITraceableChannel); + newListener.originalListener = request.setNewListener(newListener); + + } + + else if (urlTester.isWhitelisted(request.URI.spec) || + urlTester.isWhitelisted(request.originalURI.spec)) { + urlTester.clearUrl(request.URI.spec); + urlTester.clearUrl(request.originalURI.spec); + } + + } + }, + + QueryInterface : function (aIID) { + if (aIID.equals(Ci.nsIObserver) || + aIID.equals(Ci.nsISupports)) { + return this; + } + throw Cr.NS_NOINTERFACE; + } +}; + +// Copy response listener implementation. +function TracingListener() { + this.originalListener = null; + this.streamLoader = streamLoader(); +} + +TracingListener.prototype = { + + onDataAvailable: function(request, context, inputStream, offset, count) { + + try { + this.streamLoader.loader.onDataAvailable(request, context, inputStream, offset, count); + } catch (x) { + + console.debug(x, x.lineNumber, x.fileName, "In this case, charset is"); + + } + + }, + + onStartRequest: function(request, context) { + + this.streamLoader.setOriginalListener(this.originalListener); + this.streamLoader.loader.onStartRequest(request, context); + this.originalListener.onStartRequest(request, context); + + }, + + onStopRequest: function(request, context, statusCode) { + + try { + this.streamLoader.loader.onStopRequest(request, context, statusCode); + } catch (e) { + console.debug('error in onStopRequest', e, e.lineNumber); + } + }, + + QueryInterface: function (aIID) { + if (aIID.equals(Ci.nsIStreamListener) || + aIID.equals(Ci.nsISupports)) { + return this; + } + throw Cr.NS_NOINTERFACE; + }, + + +}; + +observerService.addObserver(httpRequestObserver, + "http-on-examine-response", false); + +observerService.addObserver(httpRequestObserver, + "http-on-examine-cached-response", false); + +observerService.addObserver(httpRequestObserver, + "http-on-examine-merged-response", false); + + + +/* remove observer */ +exports.removeHttpObserver = function () { + + try { + + observerService.removeObserver(httpRequestObserver, + "http-on-examine-response"); + + observerService.removeObserver(httpRequestObserver, + "http-on-examine-cached-response"); + + observerService.removeObserver(httpRequestObserver, + "http-on-examine-merged-response"); + + console.debug('turned off http observer'); + + } catch (e) { + console.debug(e); + } + +}; diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/process_response.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/process_response.js new file mode 100644 index 0000000..759adf7 --- /dev/null +++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/process_response.js @@ -0,0 +1,408 @@ +/** + * 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 checks http responses by mime type and returns a + * modified response. + */ + +var {Cc, Ci, Cu, Cm, Cr} = require("chrome"); + +var jsChecker = require("js_checker/js_checker"); + +const types = require("js_checker/constant_types"); +var checkTypes = types.checkTypes; + +// check if scripts embedded dynamically have a jsWebLabel entry indexed by referrer. +var jsWebLabelEntries = require("html_script_finder/web_labels/js_web_labels").jsWebLabelEntries; + +var htmlParser = require("html_script_finder/html_parser"); + +var removedScripts = require("script_entries/removed_scripts").removedScripts; +var allowedRef = require('http_observer/allowed_referrers').allowedReferrers; + +var acceptedScripts = require("script_entries/accepted_scripts").acceptedScripts; + +// used to display info when a url is whitelisted. +var dryRunScripts = require("script_entries/dryrun_scripts").dryRunScripts; + +// node.js url module. Makes it easier to resolve +// urls in that datauri loaded dom +var urlHandler = require("url_handler/url_handler"); +var isDryRun = require("addon_management/prefchange").isDryRun; + +var jsMimeTypeRe = /.*(javascript|ecmascript).*/i; +var htmlMimeTypeRe = /.*(xhtml\+xml|html|multipart\/x-mixed-replace).*/i; + + +var processResponseObject = { + data: null, + myParser: null, + url: null, + scriptFinder: null, + jsCheckString: null, + referrer: null, + contentType: null, + resInfo: null, + listener: null, + req: null, + + /** + * starts the handling of a new response. + */ + init: function (listener, resInfo) { + this.resInfo = resInfo; + this.req = resInfo.request; + /* needed for this.req.referrer */ + this.req.QueryInterface(Ci.nsIHttpChannel); + this.listener = listener; + this.setData(); + this.setContentType(); + this.setUrls(); + }, + + /** + * genBinaryOutput + * Set or reset binaryOutputStream and storageStream. + */ + genBinaryOutput: function () { + this.storageStream = Cc["@mozilla.org/storagestream;1"].createInstance(Ci.nsIStorageStream); + this.binaryOutputStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(Ci.nsIBinaryOutputStream); + }, + + /** + * Gather the data gathered from onDataAvailable. + */ + setData: function () { + + this.data = this.resInfo.receivedData; + console.debug("\n\nDump of whole data:\n\n", this.data, "\n\n end of dump"); + // Prevents the http response body from being empty, + // which would throw an error. + if (this.data == '' || this.data == undefined) { + this.data = " "; + } + + }, + + /** + * Set a standardized lowercase mime type. + */ + setContentType: function() { + if (this.req.contentType != undefined) { + this.contentType = String(this.req.contentType).toLowerCase(); + } + }, + + /** + * setUrls + * Set the current URL of the response, and + * set referrer if applicable. + */ + setUrls: function() { + + if (this.req.URI != undefined) { + this.fragment = urlHandler.getFragment(this.req.URI.spec); + console.debug('fragment is', this.fragment); + this.url = urlHandler.removeFragment(this.req.URI.spec); + } + if (this.req.referrer != undefined) { + this.referrerFragment = urlHandler.getFragment(this.req.referrer.spec); + this.referrer = urlHandler.removeFragment(this.req.referrer.spec); + } + }, + + /** + * processHTML + * Modifies a string of html + */ + processHTML: function() { + + var charset = this.req.contentCharset, myParser; + + if (this.req.contentCharset != undefined && this.req.contentCharset != "") { + charset = this.req.contentCharset; + } else { + charset = ""; + } + acceptedScripts.clearScripts(this.req.URI.spec); + removedScripts.clearScripts(this.req.URI.spec); + dryRunScripts.clearScripts(this.req.URI.spec); + + console.debug('charset is', charset); + console.debug('responseStatus for', this.url, 'is', this.req.responseStatus); + + // send data to htmlParser, and pass on modified data to + // originalListener. + + myParser = htmlParser.htmlParser().parse(this.data, + charset, + this.contentType, + this.url, + this.fragment, + this.req.responseStatus, + this.htmlParseCallback.bind(this)); + }, + + /** + * + * htmlParseCallback + * + * Passed on the callback result to + * the originalListener. + * + */ + htmlParseCallback: function(result) { + + var len = result.length; + + try { + + this.listener.onDataAvailable(this.req, + this.resInfo.context, + result.newInputStream(0), 0, len); + + + } catch (e) { + + this.req.cancel(this.req.NS_BINDING_ABORTED); + + } + + this.listener.onStopRequest(this.req, + this.resInfo.context, this.resInfo.statusCode); + + }, + + /** + * processJS + * Process and modify a string of JavaScript. + */ + processJS: function() { + var checker, check, jsCheckString, + that = this; + //var start = Date.now(), end; + + try { + // make sure script isn't already listed as free + // in a JS web labels table. + if (this.checkJsWebLabelsForScript()) { + // this is free. we are done. + this.jsListenerCallback(); + return; + + } + + // analyze javascript in response. + checker = jsChecker.jsChecker(); + check = checker.searchJs(this.data, function () { + console.debug("Has been analyzing", that.data); + that.processJsCallback(checker); + }, that.url); + + + + } catch(e) { + + // any error is considered nontrivial. + console.debug('js error in js app, removing script', e); + console.debug("error", e, e.lineNumber); + // modify data that will be sent to the browser. + this.data = '// LibreJS: Script contents were removed when it was loaded from a page, because another script attempted to load this one dynamically. Please place your cursor in the url bar and press the enter key to see the source.'; + this.jsListenerCallback(); + } + + }, + + /** + * checkJsWebLabelsForScript + * + * check whether script that's been received has an entry + * in a js web labels table (lookup referrer.) + * + */ + checkJsWebLabelsForScript: function () { + + console.debug('checking script', this.url); + console.debug('current list is', JSON.stringify(jsWebLabelEntries)); + if (jsWebLabelEntries[this.referrer] != undefined) { + + var scriptList = jsWebLabelEntries[this.referrer], + i = 0, + len = scriptList.length; + + for (; i < len; i++) { + + if (scriptList[i].fileUrl === this.url && + scriptList[i].free === true) { + + console.debug(this.url, "is free and dynamic!"); + + var scriptObj = {inline: false, + url: this.url, + contents: this.url, + reason: "This script is free (see JS Web Labels page for detail)"}; + + acceptedScripts.addAScript(this.req.referrer.spec, scriptObj, "Script is free"); + + return true; + + } + + } + + + } + + }, + + processJsCallback: function(checker) { + try { + var scriptObj; + + var jsCheckString = checker.parseTree.freeTrivialCheck; + console.debug("analyzing js callback for", this.url); + // for testing only. + //var jsCheckString = {'type': checkTypes.FREE_SINGLE_ITEM }; + console.debug('jscheckstring is', jsCheckString.type); + + if (jsCheckString.type === checkTypes.NONTRIVIAL) { + if (!allowedRef.urlInAllowedReferrers(this.req.referrer.spec)) { + //if (true) { + console.debug("url", this.url, " is found nontrivial", "with reason", jsCheckString.reason); + scriptObj = {inline: false, + contents: '', + removalReason: 'nontrivial', + reason: jsCheckString.reason, + url: this.url, + hash: checker.hash}; + removedScripts.addAScript(this.req.referrer.spec, scriptObj); + + // modify data that will be sent to the browser. + this.data = '// LibreJS: Script contents were removed when it was loaded from a page, because another script attempted to load this one dynamically and its contents appear to be nonfree/nontrivial. Please hit enter in the location bar to see the actual source.'; + } else { + console.debug("writing to dry run", this.url); + scriptObj = {inline:false, + contents: '', + removalReason: 'nontrivial', + reason: jsCheckString.reason, + url: this.url, + hash:checker.hash}; + dryRunScripts.addAScript(this.req.referrer.spec, scriptObj); + } + + this.jsListenerCallback(); + + } else if (jsCheckString.type === checkTypes.FREE || + jsCheckString.type === checkTypes.FREE_SINGLE_ITEM || + jsCheckString.type === checkTypes.TRIVIAL || + jsCheckString.type === checkTypes.TRIVIAL_DEFINES_FUNCTION || + jsCheckString.type === checkTypes.WHITELISTED) { + console.debug("found a free script for", this.url, this.req.referrer.spec, jsCheckString.reason); + console.debug('found a free script', this.req.referrer.spec); + + scriptObj = {inline: false, + contents: '', + reason: jsCheckString.reason, + url: this.url, + hash:checker.hash}; + + acceptedScripts.addAScript(this.req.referrer.spec, scriptObj); + this.jsListenerCallback(); + } + + //var end = Date.now(); + console.debug('exec time', this.url, ' -- ', end - start); + } catch (x) { + console.debug('error', x); + } + }, + + /** + * ProcessAllTypes + * Calls processHTML or JS if it finds an appropriate content + * type. For everything else it just passes on the data to the + * original listener. + */ + processAllTypes: function() { + // toggle xlibrejs if X-LibreJS is set. + + // process HTML + if ((htmlMimeTypeRe.test(this.contentType) || + this.req.contentType === undefined)) { + this.processHTML(); + return; + } + + else { + // process external JS files that are called from another + // file (and hence have a referrer). + + if (this.referrer != undefined && + jsMimeTypeRe.test(this.contentType) && + !(acceptedScripts.isFound(this.referrer, {inline: false, contents: this.url})) && + !(acceptedScripts.isFound(this.referrer, {inline:false, contents:this.req.originalURI.spec}))) { + + // console.debug('process js triggered for', this.url); + this.processJS(); + + } else { + this.jsListenerCallback(); + } + + } + + }, + + jsListenerCallback: function () { + + var len = this.data.length; + + this.genBinaryOutput(); + + this.storageStream.init(8192, len, null); + this.binaryOutputStream.setOutputStream(this.storageStream.getOutputStream(0)); + this.binaryOutputStream.writeBytes(this.data, len); + + try { + this.listener.onDataAvailable(this.req, + this.resInfo.context, + this.storageStream.newInputStream(0), + 0, len); + } catch (e) { + this.req.cancel(this.req.NS_BINDING_ABORTED); + } + + this.listener.onStopRequest(this.req, + this.resInfo.context, + this.resInfo.statusCode); + + } + + +}; + +// creates an instance of processResponseObject. +exports.ProcessResponse = function (listener, resInfo) { + console.debug('triggered'); + var procResponse = Object.create(processResponseObject); + procResponse.init(listener, resInfo); + return procResponse; +}; diff --git a/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/stream_loader.js b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/stream_loader.js new file mode 100644 index 0000000..bd27132 --- /dev/null +++ b/data/extensions/jid1-KtlZuoiikVfFew@jetpack/resources/librejs/lib/http_observer/stream_loader.js @@ -0,0 +1,110 @@ +/** + * 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"); + +const processResponse = require('http_observer/process_response'); + +var StreamLoader = function() { + this.loader = null; + this.listener = null; + this.originalListener = null; +} + +StreamLoader.prototype.setOriginalListener = function(listener) { + this.originalListener = listener; +}; + +StreamLoader.prototype.init = function() { + try { + var that = this; + this.listener = new StreamListener(); + + this.listener.callback = function (loader, context, status, data) { + console.debug("here is the data", data); + var responseInfo = {'request': loader.channel, + 'context': context, + 'statusCode': status, + 'receivedData': data}; + var responseHandler = processResponse.ProcessResponse(that.originalListener, responseInfo); + responseHandler.processAllTypes(); + + that.destroy(); + }; + + this.loader = Cc["@mozilla.org/network/unichar-stream-loader;1"]. + createInstance(Ci.nsIUnicharStreamLoader); + + this.loader.init(this.listener); + } catch (e) { + console.debug(e); + } +}; + +StreamLoader.prototype.destroy = function () { + this.loader = null; + this.listener = null; +}; + +var getRegexForContentType = function (contentType) { + if (/xhtml/i.test(contentType)) { + return /<\?[^>]*?encoding=(?:["']*)([^"'\s\?>]+)(?:["']*)/i; + } + + // return the regular html regexp for anything else. + return /<meta[^>]*?charset=(?:["']*)([^"'\s>]+)(?:["']*)/i; +}; + +var StreamListener = function() {}; + +StreamListener.prototype.QueryInterface = function listener_qi(iid) { + if (iid.equals(Ci.nsISupports) || + iid.equals(Ci.nsIUnicharStreamLoaderObserver)) { + return this; + } + throw Cr.NS_ERROR_NO_INTERFACE; +}; + +StreamListener.prototype.onStreamComplete = function onStreamComplete( + loader, context, status, data) { + this.callback(loader, context, status, data); +}; + +StreamListener.prototype.onDetermineCharset = function onDetermineCharset( + loader, context, data) { + var match, regex; + if (loader.channel.contentCharset != undefined && + loader.channel.contentCharset != "") { + return loader.channel.contentCharset; + } else { + match = getRegexForContentType(loader.channel.contentType).exec(data); + if (match) { + loader.channel.contentCharset = match[1]; + return match[1]; + } else { + return "UTF-8"; + } + } +}; + +exports.streamLoader = function () { + var l = new StreamLoader(); + l.init(); + return l; +}; |