| 1 | // # domain-isolator.js |
| 2 | // A component for TorBrowser that puts requests from different |
| 3 | // first party domains on separate tor circuits. |
| 4 | |
| 5 | // This file is written in call stack order (later functions |
| 6 | // call earlier functions). The code file can be processed |
| 7 | // with docco.js to provide clear documentation. |
| 8 | |
| 9 | /* jshint moz: true */ |
| 10 | /* global Components, console, XPCOMUtils */ |
| 11 | |
| 12 | // ### Abbreviations |
| 13 | const Cc = Components.classes, Ci = Components.interfaces, Cu = Components.utils; |
| 14 | |
| 15 | // Make the logger available. |
| 16 | let logger = Cc["@torproject.org/torbutton-logger;1"] |
| 17 | .getService(Components.interfaces.nsISupports).wrappedJSObject; |
| 18 | |
| 19 | // ## mozilla namespace. |
| 20 | // Useful functionality for interacting with Mozilla services. |
| 21 | let mozilla = mozilla || {}; |
| 22 | |
| 23 | // __mozilla.protocolProxyService__. |
| 24 | // Mozilla's protocol proxy service, useful for managing proxy connections made |
| 25 | // by the browser. |
| 26 | mozilla.protocolProxyService = Cc["@mozilla.org/network/protocol-proxy-service;1"] |
| 27 | .getService(Ci.nsIProtocolProxyService); |
| 28 | |
| 29 | // __mozilla.thirdPartyUtil__. |
| 30 | // Mozilla's Thirdy Party Utilities, for figuring out first party domain. |
| 31 | mozilla.thirdPartyUtil = Cc["@mozilla.org/thirdpartyutil;1"] |
| 32 | .getService(Ci.mozIThirdPartyUtil); |
| 33 | |
| 34 | // __mozilla.registerProxyChannelFilter(filterFunction, positionIndex)__. |
| 35 | // Registers a proxy channel filter with the Mozilla Protocol Proxy Service, |
| 36 | // which will help to decide the proxy to be used for a given channel. |
| 37 | // The filterFunction should expect two arguments, (aChannel, aProxy), |
| 38 | // where aProxy is the proxy or list of proxies that would be used by default |
| 39 | // for the given channel, and should return a new Proxy or list of Proxies. |
| 40 | mozilla.registerProxyChannelFilter = function (filterFunction, positionIndex) { |
| 41 | let proxyFilter = { |
| 42 | applyFilter : function (aProxyService, aChannel, aProxy) { |
| 43 | return filterFunction(aChannel, aProxy); |
| 44 | } |
| 45 | }; |
| 46 | mozilla.protocolProxyService.registerChannelFilter(proxyFilter, positionIndex); |
| 47 | }; |
| 48 | |
| 49 | // ## tor functionality. |
| 50 | let tor = tor || {}; |
| 51 | |
| 52 | // __tor.noncesForDomains__. |
| 53 | // A mutable map that records what nonce we are using for each domain. |
| 54 | tor.noncesForDomains = {}; |
| 55 | |
| 56 | // __tor.socksProxyCredentials(originalProxy, domain)__. |
| 57 | // Takes a proxyInfo object (originalProxy) and returns a new proxyInfo |
| 58 | // object with the same properties, except the username is set to the |
| 59 | // the domain, and the password is a nonce. |
| 60 | tor.socksProxyCredentials = function (originalProxy, domain) { |
| 61 | // Check if we already have a nonce. If not, create |
| 62 | // one for this domain. |
| 63 | if (!tor.noncesForDomains.hasOwnProperty(domain)) { |
| 64 | tor.noncesForDomains[domain] = 0; |
| 65 | } |
| 66 | let proxy = originalProxy.QueryInterface(Ci.nsIProxyInfo); |
| 67 | return mozilla.protocolProxyService |
| 68 | .newSOCKSProxyInfo(proxy.host, |
| 69 | proxy.port, |
| 70 | domain, // username |
| 71 | tor.noncesForDomains[domain].toString(), // password |
| 72 | proxy.flags, |
| 73 | proxy.failoverTimeout, |
| 74 | proxy.failoverProxy); |
| 75 | }; |
| 76 | |
| 77 | // __tor.isolateCircuitsByDomain()__. |
| 78 | // For every HTTPChannel, replaces the default SOCKS proxy with one that authenticates |
| 79 | // to the SOCKS server (the tor client process) with a username (the first party domain) |
| 80 | // and a nonce password. Tor provides a separate circuit for each username+password |
| 81 | // combination. |
| 82 | tor.isolateCircuitsByDomain = function () { |
| 83 | mozilla.registerProxyChannelFilter(function (aChannel, aProxy) { |
| 84 | try { |
| 85 | let channel = aChannel.QueryInterface(Ci.nsIHttpChannel), |
| 86 | firstPartyURI = mozilla.thirdPartyUtil.getFirstPartyURIFromChannel(channel, true) |
| 87 | .QueryInterface(Ci.nsIURI), |
| 88 | firstPartyDomain = mozilla.thirdPartyUtil |
| 89 | .getFirstPartyHostForIsolation(firstPartyURI), |
| 90 | proxy = aProxy.QueryInterface(Ci.nsIProxyInfo), |
| 91 | replacementProxy = tor.socksProxyCredentials(aProxy, firstPartyDomain); |
| 92 | logger.eclog(3, "tor SOCKS: " + channel.URI.spec + " via " + |
| 93 | replacementProxy.username + ":" + replacementProxy.password); |
| 94 | return replacementProxy; |
| 95 | } catch (err) { |
| 96 | // If we fail, then just use the default proxyInfo. |
| 97 | return aProxy; |
| 98 | } |
| 99 | }, 0); |
| 100 | }; |
| 101 | |
| 102 | // ## XPCOM component construction. |
| 103 | // Module specific constants |
| 104 | const kMODULE_NAME = "TorBrowser Domain Isolator"; |
| 105 | const kMODULE_CONTRACTID = "@torproject.org/domain-isolator;1"; |
| 106 | const kMODULE_CID = Components.ID("e33fd6d4-270f-475f-a96f-ff3140279f68"); |
| 107 | |
| 108 | // Import XPCOMUtils object. |
| 109 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
| 110 | |
| 111 | // DomainIsolator object. Constructor does nothing. |
| 112 | function DomainIsolator() { } |
| 113 | // Firefox component requirements |
| 114 | DomainIsolator.prototype = { |
| 115 | QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]), |
| 116 | classDescription: kMODULE_NAME, |
| 117 | classID: kMODULE_CID, |
| 118 | contractID: kMODULE_CONTRACTID, |
| 119 | observe: function (subject, topic, data) { |
| 120 | logger.eclog(3, "domain isolator: set up isolating circuits by domain"); |
| 121 | tor.isolateCircuitsByDomain(); |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | // Assign factory to global object. |
| 126 | const NSGetFactory = XPCOMUtils.generateNSGetFactory([DomainIsolator]); |