summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/CrashReporter.java
blob: 691686e230ed128bdcfcc732a3d50294b1decf84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.geckoview;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.zip.GZIPOutputStream;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.util.ProxySelector;

/**
 * Sends a crash report to the Mozilla <a href="https://wiki.mozilla.org/Socorro">Socorro</a> crash
 * report server.
 */
public class CrashReporter {
  private static final String LOGTAG = "GeckoCrashReporter";
  private static final String MINI_DUMP_PATH_KEY = "upload_file_minidump";
  private static final String PAGE_URL_KEY = "URL";
  private static final String MINIDUMP_SHA256_HASH_KEY = "MinidumpSha256Hash";
  private static final String NOTES_KEY = "Notes";
  private static final String SERVER_URL_KEY = "ServerURL";
  private static final String STACK_TRACES_KEY = "StackTraces";
  private static final String PRODUCT_NAME_KEY = "ProductName";
  private static final String PRODUCT_ID_KEY = "ProductID";
  private static final String PRODUCT_ID = "{eeb82917-e434-4870-8148-5c03d4caa81b}";
  private static final List<String> IGNORE_KEYS =
      Arrays.asList(PAGE_URL_KEY, SERVER_URL_KEY, STACK_TRACES_KEY);

  /**
   * Sends a crash report to the Mozilla <a href="https://wiki.mozilla.org/Socorro">Socorro</a>
   * crash report server. <br>
   * The {@code appName} needs to be whitelisted for the server to accept the crash. <a
   * href="https://bugzilla.mozilla.org/enter_bug.cgi?product=Socorro">File a bug</a> if you would
   * like to get your app added to the whitelist.
   *
   * @param context The current Context
   * @param intent The Intent sent to the {@link GeckoRuntime} crash handler
   * @param appName A human-readable app name.
   * @throws IOException This can be thrown if there was a networking error while sending the
   *     report.
   * @throws URISyntaxException This can be thrown if the crash server URI from the extra data was
   *     invalid.
   * @return A GeckoResult containing the crash ID as a String.
   * @see GeckoRuntimeSettings.Builder#crashHandler(Class)
   * @see GeckoRuntime#ACTION_CRASHED
   */
  @AnyThread
  public static @NonNull GeckoResult<String> sendCrashReport(
      @NonNull final Context context, @NonNull final Intent intent, @NonNull final String appName)
      throws IOException, URISyntaxException {
    return sendCrashReport(context, intent.getExtras(), appName);
  }

  /**
   * Sends a crash report to the Mozilla <a href="https://wiki.mozilla.org/Socorro">Socorro</a>
   * crash report server. <br>
   * The {@code appName} needs to be whitelisted for the server to accept the crash. <a
   * href="https://bugzilla.mozilla.org/enter_bug.cgi?product=Socorro">File a bug</a> if you would
   * like to get your app added to the whitelist.
   *
   * @param context The current Context
   * @param intentExtras The Bundle of extras attached to the Intent received by a crash handler.
   * @param appName A human-readable app name.
   * @throws IOException This can be thrown if there was a networking error while sending the
   *     report.
   * @throws URISyntaxException This can be thrown if the crash server URI from the extra data was
   *     invalid.
   * @return A GeckoResult containing the crash ID as a String.
   * @see GeckoRuntimeSettings.Builder#crashHandler(Class)
   * @see GeckoRuntime#ACTION_CRASHED
   */
  @AnyThread
  public static @NonNull GeckoResult<String> sendCrashReport(
      @NonNull final Context context,
      @NonNull final Bundle intentExtras,
      @NonNull final String appName)
      throws IOException, URISyntaxException {
    final File dumpFile = new File(intentExtras.getString(GeckoRuntime.EXTRA_MINIDUMP_PATH));
    final File extrasFile = new File(intentExtras.getString(GeckoRuntime.EXTRA_EXTRAS_PATH));

    return sendCrashReport(context, dumpFile, extrasFile, appName);
  }

  /**
   * Sends a crash report to the Mozilla <a href="https://wiki.mozilla.org/Socorro">Socorro</a>
   * crash report server. <br>
   * The {@code appName} needs to be whitelisted for the server to accept the crash. <a
   * href="https://bugzilla.mozilla.org/enter_bug.cgi?product=Socorro">File a bug</a> if you would
   * like to get your app added to the whitelist.
   *
   * @param context The current {@link Context}
   * @param minidumpFile A {@link File} referring to the minidump.
   * @param extrasFile A {@link File} referring to the extras file.
   * @param appName A human-readable app name.
   * @throws IOException This can be thrown if there was a networking error while sending the
   *     report.
   * @throws URISyntaxException This can be thrown if the crash server URI from the extra data was
   *     invalid.
   * @return A GeckoResult containing the crash ID as a String.
   * @see GeckoRuntimeSettings.Builder#crashHandler(Class)
   * @see GeckoRuntime#ACTION_CRASHED
   */
  @AnyThread
  public static @NonNull GeckoResult<String> sendCrashReport(
      @NonNull final Context context,
      @NonNull final File minidumpFile,
      @NonNull final File extrasFile,
      @NonNull final String appName)
      throws IOException, URISyntaxException {
    final JSONObject annotations = getCrashAnnotations(context, minidumpFile, extrasFile, appName);

    final String url = annotations.optString(SERVER_URL_KEY, null);
    if (url == null) {
      return GeckoResult.fromException(new Exception("No server url present"));
    }

    for (final String key : IGNORE_KEYS) {
      annotations.remove(key);
    }

    return sendCrashReport(url, minidumpFile, annotations);
  }

  /**
   * Sends a crash report to the Mozilla <a href="https://wiki.mozilla.org/Socorro">Socorro</a>
   * crash report server.
   *
   * @param serverURL The URL used to submit the crash report.
   * @param minidumpFile A {@link File} referring to the minidump.
   * @param extras A {@link JSONObject} holding the parsed JSON from the extra file.
   * @throws IOException This can be thrown if there was a networking error while sending the
   *     report.
   * @throws URISyntaxException This can be thrown if the crash server URI from the extra data was
   *     invalid.
   * @return A GeckoResult containing the crash ID as a String.
   * @see GeckoRuntimeSettings.Builder#crashHandler(Class)
   * @see GeckoRuntime#ACTION_CRASHED
   */
  @AnyThread
  public static @NonNull GeckoResult<String> sendCrashReport(
      @NonNull final String serverURL,
      @NonNull final File minidumpFile,
      @NonNull final JSONObject extras)
      throws IOException, URISyntaxException {
    Log.d(LOGTAG, "Sending crash report: " + minidumpFile.getPath());

    HttpURLConnection conn = null;
    try {
      final URL url = new URL(URLDecoder.decode(serverURL, "UTF-8"));
      final URI uri =
          new URI(
              url.getProtocol(),
              url.getUserInfo(),
              url.getHost(),
              url.getPort(),
              url.getPath(),
              url.getQuery(),
              url.getRef());
      conn = (HttpURLConnection) ProxySelector.openConnectionWithProxy(uri);
      conn.setRequestMethod("POST");
      final String boundary = generateBoundary();
      conn.setDoOutput(true);
      conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
      conn.setRequestProperty("Content-Encoding", "gzip");

      final OutputStream os = new GZIPOutputStream(conn.getOutputStream());
      sendAnnotations(os, boundary, extras);
      sendFile(os, boundary, MINI_DUMP_PATH_KEY, minidumpFile);
      os.write(("\r\n--" + boundary + "--\r\n").getBytes());
      os.flush();
      os.close();

      BufferedReader br = null;
      try {
        br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        final HashMap<String, String> responseMap = readStringsFromReader(br);

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
          final String crashid = responseMap.get("CrashID");
          if (crashid != null) {
            Log.i(LOGTAG, "Successfully sent crash report: " + crashid);
            return GeckoResult.fromValue(crashid);
          } else {
            Log.i(LOGTAG, "Server rejected crash report");
          }
        } else {
          Log.w(
              LOGTAG, "Received failure HTTP response code from server: " + conn.getResponseCode());
        }
      } catch (final Exception e) {
        return GeckoResult.fromException(new Exception("Failed to submit crash report", e));
      } finally {
        try {
          if (br != null) {
            br.close();
          }
        } catch (final IOException e) {
          return GeckoResult.fromException(new Exception("Failed to submit crash report", e));
        }
      }
    } catch (final Exception e) {
      return GeckoResult.fromException(new Exception("Failed to submit crash report", e));
    } finally {
      if (conn != null) {
        conn.disconnect();
      }
    }
    return GeckoResult.fromException(new Exception("Failed to submit crash report"));
  }

  private static String computeMinidumpHash(@NonNull final File minidump) throws IOException {
    MessageDigest md = null;
    final FileInputStream stream = new FileInputStream(minidump);
    try {
      md = MessageDigest.getInstance("SHA-256");

      final byte[] buffer = new byte[4096];
      int readBytes;

      while ((readBytes = stream.read(buffer)) != -1) {
        md.update(buffer, 0, readBytes);
      }
    } catch (final NoSuchAlgorithmException e) {
      throw new IOException(e);
    } finally {
      stream.close();
    }

    final byte[] digest = md.digest();
    final StringBuilder hash = new StringBuilder(64);

    for (int i = 0; i < digest.length; i++) {
      hash.append(Integer.toHexString((digest[i] & 0xf0) >> 4));
      hash.append(Integer.toHexString(digest[i] & 0x0f));
    }

    return hash.toString();
  }

  private static HashMap<String, String> readStringsFromReader(final BufferedReader reader)
      throws IOException {
    String line;
    final HashMap<String, String> map = new HashMap<>();
    while ((line = reader.readLine()) != null) {
      int equalsPos = -1;
      if ((equalsPos = line.indexOf('=')) != -1) {
        final String key = line.substring(0, equalsPos);
        final String val = unescape(line.substring(equalsPos + 1));
        map.put(key, val);
      }
    }
    return map;
  }

  private static JSONObject readExtraFile(final String filePath) throws IOException, JSONException {
    final byte[] buffer = new byte[4096];
    final FileInputStream inputStream = new FileInputStream(filePath);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    int bytesRead = 0;

    while ((bytesRead = inputStream.read(buffer)) != -1) {
      outputStream.write(buffer, 0, bytesRead);
    }

    final String contents = new String(outputStream.toByteArray(), "UTF-8");
    return new JSONObject(contents);
  }

  private static JSONObject getCrashAnnotations(
      @NonNull final Context context,
      @NonNull final File minidump,
      @NonNull final File extra,
      @NonNull final String appName)
      throws IOException {
    try {
      final JSONObject annotations = readExtraFile(extra.getPath());

      // Compute the minidump hash and generate the stack traces
      try {
        final String hash = computeMinidumpHash(minidump);
        annotations.put(MINIDUMP_SHA256_HASH_KEY, hash);
      } catch (final Exception e) {
        Log.e(LOGTAG, "exception while computing the minidump hash: ", e);
      }

      annotations.put(PRODUCT_NAME_KEY, appName);
      annotations.put(PRODUCT_ID_KEY, PRODUCT_ID);
      annotations.put("Android_Manufacturer", Build.MANUFACTURER);
      annotations.put("Android_Model", Build.MODEL);
      annotations.put("Android_Board", Build.BOARD);
      annotations.put("Android_Brand", Build.BRAND);
      annotations.put("Android_Device", Build.DEVICE);
      annotations.put("Android_Display", Build.DISPLAY);
      annotations.put("Android_Fingerprint", Build.FINGERPRINT);
      annotations.put("Android_CPU_ABI", Build.CPU_ABI);
      annotations.put("Android_PackageName", context.getPackageName());
      try {
        annotations.put("Android_CPU_ABI2", Build.CPU_ABI2);
        annotations.put("Android_Hardware", Build.HARDWARE);
      } catch (final Exception ex) {
        Log.e(LOGTAG, "Exception while sending SDK version 8 keys", ex);
      }
      annotations.put(
          "Android_Version", Build.VERSION.SDK_INT + " (" + Build.VERSION.CODENAME + ")");

      return annotations;
    } catch (final JSONException e) {
      throw new IOException(e);
    }
  }

  private static String generateBoundary() {
    // Generate some random numbers to fill out the boundary
    final int r0 = (int) (Integer.MAX_VALUE * Math.random());
    final int r1 = (int) (Integer.MAX_VALUE * Math.random());
    return String.format("---------------------------%08X%08X", r0, r1);
  }

  private static void sendAnnotations(
      final OutputStream os, final String boundary, final JSONObject extras) throws IOException {
    os.write(
        ("--"
                + boundary
                + "\r\n"
                + "Content-Disposition: form-data; name=\"extra\"; "
                + "filename=\"extra.json\"\r\n"
                + "Content-Type: application/json\r\n"
                + "\r\n")
            .getBytes());
    os.write(extras.toString().getBytes("UTF-8"));
    os.write('\n');
  }

  private static void sendFile(
      final OutputStream os, final String boundary, final String name, final File file)
      throws IOException {
    os.write(
        ("--"
                + boundary
                + "\r\n"
                + "Content-Disposition: form-data; name=\""
                + name
                + "\"; "
                + "filename=\""
                + file.getName()
                + "\"\r\n"
                + "Content-Type: application/octet-stream\r\n"
                + "\r\n")
            .getBytes());
    final FileChannel fc = new FileInputStream(file).getChannel();
    fc.transferTo(0, fc.size(), Channels.newChannel(os));
    fc.close();
  }

  private static String unescape(final String string) {
    return string.replaceAll("\\\\\\\\", "\\").replaceAll("\\\\n", "\n").replaceAll("\\\\t", "\t");
  }
}