forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1636251: Patch Sentry events to ensure a raw username isn't sent …
…to Sentry r=rstewart To avoid sending identifying information, common absolute paths are patched with placeholder values. For example, devs may place their Firefox repository within their home dir, so absolute paths are doctored to be prefixed with "<topsrcdir"> instead. Additionally, any paths including the user's home directory are patched to instead be a relate path from "~". Differential Revision: https://phabricator.services.mozilla.com/D78962
- Loading branch information
Mitchell Hentges
committed
Jun 11, 2020
1 parent
98acb2c
commit d6fe34f
Showing
5 changed files
with
91 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,18 +6,20 @@ | |
|
||
import os | ||
import re | ||
import sys | ||
from os.path import expanduser | ||
|
||
import mozpack.path as mozpath | ||
import sentry_sdk | ||
from six.moves.configparser import SafeConfigParser, NoOptionError | ||
|
||
from mozboot.util import get_state_dir | ||
|
||
from six import string_types | ||
from six.moves.configparser import SafeConfigParser, NoOptionError | ||
|
||
# https://sentry.prod.mozaws.net/operations/mach/ | ||
_SENTRY_DSN = "https://[email protected]/525" | ||
|
||
|
||
def register_sentry(): | ||
def register_sentry(topsrcdir=None): | ||
cfg_file = os.path.join(get_state_dir(), 'machrc') | ||
config = SafeConfigParser() | ||
|
||
|
@@ -32,10 +34,17 @@ def register_sentry(): | |
if not telemetry_enabled: | ||
return | ||
|
||
sentry_sdk.init(_SENTRY_DSN, before_send=_settle_mach_module_id) | ||
sentry_sdk.init(_SENTRY_DSN, | ||
before_send=lambda event, _: _process_event(event, topsrcdir)) | ||
|
||
|
||
def _process_event(sentry_event, topsrcdir): | ||
for map_fn in (_settle_mach_module_id, _patch_absolute_paths): | ||
sentry_event = map_fn(sentry_event, topsrcdir) | ||
return sentry_event | ||
|
||
|
||
def _settle_mach_module_id(sentry_event, exception): | ||
def _settle_mach_module_id(sentry_event, _): | ||
# Sentry groups issues according to the stack frames and their associated | ||
# "module" properties. However, one of the modules is being reported | ||
# like "mach.commands.26a828ef5164403eaff4305ab4cb0fab" (with a generated id). | ||
|
@@ -54,6 +63,52 @@ def _settle_mach_module_id(sentry_event, exception): | |
return sentry_event | ||
|
||
|
||
def _resolve_topobjdir(): | ||
topobjdir = os.path.join(os.path.dirname(sys.prefix), "..") | ||
return mozpath.normsep(os.path.normpath(topobjdir)) | ||
|
||
|
||
def _patch_absolute_paths(sentry_event, topsrcdir): | ||
# As discussed here (https://bugzilla.mozilla.org/show_bug.cgi?id=1636251#c28), | ||
# we remove usernames from file names with a best-effort basis. The most likely | ||
# place for usernames to manifest in Sentry information is within absolute paths, | ||
# such as: "/home/mitch/dev/firefox/mach" | ||
# We replace the state_dir, obj_dir, src_dir with "<...>" placeholders. | ||
# Note that we also do a blanket find-and-replace of the user's name with "<user>", | ||
# which may have ill effects if the user's name is, by happenstance, a substring | ||
# of some other value within the Sentry event. | ||
def recursive_patch(value, needle, replacement): | ||
if isinstance(value, list): | ||
return [recursive_patch(v, needle, replacement) for v in value] | ||
elif isinstance(value, dict): | ||
for key in list(value.keys()): | ||
next_value = value.pop(key) | ||
key = key.replace(needle, replacement) | ||
value[key] = recursive_patch(next_value, needle, replacement) | ||
return value | ||
elif isinstance(value, string_types): | ||
return value.replace(needle, replacement) | ||
else: | ||
return value | ||
|
||
for (needle, replacement) in ( | ||
(get_state_dir(), "<statedir>"), | ||
(_resolve_topobjdir(), "<topobjdir>"), | ||
(topsrcdir, "<topsrcdir>"), | ||
(expanduser("~"), "~"), | ||
# Sentry converts "vars" to their "representations". When paths are in local | ||
# variables on Windows, "C:\Users\MozillaUser\Desktop" becomes | ||
# "'C:\\Users\\MozillaUser\\Desktop'". To still catch this case, we "repr" | ||
# the home directory and scrub the beginning and end quotes, then | ||
# find-and-replace on that. | ||
(repr(expanduser("~"))[1:-1], "~"), | ||
): | ||
if needle is None: | ||
continue # topsrcdir isn't always defined | ||
sentry_event = recursive_patch(sentry_event, needle, replacement) | ||
return sentry_event | ||
|
||
|
||
def report_exception(exception): | ||
# sentry_sdk won't report the exception if `sentry-sdk.init(...)` hasn't been called | ||
sentry_sdk.capture_exception(exception) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters