home *** CD-ROM | disk | FTP | other *** search
/ Minami 81 / MINAMI81.ISO / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / phishing-warden.js < prev    next >
Text File  |  2006-05-15  |  7KB  |  207 lines

  1. function PROT_PhishingWarden(listManager, opt_testing) {
  2. PROT_ListWarden.call(this, listManager);
  3. this.debugZone = "phishwarden";
  4. this.testing_ = !!opt_testing;
  5. this.browserViews_ = [];
  6. this.prefs_ = new G_Preferences();
  7. this.displayers_ = {
  8. "afterload": PROT_PhishMsgDisplayer,
  9. };
  10. this.fetcher_ = new PROT_TRFetcher();
  11. if (!this.testing_) {
  12. this.navWatcher_ = new G_NavWatcher(true /* filter spurious navs */);
  13. this.navWatcher_.registerListener("docnavstart",
  14. BindToObject(this.onDocNavStart,
  15. this));
  16. }
  17. var checkRemotePrefName = PROT_globalStore.getServerCheckEnabledPrefName();
  18. this.checkRemote_ = this.prefs_.getPref(checkRemotePrefName, null);
  19. var checkRemotePrefObserver = BindToObject(this.onCheckRemotePrefChanged,
  20. this);
  21. this.prefs_.addObserver(checkRemotePrefName, checkRemotePrefObserver);
  22. var phishWardenPrefName = PROT_globalStore.getPhishWardenEnabledPrefName();
  23. this.phishWardenEnabled_ = this.prefs_.getPref(phishWardenPrefName, null);
  24. var phishWardenPrefObserver =
  25. BindToObject(this.onPhishWardenEnabledPrefChanged, this);
  26. this.prefs_.addObserver(phishWardenPrefName, phishWardenPrefObserver);
  27. this.testURLs_ = PROT_globalStore.getTestURLs();
  28. this.registerWhiteTable("goog-white-domain");
  29. this.registerWhiteTable("goog-white-url");
  30. this.registerBlackTable("goog-black-url");
  31. this.registerBlackTable("goog-black-enchash");
  32. this.maybeToggleUpdateChecking();
  33. }
  34. PROT_PhishingWarden.inherits(PROT_ListWarden);
  35. PROT_PhishingWarden.prototype.maybeToggleUpdateChecking = function() {
  36. if (this.testing_)
  37. return;
  38. var checkRemotePrefName = PROT_globalStore.getServerCheckEnabledPrefName();
  39. this.checkRemote_ = this.prefs_.getPref(checkRemotePrefName, null);
  40. var phishWardenPrefName = PROT_globalStore.getPhishWardenEnabledPrefName();
  41. var phishWardenEnabled = this.prefs_.getPref(phishWardenPrefName, null);
  42. G_Debug(this, "Maybe toggling update checking. " +
  43. "Check remote? " + this.checkRemote_ + " " +
  44. "Warden enabled? " + phishWardenEnabled);
  45. if (phishWardenEnabled === null || this.checkRemote_ === null)
  46. return;
  47. if (phishWardenEnabled === true) {
  48. this.enableWhitelistTableUpdates();
  49. if (this.checkRemote_ === true) {
  50. this.disableBlacklistTableUpdates();
  51. } else if (this.checkRemote_ === false) {
  52. this.enableBlacklistTableUpdates();
  53. }
  54. } else if (phishWardenEnabled === false) {
  55. this.disableBlacklistTableUpdates();
  56. this.disableWhitelistTableUpdates();
  57. }
  58. }
  59. PROT_PhishingWarden.prototype.addBrowserView = function(view) {
  60. G_Debug(this, "New browser view registered.");
  61. this.browserViews_.push(view);
  62. }
  63. PROT_PhishingWarden.prototype.removeBrowserView = function(view) {
  64. for (var i = 0; i < this.browserViews_.length; i++)
  65. if (this.browserViews_[i] === view) {
  66. G_Debug(this, "Browser view unregistered.");
  67. this.browserViews_.splice(i, 1);
  68. return;
  69. }
  70. G_Assert(this, false, "Tried to unregister non-existent browser view!");
  71. }
  72. PROT_PhishingWarden.prototype.onCheckRemotePrefChanged = function(prefName) {
  73. this.checkRemote_ = this.prefs_.getBoolPrefOrDefault(prefName,
  74. this.checkRemote_);
  75. this.maybeToggleUpdateChecking();
  76. }
  77. PROT_PhishingWarden.prototype.onPhishWardenEnabledPrefChanged = function(
  78. prefName) {
  79. this.phishWardenEnabled_ =
  80. this.prefs_.getBoolPrefOrDefault(prefName, this.phishWardenEnabled_);
  81. this.maybeToggleUpdateChecking();
  82. }
  83. PROT_PhishingWarden.prototype.onDocNavStart = function(e) {
  84. var url = e.url;
  85. var request = e.request;
  86. G_Debug(this, "phishWarden: " +
  87. (this.phishWardenEnabled_ ? "enabled" : "disabled"));
  88. G_Debug(this, "checkRemote: " +
  89. (this.checkRemote_ ? "yes" : "no"));
  90. G_Debug(this, "isTestURL: " +
  91. (this.isBlacklistTestURL(url) ? "yes" : "no"));
  92. if (this.isBlacklistTestURL(url) &&
  93. (this.phishWardenEnabled_ === true ||
  94. this.phishWardenEnabled_ === null)) {
  95. this.houstonWeHaveAProblem_(request);
  96. } else if (this.phishWardenEnabled_ === true) {
  97. if (this.checkRemote_) {
  98. if (!this.isWhiteURL_(url)) {
  99. G_Debug(this, "Local whitelist lookup failed");
  100. this.fetcher_.get(url,
  101. BindToObject(this.onTRFetchComplete,
  102. this,
  103. request));
  104. } else {
  105. G_Debug(this, "WL suppressing BL lookup for " + url);
  106. }
  107. } else {
  108. if (this.checkUrl(url)) {
  109. this.houstonWeHaveAProblem_(request);
  110. }
  111. }
  112. }
  113. }
  114. PROT_PhishingWarden.prototype.onTRFetchComplete = function(request,
  115. trValues) {
  116. var callback = BindToObject(this.houstonWeHaveAProblem_, this, request);
  117. this.checkRemoteData(callback, trValues);
  118. }
  119. PROT_PhishingWarden.prototype.houstonWeHaveAProblem_ = function(request) {
  120. if (this.maybeLocateProblem_(request))       // Cases 1 and 2 (see below)
  121. return;
  122. if (request.isPending()) {        // Case 3
  123. G_Debug(this, "Can't find problem Doc; Req pending. Retrying.");
  124. new G_Alarm(BindToObject(this.houstonWeHaveAProblem_,
  125. this,
  126. request),
  127. 200 /*ms*/);
  128. } else {                          // Case 4
  129. G_Debug(this,
  130. "Can't find problem Doc; Req completed. Retrying at most twice.");
  131. new G_ConditionalAlarm(BindToObject(this.maybeLocateProblem_,
  132. this,
  133. request),
  134. 0 /* next event loop */,
  135. true /* repeat */,
  136. 2 /* at most twice */);
  137. }
  138. }
  139. PROT_PhishingWarden.prototype.maybeLocateProblem_ = function(request) {
  140. G_Debug(this, "Trying to find the problem.");
  141. for (var i = 0; i < this.browserViews_.length; i++)
  142. if (this.browserViews_[i].tryToHandleProblemRequest(this, request)) {
  143. G_Debug(this, "Found browser view willing to handle problem!");
  144. return true;
  145. }
  146. return false;
  147. }
  148. PROT_PhishingWarden.prototype.isBlacklistTestURL = function(url) {
  149. for (var i = 0, testURL = null; testURL = this.testURLs_[i]; ++i) {
  150. if (testURL === url) {
  151. return true;
  152. }
  153. }
  154. return false;
  155. }
  156. PROT_PhishingWarden.prototype.checkUrl = function(url) {
  157. G_Debug(this, "Checking URL for " + url);
  158. if (this.isEvilURL_(url) || this.isBlacklistTestURL(url)) {
  159. G_Debug(this, "Local blacklist hit");
  160. (new PROT_Reporter).report("phishblhit", url);
  161. return true;
  162. }
  163. G_Debug(this, "Local blacklist miss");
  164. return false;
  165. }
  166. PROT_PhishingWarden.prototype.checkRemoteData = function(callback,
  167. trValues) {
  168. if (!trValues) {
  169. G_Debug(this, "Didn't get TR values from the server.");
  170. return;
  171. }
  172. G_Debug(this, "Page has phishiness " + trValues["phishy"]);
  173. if (trValues["phishy"] == 1) {     // It's on our blacklist
  174. G_Debug(this, "Remote blacklist hit");
  175. callback(this);
  176. } else {
  177. G_Debug(this, "Remote blacklist miss");
  178. }
  179. }
  180. function TEST_PROT_PhishingWarden() {
  181. if (G_GDEBUG) {
  182. var z = "phishwarden UNITTEST";
  183. G_debugService.enableZone(z);
  184. G_Debug(z, "Starting");
  185. var listManager = new PROT_ListManager(true /* testing */);
  186. var warden = new PROT_PhishingWarden(listManager, true /* testing */);
  187. warden.registerBlackTable("test-black-url");
  188. var blackURLs = [
  189. "http://foo.com/1",
  190. "http://foo.com/2",
  191. "http://foo.com/3",
  192. "http://foo.com/4",
  193. ];
  194. for (var i = 0; i < blackURLs.length; i++)
  195. listManager.safeInsert("test-black-url", blackURLs[i], "1");
  196. G_Assert(z, !warden.checkUrl("http://bar.com/"),
  197. "bar.com should not be found");
  198. for (var i = 0; i < blackURLs.length; i++) {
  199. G_Assert(z, warden.checkUrl(blackURLs[i]),
  200. blackURLs[i] + " not found");
  201. }
  202. for (var i = 0; i < blackURLs.length; i++)
  203. listManager.safeErase("test-black-url", blackURLs[i]);
  204. G_Debug(z, "PASSED");
  205. }
  206. }
  207.