Skip to content

Commit

Permalink
Update fileblock for Firefox 68
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaply committed Dec 4, 2019
1 parent 89e0f00 commit f9503a4
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 173 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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).
11 changes: 0 additions & 11 deletions buildit.sh

This file was deleted.

2 changes: 1 addition & 1 deletion chrome.manifest
Original file line number Diff line number Diff line change
@@ -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
183 changes: 56 additions & 127 deletions components/fileBlockService.js
Original file line number Diff line number Diff line change
@@ -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]);
}
8 changes: 0 additions & 8 deletions excludefile.txt

This file was deleted.

19 changes: 0 additions & 19 deletions install.rdf

This file was deleted.

77 changes: 77 additions & 0 deletions modules/fileBlockModule.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit f9503a4

Please sign in to comment.