toolkit/components/downloads/test/unit/test_app_rep_windows.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* Any copyright is dedicated to the Public Domain.
michael@0 4 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 5
michael@0 6 /**
michael@0 7 * This file tests signature extraction using Windows Authenticode APIs of
michael@0 8 * downloaded files.
michael@0 9 */
michael@0 10
michael@0 11 ////////////////////////////////////////////////////////////////////////////////
michael@0 12 //// Globals
michael@0 13
michael@0 14 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 15
michael@0 16 XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
michael@0 17 "resource://gre/modules/FileUtils.jsm");
michael@0 18 XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
michael@0 19 "resource://gre/modules/NetUtil.jsm");
michael@0 20 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
michael@0 21 "resource://gre/modules/Promise.jsm");
michael@0 22 XPCOMUtils.defineLazyModuleGetter(this, "Task",
michael@0 23 "resource://gre/modules/Task.jsm");
michael@0 24
michael@0 25 const BackgroundFileSaverOutputStream = Components.Constructor(
michael@0 26 "@mozilla.org/network/background-file-saver;1?mode=outputstream",
michael@0 27 "nsIBackgroundFileSaver");
michael@0 28
michael@0 29 const StringInputStream = Components.Constructor(
michael@0 30 "@mozilla.org/io/string-input-stream;1",
michael@0 31 "nsIStringInputStream",
michael@0 32 "setData");
michael@0 33
michael@0 34 const TEST_FILE_NAME_1 = "test-backgroundfilesaver-1.txt";
michael@0 35
michael@0 36 const gAppRep = Cc["@mozilla.org/downloads/application-reputation-service;1"].
michael@0 37 getService(Ci.nsIApplicationReputationService);
michael@0 38 let gStillRunning = true;
michael@0 39 let gTables = {};
michael@0 40 let gHttpServer = null;
michael@0 41
michael@0 42 /**
michael@0 43 * Returns a reference to a temporary file. If the file is then created, it
michael@0 44 * will be removed when tests in this file finish.
michael@0 45 */
michael@0 46 function getTempFile(aLeafName) {
michael@0 47 let file = FileUtils.getFile("TmpD", [aLeafName]);
michael@0 48 do_register_cleanup(function GTF_cleanup() {
michael@0 49 if (file.exists()) {
michael@0 50 file.remove(false);
michael@0 51 }
michael@0 52 });
michael@0 53 return file;
michael@0 54 }
michael@0 55
michael@0 56 function readFileToString(aFilename) {
michael@0 57 let f = do_get_file(aFilename);
michael@0 58 let stream = Cc["@mozilla.org/network/file-input-stream;1"]
michael@0 59 .createInstance(Ci.nsIFileInputStream);
michael@0 60 stream.init(f, -1, 0, 0);
michael@0 61 let buf = NetUtil.readInputStreamToString(stream, stream.available());
michael@0 62 return buf;
michael@0 63 }
michael@0 64
michael@0 65 /**
michael@0 66 * Waits for the given saver object to complete.
michael@0 67 *
michael@0 68 * @param aSaver
michael@0 69 * The saver, with the output stream or a stream listener implementation.
michael@0 70 * @param aOnTargetChangeFn
michael@0 71 * Optional callback invoked with the target file name when it changes.
michael@0 72 *
michael@0 73 * @return {Promise}
michael@0 74 * @resolves When onSaveComplete is called with a success code.
michael@0 75 * @rejects With an exception, if onSaveComplete is called with a failure code.
michael@0 76 */
michael@0 77 function promiseSaverComplete(aSaver, aOnTargetChangeFn) {
michael@0 78 let deferred = Promise.defer();
michael@0 79 aSaver.observer = {
michael@0 80 onTargetChange: function BFSO_onSaveComplete(aSaver, aTarget)
michael@0 81 {
michael@0 82 if (aOnTargetChangeFn) {
michael@0 83 aOnTargetChangeFn(aTarget);
michael@0 84 }
michael@0 85 },
michael@0 86 onSaveComplete: function BFSO_onSaveComplete(aSaver, aStatus)
michael@0 87 {
michael@0 88 if (Components.isSuccessCode(aStatus)) {
michael@0 89 deferred.resolve();
michael@0 90 } else {
michael@0 91 deferred.reject(new Components.Exception("Saver failed.", aStatus));
michael@0 92 }
michael@0 93 },
michael@0 94 };
michael@0 95 return deferred.promise;
michael@0 96 }
michael@0 97
michael@0 98 /**
michael@0 99 * Feeds a string to a BackgroundFileSaverOutputStream.
michael@0 100 *
michael@0 101 * @param aSourceString
michael@0 102 * The source data to copy.
michael@0 103 * @param aSaverOutputStream
michael@0 104 * The BackgroundFileSaverOutputStream to feed.
michael@0 105 * @param aCloseWhenDone
michael@0 106 * If true, the output stream will be closed when the copy finishes.
michael@0 107 *
michael@0 108 * @return {Promise}
michael@0 109 * @resolves When the copy completes with a success code.
michael@0 110 * @rejects With an exception, if the copy fails.
michael@0 111 */
michael@0 112 function promiseCopyToSaver(aSourceString, aSaverOutputStream, aCloseWhenDone) {
michael@0 113 let deferred = Promise.defer();
michael@0 114 let inputStream = new StringInputStream(aSourceString, aSourceString.length);
michael@0 115 let copier = Cc["@mozilla.org/network/async-stream-copier;1"]
michael@0 116 .createInstance(Ci.nsIAsyncStreamCopier);
michael@0 117 copier.init(inputStream, aSaverOutputStream, null, false, true, 0x8000, true,
michael@0 118 aCloseWhenDone);
michael@0 119 copier.asyncCopy({
michael@0 120 onStartRequest: function () { },
michael@0 121 onStopRequest: function (aRequest, aContext, aStatusCode)
michael@0 122 {
michael@0 123 if (Components.isSuccessCode(aStatusCode)) {
michael@0 124 deferred.resolve();
michael@0 125 } else {
michael@0 126 deferred.reject(new Components.Exception(aResult));
michael@0 127 }
michael@0 128 },
michael@0 129 }, null);
michael@0 130 return deferred.promise;
michael@0 131 }
michael@0 132
michael@0 133 // Registers a table for which to serve update chunks.
michael@0 134 function registerTableUpdate(aTable, aFilename) {
michael@0 135 // If we haven't been given an update for this table yet, add it to the map
michael@0 136 if (!(aTable in gTables)) {
michael@0 137 gTables[aTable] = [];
michael@0 138 }
michael@0 139
michael@0 140 // The number of chunks associated with this table.
michael@0 141 let numChunks = gTables[aTable].length + 1;
michael@0 142 let redirectPath = "/" + aTable + "-" + numChunks;
michael@0 143 let redirectUrl = "localhost:4444" + redirectPath;
michael@0 144
michael@0 145 // Store redirect url for that table so we can return it later when we
michael@0 146 // process an update request.
michael@0 147 gTables[aTable].push(redirectUrl);
michael@0 148
michael@0 149 gHttpServer.registerPathHandler(redirectPath, function(request, response) {
michael@0 150 do_print("Mock safebrowsing server handling request for " + redirectPath);
michael@0 151 let contents = readFileToString(aFilename);
michael@0 152 do_print("Length of " + aFilename + ": " + contents.length);
michael@0 153 response.setHeader("Content-Type",
michael@0 154 "application/vnd.google.safebrowsing-update", false);
michael@0 155 response.setStatusLine(request.httpVersion, 200, "OK");
michael@0 156 response.bodyOutputStream.write(contents, contents.length);
michael@0 157 });
michael@0 158 }
michael@0 159
michael@0 160 ////////////////////////////////////////////////////////////////////////////////
michael@0 161 //// Tests
michael@0 162
michael@0 163 function run_test()
michael@0 164 {
michael@0 165 run_next_test();
michael@0 166 }
michael@0 167
michael@0 168 add_task(function test_setup()
michael@0 169 {
michael@0 170 // Wait 10 minutes, that is half of the external xpcshell timeout.
michael@0 171 do_timeout(10 * 60 * 1000, function() {
michael@0 172 if (gStillRunning) {
michael@0 173 do_throw("Test timed out.");
michael@0 174 }
michael@0 175 });
michael@0 176 // Set up a local HTTP server to return bad verdicts.
michael@0 177 Services.prefs.setCharPref("browser.safebrowsing.appRepURL",
michael@0 178 "http://localhost:4444/download");
michael@0 179 // Ensure safebrowsing is enabled for this test, even if the app
michael@0 180 // doesn't have it enabled.
michael@0 181 Services.prefs.setBoolPref("browser.safebrowsing.malware.enabled", true);
michael@0 182 do_register_cleanup(function() {
michael@0 183 Services.prefs.clearUserPref("browser.safebrowsing.malware.enabled");
michael@0 184 });
michael@0 185
michael@0 186 gHttpServer = new HttpServer();
michael@0 187 gHttpServer.registerDirectory("/", do_get_cwd());
michael@0 188
michael@0 189 function createVerdict(aShouldBlock) {
michael@0 190 // We can't programmatically create a protocol buffer here, so just
michael@0 191 // hardcode some already serialized ones.
michael@0 192 blob = String.fromCharCode(parseInt(0x08, 16));
michael@0 193 if (aShouldBlock) {
michael@0 194 // A safe_browsing::ClientDownloadRequest with a DANGEROUS verdict
michael@0 195 blob += String.fromCharCode(parseInt(0x01, 16));
michael@0 196 } else {
michael@0 197 // A safe_browsing::ClientDownloadRequest with a SAFE verdict
michael@0 198 blob += String.fromCharCode(parseInt(0x00, 16));
michael@0 199 }
michael@0 200 return blob;
michael@0 201 }
michael@0 202
michael@0 203 gHttpServer.registerPathHandler("/throw", function(request, response) {
michael@0 204 do_throw("We shouldn't be getting here");
michael@0 205 });
michael@0 206
michael@0 207 gHttpServer.registerPathHandler("/download", function(request, response) {
michael@0 208 do_print("Querying remote server for verdict");
michael@0 209 response.setHeader("Content-Type", "application/octet-stream", false);
michael@0 210 let buf = NetUtil.readInputStreamToString(
michael@0 211 request.bodyInputStream,
michael@0 212 request.bodyInputStream.available());
michael@0 213 do_print("Request length: " + buf.length);
michael@0 214 // A garbage response. By default this produces NS_CANNOT_CONVERT_DATA as
michael@0 215 // the callback status.
michael@0 216 let blob = "this is not a serialized protocol buffer";
michael@0 217 // We can't actually parse the protocol buffer here, so just switch on the
michael@0 218 // length instead of inspecting the contents.
michael@0 219 if (buf.length == 45) {
michael@0 220 // evil.com
michael@0 221 blob = createVerdict(true);
michael@0 222 } else if (buf.length == 48) {
michael@0 223 // mozilla.com
michael@0 224 blob = createVerdict(false);
michael@0 225 }
michael@0 226 response.bodyOutputStream.write(blob, blob.length);
michael@0 227 });
michael@0 228
michael@0 229 gHttpServer.start(4444);
michael@0 230 });
michael@0 231
michael@0 232 // Construct a response with redirect urls.
michael@0 233 function processUpdateRequest() {
michael@0 234 let response = "n:1000\n";
michael@0 235 for (let table in gTables) {
michael@0 236 response += "i:" + table + "\n";
michael@0 237 for (let i = 0; i < gTables[table].length; ++i) {
michael@0 238 response += "u:" + gTables[table][i] + "\n";
michael@0 239 }
michael@0 240 }
michael@0 241 do_print("Returning update response: " + response);
michael@0 242 return response;
michael@0 243 }
michael@0 244
michael@0 245 // Set up the local whitelist.
michael@0 246 function waitForUpdates() {
michael@0 247 let deferred = Promise.defer();
michael@0 248 gHttpServer.registerPathHandler("/downloads", function(request, response) {
michael@0 249 let buf = NetUtil.readInputStreamToString(request.bodyInputStream,
michael@0 250 request.bodyInputStream.available());
michael@0 251 let blob = processUpdateRequest();
michael@0 252 response.setHeader("Content-Type",
michael@0 253 "application/vnd.google.safebrowsing-update", false);
michael@0 254 response.setStatusLine(request.httpVersion, 200, "OK");
michael@0 255 response.bodyOutputStream.write(blob, blob.length);
michael@0 256 });
michael@0 257
michael@0 258 let streamUpdater = Cc["@mozilla.org/url-classifier/streamupdater;1"]
michael@0 259 .getService(Ci.nsIUrlClassifierStreamUpdater);
michael@0 260 streamUpdater.updateUrl = "http://localhost:4444/downloads";
michael@0 261
michael@0 262 // Load up some update chunks for the safebrowsing server to serve. This
michael@0 263 // particular chunk contains the hash of whitelisted.com/ and
michael@0 264 // sb-ssl.google.com/safebrowsing/csd/certificate/.
michael@0 265 registerTableUpdate("goog-downloadwhite-digest256", "data/digest.chunk");
michael@0 266
michael@0 267 // Resolve the promise once processing the updates is complete.
michael@0 268 function updateSuccess(aEvent) {
michael@0 269 // Timeout of n:1000 is constructed in processUpdateRequest above and
michael@0 270 // passed back in the callback in nsIUrlClassifierStreamUpdater on success.
michael@0 271 do_check_eq("1000", aEvent);
michael@0 272 do_print("All data processed");
michael@0 273 deferred.resolve(true);
michael@0 274 }
michael@0 275 // Just throw if we ever get an update or download error.
michael@0 276 function handleError(aEvent) {
michael@0 277 do_throw("We didn't download or update correctly: " + aEvent);
michael@0 278 deferred.reject();
michael@0 279 }
michael@0 280 streamUpdater.downloadUpdates(
michael@0 281 "goog-downloadwhite-digest256",
michael@0 282 "goog-downloadwhite-digest256;\n",
michael@0 283 updateSuccess, handleError, handleError);
michael@0 284 return deferred.promise;
michael@0 285 }
michael@0 286
michael@0 287 function promiseQueryReputation(query, expectedShouldBlock) {
michael@0 288 let deferred = Promise.defer();
michael@0 289 function onComplete(aShouldBlock, aStatus) {
michael@0 290 do_check_eq(Cr.NS_OK, aStatus);
michael@0 291 do_check_eq(aShouldBlock, expectedShouldBlock);
michael@0 292 deferred.resolve(true);
michael@0 293 }
michael@0 294 gAppRep.queryReputation(query, onComplete);
michael@0 295 return deferred.promise;
michael@0 296 }
michael@0 297
michael@0 298 add_task(function()
michael@0 299 {
michael@0 300 // Wait for Safebrowsing local list updates to complete.
michael@0 301 yield waitForUpdates();
michael@0 302 });
michael@0 303
michael@0 304 add_task(function test_signature_whitelists()
michael@0 305 {
michael@0 306 // We should never get to the remote server.
michael@0 307 Services.prefs.setCharPref("browser.safebrowsing.appRepURL",
michael@0 308 "http://localhost:4444/throw");
michael@0 309
michael@0 310 // Use BackgroundFileSaver to extract the signature on Windows.
michael@0 311 let destFile = getTempFile(TEST_FILE_NAME_1);
michael@0 312
michael@0 313 let data = readFileToString("data/signed_win.exe");
michael@0 314 let saver = new BackgroundFileSaverOutputStream();
michael@0 315 let completionPromise = promiseSaverComplete(saver);
michael@0 316 saver.enableSignatureInfo();
michael@0 317 saver.setTarget(destFile, false);
michael@0 318 yield promiseCopyToSaver(data, saver, true);
michael@0 319
michael@0 320 saver.finish(Cr.NS_OK);
michael@0 321 yield completionPromise;
michael@0 322
michael@0 323 // Clean up.
michael@0 324 destFile.remove(false);
michael@0 325
michael@0 326 // evil.com is not on the allowlist, but this binary is signed by an entity
michael@0 327 // whose certificate information is on the allowlist.
michael@0 328 yield promiseQueryReputation({sourceURI: createURI("http://evil.com"),
michael@0 329 signatureInfo: saver.signatureInfo,
michael@0 330 fileSize: 12}, false);
michael@0 331 });
michael@0 332
michael@0 333 add_task(function test_blocked_binary()
michael@0 334 {
michael@0 335 // We should reach the remote server for a verdict.
michael@0 336 Services.prefs.setCharPref("browser.safebrowsing.appRepURL",
michael@0 337 "http://localhost:4444/download");
michael@0 338 // evil.com should return a malware verdict from the remote server.
michael@0 339 yield promiseQueryReputation({sourceURI: createURI("http://evil.com"),
michael@0 340 suggestedFileName: "noop.bat",
michael@0 341 fileSize: 12}, true);
michael@0 342 });
michael@0 343
michael@0 344 add_task(function test_non_binary()
michael@0 345 {
michael@0 346 // We should not reach the remote server for a verdict for non-binary files.
michael@0 347 Services.prefs.setCharPref("browser.safebrowsing.appRepURL",
michael@0 348 "http://localhost:4444/throw");
michael@0 349 yield promiseQueryReputation({sourceURI: createURI("http://evil.com"),
michael@0 350 suggestedFileName: "noop.txt",
michael@0 351 fileSize: 12}, false);
michael@0 352 });
michael@0 353
michael@0 354 add_task(function test_good_binary()
michael@0 355 {
michael@0 356 // We should reach the remote server for a verdict.
michael@0 357 Services.prefs.setCharPref("browser.safebrowsing.appRepURL",
michael@0 358 "http://localhost:4444/download");
michael@0 359 // mozilla.com should return a not-guilty verdict from the remote server.
michael@0 360 yield promiseQueryReputation({sourceURI: createURI("http://mozilla.com"),
michael@0 361 suggestedFileName: "noop.bat",
michael@0 362 fileSize: 12}, false);
michael@0 363 });
michael@0 364
michael@0 365 add_task(function test_teardown()
michael@0 366 {
michael@0 367 gStillRunning = false;
michael@0 368 });

mercurial