Skip to content

Commit

Permalink
solve problems about URIs without host
Browse files Browse the repository at this point in the history
Beginning with this commit it's possible to add rules which do
not specify a host, but a scheme instead. Such rules can match
URIs which do not have a host.

This commit resolves #447.
  • Loading branch information
myrdd committed Apr 28, 2015
1 parent 160ceb6 commit aee58a1
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 186 deletions.
13 changes: 0 additions & 13 deletions src/content/lib/request-processor.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -586,19 +586,6 @@ let RequestProcessor = (function(self) {



let uriSchemeShouldLoadResult = request.checkURISchemes().shouldLoad;
if (uriSchemeShouldLoadResult !== null) {
request.requestResult = new RequestResult(uriSchemeShouldLoadResult,
REQUEST_REASON_COMPATIBILITY);
if (uriSchemeShouldLoadResult === true) {
return accept("Allowing request due to scheme-workaround", request);
} else {
return reject("Blocking request due to scheme-workaround", request);
}
}



// Note: If changing the logic here, also make necessary changes to
// isAllowedRedirect).

Expand Down
124 changes: 0 additions & 124 deletions src/content/lib/request.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -265,130 +265,6 @@ NormalRequest.prototype.getBrowser = function() {
};


// see https://github.com/RequestPolicyContinued/requestpolicy/issues/447
var knownSchemesWithoutHost = [
// common schemes
"about",
"feed",
"mediasource",
"mailto",

// custom schemes
"magnet",
"UT2004",
"gopher",
"spotify",
"greasemonkey-script",
"floatnotes"
];

function isKnownSchemeWithoutHost(scheme) {
for (let i = 0, len = knownSchemesWithoutHost.length; i < len; ++i) {
if (scheme == knownSchemesWithoutHost[i]) {
return true;
}
}
return false;
}

NormalRequest.prototype.checkURISchemes = function() {
/**
* This is a workaround to the problem that RequestPolicy currently cannot
* handle some URIs. This workaround should be removed not later than for
* the stable 1.0 release.
*
* see https://github.com/RequestPolicyContinued/requestpolicy/issues/447
*
* TODO: solve this problem and remove this workaround.
*/
let unknownSchemesDetected = false;
let uris = [this.aContentLocation, this.aRequestOrigin];
for (let i = 0; i < 2; ++i) {
let uri = uris[i];

// filter URIs which *do* have a host
try {
// this might throw NS_ERROR_FAILURE
if (uri.host) {
continue;
}
} catch(e) {}

// ensure that the URI has a scheme
try {
if (!uri.scheme) {
throw "no scheme!";
}
} catch(e) {
Logger.warning(Logger.TYPE_CONTENT,
"URI <" + uri.spec + "> has no scheme!");
continue;
}

let scheme = uri.scheme;
if (scheme == "file") {
continue;
}

if (isKnownSchemeWithoutHost(scheme)) {
Logger.warning(Logger.TYPE_CONTENT,
"RequestPolicy currently cannot handle '" + scheme + "' schemes. " +
"Therefore the request from <" + this.originURI + "> to <" +
this.destURI + "> is allowed (but not recorded).");
// tell shouldLoad() to return CP_OK:
return {shouldLoad: true};
}

// if we get here, the scheme is unknown. try to show a notification.
unknownSchemesDetected = true;
Logger.warning(Logger.TYPE_CONTENT,
"uncatched scheme '" + scheme + "'. The request is from <" +
this.originURI + "> to <" + this.destURI + "> ");

let chromeWin, browser;
try {
chromeWin = this.getChromeWindow();
if (!chromeWin) {
throw "The chrome window could not be extracted from aContext.";
}
browser = this.getBrowser();
} catch (e) {
Logger.warning(Logger.TYPE_INTERNAL,
"The user could not be informed about the " +
"unknown scheme. Error was: " + e, e);
}

// Try showing the notification multiple times. This is done
// async for two reasons:
// (a) The chrome window's overlay might not be initialized
// yet.
// (b) This function is called by the request processor, so
// it should terminate asap.
Utils.tryMultipleTimes(function (aTriesLeft) {
try {
let overlay = chromeWin.requestpolicy.overlay;
overlay.showSchemeNotification(browser, scheme);
return true;
} catch (e) {
if (aTriesLeft === 0) {
Logger.warning(Logger.TYPE_INTERNAL,
"Failed to show the scheme notification. " +
"Error was: " + e, e);
} else {
Logger.warning(Logger.TYPE_INTERNAL,
"Failed to show the scheme notification. " +
"Trying again...");
}
return false;
}
}, 3);

}

return {shouldLoad: unknownSchemesDetected ? false : null};
};




function RedirectRequest(httpResponse) {
Expand Down
49 changes: 0 additions & 49 deletions src/content/ui/overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,55 +389,6 @@ requestpolicy.overlay = (function() {
}
};

/**
* Shows a notification that an unknown scheme has been detected.
* This notification in only necessary for 1.0 beta versions until custom
* URI schemes are supported in RequestPolicy.
*
* issue: https://github.com/RequestPolicyContinued/requestpolicy/issues/447
*
* @param {browser} browser
* @param {String} scheme
*/
self.showSchemeNotification = function(browser, scheme) {
let notificationBox = gBrowser.getNotificationBox(browser);
let notificationValue = "requestpolicy-scheme-notification";

let notification = notificationBox
.getNotificationWithValue(notificationValue);

var notificationLabel = "This page contains a request with a '" + scheme +
"' scheme which is unknown to RequestPolicy. Please report it.";

if (notification) {
notification.label = notificationLabel;
} else {
var buttons = [
{
label : "report this / more info",
accessKey : "r",
popup : null,
callback : function() {
let url = "https://github.com/RequestPolicyContinued/requestpolicy/issues/447";
window.openUILinkIn(url, "tab", {relatedToCurrent: true});
}
},
{
label : "hide",
accessKey : "h",
popup : null,
callback : function() {
// Do nothing. The notification closes when this is called.
}
}
];
const priority = notificationBox.PRIORITY_WARNING_LOW;
let iconURI = "chrome://requestpolicy/skin/requestpolicy-icon-blocked.png";
notificationBox.appendNotification(notificationLabel, notificationValue,
iconURI, priority, buttons);
}
};


/**
* Takes an URI, crops it if necessary, and returns it.
Expand Down

0 comments on commit aee58a1

Please sign in to comment.