Commit 08959968 authored by Tatiana Meshkova's avatar Tatiana Meshkova

Added Certificates error handler component

parent d60a97c6
SUBDIRS = history chromehelper prompt widgetfactory jscomps touchhelper jsscripts
SUBDIRS = history chromehelper prompt widgetfactory jscomps touchhelper jsscripts overrides
......@@ -77,4 +77,5 @@ AC_OUTPUT([
......@@ -13,6 +13,17 @@ let modules = {
privileged: true
certerror: {
uri: "chrome://browser/content/aboutCertError.xhtml",
privileged: false,
hide: true
home: {
uri: "about:mozilla",
privileged: false
// about:fennec and about:firefox are aliases for about:,
// but hidden from about:about
embedlite: {
......@@ -49,7 +60,7 @@ AboutRedirector.prototype = {
var channel = ios.newChannel(moduleInfo.uri, null, null);
if (!moduleInfo.privileged) {
// Setting the owner to null means that we'll go through the normal
// path in GetChannelPrincipal and create a codebase principal based
/* 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 */
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
XPCOMUtils.defineLazyServiceGetter(Services, "embedlite",
// Common helper service
function EmbedLiteErrorPageHandler()
function EventLinkListener(aWindow)
this._winID = Services.embedlite.getIDByWindow(aWindow);
let browser = Services.embedlite.getBrowserByID(this._winID);
this._targetWindow = browser.contentDOMWindow;
EventLinkListener.prototype = {
_winID: -1,
_targetWindow: null,
handleEvent: function Input_handleEvent(aEvent) {
switch (aEvent.type) {
case "DOMContentLoaded": {
let target = aEvent.originalTarget;
// Attach a listener to watch for "click" events bubbling up from error
// pages and other similar page. This lets us fix bugs like 401575 which
// require error page UI to do privileged things, without letting error
// pages have any privilege themselves.
if (/^about:/.test(target.documentURI)) {
ErrorPageEventHandler._targetWindow = this._targetWindow;
Services.embedlite.chromeEventHandler(this._targetWindow).addEventListener("click", ErrorPageEventHandler, true);
let listener = function() {
Services.embedlite.chromeEventHandler(this._targetWindow).removeEventListener("click", ErrorPageEventHandler, true);
Services.embedlite.chromeEventHandler(this._targetWindow).removeEventListener("pagehide", listener, true);
ErrorPageEventHandler._targetWindow = null;
Services.embedlite.chromeEventHandler(this._targetWindow).addEventListener("pagehide", listener, true);
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMEventListener])
EmbedLiteErrorPageHandler.prototype = {
classID: Components.ID("{ad8b729c-b000-11e2-8ed2-bfd39531b0a6}"),
_linkListeners: {},
observe: function (aSubject, aTopic, aData) {
let self = this;
switch(aTopic) {
case "app-startup": {
// Name of alternate about: page for certificate errors (when undefined, defaults to about:neterror)
Services.prefs.setCharPref("security.alternate_certificate_error_page", "certerror");
Services.obs.addObserver(this, "embedliteviewcreated", true);
Services.obs.addObserver(this, "domwindowclosed", true);
case "embedliteviewcreated": {
case "domwindowclosed": {
_getProgress: function(aWindow) {
return aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
onWindowOpen: function ss_onWindowOpen(aWindow) {
// Return if window has already been initialized
this._linkListeners[aWindow] = new EventLinkListener(aWindow);
Services.embedlite.chromeEventHandler(aWindow).addEventListener("DOMContentLoaded", this._linkListeners[aWindow], false);
onWindowClose: function ss_onWindowClose(aWindow) {
// Ignore windows not tracked by SessionStore
Services.embedlite.chromeEventHandler(aWindow).removeEventListener("DOMContentLoaded", this._linkListeners[aWindow], false);
delete this._linkListeners[aWindow];
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference])
var ErrorPageEventHandler = {
_targetWindow: null,
handleEvent: function(aEvent) {
switch (aEvent.type) {
case "click": {
// Don't trust synthetic events
if (!aEvent.isTrusted)
let target = aEvent.originalTarget;
let errorDoc = target.ownerDocument;
// If the event came from an ssl error page, it is probably either the "Add
// Exception…" or "Get me out of here!" button
if (/^about:certerror\?e=nssBadCert/.test(errorDoc.documentURI)) {
let perm = errorDoc.getElementById("permanentExceptionButton");
let temp = errorDoc.getElementById("temporaryExceptionButton");
if (target == temp || target == perm) {
// Handle setting an cert exception and reloading the page
try {
// Add a new SSL exception for this URL
let uri =, null, null);
let sslExceptions = new SSLExceptions();
if (target == perm) {
sslExceptions.addPermanentException(uri, errorDoc.defaultView, this._targetWindow);
else {
sslExceptions.addTemporaryException(uri, errorDoc.defaultView, this._targetWindow);
} catch (e) {
dump("Failed to set cert exception: " + e + "\n");
} else if (target == errorDoc.getElementById("getMeOutOfHereButton")) {
errorDoc.location = "about:home";
A class to add exceptions to override SSL certificate problems. The functionality
itself is borrowed from exceptionDialog.js.
function SSLExceptions() {
this._overrideService = Cc[";1"]
SSLExceptions.prototype = {
_overrideService: null,
_sslStatus: null,
getInterface: function SSLE_getInterface(aIID) {
return this.QueryInterface(aIID);
QueryInterface: function SSLE_QueryInterface(aIID) {
if (aIID.equals(Ci.nsIBadCertListener2) ||
return this;
throw Components.results.NS_ERROR_NO_INTERFACE;
To collect the SSL status we intercept the certificate error here
and store the status for later use.
notifyCertProblem: function SSLE_notifyCertProblem(socketInfo, sslStatus, targetHost) {
this._sslStatus = sslStatus.QueryInterface(Ci.nsISSLStatus);
return true; // suppress error UI
Attempt to download the certificate for the location specified to get the SSLState
for the certificate and the errors.
_checkCert: function SSLE_checkCert(aURI, aWindow) {
this._sslStatus = null;
dump("Check server Cert:" + aURI.prePath + "\n");
let req = new aWindow.XMLHttpRequest();
try {
if (aURI) {"GET", aURI.prePath, false); = this;
} catch (e) {
// We *expect* exceptions if there are problems with the certificate
// presented by the site. Log it, just in case, but we can proceed here,
// with appropriate sanity checks
Components.utils.reportError("Attempted to connect to a site with a bad certificate in the add exception dialog. " +
"This results in a (mostly harmless) exception being thrown. " +
"Logged for information purposes only: " + e);
return this._sslStatus;
Internal method to create an override.
_addOverride: function SSLE_addOverride(aURI, aWindow, aTargetWindow, aTemporary) {
let SSLStatus = this._checkCert(aURI, aTargetWindow);
let certificate = SSLStatus.serverCert;
let flags = 0;
// in private browsing do not store exceptions permanently ever
if (PrivateBrowsingUtils.isWindowPrivate(aWindow)) {
aTemporary = true;
if (SSLStatus.isUntrusted)
flags |= this._overrideService.ERROR_UNTRUSTED;
if (SSLStatus.isDomainMismatch)
flags |= this._overrideService.ERROR_MISMATCH;
if (SSLStatus.isNotValidAtThisTime)
flags |= this._overrideService.ERROR_TIME;
Creates a permanent exception to override all overridable errors for
the given URL.
addPermanentException: function SSLE_addPermanentException(aURI, aWindow, aTargetWindow) {
this._addOverride(aURI, aWindow, aTargetWindow, false);
Creates a temporary exception to override all overridable errors for
the given URL.
addTemporaryException: function SSLE_addTemporaryException(aURI, aWindow, aTargetWindow) {
this._addOverride(aURI, aWindow, aTargetWindow, true);
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([EmbedLiteErrorPageHandler]);
......@@ -10,6 +10,8 @@ component {b98ab6b8-6c88-11e2-99bc-6745f7369235} AlertsService.js
component {59f3da9a-6c88-11e2-b875-33d1bd379849} AboutRedirector.js
contract;1?what= {59f3da9a-6c88-11e2-b875-33d1bd379849}
contract;1?what=embedlite {59f3da9a-6c88-11e2-b875-33d1bd379849}
contract;1?what=certerror {59f3da9a-6c88-11e2-b875-33d1bd379849}
contract;1?what=home {59f3da9a-6c88-11e2-b875-33d1bd379849}
# LoginManagerPrompter.js
component {72de694e-6c88-11e2-a4ee-6b515bdf0cb7} LoginManagerPrompter.js
......@@ -52,3 +54,8 @@ category app-startup EmbedLiteFaviconService service,
component {924fe7ba-afa1-11e2-9d4f-533572064b73} EmbedLiteSearchEngine.js
contract;1 {924fe7ba-afa1-11e2-9d4f-533572064b73}
category app-startup EmbedLiteSearchEngine service,;1
# EmbedLiteErrorPageHandler.js
component {ad8b729c-b000-11e2-8ed2-bfd39531b0a6} EmbedLiteErrorPageHandler.js
contract;1 {ad8b729c-b000-11e2-8ed2-bfd39531b0a6}
category app-startup EmbedLiteErrorPageHandler service,;1
......@@ -53,3 +53,22 @@ mkdir -p $TARGET_DIR/chrome/embedlite/content;
ln -s $(pwd)/jsscripts/embedhelper.js $TARGET_DIR/chrome/embedlite/content/embedhelper.js;
ln -s $(pwd)/jsscripts/SelectHelper.js $TARGET_DIR/chrome/embedlite/content/SelectHelper.js;
ln -s $(pwd)/jsscripts/google.xml $TARGET_DIR/chrome/embedlite/content/google.xml;
rm -f $TARGET_DIR/chrome/EmbedLiteOverrides.manifest;
ln -s $(pwd)/overrides/EmbedLiteOverrides.manifest $TARGET_DIR/chrome/EmbedLiteOverrides.manifest;
rm -rf $TARGET_DIR/chrome/chrome;
mkdir -p $TARGET_DIR/chrome/chrome/content;
mkdir -p $TARGET_DIR/chrome/chrome/skin;
ln -s $(pwd)/overrides/aboutCertError.xhtml $TARGET_DIR/chrome/chrome/content/
ln -s $(pwd)/overrides/netError.xhtml $TARGET_DIR/chrome/chrome/content/
ln -s $(pwd)/overrides/netError.css $TARGET_DIR/chrome/chrome/skin/
rm -rf $TARGET_DIR/chrome/en-US/locale/branding;
rm -rf $TARGET_DIR/chrome/en-US/locale/en-US/browser;
mkdir -p $TARGET_DIR/chrome/en-US/locale/branding;
mkdir -p $TARGET_DIR/chrome/en-US/locale/en-US/browser;
ln -s $(pwd)/overrides/brand.dtd $TARGET_DIR/chrome/en-US/locale/branding/
ln -s $(pwd)/overrides/ $TARGET_DIR/chrome/en-US/locale/branding/
ln -s $(pwd)/overrides/aboutCertError.dtd $TARGET_DIR/chrome/en-US/locale/en-US/browser/
ln -s $(pwd)/overrides/netError.dtd $TARGET_DIR/chrome/en-US/locale/en-US/browser/
skin browser classic/1.0 chrome/skin/
content branding chrome/content/branding/
override chrome://global/content/netError.xhtml chrome://browser/content/netError.xhtml
override chrome://global/skin/netError.css chrome://browser/skin/netError.css
content browser chrome/content/ contentaccessible=yes
override chrome://global/locale/netError.dtd chrome://browser/locale/netError.dtd
locale browser en-US en-US/locale/en-US/browser/
locale branding en-US en-US/locale/branding/
override chrome://global/locale/netError.dtd chrome://browser/locale/netError.dtd
locale browser en-US en-US/locale/en-US/browser/
chromecontent_manifest_DATA = \
aboutCertError.xhtml \
netError.xhtml \
chromeskin_manifest_DATA = \
netError.css \
manifestsscript_manifest_DATA = \
EmbedLiteOverrides.manifest \
localebrand_manifest_DATA = \
brand.dtd \ \
localebrowser_manifest_DATA = \
aboutCertError.dtd \
netError.dtd \
<!-- 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 -->
<!ENTITY % brandDTD
SYSTEM "chrome://branding/locale/brand.dtd">
<!-- These strings are used by Firefox's custom about:certerror page,
a replacement for the standard security certificate errors produced
by NSS/PSM via netError.xhtml. -->
<!ENTITY certerror.pagetitle "Untrusted Connection">
<!ENTITY certerror.longpagetitle "This Connection is Untrusted">
<!-- Localization note (certerror.introPara1) - The string "#1" will
be replaced at runtime with the name of the server to which the user
was trying to connect. -->
<!ENTITY certerror.introPara1 "You have asked &brandShortName; to connect
securely to <b>#1</b>, but we can't confirm that your connection is secure.">
<!ENTITY certerror.whatShouldIDo.heading "What Should I Do?">
<!ENTITY certerror.whatShouldIDo.content "If you usually connect to
this site without problems, this error could mean that someone is
trying to impersonate the site, and you shouldn't continue.">
<!ENTITY certerror.getMeOutOfHere.label "Get me out of here!">
<!ENTITY "I Understand the Risks">
<!ENTITY "If you understand what's going on, you
can tell &brandShortName; to start trusting this site's identification.
<b>Even if you trust the site, this error could mean that someone is
tampering with your connection.</b>">
<!ENTITY "Don't add an exception unless
you know there's a good reason why this site doesn't use trusted identification.">
<!ENTITY certerror.addTemporaryException.label "Visit site">
<!ENTITY certerror.addPermanentException.label "Add permanent exception">
<!ENTITY certerror.technical.heading "Technical Details">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html [
<!ENTITY % htmlDTD
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<!ENTITY % globalDTD
SYSTEM "chrome://global/locale/global.dtd">
<!ENTITY % certerrorDTD
SYSTEM "chrome://browser/locale/aboutCertError.dtd">
<!-- 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 -->
<html xmlns="">
<meta name="viewport" content="width=device-width; user-scalable=false" />
<link rel="stylesheet" href="chrome://global/skin/netError.css" type="text/css" media="all" />
<!-- This page currently uses the same favicon as neterror.xhtml.
If the location of the favicon is changed for both pages, the
FAVICON_ERRORPAGE_URL symbol in toolkit/components/places/src/nsFaviconService.h
should be updated. If this page starts using a different favicon
than neterrorm nsFaviconService->SetAndLoadFaviconForPage
should be updated to ignore this one as well. -->
<link rel="icon" type="image/png" id="favicon" sizes="64x64" href="chrome://global/skin/icons/warning-64.png"/>
<script type="application/javascript"><![CDATA[
// Error url MUST be formatted like this:
// about:certerror?e=error&u=url&d=desc
// Note that this file uses document.documentURI to get
// the URL (with the format from above). This is because
// document.location.href gets the current URI off the docshell,
// which is the URL displayed in the location bar, i.e.
// the URI that the user attempted to load.
function getCSSClass()
var url = document.documentURI;
var matches = url.match(/s\=([^&]+)\&/);
// s is optional, if no match just return nothing
if (!matches || matches.length < 2)
return "";
// parenthetical match is the second entry
return decodeURIComponent(matches[1]);
function getDescription()
var url = document.documentURI;
var desc =\=/);
// desc == -1 if not found; if so, return an empty string
// instead of what would turn out to be portions of the URI
if (desc == -1)
return "";
return decodeURIComponent(url.slice(desc + 2));
function initPage()
// Replace the "#1" string in the intro with the hostname. Trickier
// than it might seem since we want to preserve the <b> tags, but
// not allow for any injection by just using innerHTML. Instead,
// just find the right target text node.
var intro = document.getElementById('introContentP1');
function replaceWithHost(node) {
if (node.textContent == "#1")
node.textContent =;
for(var i = 0; i < node.childNodes.length; i++)
if (getCSSClass() == "expertBadCert") {
var tech = document.getElementById("technicalContentText");
if (tech)
tech.textContent = getDescription();
/* In the case of SSL error pages about domain mismatch, see if
we can hyperlink the user to the correct site. We don't want
to do this generically since it allows MitM attacks to redirect
users to a site under attacker control, but in certain cases
it is safe (and helpful!) to do so. Bug 402210
function addDomainErrorLink() {
// Rather than textContent, we need to treat description as HTML
var sd = document.getElementById("technicalContentText");
if (sd) {
var desc = getDescription();
// sanitize description text - see bug 441169
// First, find the index of the <a> tag we care about, being careful not to
// use an over-greedy regex
var re = /<a id="cert_domain_link" title="([^"]+)">/;
var result = re.exec(desc);
// Remove sd's existing children
sd.textContent = "";
// Everything up to the link should be text content
sd.appendChild(document.createTextNode(desc.slice(0, result.index)));
// Now create the link itself
var anchorEl = document.createElement("a");
anchorEl.setAttribute("id", "cert_domain_link");
anchorEl.setAttribute("title", result[1]);
// Finally, append text for anything after the closing </a>
sd.appendChild(document.createTextNode(desc.slice(desc.indexOf("</a>") + "</a>".length)));
var link = document.getElementById('cert_domain_link');
if (!link)
var okHost = link.getAttribute("title");
var thisHost = document.location.hostname;
var proto = document.location.protocol;
// If okHost is a wildcard domain ("*") let's
// use "www" instead. "*" isn't going to
// get anyone anywhere useful. bug 432491
okHost = okHost.replace(/^\*\./, "www.");
/* case #1:
* uses an invalid security certificate.
* The certificate is only valid for
* Make sure to include the "." ahead of thisHost so that
* a MitM attack on doesn't hyperlink to ""
* We'd normally just use a RegExp here except that we lack a
* library function to escape them properly (bug 248062), and
* domain names are famous for having '.' characters in them,
* which would allow spurious and possibly hostile matches.
if (endsWith(okHost, "." + thisHost))
link.href = proto + okHost;
/* case #2:
* uses an invalid security certificate.
* The certificate is only valid for
if (endsWith(thisHost, "." + okHost))
link.href = proto + okHost;
// If we set a link, meaning there's something helpful for
// the user here, expand the section by default
if (link.href && getCSSClass() != "expertBadCert")
function endsWith(haystack, needle) {
return haystack.slice(-needle.length) == needle;
function toggle(id) {
var el = document.getElementById(id);
if (el.getAttribute("collapsed"))
el.setAttribute("collapsed", false);
el.setAttribute("collapsed", true);
<body id="errorPage" class="certerror" dir="&locale.dir;">
<!-- Error Title -->
<div id="errorTitle">
<h1 class="errorTitleText">&certerror.longpagetitle;</h1>
<!-- PAGE CONTAINER (for styling purposes only) -->
<div id="errorPageContainer">
<!-- LONG CONTENT (the section most likely to require scrolling) -->
<div id="errorLongContent">
<div id="introContent">
<p id="introContentP1">&certerror.introPara1;</p>
<div id="whatShouldIDoContent">
<div id="whatShouldIDoContentText">
<button id="getMeOutOfHereButton">&certerror.getMeOutOfHere.label;</button>
<!-- The following sections can be unhidden by default by setting the
"browser.xul.error_pages.expert_bad_cert" pref to true -->
<div id="technicalContent" collapsed="true">
<h2 onclick="toggle('technicalContent');" id="technicalContentHeading">&certerror.technical.heading;</h2>
<p id="technicalContentText"/>
<div id="expertContent" collapsed="true">
<h2 onclick="toggle('expertContent');" id="expertContentHeading">&;</h2>
<button id="temporaryExceptionButton">&certerror.addTemporaryException.label;</button>
<button id="permanentExceptionButton">&certerror.addPermanentException.label;</button>
- Note: It is important to run the script this way, instead of using
- an onload handler. This is because error pages are loaded as
- LOAD_BACKGROUND, which means that onload handlers will not be executed.
<script type="application/javascript">initPage();</script>
<!-- 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 -->
<!ENTITY brandShortName "Nightly">
<!ENTITY brandFullName "Mozilla Nightly">
<!ENTITY vendorShortName "Mozilla">
<!ENTITY logoTrademark "">
brandFullName=Mozilla Nightly
/* 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 */
* This defines the look-and-feel styling of the error pages.
* (see: netError.xhtml)
* Original styling by William Price <>
* Updated for mobile by: Wes Johnston <>
body {
margin: 0;
padding: 0 8px 8px;
font-family: "Nokia Sans", Tahoma, sans-serif !important;
h1 {
font-size: 22px;
h2 {
font-size: 16px;
ul {
margin: 0px;
padding: 0px 0px 0px 1em;
li {
margin: 0px;
padding: 8px 0px;
#errorPage {