Skip to content

Commit

Permalink
Bug 1464552: Part 2 - Split blocked site handler into separate JSM. r…
Browse files Browse the repository at this point in the history
…=felipe

MozReview-Commit-ID: H4d6qThnguk
  • Loading branch information
kmaglione committed May 26, 2018
1 parent e90be90 commit 0e954ea
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 126 deletions.
131 changes: 5 additions & 126 deletions browser/base/content/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ChromeUtils.import("resource://gre/modules/Services.jsm");
var global = this;

XPCOMUtils.defineLazyModuleGetters(this, {
BlockedSiteContent: "resource:///modules/BlockedSiteContent.jsm",
BrowserUtils: "resource://gre/modules/BrowserUtils.jsm",
ContentLinkHandler: "resource:///modules/ContentLinkHandler.jsm",
ContentMetaHandler: "resource:///modules/ContentMetaHandler.jsm",
Expand All @@ -28,7 +29,6 @@ XPCOMUtils.defineLazyModuleGetters(this, {
FormSubmitObserver: "resource:///modules/FormSubmitObserver.jsm",
NetErrorContent: "resource:///modules/NetErrorContent.jsm",
PageMetadata: "resource://gre/modules/PageMetadata.jsm",
SafeBrowsing: "resource://gre/modules/SafeBrowsing.jsm",
WebNavigationFrames: "resource://gre/modules/WebNavigationFrames.jsm",
ContextMenu: "resource:///modules/ContextMenu.jsm",
});
Expand Down Expand Up @@ -81,31 +81,6 @@ addEventListener("blur", function(event) {
LoginManagerContent.onUsernameInput(event);
});

function getSiteBlockedErrorDetails(docShell) {
let blockedInfo = {};
if (docShell.failedChannel) {
let classifiedChannel = docShell.failedChannel.
QueryInterface(Ci.nsIClassifiedChannel);
if (classifiedChannel) {
let httpChannel = docShell.failedChannel.QueryInterface(Ci.nsIHttpChannel);

let reportUri = httpChannel.URI.clone();

// Remove the query to avoid leaking sensitive data
if (reportUri instanceof Ci.nsIURL) {
reportUri = reportUri.mutate()
.setQuery("")
.finalize();
}

blockedInfo = { list: classifiedChannel.matchedList,
provider: classifiedChannel.matchedProvider,
uri: reportUri.asciiSpec };
}
}
return blockedInfo;
}

var AboutBlockedSiteListener = {
init(chromeGlobal) {
addMessageListener("DeceptiveBlockedDetails", this);
Expand All @@ -121,11 +96,7 @@ var AboutBlockedSiteListener = {
return;
}

if (msg.name == "DeceptiveBlockedDetails") {
sendAsyncMessage("DeceptiveBlockedDetails:Result", {
blockedInfo: getSiteBlockedErrorDetails(docShell),
});
}
BlockedSiteContent.receiveMessage(global, msg);
},

handleEvent(aEvent) {
Expand All @@ -137,77 +108,10 @@ var AboutBlockedSiteListener = {
return;
}

let blockedInfo = getSiteBlockedErrorDetails(docShell);
let provider = blockedInfo.provider || "";

let doc = content.document;

/**
* Set error description link in error details.
* For example, the "reported as a deceptive site" link for
* blocked phishing pages.
*/
let desc = Services.prefs.getCharPref(
"browser.safebrowsing.provider." + provider + ".reportURL", "");
if (desc) {
doc.getElementById("error_desc_link").setAttribute("href", desc + aEvent.detail.url);
}

// Set other links in error details.
switch (aEvent.detail.err) {
case "malware":
doc.getElementById("report_detection").setAttribute("href",
(SafeBrowsing.getReportURL("MalwareMistake", blockedInfo) ||
"https://www.stopbadware.org/firefox"));
doc.getElementById("learn_more_link").setAttribute("href",
"https://www.stopbadware.org/firefox");
break;
case "unwanted":
doc.getElementById("learn_more_link").setAttribute("href",
"https://www.google.com/about/unwanted-software-policy.html");
break;
case "phishing":
doc.getElementById("report_detection").setAttribute("href",
(SafeBrowsing.getReportURL("PhishMistake", blockedInfo) ||
"https://safebrowsing.google.com/safebrowsing/report_error/?tpl=mozilla"));
doc.getElementById("learn_more_link").setAttribute("href",
"https://www.antiphishing.org//");
break;
}

// Set the firefox support url.
doc.getElementById("firefox_support").setAttribute("href",
Services.urlFormatter.formatURLPref("app.support.baseURL") + "phishing-malware");

// Show safe browsing details on load if the pref is set to true.
let showDetails = Services.prefs.getBoolPref("browser.xul.error_pages.show_safe_browsing_details_on_load");
if (showDetails) {
let details = content.document.getElementById("errorDescriptionContainer");
details.removeAttribute("hidden");
}

// Set safe browsing advisory link.
let advisoryUrl = Services.prefs.getCharPref(
"browser.safebrowsing.provider." + provider + ".advisoryURL", "");
if (!advisoryUrl) {
let el = content.document.getElementById("advisoryDesc");
el.remove();
return;
}

let advisoryLinkText = Services.prefs.getCharPref(
"browser.safebrowsing.provider." + provider + ".advisoryName", "");
if (!advisoryLinkText) {
let el = content.document.getElementById("advisoryDesc");
el.remove();
return;
}

let anchorEl = content.document.getElementById("advisory_provider");
anchorEl.setAttribute("href", advisoryUrl);
anchorEl.textContent = advisoryLinkText;
BlockedSiteContent.handleEvent(global, aEvent);
},
};
AboutBlockedSiteListener.init(this);

var AboutNetAndCertErrorListener = {
init(chromeGlobal) {
Expand Down Expand Up @@ -262,9 +166,7 @@ var AboutNetAndCertErrorListener = {
NetErrorContent.handleEvent(global, aEvent);
},
};

AboutNetAndCertErrorListener.init(this);
AboutBlockedSiteListener.init(this);

var ClickEventHandler = {
init: function init() {
Expand All @@ -288,7 +190,7 @@ var ClickEventHandler = {
NetErrorContent.onCertError(global, originalTarget, ownerDoc.defaultView);
return;
} else if (ownerDoc.documentURI.startsWith("about:blocked")) {
this.onAboutBlocked(originalTarget, ownerDoc);
BlockedSiteContent.onAboutBlocked(global, originalTarget, ownerDoc);
return;
} else if (AboutNetAndCertErrorListener.isAboutNetError(ownerDoc)) {
NetErrorContent.onAboutNetError(global, event, ownerDoc.documentURI);
Expand Down Expand Up @@ -366,29 +268,6 @@ var ClickEventHandler = {
}
},

onAboutBlocked(targetElement, ownerDoc) {
var reason = "phishing";
if (/e=malwareBlocked/.test(ownerDoc.documentURI)) {
reason = "malware";
} else if (/e=unwantedBlocked/.test(ownerDoc.documentURI)) {
reason = "unwanted";
} else if (/e=harmfulBlocked/.test(ownerDoc.documentURI)) {
reason = "harmful";
}

let docShell = ownerDoc.defaultView.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell);

sendAsyncMessage("Browser:SiteBlockedError", {
location: ownerDoc.location.href,
reason,
elementId: targetElement.getAttribute("id"),
isTopFrame: (ownerDoc.defaultView.parent === ownerDoc.defaultView),
blockedInfo: getSiteBlockedErrorDetails(docShell),
});
},

/**
* Extracts linkNode and href for the current click target.
*
Expand Down
148 changes: 148 additions & 0 deletions browser/modules/BlockedSiteContent.jsm
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");

var EXPORTED_SYMBOLS = ["BlockedSiteContent"];

ChromeUtils.defineModuleGetter(this, "SafeBrowsing",
"resource://gre/modules/SafeBrowsing.jsm");

function getSiteBlockedErrorDetails(docShell) {
let blockedInfo = {};
if (docShell.failedChannel) {
let classifiedChannel = docShell.failedChannel.
QueryInterface(Ci.nsIClassifiedChannel);
if (classifiedChannel) {
let httpChannel = docShell.failedChannel.QueryInterface(Ci.nsIHttpChannel);

let reportUri = httpChannel.URI.clone();

// Remove the query to avoid leaking sensitive data
if (reportUri instanceof Ci.nsIURL) {
reportUri = reportUri.mutate()
.setQuery("")
.finalize();
}

blockedInfo = { list: classifiedChannel.matchedList,
provider: classifiedChannel.matchedProvider,
uri: reportUri.asciiSpec };
}
}
return blockedInfo;
}

var BlockedSiteContent = {
receiveMessage(global, msg) {
if (msg.name == "DeceptiveBlockedDetails") {
global.sendAsyncMessage("DeceptiveBlockedDetails:Result", {
blockedInfo: getSiteBlockedErrorDetails(global.docShell),
});
}
},

handleEvent(global, aEvent) {
if (aEvent.type != "AboutBlockedLoaded") {
return;
}

let {content} = global;

let blockedInfo = getSiteBlockedErrorDetails(global.docShell);
let provider = blockedInfo.provider || "";

let doc = content.document;

/**
* Set error description link in error details.
* For example, the "reported as a deceptive site" link for
* blocked phishing pages.
*/
let desc = Services.prefs.getCharPref(
"browser.safebrowsing.provider." + provider + ".reportURL", "");
if (desc) {
doc.getElementById("error_desc_link").setAttribute("href", desc + aEvent.detail.url);
}

// Set other links in error details.
switch (aEvent.detail.err) {
case "malware":
doc.getElementById("report_detection").setAttribute("href",
(SafeBrowsing.getReportURL("MalwareMistake", blockedInfo) ||
"https://www.stopbadware.org/firefox"));
doc.getElementById("learn_more_link").setAttribute("href",
"https://www.stopbadware.org/firefox");
break;
case "unwanted":
doc.getElementById("learn_more_link").setAttribute("href",
"https://www.google.com/about/unwanted-software-policy.html");
break;
case "phishing":
doc.getElementById("report_detection").setAttribute("href",
(SafeBrowsing.getReportURL("PhishMistake", blockedInfo) ||
"https://safebrowsing.google.com/safebrowsing/report_error/?tpl=mozilla"));
doc.getElementById("learn_more_link").setAttribute("href",
"https://www.antiphishing.org//");
break;
}

// Set the firefox support url.
doc.getElementById("firefox_support").setAttribute("href",
Services.urlFormatter.formatURLPref("app.support.baseURL") + "phishing-malware");

// Show safe browsing details on load if the pref is set to true.
let showDetails = Services.prefs.getBoolPref("browser.xul.error_pages.show_safe_browsing_details_on_load");
if (showDetails) {
let details = content.document.getElementById("errorDescriptionContainer");
details.removeAttribute("hidden");
}

// Set safe browsing advisory link.
let advisoryUrl = Services.prefs.getCharPref(
"browser.safebrowsing.provider." + provider + ".advisoryURL", "");
if (!advisoryUrl) {
let el = content.document.getElementById("advisoryDesc");
el.remove();
return;
}

let advisoryLinkText = Services.prefs.getCharPref(
"browser.safebrowsing.provider." + provider + ".advisoryName", "");
if (!advisoryLinkText) {
let el = content.document.getElementById("advisoryDesc");
el.remove();
return;
}

let anchorEl = content.document.getElementById("advisory_provider");
anchorEl.setAttribute("href", advisoryUrl);
anchorEl.textContent = advisoryLinkText;
},

onAboutBlocked(global, targetElement, ownerDoc) {
var reason = "phishing";
if (/e=malwareBlocked/.test(ownerDoc.documentURI)) {
reason = "malware";
} else if (/e=unwantedBlocked/.test(ownerDoc.documentURI)) {
reason = "unwanted";
} else if (/e=harmfulBlocked/.test(ownerDoc.documentURI)) {
reason = "harmful";
}

let docShell = ownerDoc.defaultView.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell);

global.sendAsyncMessage("Browser:SiteBlockedError", {
location: ownerDoc.location.href,
reason,
elementId: targetElement.getAttribute("id"),
isTopFrame: (ownerDoc.defaultView.parent === ownerDoc.defaultView),
blockedInfo: getSiteBlockedErrorDetails(docShell),
});
},
};
1 change: 1 addition & 0 deletions browser/modules/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ EXTRA_JS_MODULES += [
'AboutNewTab.jsm',
'AsyncTabSwitcher.jsm',
'AttributionCode.jsm',
'BlockedSiteContent.jsm',
'BrowserErrorReporter.jsm',
'BrowserUITelemetry.jsm',
'BrowserUsageTelemetry.jsm',
Expand Down

0 comments on commit 0e954ea

Please sign in to comment.