Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/url-classifier/nsUrlClassifierStreamUpdater.cpp
Line
Count
Source (jump to first uncovered line)
1
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "nsCRT.h"
7
#include "nsIHttpChannel.h"
8
#include "nsIObserverService.h"
9
#include "nsIStringStream.h"
10
#include "nsIUploadChannel.h"
11
#include "nsIURI.h"
12
#include "nsIUrlClassifierDBService.h"
13
#include "nsUrlClassifierUtils.h"
14
#include "nsNetUtil.h"
15
#include "nsStreamUtils.h"
16
#include "nsStringStream.h"
17
#include "nsToolkitCompsCID.h"
18
#include "nsUrlClassifierStreamUpdater.h"
19
#include "mozilla/BasePrincipal.h"
20
#include "mozilla/ErrorNames.h"
21
#include "mozilla/Logging.h"
22
#include "mozilla/ResultExtensions.h"
23
#include "nsIInterfaceRequestor.h"
24
#include "mozilla/LoadContext.h"
25
#include "mozilla/Telemetry.h"
26
#include "nsContentUtils.h"
27
#include "nsIURLFormatter.h"
28
#include "Classifier.h"
29
#include "UrlClassifierTelemetryUtils.h"
30
31
using namespace mozilla::safebrowsing;
32
using namespace mozilla;
33
34
0
#define DEFAULT_RESPONSE_TIMEOUT_MS 15 * 1000
35
0
#define DEFAULT_TIMEOUT_MS 60 * 1000
36
static_assert(DEFAULT_TIMEOUT_MS > DEFAULT_RESPONSE_TIMEOUT_MS,
37
  "General timeout must be greater than reponse timeout");
38
39
static const char* gQuitApplicationMessage = "quit-application";
40
41
static uint32_t sResponseTimeoutMs = DEFAULT_RESPONSE_TIMEOUT_MS;
42
static uint32_t sTimeoutMs = DEFAULT_TIMEOUT_MS;
43
44
// Limit the list file size to 32mb
45
const uint32_t MAX_FILE_SIZE = (32 * 1024 * 1024);
46
47
// Retry delay when we failed to DownloadUpdate() if due to
48
// DBService busy.
49
const uint32_t FETCH_NEXT_REQUEST_RETRY_DELAY_MS = 1000;
50
51
#undef LOG
52
53
// MOZ_LOG=UrlClassifierStreamUpdater:5
54
static mozilla::LazyLogModule gUrlClassifierStreamUpdaterLog("UrlClassifierStreamUpdater");
55
0
#define LOG(args) TrimAndLog args
56
0
#define LOG_ENABLED() MOZ_LOG_TEST(gUrlClassifierStreamUpdaterLog, mozilla::LogLevel::Debug)
57
58
// Calls nsIURLFormatter::TrimSensitiveURLs to remove sensitive
59
// info from the logging message.
60
static MOZ_FORMAT_PRINTF(1, 2) void TrimAndLog(const char* aFmt, ...)
61
0
{
62
0
  nsString raw;
63
0
64
0
  va_list ap;
65
0
  va_start(ap, aFmt);
66
0
  raw.AppendPrintf(aFmt, ap);
67
0
  va_end(ap);
68
0
69
0
  nsCOMPtr<nsIURLFormatter> urlFormatter =
70
0
    do_GetService("@mozilla.org/toolkit/URLFormatterService;1");
71
0
72
0
  nsString trimmed;
73
0
  nsresult rv = urlFormatter->TrimSensitiveURLs(raw, trimmed);
74
0
  if (NS_FAILED(rv)) {
75
0
    trimmed = EmptyString();
76
0
  }
77
0
78
0
  // Use %s so we aren't exposing random strings to printf interpolation.
79
0
  MOZ_LOG(gUrlClassifierStreamUpdaterLog,
80
0
          mozilla::LogLevel::Debug,
81
0
          ("%s", NS_ConvertUTF16toUTF8(trimmed).get()));
82
0
}
83
84
// This class does absolutely nothing, except pass requests onto the DBService.
85
86
///////////////////////////////////////////////////////////////////////////////
87
// nsIUrlClassiferStreamUpdater implementation
88
// Handles creating/running the stream listener
89
90
nsUrlClassifierStreamUpdater::nsUrlClassifierStreamUpdater()
91
  : mIsUpdating(false), mInitialized(false), mDownloadError(false),
92
    mBeganStream(false), mChannel(nullptr), mTelemetryClockStart(0)
93
0
{
94
0
  LOG(("nsUrlClassifierStreamUpdater init [this=%p]", this));
95
0
}
96
97
NS_IMPL_ISUPPORTS(nsUrlClassifierStreamUpdater,
98
                  nsIUrlClassifierStreamUpdater,
99
                  nsIUrlClassifierUpdateObserver,
100
                  nsIRequestObserver,
101
                  nsIStreamListener,
102
                  nsIObserver,
103
                  nsIInterfaceRequestor,
104
                  nsITimerCallback,
105
                  nsINamed)
106
107
/**
108
 * Clear out the update.
109
 */
110
void
111
nsUrlClassifierStreamUpdater::DownloadDone()
112
0
{
113
0
  LOG(("nsUrlClassifierStreamUpdater::DownloadDone [this=%p]", this));
114
0
  mIsUpdating = false;
115
0
116
0
  mPendingUpdates.Clear();
117
0
  mDownloadError = false;
118
0
  mCurrentRequest = nullptr;
119
0
}
120
121
///////////////////////////////////////////////////////////////////////////////
122
// nsIUrlClassifierStreamUpdater implementation
123
124
nsresult
125
nsUrlClassifierStreamUpdater::FetchUpdate(nsIURI *aUpdateUrl,
126
                                          const nsACString & aRequestPayload,
127
                                          bool aIsPostRequest,
128
                                          const nsACString & aStreamTable)
129
0
{
130
0
131
#ifdef DEBUG
132
  LOG(("Fetching update %s from %s",
133
       aRequestPayload.Data(), aUpdateUrl->GetSpecOrDefault().get()));
134
#endif
135
136
0
  nsresult rv;
137
0
  uint32_t loadFlags = nsIChannel::INHIBIT_CACHING |
138
0
                       nsIChannel::LOAD_BYPASS_CACHE;
139
0
  rv = NS_NewChannel(getter_AddRefs(mChannel),
140
0
                     aUpdateUrl,
141
0
                     nsContentUtils::GetSystemPrincipal(),
142
0
                     nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
143
0
                     nsIContentPolicy::TYPE_OTHER,
144
0
                     nullptr,  // aPerformanceStorage
145
0
                     nullptr,  // aLoadGroup
146
0
                     this,     // aInterfaceRequestor
147
0
                     loadFlags);
148
0
149
0
  NS_ENSURE_SUCCESS(rv, rv);
150
0
151
0
  nsCOMPtr<nsILoadInfo> loadInfo = mChannel->GetLoadInfo();
152
0
  mozilla::OriginAttributes attrs;
153
0
  attrs.mFirstPartyDomain.AssignLiteral(NECKO_SAFEBROWSING_FIRST_PARTY_DOMAIN);
154
0
  if (loadInfo) {
155
0
    loadInfo->SetOriginAttributes(attrs);
156
0
  }
157
0
158
0
  mBeganStream = false;
159
0
160
0
  if (!aIsPostRequest) {
161
0
    // We use POST method to send our request in v2. In v4, the request
162
0
    // needs to be embedded to the URL and use GET method to send.
163
0
    // However, from the Chromium source code, a extended HTTP header has
164
0
    // to be sent along with the request to make the request succeed.
165
0
    // The following description is from Chromium source code:
166
0
    //
167
0
    // "The following header informs the envelope server (which sits in
168
0
    // front of Google's stubby server) that the received GET request should be
169
0
    // interpreted as a POST."
170
0
    //
171
0
    nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel, &rv);
172
0
    NS_ENSURE_SUCCESS(rv, rv);
173
0
174
0
    rv = httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("X-HTTP-Method-Override"),
175
0
                                       NS_LITERAL_CSTRING("POST"),
176
0
                                       false);
177
0
    NS_ENSURE_SUCCESS(rv, rv);
178
0
  } else if (!aRequestPayload.IsEmpty()) {
179
0
    rv = AddRequestBody(aRequestPayload);
180
0
    NS_ENSURE_SUCCESS(rv, rv);
181
0
  }
182
0
183
0
  // Set the appropriate content type for file/data URIs, for unit testing
184
0
  // purposes.
185
0
  // This is only used for testing and should be deleted.
186
0
  bool match;
187
0
  if ((NS_SUCCEEDED(aUpdateUrl->SchemeIs("file", &match)) && match) ||
188
0
      (NS_SUCCEEDED(aUpdateUrl->SchemeIs("data", &match)) && match)) {
189
0
    mChannel->SetContentType(NS_LITERAL_CSTRING("application/vnd.google.safebrowsing-update"));
190
0
  } else {
191
0
    // We assume everything else is an HTTP request.
192
0
193
0
    // Disable keepalive.
194
0
    nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel, &rv);
195
0
    NS_ENSURE_SUCCESS(rv, rv);
196
0
    rv = httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Connection"), NS_LITERAL_CSTRING("close"), false);
197
0
    NS_ENSURE_SUCCESS(rv, rv);
198
0
  }
199
0
200
0
  // Make the request.
201
0
  rv = mChannel->AsyncOpen2(this);
202
0
  NS_ENSURE_SUCCESS(rv, rv);
203
0
204
0
  mTelemetryClockStart = PR_IntervalNow();
205
0
  mStreamTable = aStreamTable;
206
0
207
0
  static bool preferencesInitialized = false;
208
0
209
0
  if (!preferencesInitialized) {
210
0
    mozilla::Preferences::AddUintVarCache(&sTimeoutMs,
211
0
                                          "urlclassifier.update.timeout_ms",
212
0
                                          DEFAULT_TIMEOUT_MS);
213
0
    mozilla::Preferences::AddUintVarCache(&sResponseTimeoutMs,
214
0
                                          "urlclassifier.update.response_timeout_ms",
215
0
                                          DEFAULT_RESPONSE_TIMEOUT_MS);
216
0
    preferencesInitialized = true;
217
0
  }
218
0
219
0
  if (sResponseTimeoutMs > sTimeoutMs) {
220
0
    NS_WARNING("Safe Browsing response timeout is greater than the general "
221
0
      "timeout. Disabling these update timeouts.");
222
0
    return NS_OK;
223
0
  }
224
0
  MOZ_TRY_VAR(mResponseTimeoutTimer,
225
0
              NS_NewTimerWithCallback(this, sResponseTimeoutMs,
226
0
                                      nsITimer::TYPE_ONE_SHOT));
227
0
228
0
  MOZ_TRY_VAR(mTimeoutTimer,
229
0
              NS_NewTimerWithCallback(this, sTimeoutMs,
230
0
                                      nsITimer::TYPE_ONE_SHOT));
231
0
232
0
  if (sTimeoutMs < DEFAULT_TIMEOUT_MS) {
233
0
    LOG(("Download update timeout %d ms (< %d ms) would be too small",
234
0
         sTimeoutMs, DEFAULT_TIMEOUT_MS));
235
0
  }
236
0
237
0
  return NS_OK;
238
0
}
239
240
nsresult
241
nsUrlClassifierStreamUpdater::FetchUpdate(const nsACString & aUpdateUrl,
242
                                          const nsACString & aRequestPayload,
243
                                          bool aIsPostRequest,
244
                                          const nsACString & aStreamTable)
245
0
{
246
0
  LOG(("(pre) Fetching update from %s\n", PromiseFlatCString(aUpdateUrl).get()));
247
0
248
0
  nsCString updateUrl(aUpdateUrl);
249
0
  if (!aIsPostRequest) {
250
0
    updateUrl.AppendPrintf("&$req=%s", nsCString(aRequestPayload).get());
251
0
  }
252
0
253
0
  nsCOMPtr<nsIURI> uri;
254
0
  nsresult rv = NS_NewURI(getter_AddRefs(uri), updateUrl);
255
0
  NS_ENSURE_SUCCESS(rv, rv);
256
0
257
0
  nsAutoCString urlSpec;
258
0
  uri->GetAsciiSpec(urlSpec);
259
0
260
0
  LOG(("(post) Fetching update from %s\n", urlSpec.get()));
261
0
262
0
  return FetchUpdate(uri, aRequestPayload, aIsPostRequest, aStreamTable);
263
0
}
264
265
NS_IMETHODIMP
266
nsUrlClassifierStreamUpdater::DownloadUpdates(
267
  const nsACString &aRequestTables,
268
  const nsACString &aRequestPayload,
269
  bool aIsPostRequest,
270
  const nsACString &aUpdateUrl,
271
  nsIUrlClassifierCallback *aSuccessCallback,
272
  nsIUrlClassifierCallback *aUpdateErrorCallback,
273
  nsIUrlClassifierCallback *aDownloadErrorCallback,
274
  bool *_retval)
275
0
{
276
0
  NS_ENSURE_ARG(aSuccessCallback);
277
0
  NS_ENSURE_ARG(aUpdateErrorCallback);
278
0
  NS_ENSURE_ARG(aDownloadErrorCallback);
279
0
280
0
  if (mIsUpdating) {
281
0
    LOG(("Already updating, queueing update %s from %s", aRequestPayload.Data(),
282
0
         aUpdateUrl.Data()));
283
0
    *_retval = false;
284
0
    UpdateRequest *request = mPendingRequests.AppendElement(fallible);
285
0
    if (!request) {
286
0
      return NS_ERROR_OUT_OF_MEMORY;
287
0
    }
288
0
    BuildUpdateRequest(aRequestTables, aRequestPayload, aIsPostRequest, aUpdateUrl,
289
0
                       aSuccessCallback, aUpdateErrorCallback, aDownloadErrorCallback,
290
0
                       request);
291
0
    return NS_OK;
292
0
  }
293
0
294
0
  if (aUpdateUrl.IsEmpty()) {
295
0
    NS_ERROR("updateUrl not set");
296
0
    return NS_ERROR_NOT_INITIALIZED;
297
0
  }
298
0
299
0
  nsresult rv;
300
0
301
0
  if (!mInitialized) {
302
0
    // Add an observer for shutdown so we can cancel any pending list
303
0
    // downloads.  quit-application is the same event that the download
304
0
    // manager listens for and uses to cancel pending downloads.
305
0
    nsCOMPtr<nsIObserverService> observerService =
306
0
      mozilla::services::GetObserverService();
307
0
    if (!observerService)
308
0
      return NS_ERROR_FAILURE;
309
0
310
0
    observerService->AddObserver(this, gQuitApplicationMessage, false);
311
0
312
0
    mDBService = do_GetService(NS_URLCLASSIFIERDBSERVICE_CONTRACTID, &rv);
313
0
    NS_ENSURE_SUCCESS(rv, rv);
314
0
315
0
    mInitialized = true;
316
0
  }
317
0
318
0
  rv = mDBService->BeginUpdate(this, aRequestTables);
319
0
  if (rv == NS_ERROR_NOT_AVAILABLE) {
320
0
    LOG(("Service busy, already updating, queuing update %s from %s",
321
0
         aRequestPayload.Data(), aUpdateUrl.Data()));
322
0
    *_retval = false;
323
0
    UpdateRequest *request = mPendingRequests.AppendElement(fallible);
324
0
    if (!request) {
325
0
      return NS_ERROR_OUT_OF_MEMORY;
326
0
    }
327
0
    BuildUpdateRequest(aRequestTables, aRequestPayload, aIsPostRequest, aUpdateUrl,
328
0
                       aSuccessCallback, aUpdateErrorCallback, aDownloadErrorCallback,
329
0
                       request);
330
0
331
0
    // We cannot guarantee that we will be notified when DBService is done
332
0
    // processing the current update, so we fire a retry timer on our own.
333
0
    MOZ_TRY_VAR(mFetchNextRequestTimer,
334
0
                NS_NewTimerWithCallback(this, FETCH_NEXT_REQUEST_RETRY_DELAY_MS,
335
0
                                        nsITimer::TYPE_ONE_SHOT));
336
0
337
0
    return NS_OK;
338
0
  }
339
0
340
0
  if (NS_FAILED(rv)) {
341
0
    return rv;
342
0
  }
343
0
344
0
  nsCOMPtr<nsIUrlClassifierUtils> urlUtil =
345
0
    do_GetService(NS_URLCLASSIFIERUTILS_CONTRACTID);
346
0
347
0
  nsTArray<nsCString> tables;
348
0
  mozilla::safebrowsing::Classifier::SplitTables(aRequestTables, tables);
349
0
  urlUtil->GetTelemetryProvider(tables.SafeElementAt(0, EmptyCString()),
350
0
                                mTelemetryProvider);
351
0
352
0
  mCurrentRequest = MakeUnique<UpdateRequest>();
353
0
  BuildUpdateRequest(aRequestTables, aRequestPayload, aIsPostRequest, aUpdateUrl,
354
0
                     aSuccessCallback, aUpdateErrorCallback, aDownloadErrorCallback,
355
0
                     mCurrentRequest.get());
356
0
357
0
  mIsUpdating = true;
358
0
  *_retval = true;
359
0
360
0
  LOG(("FetchUpdate: %s", mCurrentRequest->mUrl.Data()));
361
0
362
0
  return FetchUpdate(aUpdateUrl, aRequestPayload, aIsPostRequest, EmptyCString());
363
0
}
364
365
///////////////////////////////////////////////////////////////////////////////
366
// nsIUrlClassifierUpdateObserver implementation
367
368
NS_IMETHODIMP
369
nsUrlClassifierStreamUpdater::UpdateUrlRequested(const nsACString &aUrl,
370
                                                 const nsACString &aTable)
371
0
{
372
0
  LOG(("Queuing requested update from %s\n", PromiseFlatCString(aUrl).get()));
373
0
374
0
  PendingUpdate *update = mPendingUpdates.AppendElement(fallible);
375
0
  if (!update) {
376
0
    return NS_ERROR_OUT_OF_MEMORY;
377
0
  }
378
0
379
0
  // Allow data: and file: urls for unit testing purposes, otherwise assume http
380
0
  if (StringBeginsWith(aUrl, NS_LITERAL_CSTRING("data:")) ||
381
0
      StringBeginsWith(aUrl, NS_LITERAL_CSTRING("file:"))) {
382
0
    update->mUrl = aUrl;
383
0
  } else {
384
0
    // For unittesting update urls to localhost should use http, not https
385
0
    // (otherwise the connection will fail silently, since there will be no
386
0
    // cert available).
387
0
    if (!StringBeginsWith(aUrl, NS_LITERAL_CSTRING("localhost"))) {
388
0
      update->mUrl = NS_LITERAL_CSTRING("https://") + aUrl;
389
0
    } else {
390
0
      update->mUrl = NS_LITERAL_CSTRING("http://") + aUrl;
391
0
    }
392
0
  }
393
0
  update->mTable = aTable;
394
0
395
0
  return NS_OK;
396
0
}
397
398
nsresult
399
nsUrlClassifierStreamUpdater::FetchNext()
400
0
{
401
0
  if (mPendingUpdates.Length() == 0) {
402
0
    return NS_OK;
403
0
  }
404
0
405
0
  PendingUpdate &update = mPendingUpdates[0];
406
0
  LOG(("Fetching update url: %s\n", update.mUrl.get()));
407
0
  nsresult rv = FetchUpdate(update.mUrl,
408
0
                            EmptyCString(),
409
0
                            true, // This method is for v2 and v2 is always a POST.
410
0
                            update.mTable);
411
0
  if (NS_FAILED(rv)) {
412
0
    nsAutoCString errorName;
413
0
    mozilla::GetErrorName(rv, errorName);
414
0
    LOG(("Error (%s) fetching update url: %s\n", errorName.get(),
415
0
         update.mUrl.get()));
416
0
    // We can commit the urls that we've applied so far.  This is
417
0
    // probably a transient server problem, so trigger backoff.
418
0
    mDownloadError = true;
419
0
    mDBService->FinishUpdate();
420
0
    return rv;
421
0
  }
422
0
423
0
  mPendingUpdates.RemoveElementAt(0);
424
0
425
0
  return NS_OK;
426
0
}
427
428
nsresult
429
nsUrlClassifierStreamUpdater::FetchNextRequest()
430
0
{
431
0
  if (mPendingRequests.Length() == 0) {
432
0
    LOG(("No more requests, returning"));
433
0
    return NS_OK;
434
0
  }
435
0
436
0
  UpdateRequest request = mPendingRequests[0];
437
0
  mPendingRequests.RemoveElementAt(0);
438
0
  LOG(("Stream updater: fetching next request: %s, %s",
439
0
       request.mTables.get(), request.mUrl.get()));
440
0
  bool dummy;
441
0
  DownloadUpdates(
442
0
    request.mTables,
443
0
    request.mRequestPayload,
444
0
    request.mIsPostRequest,
445
0
    request.mUrl,
446
0
    request.mSuccessCallback,
447
0
    request.mUpdateErrorCallback,
448
0
    request.mDownloadErrorCallback,
449
0
    &dummy);
450
0
  return NS_OK;
451
0
}
452
453
void
454
nsUrlClassifierStreamUpdater::BuildUpdateRequest(
455
  const nsACString &aRequestTables,
456
  const nsACString &aRequestPayload,
457
  bool aIsPostRequest,
458
  const nsACString &aUpdateUrl,
459
  nsIUrlClassifierCallback *aSuccessCallback,
460
  nsIUrlClassifierCallback *aUpdateErrorCallback,
461
  nsIUrlClassifierCallback *aDownloadErrorCallback,
462
  UpdateRequest* aRequest)
463
0
{
464
0
  MOZ_ASSERT(aRequest);
465
0
466
0
  aRequest->mTables = aRequestTables;
467
0
  aRequest->mRequestPayload = aRequestPayload;
468
0
  aRequest->mIsPostRequest = aIsPostRequest;
469
0
  aRequest->mUrl = aUpdateUrl;
470
0
  aRequest->mSuccessCallback = aSuccessCallback;
471
0
  aRequest->mUpdateErrorCallback = aUpdateErrorCallback;
472
0
  aRequest->mDownloadErrorCallback = aDownloadErrorCallback;
473
0
}
474
475
NS_IMETHODIMP
476
nsUrlClassifierStreamUpdater::StreamFinished(nsresult status,
477
                                             uint32_t requestedDelay)
478
0
{
479
0
  // We are a service and may not be reset with Init between calls, so reset
480
0
  // mBeganStream manually.
481
0
  mBeganStream = false;
482
0
  if (LOG_ENABLED()) {
483
0
    nsAutoCString errorName;
484
0
    mozilla::GetErrorName(status, errorName);
485
0
    LOG(("nsUrlClassifierStreamUpdater::StreamFinished [%s, %d]",
486
0
         errorName.get(), requestedDelay));
487
0
  }
488
0
  if (NS_FAILED(status) || mPendingUpdates.Length() == 0) {
489
0
    // We're done.
490
0
    LOG(("nsUrlClassifierStreamUpdater::Done [this=%p]", this));
491
0
    mDBService->FinishUpdate();
492
0
    return NS_OK;
493
0
  }
494
0
495
0
  // This timer is for fetching indirect updates ("forwards") from any "u:" lines
496
0
  // that we encountered while processing the server response. It is NOT for
497
0
  // scheduling the next time we pull the list from the server. That's a different
498
0
  // timer in listmanager.js (see bug 1110891).
499
0
  nsresult rv;
500
0
  rv = NS_NewTimerWithCallback(getter_AddRefs(mFetchIndirectUpdatesTimer),
501
0
                               this, requestedDelay, nsITimer::TYPE_ONE_SHOT);
502
0
503
0
  if (NS_FAILED(rv)) {
504
0
    NS_WARNING("Unable to initialize timer, fetching next safebrowsing item immediately");
505
0
    return FetchNext();
506
0
  }
507
0
508
0
  return NS_OK;
509
0
}
510
511
NS_IMETHODIMP
512
nsUrlClassifierStreamUpdater::UpdateSuccess(uint32_t requestedTimeout)
513
0
{
514
0
  LOG(("nsUrlClassifierStreamUpdater::UpdateSuccess [this=%p]", this));
515
0
  if (mPendingUpdates.Length() != 0) {
516
0
    NS_WARNING("Didn't fetch all safebrowsing update redirects");
517
0
  }
518
0
519
0
  // DownloadDone() clears mSuccessCallback, so we save it off here.
520
0
  nsCOMPtr<nsIUrlClassifierCallback> successCallback =
521
0
    mDownloadError ? nullptr : mCurrentRequest->mSuccessCallback.get();
522
0
  nsCOMPtr<nsIUrlClassifierCallback> downloadErrorCallback =
523
0
    mDownloadError ? mCurrentRequest->mDownloadErrorCallback.get() : nullptr;
524
0
525
0
  DownloadDone();
526
0
527
0
  nsAutoCString strTimeout;
528
0
  strTimeout.AppendInt(requestedTimeout);
529
0
  if (successCallback) {
530
0
    LOG(("nsUrlClassifierStreamUpdater::UpdateSuccess callback [this=%p]",
531
0
         this));
532
0
    successCallback->HandleEvent(strTimeout);
533
0
  } else if (downloadErrorCallback) {
534
0
    downloadErrorCallback->HandleEvent(mDownloadErrorStatusStr);
535
0
    mDownloadErrorStatusStr = EmptyCString();
536
0
    LOG(("Notify download error callback in UpdateSuccess [this=%p]", this));
537
0
  }
538
0
  // Now fetch the next request
539
0
  LOG(("stream updater: calling into fetch next request"));
540
0
  FetchNextRequest();
541
0
542
0
  return NS_OK;
543
0
}
544
545
NS_IMETHODIMP
546
nsUrlClassifierStreamUpdater::UpdateError(nsresult result)
547
0
{
548
0
  LOG(("nsUrlClassifierStreamUpdater::UpdateError [this=%p]", this));
549
0
550
0
  // DownloadDone() clears mUpdateErrorCallback, so we save it off here.
551
0
  nsCOMPtr<nsIUrlClassifierCallback> errorCallback =
552
0
    mDownloadError ? nullptr : mCurrentRequest->mUpdateErrorCallback.get();
553
0
  nsCOMPtr<nsIUrlClassifierCallback> downloadErrorCallback =
554
0
    mDownloadError ? mCurrentRequest->mDownloadErrorCallback.get() : nullptr;
555
0
  DownloadDone();
556
0
557
0
  if (errorCallback) {
558
0
    nsAutoCString strResult;
559
0
    mozilla::GetErrorName(result, strResult);
560
0
    errorCallback->HandleEvent(strResult);
561
0
  } else if (downloadErrorCallback) {
562
0
    LOG(("Notify download error callback in UpdateError [this=%p]", this));
563
0
    downloadErrorCallback->HandleEvent(mDownloadErrorStatusStr);
564
0
    mDownloadErrorStatusStr = EmptyCString();
565
0
  }
566
0
567
0
  return NS_OK;
568
0
}
569
570
nsresult
571
nsUrlClassifierStreamUpdater::AddRequestBody(const nsACString &aRequestBody)
572
0
{
573
0
  nsresult rv;
574
0
  nsCOMPtr<nsIStringInputStream> strStream =
575
0
    do_CreateInstance(NS_STRINGINPUTSTREAM_CONTRACTID, &rv);
576
0
  NS_ENSURE_SUCCESS(rv, rv);
577
0
578
0
  rv = strStream->SetData(aRequestBody.BeginReading(),
579
0
                          aRequestBody.Length());
580
0
  NS_ENSURE_SUCCESS(rv, rv);
581
0
582
0
  nsCOMPtr<nsIUploadChannel> uploadChannel = do_QueryInterface(mChannel, &rv);
583
0
  NS_ENSURE_SUCCESS(rv, rv);
584
0
585
0
  rv = uploadChannel->SetUploadStream(strStream,
586
0
                                      NS_LITERAL_CSTRING("text/plain"),
587
0
                                      -1);
588
0
  NS_ENSURE_SUCCESS(rv, rv);
589
0
590
0
  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel, &rv);
591
0
  NS_ENSURE_SUCCESS(rv, rv);
592
0
593
0
  rv = httpChannel->SetRequestMethod(NS_LITERAL_CSTRING("POST"));
594
0
  NS_ENSURE_SUCCESS(rv, rv);
595
0
596
0
  return NS_OK;
597
0
}
598
599
///////////////////////////////////////////////////////////////////////////////
600
// nsIStreamListenerObserver implementation
601
602
NS_IMETHODIMP
603
nsUrlClassifierStreamUpdater::OnStartRequest(nsIRequest *request,
604
                                             nsISupports* context)
605
0
{
606
0
  nsresult rv;
607
0
  bool downloadError = false;
608
0
  nsAutoCString strStatus;
609
0
  nsresult status = NS_OK;
610
0
611
0
  // Only update if we got http success header
612
0
  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
613
0
  if (httpChannel) {
614
0
    rv = httpChannel->GetStatus(&status);
615
0
    NS_ENSURE_SUCCESS(rv, rv);
616
0
617
0
    if (LOG_ENABLED()) {
618
0
      nsAutoCString errorName, spec;
619
0
      mozilla::GetErrorName(status, errorName);
620
0
      nsCOMPtr<nsIURI> uri;
621
0
      rv = httpChannel->GetURI(getter_AddRefs(uri));
622
0
      if (NS_SUCCEEDED(rv) && uri) {
623
0
        uri->GetAsciiSpec(spec);
624
0
      }
625
0
      LOG(("nsUrlClassifierStreamUpdater::OnStartRequest "
626
0
           "(status=%s, uri=%s, this=%p)", errorName.get(),
627
0
           spec.get(), this));
628
0
    }
629
0
    if (mTelemetryClockStart > 0) {
630
0
      uint32_t msecs = PR_IntervalToMilliseconds(PR_IntervalNow() - mTelemetryClockStart);
631
0
      mozilla::Telemetry::Accumulate(mozilla::Telemetry::URLCLASSIFIER_UPDATE_SERVER_RESPONSE_TIME,
632
0
                                     mTelemetryProvider, msecs);
633
0
634
0
    }
635
0
636
0
    if (mResponseTimeoutTimer) {
637
0
      mResponseTimeoutTimer->Cancel();
638
0
      mResponseTimeoutTimer = nullptr;
639
0
    }
640
0
641
0
    uint8_t netErrCode = NS_FAILED(status) ? NetworkErrorToBucket(status) : 0;
642
0
    mozilla::Telemetry::Accumulate(
643
0
      mozilla::Telemetry::URLCLASSIFIER_UPDATE_REMOTE_NETWORK_ERROR,
644
0
      mTelemetryProvider, netErrCode);
645
0
646
0
    if (NS_FAILED(status)) {
647
0
      // Assume we're overloading the server and trigger backoff.
648
0
      downloadError = true;
649
0
    } else {
650
0
      bool succeeded = false;
651
0
      rv = httpChannel->GetRequestSucceeded(&succeeded);
652
0
      NS_ENSURE_SUCCESS(rv, rv);
653
0
654
0
      uint32_t requestStatus;
655
0
      rv = httpChannel->GetResponseStatus(&requestStatus);
656
0
      NS_ENSURE_SUCCESS(rv, rv);
657
0
      mozilla::Telemetry::Accumulate(mozilla::Telemetry::URLCLASSIFIER_UPDATE_REMOTE_STATUS2,
658
0
                                     mTelemetryProvider, HTTPStatusToBucket(requestStatus));
659
0
      if (requestStatus == 400) {
660
0
        printf_stderr("Safe Browsing server returned a 400 during update:"
661
0
                       "request url = %s, payload = %s\n",
662
0
                       mCurrentRequest->mUrl.get(), mCurrentRequest->mRequestPayload.get());
663
0
      }
664
0
665
0
      LOG(("nsUrlClassifierStreamUpdater::OnStartRequest %s (%d)", succeeded ?
666
0
           "succeeded" : "failed", requestStatus));
667
0
      if (!succeeded) {
668
0
        // 404 or other error, pass error status back
669
0
        strStatus.AppendInt(requestStatus);
670
0
        downloadError = true;
671
0
      }
672
0
    }
673
0
  }
674
0
675
0
  if (downloadError) {
676
0
    LOG(("nsUrlClassifierStreamUpdater::Download error [this=%p]", this));
677
0
    mDownloadError = true;
678
0
    mDownloadErrorStatusStr = strStatus;
679
0
    status = NS_ERROR_ABORT;
680
0
  } else if (NS_SUCCEEDED(status)) {
681
0
    MOZ_ASSERT(mCurrentRequest->mDownloadErrorCallback);
682
0
    mBeganStream = true;
683
0
    LOG(("nsUrlClassifierStreamUpdater::Beginning stream [this=%p]", this));
684
0
    rv = mDBService->BeginStream(mStreamTable);
685
0
    NS_ENSURE_SUCCESS(rv, rv);
686
0
  }
687
0
688
0
  mStreamTable.Truncate();
689
0
690
0
  return status;
691
0
}
692
693
NS_IMETHODIMP
694
nsUrlClassifierStreamUpdater::OnDataAvailable(nsIRequest *request,
695
                                              nsISupports* context,
696
                                              nsIInputStream *aIStream,
697
                                              uint64_t aSourceOffset,
698
                                              uint32_t aLength)
699
0
{
700
0
  if (!mDBService)
701
0
    return NS_ERROR_NOT_INITIALIZED;
702
0
703
0
  LOG(("OnDataAvailable (%d bytes)", aLength));
704
0
705
0
  if (aSourceOffset > MAX_FILE_SIZE) {
706
0
    LOG(("OnDataAvailable::Abort because exceeded the maximum file size(%" PRIu64 ")", aSourceOffset));
707
0
    return NS_ERROR_FILE_TOO_BIG;
708
0
  }
709
0
710
0
  nsresult rv;
711
0
712
0
  // Copy the data into a nsCString
713
0
  nsCString chunk;
714
0
  rv = NS_ConsumeStream(aIStream, aLength, chunk);
715
0
  NS_ENSURE_SUCCESS(rv, rv);
716
0
717
0
  //LOG(("Chunk (%d): %s\n\n", chunk.Length(), chunk.get()));
718
0
  rv = mDBService->UpdateStream(chunk);
719
0
  NS_ENSURE_SUCCESS(rv, rv);
720
0
721
0
  return NS_OK;
722
0
}
723
724
NS_IMETHODIMP
725
nsUrlClassifierStreamUpdater::OnStopRequest(nsIRequest *request, nsISupports* context,
726
                                            nsresult aStatus)
727
0
{
728
0
  if (!mDBService)
729
0
    return NS_ERROR_NOT_INITIALIZED;
730
0
731
0
  if (LOG_ENABLED()) {
732
0
    nsAutoCString errorName;
733
0
    mozilla::GetErrorName(aStatus, errorName);
734
0
    LOG(("OnStopRequest (status %s, beganStream %s, this=%p)",
735
0
         errorName.get(), mBeganStream ? "true" : "false", this));
736
0
  }
737
0
738
0
  nsresult rv;
739
0
740
0
  if (NS_SUCCEEDED(aStatus)) {
741
0
    // Success, finish this stream and move on to the next.
742
0
    rv = mDBService->FinishStream();
743
0
  } else if (mBeganStream) {
744
0
    LOG(("OnStopRequest::Canceling update [this=%p]", this));
745
0
    // We began this stream and couldn't finish it.  We have to cancel the
746
0
    // update, it's not in a consistent state.
747
0
    rv = mDBService->CancelUpdate();
748
0
  } else {
749
0
    LOG(("OnStopRequest::Finishing update [this=%p]", this));
750
0
    // The fetch failed, but we didn't start the stream (probably a
751
0
    // server or connection error).  We can commit what we've applied
752
0
    // so far, and request again later.
753
0
    rv = mDBService->FinishUpdate();
754
0
  }
755
0
756
0
  if (mResponseTimeoutTimer) {
757
0
    mResponseTimeoutTimer->Cancel();
758
0
    mResponseTimeoutTimer = nullptr;
759
0
  }
760
0
761
0
  // mResponseTimeoutTimer may be cleared in OnStartRequest, so we check mTimeoutTimer
762
0
  // to see whether the update was has timed out
763
0
  if (mTimeoutTimer) {
764
0
    mozilla::Telemetry::Accumulate(mozilla::Telemetry::URLCLASSIFIER_UPDATE_TIMEOUT,
765
0
                                   mTelemetryProvider,
766
0
                                   static_cast<uint8_t>(eNoTimeout));
767
0
    mTimeoutTimer->Cancel();
768
0
    mTimeoutTimer = nullptr;
769
0
  }
770
0
771
0
  mTelemetryProvider.Truncate();
772
0
  mTelemetryClockStart = 0;
773
0
  mChannel = nullptr;
774
0
775
0
  // If the fetch failed, return the network status rather than NS_OK, the
776
0
  // result of finishing a possibly-empty update
777
0
  if (NS_SUCCEEDED(aStatus)) {
778
0
    return rv;
779
0
  }
780
0
  return aStatus;
781
0
}
782
783
///////////////////////////////////////////////////////////////////////////////
784
// nsIObserver implementation
785
786
NS_IMETHODIMP
787
nsUrlClassifierStreamUpdater::Observe(nsISupports *aSubject, const char *aTopic,
788
                                      const char16_t *aData)
789
0
{
790
0
  if (nsCRT::strcmp(aTopic, gQuitApplicationMessage) == 0) {
791
0
    if (mIsUpdating && mChannel) {
792
0
      LOG(("Cancel download"));
793
0
      nsresult rv;
794
0
      rv = mChannel->Cancel(NS_ERROR_ABORT);
795
0
      NS_ENSURE_SUCCESS(rv, rv);
796
0
      mIsUpdating = false;
797
0
      mChannel = nullptr;
798
0
      mTelemetryClockStart = 0;
799
0
    }
800
0
    if (mFetchIndirectUpdatesTimer) {
801
0
      mFetchIndirectUpdatesTimer->Cancel();
802
0
      mFetchIndirectUpdatesTimer = nullptr;
803
0
    }
804
0
    if (mFetchNextRequestTimer) {
805
0
      mFetchNextRequestTimer->Cancel();
806
0
      mFetchNextRequestTimer = nullptr;
807
0
    }
808
0
    if (mResponseTimeoutTimer) {
809
0
      mResponseTimeoutTimer->Cancel();
810
0
      mResponseTimeoutTimer = nullptr;
811
0
    }
812
0
    if (mTimeoutTimer) {
813
0
      mTimeoutTimer->Cancel();
814
0
      mTimeoutTimer = nullptr;
815
0
    }
816
0
817
0
  }
818
0
  return NS_OK;
819
0
}
820
821
///////////////////////////////////////////////////////////////////////////////
822
// nsIInterfaceRequestor implementation
823
824
NS_IMETHODIMP
825
nsUrlClassifierStreamUpdater::GetInterface(const nsIID & eventSinkIID, void* *_retval)
826
0
{
827
0
  return QueryInterface(eventSinkIID, _retval);
828
0
}
829
830
831
///////////////////////////////////////////////////////////////////////////////
832
// nsITimerCallback implementation
833
NS_IMETHODIMP
834
nsUrlClassifierStreamUpdater::Notify(nsITimer *timer)
835
0
{
836
0
  LOG(("nsUrlClassifierStreamUpdater::Notify [%p]", this));
837
0
838
0
  if (timer == mFetchNextRequestTimer) {
839
0
    mFetchNextRequestTimer = nullptr;
840
0
    FetchNextRequest();
841
0
    return NS_OK;
842
0
  }
843
0
844
0
  if (timer == mFetchIndirectUpdatesTimer) {
845
0
    mFetchIndirectUpdatesTimer = nullptr;
846
0
    // Start the update process up again.
847
0
    FetchNext();
848
0
    return NS_OK;
849
0
  }
850
0
851
0
  bool updateFailed = false;
852
0
  if (timer == mResponseTimeoutTimer) {
853
0
    mResponseTimeoutTimer = nullptr;
854
0
    if (mTimeoutTimer) {
855
0
      mTimeoutTimer->Cancel();
856
0
      mTimeoutTimer = nullptr;
857
0
    }
858
0
    mDownloadError = true; // Trigger backoff
859
0
    updateFailed = true;
860
0
    MOZ_LOG(gUrlClassifierStreamUpdaterLog, mozilla::LogLevel::Error,
861
0
            ("Safe Browsing timed out while waiting for the update server to respond."));
862
0
    mozilla::Telemetry::Accumulate(mozilla::Telemetry::URLCLASSIFIER_UPDATE_TIMEOUT,
863
0
                                   mTelemetryProvider,
864
0
                                   static_cast<uint8_t>(eResponseTimeout));
865
0
  }
866
0
867
0
  if (timer == mTimeoutTimer) {
868
0
    mTimeoutTimer = nullptr;
869
0
    // No backoff since the connection may just be temporarily slow.
870
0
    updateFailed = true;
871
0
    MOZ_LOG(gUrlClassifierStreamUpdaterLog, mozilla::LogLevel::Error,
872
0
            ("Safe Browsing timed out while waiting for the update server to finish."));
873
0
    mozilla::Telemetry::Accumulate(mozilla::Telemetry::URLCLASSIFIER_UPDATE_TIMEOUT,
874
0
                                   mTelemetryProvider,
875
0
                                   static_cast<uint8_t>(eDownloadTimeout));
876
0
  }
877
0
878
0
  if (updateFailed) {
879
0
    // Cancelling the channel will trigger OnStopRequest.
880
0
    mozilla::Unused << mChannel->Cancel(NS_ERROR_ABORT);
881
0
    mChannel = nullptr;
882
0
    mTelemetryClockStart = 0;
883
0
884
0
    if (mFetchIndirectUpdatesTimer) {
885
0
      mFetchIndirectUpdatesTimer->Cancel();
886
0
      mFetchIndirectUpdatesTimer = nullptr;
887
0
    }
888
0
    if (mFetchNextRequestTimer) {
889
0
      mFetchNextRequestTimer->Cancel();
890
0
      mFetchNextRequestTimer = nullptr;
891
0
    }
892
0
893
0
    return NS_OK;
894
0
  }
895
0
896
0
  MOZ_ASSERT_UNREACHABLE("A timer is fired from nowhere.");
897
0
  return NS_OK;
898
0
}
899
900
////////////////////////////////////////////////////////////////////////
901
//// nsINamed
902
903
NS_IMETHODIMP
904
nsUrlClassifierStreamUpdater::GetName(nsACString& aName)
905
0
{
906
0
  aName.AssignLiteral("nsUrlClassifierStreamUpdater");
907
0
  return NS_OK;
908
0
}
909