|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 this.EXPORTED_SYMBOLS = ["SafeBrowsing"]; |
|
6 |
|
7 const Cc = Components.classes; |
|
8 const Ci = Components.interfaces; |
|
9 const Cu = Components.utils; |
|
10 |
|
11 Cu.import("resource://gre/modules/Services.jsm"); |
|
12 |
|
13 // Skip all the ones containining "test", because we never need to ask for |
|
14 // updates for them. |
|
15 function getLists(prefName) { |
|
16 let pref = Services.prefs.getCharPref(prefName); |
|
17 // Splitting an empty string returns [''], we really want an empty array. |
|
18 if (!pref) { |
|
19 return []; |
|
20 } |
|
21 return pref.split(",") |
|
22 .filter(function(value) { return value.indexOf("test-") == -1; }) |
|
23 .map(function(value) { return value.trim(); }); |
|
24 } |
|
25 |
|
26 // These may be a comma-separated lists of tables. |
|
27 const phishingLists = getLists("urlclassifier.phish_table"); |
|
28 const malwareLists = getLists("urlclassifier.malware_table"); |
|
29 const downloadBlockLists = getLists("urlclassifier.downloadBlockTable"); |
|
30 const downloadAllowLists = getLists("urlclassifier.downloadAllowTable"); |
|
31 |
|
32 var debug = false; |
|
33 function log(...stuff) { |
|
34 if (!debug) |
|
35 return; |
|
36 |
|
37 let msg = "SafeBrowsing: " + stuff.join(" "); |
|
38 Services.console.logStringMessage(msg); |
|
39 dump(msg + "\n"); |
|
40 } |
|
41 |
|
42 this.SafeBrowsing = { |
|
43 |
|
44 init: function() { |
|
45 if (this.initialized) { |
|
46 log("Already initialized"); |
|
47 return; |
|
48 } |
|
49 |
|
50 Services.prefs.addObserver("browser.safebrowsing", this.readPrefs.bind(this), false); |
|
51 this.readPrefs(); |
|
52 |
|
53 // Register our two types of tables, and add custom Mozilla entries |
|
54 let listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]. |
|
55 getService(Ci.nsIUrlListManager); |
|
56 for (let i = 0; i < phishingLists.length; ++i) { |
|
57 listManager.registerTable(phishingLists[i], false); |
|
58 } |
|
59 for (let i = 0; i < malwareLists.length; ++i) { |
|
60 listManager.registerTable(malwareLists[i], false); |
|
61 } |
|
62 for (let i = 0; i < downloadBlockLists.length; ++i) { |
|
63 listManager.registerTable(downloadBlockLists[i], false); |
|
64 } |
|
65 for (let i = 0; i < downloadAllowLists.length; ++i) { |
|
66 listManager.registerTable(downloadAllowLists[i], false); |
|
67 } |
|
68 this.addMozEntries(); |
|
69 |
|
70 this.controlUpdateChecking(); |
|
71 this.initialized = true; |
|
72 |
|
73 log("init() finished"); |
|
74 }, |
|
75 |
|
76 |
|
77 initialized: false, |
|
78 phishingEnabled: false, |
|
79 malwareEnabled: false, |
|
80 |
|
81 updateURL: null, |
|
82 gethashURL: null, |
|
83 |
|
84 reportURL: null, |
|
85 reportGenericURL: null, |
|
86 reportErrorURL: null, |
|
87 reportPhishURL: null, |
|
88 reportMalwareURL: null, |
|
89 reportMalwareErrorURL: null, |
|
90 |
|
91 |
|
92 getReportURL: function(kind) { |
|
93 return this["report" + kind + "URL"]; |
|
94 }, |
|
95 |
|
96 |
|
97 readPrefs: function() { |
|
98 log("reading prefs"); |
|
99 |
|
100 debug = Services.prefs.getBoolPref("browser.safebrowsing.debug"); |
|
101 this.phishingEnabled = Services.prefs.getBoolPref("browser.safebrowsing.enabled"); |
|
102 this.malwareEnabled = Services.prefs.getBoolPref("browser.safebrowsing.malware.enabled"); |
|
103 this.updateProviderURLs(); |
|
104 |
|
105 // XXX The listManager backend gets confused if this is called before the |
|
106 // lists are registered. So only call it here when a pref changes, and not |
|
107 // when doing initialization. I expect to refactor this later, so pardon the hack. |
|
108 if (this.initialized) |
|
109 this.controlUpdateChecking(); |
|
110 }, |
|
111 |
|
112 |
|
113 updateProviderURLs: function() { |
|
114 try { |
|
115 var clientID = Services.prefs.getCharPref("browser.safebrowsing.id"); |
|
116 } catch(e) { |
|
117 var clientID = Services.appinfo.name; |
|
118 } |
|
119 |
|
120 log("initializing safe browsing URLs, client id ", clientID); |
|
121 let basePref = "browser.safebrowsing."; |
|
122 |
|
123 // Urls to HTML report pages |
|
124 this.reportURL = Services.urlFormatter.formatURLPref(basePref + "reportURL"); |
|
125 this.reportGenericURL = Services.urlFormatter.formatURLPref(basePref + "reportGenericURL"); |
|
126 this.reportErrorURL = Services.urlFormatter.formatURLPref(basePref + "reportErrorURL"); |
|
127 this.reportPhishURL = Services.urlFormatter.formatURLPref(basePref + "reportPhishURL"); |
|
128 this.reportMalwareURL = Services.urlFormatter.formatURLPref(basePref + "reportMalwareURL"); |
|
129 this.reportMalwareErrorURL = Services.urlFormatter.formatURLPref(basePref + "reportMalwareErrorURL"); |
|
130 |
|
131 // Urls used to update DB |
|
132 this.updateURL = Services.urlFormatter.formatURLPref(basePref + "updateURL"); |
|
133 this.gethashURL = Services.urlFormatter.formatURLPref(basePref + "gethashURL"); |
|
134 |
|
135 this.updateURL = this.updateURL.replace("SAFEBROWSING_ID", clientID); |
|
136 this.gethashURL = this.gethashURL.replace("SAFEBROWSING_ID", clientID); |
|
137 |
|
138 let listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]. |
|
139 getService(Ci.nsIUrlListManager); |
|
140 |
|
141 listManager.setUpdateUrl(this.updateURL); |
|
142 listManager.setGethashUrl(this.gethashURL); |
|
143 }, |
|
144 |
|
145 |
|
146 controlUpdateChecking: function() { |
|
147 log("phishingEnabled:", this.phishingEnabled, "malwareEnabled:", this.malwareEnabled); |
|
148 |
|
149 let listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]. |
|
150 getService(Ci.nsIUrlListManager); |
|
151 |
|
152 for (let i = 0; i < phishingLists.length; ++i) { |
|
153 if (this.phishingEnabled) { |
|
154 listManager.enableUpdate(phishingLists[i]); |
|
155 } else { |
|
156 listManager.disableUpdate(phishingLists[i]); |
|
157 } |
|
158 } |
|
159 for (let i = 0; i < malwareLists.length; ++i) { |
|
160 if (this.malwareEnabled) { |
|
161 listManager.enableUpdate(malwareLists[i]); |
|
162 } else { |
|
163 listManager.disableUpdate(malwareLists[i]); |
|
164 } |
|
165 } |
|
166 for (let i = 0; i < downloadBlockLists.length; ++i) { |
|
167 if (this.malwareEnabled) { |
|
168 listManager.enableUpdate(downloadBlockLists[i]); |
|
169 } else { |
|
170 listManager.disableUpdate(downloadBlockLists[i]); |
|
171 } |
|
172 } |
|
173 for (let i = 0; i < downloadAllowLists.length; ++i) { |
|
174 if (this.malwareEnabled) { |
|
175 listManager.enableUpdate(downloadAllowLists[i]); |
|
176 } else { |
|
177 listManager.disableUpdate(downloadAllowLists[i]); |
|
178 } |
|
179 } |
|
180 }, |
|
181 |
|
182 |
|
183 addMozEntries: function() { |
|
184 // Add test entries to the DB. |
|
185 // XXX bug 779008 - this could be done by DB itself? |
|
186 const phishURL = "itisatrap.org/firefox/its-a-trap.html"; |
|
187 const malwareURL = "itisatrap.org/firefox/its-an-attack.html"; |
|
188 |
|
189 let update = "n:1000\ni:test-malware-simple\nad:1\n" + |
|
190 "a:1:32:" + malwareURL.length + "\n" + |
|
191 malwareURL; |
|
192 update += "n:1000\ni:test-phish-simple\nad:1\n" + |
|
193 "a:1:32:" + phishURL.length + "\n" + |
|
194 phishURL; |
|
195 log("addMozEntries:", update); |
|
196 |
|
197 let db = Cc["@mozilla.org/url-classifier/dbservice;1"]. |
|
198 getService(Ci.nsIUrlClassifierDBService); |
|
199 |
|
200 // nsIUrlClassifierUpdateObserver |
|
201 let dummyListener = { |
|
202 updateUrlRequested: function() { }, |
|
203 streamFinished: function() { }, |
|
204 updateError: function() { }, |
|
205 updateSuccess: function() { } |
|
206 }; |
|
207 |
|
208 try { |
|
209 db.beginUpdate(dummyListener, "test-malware-simple,test-phish-simple", ""); |
|
210 db.beginStream("", ""); |
|
211 db.updateStream(update); |
|
212 db.finishStream(); |
|
213 db.finishUpdate(); |
|
214 } catch(ex) { |
|
215 // beginUpdate will throw harmlessly if there's an existing update in progress, ignore failures. |
|
216 log("addMozEntries failed!", ex); |
|
217 } |
|
218 }, |
|
219 }; |