Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | 'use strict'; |
michael@0 | 6 | |
michael@0 | 7 | this.EXPORTED_SYMBOLS = ['ErrorPage']; |
michael@0 | 8 | |
michael@0 | 9 | const Cu = Components.utils; |
michael@0 | 10 | const Cc = Components.classes; |
michael@0 | 11 | const Ci = Components.interfaces; |
michael@0 | 12 | const kErrorPageFrameScript = 'chrome://b2g/content/ErrorPage.js'; |
michael@0 | 13 | |
michael@0 | 14 | Cu.import('resource://gre/modules/Services.jsm'); |
michael@0 | 15 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 16 | |
michael@0 | 17 | XPCOMUtils.defineLazyGetter(this, "CertOverrideService", function () { |
michael@0 | 18 | return Cc["@mozilla.org/security/certoverride;1"] |
michael@0 | 19 | .getService(Ci.nsICertOverrideService); |
michael@0 | 20 | }); |
michael@0 | 21 | |
michael@0 | 22 | /** |
michael@0 | 23 | * A class to add exceptions to override SSL certificate problems. |
michael@0 | 24 | * The functionality itself is borrowed from exceptionDialog.js. |
michael@0 | 25 | */ |
michael@0 | 26 | function SSLExceptions(aCallback, aUri, aWindow) { |
michael@0 | 27 | this._finishCallback = aCallback; |
michael@0 | 28 | this._uri = aUri; |
michael@0 | 29 | this._window = aWindow; |
michael@0 | 30 | }; |
michael@0 | 31 | |
michael@0 | 32 | SSLExceptions.prototype = { |
michael@0 | 33 | _finishCallback: null, |
michael@0 | 34 | _window: null, |
michael@0 | 35 | _uri: null, |
michael@0 | 36 | _temporary: null, |
michael@0 | 37 | _sslStatus: null, |
michael@0 | 38 | |
michael@0 | 39 | getInterface: function SSLE_getInterface(aIID) { |
michael@0 | 40 | return this.QueryInterface(aIID); |
michael@0 | 41 | }, |
michael@0 | 42 | |
michael@0 | 43 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIBadCertListener2]), |
michael@0 | 44 | |
michael@0 | 45 | /** |
michael@0 | 46 | * To collect the SSL status we intercept the certificate error here |
michael@0 | 47 | * and store the status for later use. |
michael@0 | 48 | */ |
michael@0 | 49 | notifyCertProblem: function SSLE_notifyCertProblem(aSocketInfo, |
michael@0 | 50 | aSslStatus, |
michael@0 | 51 | aTargetHost) { |
michael@0 | 52 | this._sslStatus = aSslStatus.QueryInterface(Ci.nsISSLStatus); |
michael@0 | 53 | Services.tm.currentThread.dispatch({ |
michael@0 | 54 | run: this._addOverride.bind(this) |
michael@0 | 55 | }, Ci.nsIThread.DISPATCH_NORMAL); |
michael@0 | 56 | return true; // suppress error UI |
michael@0 | 57 | }, |
michael@0 | 58 | |
michael@0 | 59 | /** |
michael@0 | 60 | * Attempt to download the certificate for the location specified to get |
michael@0 | 61 | * the SSLState for the certificate and the errors. |
michael@0 | 62 | */ |
michael@0 | 63 | _checkCert: function SSLE_checkCert() { |
michael@0 | 64 | this._sslStatus = null; |
michael@0 | 65 | if (!this._uri) { |
michael@0 | 66 | return; |
michael@0 | 67 | } |
michael@0 | 68 | let req = new this._window.XMLHttpRequest(); |
michael@0 | 69 | try { |
michael@0 | 70 | req.open("GET", this._uri.prePath, true); |
michael@0 | 71 | req.channel.notificationCallbacks = this; |
michael@0 | 72 | let xhrHandler = (function() { |
michael@0 | 73 | req.removeEventListener("load", xhrHandler); |
michael@0 | 74 | req.removeEventListener("error", xhrHandler); |
michael@0 | 75 | if (!this._sslStatus) { |
michael@0 | 76 | // Got response from server without an SSL error. |
michael@0 | 77 | if (this._finishCallback) { |
michael@0 | 78 | this._finishCallback(); |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | }).bind(this); |
michael@0 | 82 | req.addEventListener("load", xhrHandler); |
michael@0 | 83 | req.addEventListener("error", xhrHandler); |
michael@0 | 84 | req.send(null); |
michael@0 | 85 | } catch (e) { |
michael@0 | 86 | // We *expect* exceptions if there are problems with the certificate |
michael@0 | 87 | // presented by the site. Log it, just in case, but we can proceed here, |
michael@0 | 88 | // with appropriate sanity checks |
michael@0 | 89 | Components.utils.reportError("Attempted to connect to a site with a bad certificate in the add exception dialog. " + |
michael@0 | 90 | "This results in a (mostly harmless) exception being thrown. " + |
michael@0 | 91 | "Logged for information purposes only: " + e); |
michael@0 | 92 | } |
michael@0 | 93 | }, |
michael@0 | 94 | |
michael@0 | 95 | /** |
michael@0 | 96 | * Internal method to create an override. |
michael@0 | 97 | */ |
michael@0 | 98 | _addOverride: function SSLE_addOverride() { |
michael@0 | 99 | let SSLStatus = this._sslStatus; |
michael@0 | 100 | let uri = this._uri; |
michael@0 | 101 | let flags = 0; |
michael@0 | 102 | |
michael@0 | 103 | if (SSLStatus.isUntrusted) { |
michael@0 | 104 | flags |= Ci.nsICertOverrideService.ERROR_UNTRUSTED; |
michael@0 | 105 | } |
michael@0 | 106 | if (SSLStatus.isDomainMismatch) { |
michael@0 | 107 | flags |= Ci.nsICertOverrideService.ERROR_MISMATCH; |
michael@0 | 108 | } |
michael@0 | 109 | if (SSLStatus.isNotValidAtThisTime) { |
michael@0 | 110 | flags |= Ci.nsICertOverrideService.ERROR_TIME; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | CertOverrideService.rememberValidityOverride( |
michael@0 | 114 | uri.asciiHost, |
michael@0 | 115 | uri.port, |
michael@0 | 116 | SSLStatus.serverCert, |
michael@0 | 117 | flags, |
michael@0 | 118 | this._temporary); |
michael@0 | 119 | |
michael@0 | 120 | if (this._finishCallback) { |
michael@0 | 121 | this._finishCallback(); |
michael@0 | 122 | } |
michael@0 | 123 | }, |
michael@0 | 124 | |
michael@0 | 125 | /** |
michael@0 | 126 | * Creates a permanent exception to override all overridable errors for |
michael@0 | 127 | * the given URL. |
michael@0 | 128 | */ |
michael@0 | 129 | addException: function SSLE_addException(aTemporary) { |
michael@0 | 130 | this._temporary = aTemporary; |
michael@0 | 131 | this._checkCert(); |
michael@0 | 132 | } |
michael@0 | 133 | }; |
michael@0 | 134 | |
michael@0 | 135 | let ErrorPage = { |
michael@0 | 136 | _addCertException: function(aMessage) { |
michael@0 | 137 | let frameLoaderOwner = aMessage.target.QueryInterface(Ci.nsIFrameLoaderOwner); |
michael@0 | 138 | let win = frameLoaderOwner.ownerDocument.defaultView; |
michael@0 | 139 | let mm = frameLoaderOwner.frameLoader.messageManager; |
michael@0 | 140 | |
michael@0 | 141 | let uri = Services.io.newURI(aMessage.data.url, null, null); |
michael@0 | 142 | let sslExceptions = new SSLExceptions((function() { |
michael@0 | 143 | mm.sendAsyncMessage('ErrorPage:ReloadPage'); |
michael@0 | 144 | }).bind(this), uri, win); |
michael@0 | 145 | try { |
michael@0 | 146 | sslExceptions.addException(!aMessage.data.isPermanent); |
michael@0 | 147 | } catch (e) { |
michael@0 | 148 | dump("Failed to set cert exception: " + e + "\n"); |
michael@0 | 149 | } |
michael@0 | 150 | }, |
michael@0 | 151 | |
michael@0 | 152 | _listenError: function(frameLoader) { |
michael@0 | 153 | let self = this; |
michael@0 | 154 | let frameElement = frameLoader.ownerElement; |
michael@0 | 155 | let injectErrorPageScript = function() { |
michael@0 | 156 | let mm = frameLoader.messageManager; |
michael@0 | 157 | try { |
michael@0 | 158 | mm.loadFrameScript(kErrorPageFrameScript, true, true); |
michael@0 | 159 | } catch (e) { |
michael@0 | 160 | dump('Error loading ' + kErrorPageFrameScript + ' as frame script: ' + e + '\n'); |
michael@0 | 161 | } |
michael@0 | 162 | mm.addMessageListener('ErrorPage:AddCertException', self._addCertException.bind(self)); |
michael@0 | 163 | frameElement.removeEventListener('mozbrowsererror', injectErrorPageScript, true); |
michael@0 | 164 | }; |
michael@0 | 165 | |
michael@0 | 166 | frameElement.addEventListener('mozbrowsererror', |
michael@0 | 167 | injectErrorPageScript, |
michael@0 | 168 | true // use capture |
michael@0 | 169 | ); |
michael@0 | 170 | }, |
michael@0 | 171 | |
michael@0 | 172 | init: function errorPageInit() { |
michael@0 | 173 | Services.obs.addObserver(this, 'inprocess-browser-shown', false); |
michael@0 | 174 | Services.obs.addObserver(this, 'remote-browser-shown', false); |
michael@0 | 175 | }, |
michael@0 | 176 | |
michael@0 | 177 | observe: function errorPageObserve(aSubject, aTopic, aData) { |
michael@0 | 178 | let frameLoader = aSubject.QueryInterface(Ci.nsIFrameLoader); |
michael@0 | 179 | // Ignore notifications that aren't from a BrowserOrApp |
michael@0 | 180 | if (!frameLoader.ownerIsBrowserOrAppFrame) { |
michael@0 | 181 | return; |
michael@0 | 182 | } |
michael@0 | 183 | this._listenError(frameLoader); |
michael@0 | 184 | } |
michael@0 | 185 | }; |
michael@0 | 186 | |
michael@0 | 187 | ErrorPage.init(); |