serializers.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. import uuid
  2. from typing import Dict, List, Tuple, Union
  3. from urllib.parse import urlparse
  4. from anonymizeip import anonymize_ip
  5. from django.db import transaction
  6. from django.db.models.expressions import OuterRef, RawSQL
  7. from django.db.utils import IntegrityError
  8. from ipware import get_client_ip
  9. from rest_framework import serializers
  10. from rest_framework.exceptions import PermissionDenied
  11. from environments.models import Environment
  12. from glitchtip.serializers import FlexibleDateTimeField
  13. from issues.models import EventType, Issue
  14. from issues.serializers import BaseBreadcrumbsSerializer
  15. from issues.tasks import update_search_index_issue
  16. from releases.models import Release
  17. from sentry.eventtypes.base import DefaultEvent
  18. from sentry.eventtypes.error import ErrorEvent
  19. from .event_context_processors import EVENT_CONTEXT_PROCESSORS
  20. from .event_processors import EVENT_PROCESSORS
  21. from .event_tag_processors import TAG_PROCESSORS
  22. from .fields import (
  23. ForgivingDisallowRegexField,
  24. ForgivingHStoreField,
  25. GenericField,
  26. QueryStringField,
  27. )
  28. from .models import Event, LogLevel
  29. def replace(data: Union[str, dict, list], match: str, repl: str):
  30. """A recursive replace function"""
  31. if isinstance(data, dict):
  32. return {k: replace(v, match, repl) for k, v in data.items()}
  33. elif isinstance(data, list):
  34. return [replace(i, match, repl) for i in data]
  35. elif isinstance(data, str):
  36. return data.replace(match, repl)
  37. return data
  38. def sanitize_bad_postgres_chars(data: Union[str, dict, list]):
  39. """
  40. Remove values which are not supported by the postgres string data types
  41. """
  42. known_bads = ["\x00"]
  43. for known_bad in known_bads:
  44. data = data.replace(known_bad, " ")
  45. return data
  46. def sanitize_bad_postgres_json(data: Union[str, dict, list]):
  47. """
  48. Remove values which are not supported by the postgres JSONB data type
  49. """
  50. known_bads = ["\u0000"]
  51. for known_bad in known_bads:
  52. data = replace(data, known_bad, " ")
  53. return data
  54. class RequestSerializer(serializers.Serializer):
  55. env = serializers.DictField(
  56. child=serializers.CharField(allow_blank=True, allow_null=True), required=False
  57. )
  58. # Dict values can be both str and List[str]
  59. headers = serializers.DictField(required=False)
  60. url = serializers.CharField(required=False, allow_blank=True)
  61. method = serializers.CharField(required=False, allow_blank=True)
  62. query_string = QueryStringField(required=False, allow_null=True)
  63. class BreadcrumbsSerializer(BaseBreadcrumbsSerializer):
  64. timestamp = GenericField(required=False)
  65. def validate_level(self, value):
  66. if value == "log":
  67. return "info"
  68. return value
  69. class BaseSerializer(serializers.Serializer):
  70. def process_user(self, project, data):
  71. """Fetch user data from SDK event and request"""
  72. user = data.get("user", {})
  73. if self.context and self.context.get("request"):
  74. client_ip, is_routable = get_client_ip(self.context["request"])
  75. if user or is_routable:
  76. if is_routable:
  77. if project.should_scrub_ip_addresses:
  78. client_ip = anonymize_ip(client_ip)
  79. user["ip_address"] = client_ip
  80. return user
  81. class SentrySDKEventSerializer(BaseSerializer):
  82. """Represents events coming from a OSS sentry SDK client"""
  83. breadcrumbs = serializers.JSONField(required=False)
  84. tags = ForgivingHStoreField(required=False)
  85. event_id = serializers.UUIDField(required=False, default=uuid.uuid4)
  86. extra = serializers.JSONField(required=False)
  87. request = RequestSerializer(required=False)
  88. server_name = serializers.CharField(required=False)
  89. sdk = serializers.JSONField(required=False)
  90. platform = serializers.CharField(required=False)
  91. release = serializers.CharField(required=False, allow_null=True, allow_blank=True)
  92. environment = ForgivingDisallowRegexField(
  93. required=False, allow_null=True, disallow_regex=r"^[^\n\r\f\/]*$"
  94. )
  95. _meta = serializers.JSONField(required=False)
  96. def get_environment(self, name: str, project):
  97. environment, _ = Environment.objects.get_or_create(
  98. name=name[: Environment._meta.get_field("name").max_length],
  99. organization=project.organization,
  100. )
  101. environment.projects.add(project)
  102. return environment
  103. def get_release(self, version: str, project):
  104. release, _ = Release.objects.get_or_create(
  105. version=version, organization=project.organization
  106. )
  107. release.projects.add(project)
  108. return release
  109. class FormattedMessageSerializer(serializers.Serializer):
  110. formatted = serializers.CharField(
  111. required=False
  112. ) # Documented as required, but some Sentry SDKs don't send it
  113. message = serializers.CharField(required=False)
  114. params = serializers.JSONField(required=False)
  115. def validate(self, attrs):
  116. data = super().validate(attrs)
  117. if not data.get("formatted") and data.get("params"):
  118. params = data["params"]
  119. if isinstance(params, list):
  120. data["formatted"] = data["message"] % tuple(params)
  121. elif isinstance(params, dict):
  122. data["formatted"] = data["message"].format(**params)
  123. return data
  124. # OSS Sentry only keeps unformatted "message" when it creates a formatted message
  125. return {key: data[key] for key in data if key != "message"}
  126. class MessageField(serializers.CharField):
  127. def to_internal_value(self, data):
  128. if isinstance(data, dict):
  129. serializer = FormattedMessageSerializer(data=data)
  130. serializer.is_valid(raise_exception=True)
  131. return serializer.validated_data
  132. return super().to_internal_value(data)
  133. class LogEntrySerializer(serializers.Serializer):
  134. formatted = serializers.CharField(required=False)
  135. message = serializers.CharField(required=False)
  136. params = serializers.JSONField(required=False)
  137. def validate(self, attrs):
  138. data = super().validate(attrs)
  139. if not data.get("formatted") and data.get("params"):
  140. params = data["params"]
  141. if isinstance(params, list):
  142. data["formatted"] = data["message"] % tuple(data["params"])
  143. elif isinstance(params, dict):
  144. data["formatted"] = data["message"].format(**params)
  145. return data
  146. class StoreDefaultSerializer(SentrySDKEventSerializer):
  147. """
  148. Default serializer. Used as both a base class and for default error types
  149. """
  150. type = EventType.DEFAULT
  151. contexts = serializers.JSONField(required=False)
  152. level = serializers.CharField(required=False)
  153. logentry = LogEntrySerializer(required=False)
  154. message = MessageField(required=False, allow_blank=True, allow_null=True)
  155. timestamp = FlexibleDateTimeField(required=False)
  156. transaction = serializers.CharField(
  157. required=False, allow_null=True, allow_blank=True
  158. )
  159. user = serializers.JSONField(required=False)
  160. modules = serializers.JSONField(required=False)
  161. def validate_breadcrumbs(self, value):
  162. """
  163. Normalize breadcrumbs, which may come in as dict or list
  164. """
  165. if isinstance(value, list):
  166. value = {"values": value}
  167. if value.get("values") == []:
  168. return None
  169. serializer = BreadcrumbsSerializer(data=value.get("values"), many=True)
  170. if serializer.is_valid():
  171. return {"values": serializer.validated_data}
  172. return value
  173. def get_eventtype(self):
  174. """Get event type class from self.type"""
  175. if self.type is EventType.DEFAULT:
  176. return DefaultEvent()
  177. if self.type is EventType.ERROR:
  178. return ErrorEvent()
  179. def modify_exception(self, exception):
  180. """OSS Sentry does this, I have no idea why"""
  181. if exception:
  182. for value in exception.get("values", []):
  183. value.pop("module", None)
  184. if value.get("stacktrace") and value["stacktrace"].get("frames"):
  185. frames = value["stacktrace"]["frames"]
  186. # If in_app is always true, make it false ¯\_(ツ)_/¯
  187. if all(x.get("in_app") for x in frames):
  188. for frame in frames:
  189. frame["in_app"] = False
  190. return exception
  191. def generate_tags(self, data: Dict, tags: List[Tuple[str, str]] = None):
  192. """
  193. Determine tag relational data
  194. Optionally pass tags array for existing known tags to generate
  195. """
  196. if tags is None:
  197. tags = []
  198. for Processor in TAG_PROCESSORS:
  199. processor = Processor()
  200. value = processor.get_tag_values(data)
  201. if value:
  202. tags.append((processor.tag, value))
  203. if data.get("tags"):
  204. tags += [(k, v) for k, v in data["tags"].items()]
  205. return tags
  206. def annotate_contexts(self, event):
  207. """
  208. SDK events may contain contexts. This function adds additional contexts data
  209. """
  210. contexts = event.get("contexts")
  211. for Processor in EVENT_CONTEXT_PROCESSORS:
  212. processor = Processor()
  213. if contexts is None or not contexts.get(processor.name):
  214. processor_contexts = processor.get_context(event)
  215. if processor_contexts:
  216. if contexts is None:
  217. contexts = {}
  218. contexts[processor.name] = processor_contexts
  219. return contexts
  220. def get_message(self, data):
  221. """Prefer message over logentry"""
  222. if "message" in data:
  223. if isinstance(data["message"], dict):
  224. return data["message"].get("formatted") or data["message"].get(
  225. "message", ""
  226. )
  227. return data["message"]
  228. return data.get("logentry", {}).get("message", "")
  229. def get_logentry(self, data):
  230. if "logentry" in data:
  231. return data.get("logentry")
  232. elif "message" in data:
  233. message = data["message"]
  234. if isinstance(message, dict):
  235. return message
  236. return {"formatted": message}
  237. def is_url(self, filename: str) -> bool:
  238. return filename.startswith(("file:", "http:", "https:", "applewebdata:"))
  239. def normalize_stacktrace(self, stacktrace):
  240. """
  241. Port of semaphore store/normalize/stacktrace.rs
  242. """
  243. if not stacktrace:
  244. return
  245. for frame in stacktrace.get("frames", []):
  246. if not frame.get("abs_path") and frame.get("filename"):
  247. frame["abs_path"] = frame["filename"]
  248. if frame.get("filename") and self.is_url(frame["filename"]):
  249. frame["filename"] = urlparse(frame["filename"]).path
  250. def create(self, validated_data):
  251. data = validated_data
  252. project = self.context.get("project")
  253. eventtype = self.get_eventtype()
  254. metadata = eventtype.get_metadata(data)
  255. exception = data.get("exception")
  256. if (
  257. data.get("stacktrace")
  258. and exception
  259. and len(exception.get("values", 0)) > 0
  260. and not exception["values"][0].get("stacktrace")
  261. ):
  262. # stacktrace is deprecated, but supported at this time
  263. # Assume it's for the first exception value
  264. exception["values"][0]["stacktrace"] = data.get("stacktrace")
  265. exception = self.modify_exception(exception)
  266. if isinstance(exception, dict):
  267. for value in exception.get("values", []):
  268. self.normalize_stacktrace(value.get("stacktrace"))
  269. if not project.release_id and data.get("release"):
  270. release = self.get_release(data.get("release"), project)
  271. project.release_id = release.id
  272. for Processor in EVENT_PROCESSORS:
  273. Processor(project, project.release_id, data).run()
  274. title = eventtype.get_title(metadata)
  275. culprit = eventtype.get_location(data)
  276. request = data.get("request")
  277. breadcrumbs = data.get("breadcrumbs")
  278. level = None
  279. if data.get("level"):
  280. level = LogLevel.from_string(data["level"])
  281. if request:
  282. headers = request.get("headers")
  283. if headers:
  284. request["inferred_content_type"] = headers.get("Content-Type")
  285. sorted_headers = sorted([pair for pair in headers.items()])
  286. for idx, header in enumerate(sorted_headers):
  287. if isinstance(header[1], list):
  288. sorted_headers[idx] = (header[0], header[1][0])
  289. request["headers"] = sorted_headers
  290. contexts = self.annotate_contexts(data)
  291. data["contexts"] = contexts
  292. with transaction.atomic():
  293. if not project.first_event:
  294. project.first_event = data.get("timestamp")
  295. project.save(update_fields=["first_event"])
  296. defaults = {
  297. "metadata": sanitize_bad_postgres_json(metadata),
  298. }
  299. if level:
  300. defaults["level"] = level
  301. if not project.environment_id and data.get("environment"):
  302. environment = self.get_environment(data["environment"], project)
  303. project.environment_id = environment.id
  304. tags = []
  305. if project.environment_id:
  306. tags.append(("environment", data.get("environment")))
  307. if project.release_id:
  308. tags.append(("release", data.get("release")))
  309. tags = self.generate_tags(data, tags)
  310. defaults["tags"] = {tag[0]: [tag[1]] for tag in tags}
  311. issue, issue_created = Issue.objects.get_or_create(
  312. title=sanitize_bad_postgres_chars(title),
  313. culprit=sanitize_bad_postgres_chars(culprit),
  314. project_id=project.id,
  315. type=self.type,
  316. defaults=defaults,
  317. )
  318. json_data = {
  319. "breadcrumbs": breadcrumbs,
  320. "contexts": contexts,
  321. "culprit": culprit,
  322. "exception": exception,
  323. "logentry": self.get_logentry(data),
  324. "metadata": metadata,
  325. "message": self.get_message(data),
  326. "modules": data.get("modules"),
  327. "platform": data.get("platform", "other"),
  328. "request": request,
  329. "sdk": data.get("sdk"),
  330. "title": title,
  331. "type": self.type.label,
  332. }
  333. if project.environment_id:
  334. json_data["environment"] = data.get("environment")
  335. if data.get("logentry"):
  336. json_data["logentry"] = data.get("logentry")
  337. extra = data.get("extra")
  338. if extra:
  339. json_data["extra"] = extra
  340. user = self.process_user(project, data)
  341. if user:
  342. json_data["user"] = user
  343. errors = None
  344. handled_errors = self.context.get("handled_errors")
  345. if handled_errors:
  346. errors = []
  347. for field_name, field_errors in handled_errors.items():
  348. for error in field_errors:
  349. errors.append(
  350. {
  351. "reason": str(error),
  352. "type": error.code,
  353. "name": field_name,
  354. "value": error.value,
  355. }
  356. )
  357. params = {
  358. "event_id": data["event_id"],
  359. "issue": issue,
  360. "tags": {tag[0]: tag[1] for tag in tags},
  361. "errors": errors,
  362. "timestamp": data.get("timestamp"),
  363. "data": sanitize_bad_postgres_json(json_data),
  364. "release_id": project.release_id,
  365. }
  366. if level:
  367. params["level"] = level
  368. try:
  369. event = Event.objects.create(**params)
  370. except IntegrityError as err:
  371. # This except is more efficient than a query for exists().
  372. if err.args and "event_id" in err.args[0]:
  373. raise PermissionDenied(
  374. "An event with the same ID already exists (%s)"
  375. % params["event_id"]
  376. ) from err
  377. raise err
  378. if issue_created: # Do it right now, so that new issues look correct
  379. event_data = Event.objects.filter(issue_id=OuterRef("id")).values("data")[
  380. :1
  381. ]
  382. event_vector = event_data.annotate(
  383. search_vector=RawSQL("select generate_issue_tsvector(data)", [])
  384. ).values("search_vector")
  385. Issue.objects.filter(pk=issue.pk).update(
  386. search_vector=event_vector, last_seen=event.created
  387. )
  388. else: # Updates can be slower and debounced
  389. issue.check_for_status_update()
  390. # Expire after 1 hour - in case of major backup
  391. update_search_index_issue(args=[issue.pk])
  392. return event
  393. class StoreErrorSerializer(StoreDefaultSerializer):
  394. """Primary difference is the presense of exception attribute"""
  395. type = EventType.ERROR
  396. exception = serializers.JSONField(required=False)
  397. stacktrace = serializers.JSONField(
  398. required=False, help_text="Deprecated but supported at this time"
  399. )
  400. class StoreCSPReportSerializer(BaseSerializer):
  401. """
  402. CSP Report Serializer
  403. Very different format from others Store serializers.
  404. Does not extend base class due to differences.
  405. """
  406. type = EventType.CSP
  407. def __init__(self, *args, **kwargs):
  408. super().__init__(*args, **kwargs)
  409. # This is done to support the hyphen
  410. self.fields.update({"csp-report": serializers.JSONField()})
  411. def create(self, validated_data):
  412. project = self.context.get("project")
  413. csp = validated_data["csp-report"]
  414. title = self.get_title(csp)
  415. culprit = self.get_culprit(csp)
  416. uri = self.get_uri(csp)
  417. directive = self.get_effective_directive(csp)
  418. metadata = {
  419. "message": title,
  420. "uri": uri,
  421. "directive": directive,
  422. }
  423. issue, _ = Issue.objects.get_or_create(
  424. title=title,
  425. culprit=culprit,
  426. project_id=project.id,
  427. type=EventType.CSP,
  428. defaults={"metadata": metadata},
  429. )
  430. # Convert - to _
  431. normalized_csp = dict((k.replace("-", "_"), v) for k, v in csp.items())
  432. if "effective_directive" not in normalized_csp:
  433. normalized_csp["effective_directive"] = directive
  434. json_data = {
  435. "culprit": culprit,
  436. "csp": normalized_csp,
  437. "title": title,
  438. "metadata": metadata,
  439. "message": title,
  440. "type": EventType.CSP.label,
  441. }
  442. user = self.process_user(project, validated_data)
  443. if user:
  444. json_data["user"] = user
  445. params = {
  446. "issue": issue,
  447. "data": json_data,
  448. }
  449. return Event.objects.create(**params)
  450. def get_effective_directive(self, data):
  451. """
  452. Some browers return effective-directive and others don't.
  453. Infer missing ones from violated directive
  454. """
  455. if "effective-directive" in data:
  456. return data["effective-directive"]
  457. first_violation = data["violated-directive"].split()[0]
  458. return first_violation
  459. def get_uri(self, data):
  460. url = data["blocked-uri"]
  461. return urlparse(url).netloc
  462. def get_title(self, data):
  463. effective_directive = self.get_effective_directive(data)
  464. humanized_directive = effective_directive.replace("-src", "")
  465. uri = self.get_uri(data)
  466. return f"Blocked '{humanized_directive}' from '{uri}'"
  467. def get_culprit(self, data):
  468. # "style-src cdn.example.com"
  469. return data.get("violated-directive")
  470. class EnvelopeHeaderSerializer(serializers.Serializer):
  471. event_id = serializers.UUIDField(required=False)
  472. sent_at = FlexibleDateTimeField(required=False)