From f9503a4a214e35afec0d1223278237d253127c8c Mon Sep 17 00:00:00 2001 From: Michael Kaply <345868+mkaply@users.noreply.github.com> Date: Wed, 4 Dec 2019 10:48:26 -0600 Subject: [PATCH] Update fileblock for Firefox 68 --- README.md | 11 +- buildit.sh | 11 -- chrome.manifest | 2 +- components/fileBlockService.js | 183 ++++++++++----------------------- excludefile.txt | 8 -- install.rdf | 19 ---- modules/fileBlockModule.js | 77 ++++++++++++++ 7 files changed, 138 insertions(+), 173 deletions(-) delete mode 100644 buildit.sh delete mode 100644 excludefile.txt delete mode 100644 install.rdf create mode 100644 modules/fileBlockModule.js diff --git a/README.md b/README.md index 184963b..2ac2ae9 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,12 @@ It is not 100% foolproof, but it covers most cases. It prevents the loading of files within the browser and removes all the UI associated with opening files. -The best way to use it is with the CCK2. Create a directory called "bundles" under the CCK2 +The only way to use it is with the CCK2. Create a directory called "bundles" under the CCK2 directory. Then create a directory called fileblock in that directory. -Unzip the XPI into that directory. (It's just a zip file.) -If you would like to allow certain files, you can modify fileBlockService.js. +Unzip the ZIP into that directory. (It's just a zip file.) + +If you would like to allow certain files, you can modify fileBlockModule.js. To access fileBlockService.js, unzip the XPI file using a standard zip program. @@ -54,7 +55,3 @@ FileBlock is a product of Kaply Consulting. We provide other products to help yo customize Firefox like the CCK2. We also provide consulting services. For more information, go to http://mike.kaply.com. - -And if this extension has benefited you or your organization, please consider -puchasing support for the CCK2 (http://mike.kaply.com/cck2/) or making a donation -(https://addons.mozilla.org/en-US/firefox/addon/cck2wizard/developers). \ No newline at end of file diff --git a/buildit.sh b/buildit.sh deleted file mode 100644 index 1ebb42c..0000000 --- a/buildit.sh +++ /dev/null @@ -1,11 +0,0 @@ -SHORTNAME=fileblock -rm *.xpi -rm -rf $SHORTNAME -mkdir $SHORTNAME -cd $SHORTNAME -rsync -r --exclude=.svn --exclude-from=../excludefile.txt ../* . -VERSION=`grep "em:version" install.rdf | sed -e 's/[ \t]*em:version=//;s/"//g'` -TOOLBAR=$SHORTNAME-$VERSION -zip -r -D ../$TOOLBAR.xpi * -cd .. -rm -rf $SHORTNAME diff --git a/chrome.manifest b/chrome.manifest index 618d476..2eabca3 100644 --- a/chrome.manifest +++ b/chrome.manifest @@ -1,4 +1,4 @@ +resource fileblock modules/ component {cbea9627-60c0-4718-8545-72a6c3c4b8a3} components/fileBlockService.js contract @kaply.com/fileblock-service;1 {cbea9627-60c0-4718-8545-72a6c3c4b8a3} -category content-policy policy @kaply.com/fileblock-service;1 category profile-after-change FileBlock @kaply.com/fileblock-service;1 diff --git a/components/fileBlockService.js b/components/fileBlockService.js index 8ad7557..87505df 100644 --- a/components/fileBlockService.js +++ b/components/fileBlockService.js @@ -1,127 +1,56 @@ -var gPathWhitelist = []; -var gExtensionWhitelist = []; - -var gHideAddon = false; - -const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - -Cu.import("resource://gre/modules/Services.jsm"); -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); - -function FileBlock() {} - -FileBlock.prototype = { - appDir: null, - profileDir: null, - tempDir: null, - shouldLoad: function(aContentType, aContentLocation, aRequestOrigin, aContext, aMimeTypeGuess, aExtra) { - if (!this.appDir) { - this.appDir = Services.io.newFileURI(Services.dirsvc.get("CurProcD", Ci.nsIFile)).spec; - this.profileDir = Services.io.newFileURI(Services.dirsvc.get("ProfD", Ci.nsIFile)).spec; - this.tempDir = Services.io.newFileURI(Services.dirsvc.get("TmpD", Ci.nsIFile)).spec; - var userDir = Services.dirsvc.get("Home", Ci.nsIFile); - for (var i=0; i < gPathWhitelist.length; i++) { - gPathWhitelist[i] = gPathWhitelist[i].replace("~", userDir.path); - } - } - // We need to allow access to any files in the profile directory, - // application directory or the temporary directory. Without these, - // Firefox won't start - if (aContentLocation.spec.match(this.profileDir) || - aContentLocation.spec.match(this.appDir) || - aContentLocation.spec.match(this.tempDir)) { - return Ci.nsIContentPolicy.ACCEPT; - } - // Deny all files except those on the whitelists - if (aContentLocation.scheme == "file") { - var file = aContentLocation.QueryInterface(Components.interfaces.nsIFileURL).file; - for (var i=0; i< gPathWhitelist.length; i++) { - if (file.path.toLowerCase().indexOf(gPathWhitelist[i].toLowerCase()) == 0) { - return Ci.nsIContentPolicy.ACCEPT; - } - } - for (var i=0; i< gExtensionWhitelist.length; i++) { - var filename = file.leafName.toLowerCase(); - var extension = "." + gExtensionWhitelist[i].toLowerCase(); - if (filename.indexOf(extension, filename.length - extension.length) !== -1) { - return Ci.nsIContentPolicy.ACCEPT; - } - } - return Ci.nsIContentPolicy.REJECT_REQUEST; - } - // Allow everything else - return Ci.nsIContentPolicy.ACCEPT; - }, - shouldProcess: function(aContentType, aContentLocation, aRequestOrigin, aContext, aMimeTypeGuess, aExtra) { - return Ci.nsIContentPolicy.ACCEPT; - }, - observe: function(aSubject, aTopic, aData) { - switch(aTopic) { - case "profile-after-change": - Services.obs.addObserver(documentObserver, "chrome-document-global-created", false); - Services.obs.addObserver(FileBlock, "profile-before-change", false); - break; - case "profile-before-change": - Services.obs.removeObserver(FileBlock, "profile-before-change", false); - Services.obs.removeObserver(documentObserver, "chrome-document-global-created", false); - break; - } - }, - classDescription: "FileBlock Service", - contractID: "@kaply.com/fileblock-service;1", - classID: Components.ID('{cbea9627-60c0-4718-8545-72a6c3c4b8a3}'), - QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIObserver]) -} - -var documentObserver = { - observe: function(aSubject, aTopic, aData) { - switch(aTopic) { - case "chrome-document-global-created": - if (!(aSubject instanceof Ci.nsIDOMWindow)) { - return; - } - var win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); - win.addEventListener("load", function(event) { - win.removeEventListener("load", arguments.callee, false); - var doc = event.target; - var url = doc.location.href.split("?")[0].split("#")[0]; - switch (url) { - case "chrome://browser/content/browser.xul": - try { - doc.getElementById("menu_openFile").hidden = true; - } catch (e) {} - try { - doc.getElementById("appmenu_openFile").hidden = true; - } catch (e) {} - try { - var keyset = doc.getElementById("openFileKb").removeAttribute("command"); - } catch (e) {} - try { - Components.utils.import("resource:///modules/CustomizableUI.jsm"); - CustomizableUI.destroyWidget("open-file-button") - } catch (e) {} - break; - case "chrome://mozapps/content/extensions/extensions.xul": - case "about:addons": - if (!gHideAddon) { - return; - } - try { - doc.querySelector("richlistitem[value='fileblock@kaply.com']").hidden = true; - } catch (e) {} - win.addEventListener("ViewChanged", function() { - try { - doc.querySelector("richlistitem[value='fileblock@kaply.com']").hidden = true; - } catch (e) {} - } , false) - } - }, false); - } - } -} - -if (XPCOMUtils.generateNSGetFactory) { - var NSGetFactory = XPCOMUtils.generateNSGetFactory([FileBlock]); -} else { - var NSGetModule = XPCOMUtils.generateNSGetModule([FileBlock]); -} +const {classes: Cc, interfaces: Ci, utils: Cu} = Components; + +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +function FileBlock() {} + +FileBlock.prototype = { + observe: function(aSubject, aTopic, aData) { + switch(aTopic) { + case "profile-after-change": + Services.ppmm.loadProcessScript("resource://fileblock/fileBlockModule.js", true); + Services.obs.addObserver(FileBlock, "profile-before-change", false); + Services.obs.addObserver(documentObserver, "browser-window-before-show", false); + break; + case "profile-before-change": + Services.obs.removeObserver(FileBlock, "profile-before-change", false); + Services.obs.removeObserver(documentObserver, "browser-window-before-show", false); + break; + } + }, + classDescription: "FileBlock Service", + contractID: "@kaply.com/fileblock-service;1", + classID: Components.ID('{cbea9627-60c0-4718-8545-72a6c3c4b8a3}'), + QueryInterface: ("generateQI" in XPCOMUtils) ? XPCOMUtils.generateQI([Ci.nsIObserver]) : ChromeUtils.generateQI([Ci.nsIObserver]), +} + +var documentObserver = { + observe: function(aSubject, aTopic, aData) { + switch(aTopic) { + case "browser-window-before-show": + var win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); + let doc = win.document; + try { + doc.getElementById("menu_openFile").hidden = true; + } catch (e) {} + try { + doc.getElementById("appMenu-open-file-button").hidden = true; + } catch (e) {} + try { + var keyset = doc.getElementById("openFileKb").removeAttribute("command"); + } catch (e) {} + try { + Components.utils.import("resource:///modules/CustomizableUI.jsm"); + CustomizableUI.destroyWidget("open-file-button") + } catch (e) {} + break; + } + } +} + +if (XPCOMUtils.generateNSGetFactory) { + var NSGetFactory = XPCOMUtils.generateNSGetFactory([FileBlock]); +} else { + var NSGetModule = XPCOMUtils.generateNSGetModule([FileBlock]); +} diff --git a/excludefile.txt b/excludefile.txt deleted file mode 100644 index 49a4cbd..0000000 --- a/excludefile.txt +++ /dev/null @@ -1,8 +0,0 @@ -.DS_Store -*.kpf -buildit.sh -excludefile.txt -idls -update.rdf -update.rdf.orig -README.txt \ No newline at end of file diff --git a/install.rdf b/install.rdf deleted file mode 100644 index 46e488e..0000000 --- a/install.rdf +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - {ec8030f7-c20a-464f-9b0e-13a3a9e97384} - 4.0 - 10.* - - - - diff --git a/modules/fileBlockModule.js b/modules/fileBlockModule.js new file mode 100644 index 0000000..6e24a32 --- /dev/null +++ b/modules/fileBlockModule.js @@ -0,0 +1,77 @@ +const {classes: Cc, interfaces: Ci, utils: Cu} = Components; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import('resource://gre/modules/Timer.jsm'); + +let LocalFileBlockPolicy = { + appDir: null, + profileDir: null, + tempDir: null, + fileExtensionsBlackList: [], + shouldLoad(contentLocation, loadInfo, mimeTypeGuess) { + if (!this.appDir) { + try { + this.appDir = Services.io.newFileURI(Services.dirsvc.get("CurProcD", Ci.nsIFile)).spec; + } catch (e) {} + } + if (!this.profileDir) { + try { + this.profileDir = Services.io.newFileURI(Services.dirsvc.get("ProfD", Ci.nsIFile)).spec; + } catch (e) {} + } + if (!this.tempDir) { + try { + this.tempDir = Services.io.newFileURI(Services.dirsvc.get("TmpD", Ci.nsIFile)).spec; + } catch (e) {} + } + if (!this.fileExtensionsBlackList.length) { + try { + this.fileExtensionsBlackList = Services.prefs.getCharPref("fileExtensions.blacklist").split(","); + } catch (e) {} + } + // We need to allow access to any files in the profile directory, + // application directory or the temporary directory. Without these, + // Firefox won't start + if (contentLocation.spec.match(this.profileDir) || + contentLocation.spec.match(this.appDir) || + contentLocation.spec.match(this.tempDir)) { + return Ci.nsIContentPolicy.ACCEPT; + } + if (contentLocation.scheme == "file") { + let contentType = loadInfo.externalContentPolicyType; + if (contentType == Ci.nsIContentPolicy.TYPE_DOCUMENT) { + setTimeout(function() { + Services.ww.activeWindow.location.href = "about:blank"; + }, 0); + } + return Ci.nsIContentPolicy.REJECT_REQUEST; + } + try { + // Might not be an nsIURL + if (this.fileExtensionsBlackList.includes(contentLocation.QueryInterface(Ci.nsIURL).fileExtension)) { + return Ci.nsIContentPolicy.REJECT_REQUEST; + } + } catch (e) {} + return Ci.nsIContentPolicy.ACCEPT; + }, + shouldProcess(contentLocation, loadInfo, mimeTypeGuess) { + return Ci.nsIContentPolicy.ACCEPT; + }, + classDescription: "FileBlock Content Service", + contractID: "@kaply.com/fileblock-content-service;1", + classID: Components.ID('{d85791cf-3ae5-45e3-bebe-b567df75c7f4}'), + QueryInterface: ChromeUtils.generateQI([Ci.nsIContentPolicy]), + createInstance(outer, iid) { + return this.QueryInterface(iid); + } +}; + +let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); +registrar.registerFactory(LocalFileBlockPolicy.classID, + LocalFileBlockPolicy.classDescription, + LocalFileBlockPolicy.contractID, + LocalFileBlockPolicy); + +let cm = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); +cm.addCategoryEntry("content-policy", LocalFileBlockPolicy.contractID, LocalFileBlockPolicy.contractID, false, true);