|
| 1 | +import json |
| 2 | + |
| 3 | +from sentry_sdk.hub import Hub, _should_send_default_pii |
| 4 | +from sentry_sdk.utils import AnnotatedValue |
| 5 | +from sentry_sdk._compat import text_type |
| 6 | + |
| 7 | + |
| 8 | +class RequestExtractor(object): |
| 9 | + def __init__(self, request): |
| 10 | + self.request = request |
| 11 | + |
| 12 | + def extract_into_event(self, event): |
| 13 | + client = Hub.current.client |
| 14 | + if client is None: |
| 15 | + return |
| 16 | + |
| 17 | + content_length = self.content_length() |
| 18 | + request_info = event.setdefault("request", {}) |
| 19 | + request_info["url"] = self.url() |
| 20 | + |
| 21 | + if _should_send_default_pii(): |
| 22 | + request_info["cookies"] = dict(self.cookies()) |
| 23 | + |
| 24 | + bodies = client.options["request_bodies"] |
| 25 | + if ( |
| 26 | + bodies == "never" |
| 27 | + or (bodies == "small" and content_length > 10 ** 3) |
| 28 | + or (bodies == "medium" and content_length > 10 ** 4) |
| 29 | + ): |
| 30 | + data = AnnotatedValue( |
| 31 | + "", |
| 32 | + {"rem": [["!config", "x", 0, content_length]], "len": content_length}, |
| 33 | + ) |
| 34 | + else: |
| 35 | + parsed_body = self.parsed_body() |
| 36 | + if parsed_body: |
| 37 | + data = parsed_body |
| 38 | + elif self.raw_data(): |
| 39 | + data = AnnotatedValue( |
| 40 | + "", |
| 41 | + {"rem": [["!raw", "x", 0, content_length]], "len": content_length}, |
| 42 | + ) |
| 43 | + else: |
| 44 | + return |
| 45 | + |
| 46 | + request_info["data"] = data |
| 47 | + |
| 48 | + def content_length(self): |
| 49 | + try: |
| 50 | + return int(self.env().get("CONTENT_LENGTH", 0)) |
| 51 | + except ValueError: |
| 52 | + return 0 |
| 53 | + |
| 54 | + def url(self): |
| 55 | + raise NotImplementedError() |
| 56 | + |
| 57 | + def cookies(self): |
| 58 | + raise NotImplementedError() |
| 59 | + |
| 60 | + def raw_data(self): |
| 61 | + raise NotImplementedError() |
| 62 | + |
| 63 | + def form(self): |
| 64 | + raise NotImplementedError() |
| 65 | + |
| 66 | + def parsed_body(self): |
| 67 | + form = self.form() |
| 68 | + files = self.files() |
| 69 | + if form or files: |
| 70 | + data = dict(form.items()) |
| 71 | + for k, v in files.items(): |
| 72 | + size = self.size_of_file(v) |
| 73 | + data[k] = AnnotatedValue( |
| 74 | + "", {"len": size, "rem": [["!raw", "x", 0, size]]} |
| 75 | + ) |
| 76 | + |
| 77 | + return data |
| 78 | + |
| 79 | + return self.json() |
| 80 | + |
| 81 | + def is_json(self): |
| 82 | + mt = (self.env().get("CONTENT_TYPE") or "").split(";", 1)[0] |
| 83 | + return ( |
| 84 | + mt == "application/json" |
| 85 | + or (mt.startswith("application/")) |
| 86 | + and mt.endswith("+json") |
| 87 | + ) |
| 88 | + |
| 89 | + def json(self): |
| 90 | + try: |
| 91 | + if self.is_json(): |
| 92 | + raw_data = self.raw_data() |
| 93 | + if not isinstance(raw_data, text_type): |
| 94 | + raw_data = raw_data.decode("utf-8") |
| 95 | + return json.loads(raw_data) |
| 96 | + except ValueError: |
| 97 | + pass |
| 98 | + |
| 99 | + def files(self): |
| 100 | + raise NotImplementedError() |
| 101 | + |
| 102 | + def size_of_file(self, file): |
| 103 | + raise NotImplementedError() |
| 104 | + |
| 105 | + |
| 106 | +def _filter_headers(headers): |
| 107 | + if _should_send_default_pii(): |
| 108 | + return headers |
| 109 | + |
| 110 | + return { |
| 111 | +
9272
k: v |
| 112 | + for k, v in headers.items() |
| 113 | + if k.lower().replace("_", "-") not in ("set-cookie", "cookie", "authorization") |
| 114 | + } |
0 commit comments