|
| 1 | +import json |
| 2 | +import base64 |
| 3 | + |
| 4 | +from sentry_sdk.stripping import AnnotatedValue |
| 5 | + |
| 6 | + |
1 | 7 | def get_environ(environ): |
2 | 8 | """ |
3 | 9 | Returns our whitelisted environment variables. |
4 | 10 | """ |
5 | 11 | for key in ("REMOTE_ADDR", "SERVER_NAME", "SERVER_PORT"): |
6 | 12 | if key in environ: |
7 | 13 | yield key, environ[key] |
| 14 | + |
| 15 | + |
| 16 | +# `get_headers` comes from `werkzeug.datastructures.EnvironHeaders` |
| 17 | +# |
| 18 | +# We need this function because Django does not give us a "pure" http header |
| 19 | +# dict. So we might as well use it for all WSGI integrations. |
| 20 | +def get_headers(environ): |
| 21 | + """ |
| 22 | + Returns only proper HTTP headers. |
| 23 | +
|
| 24 | + """ |
| 25 | + for key, value in environ.items(): |
| 26 | + key = str(key) |
| 27 | + if key.startswith("HTTP_") and key not in ( |
| 28 | + "HTTP_CONTENT_TYPE", |
| 29 | + "HTTP_CONTENT_LENGTH", |
| 30 | + ): |
| 31 | + yield key[5:].replace("_", "-").title(), value |
| 32 | + elif key in ("CONTENT_TYPE", "CONTENT_LENGTH"): |
| 33 | + yield key.replace("_", "-").title(), value |
| 34 | + |
| 35 | + |
| 36 | +class RequestExtractor(object): |
| 37 | + def __init__(self, request): |
| 38 | + self.request = request |
| 39 | + |
| 40 | + def extract_into_scope(self, scope): |
| 41 | + # if the code below fails halfway through we at least have some data |
| 42 | + scope.request = request_info = {} |
| 43 | + |
| 44 | + request_info["url"] = self.url |
| 45 | + request_info["query_string"] = self.query_string |
| 46 | + request_info["method"] = self.method |
| 47 | + request_info["headers"] = dict(self.headers) |
| 48 | + request_info["env"] = dict(get_environ(self.env)) |
| 49 | + request_info["cookies"] = dict(self.cookies) |
| 50 | + |
| 51 | + if self.form or self.files: |
| 52 | + data = dict(self.form.items()) |
| 53 | + for k, v in self.files.items(): |
| 54 | + data[k] = AnnotatedValue( |
| 55 | + "", |
| 56 | + {"len": self.size_of_file(v), "rem": [["!filecontent", "x", 0, 0]]}, |
| 57 | + ) |
| 58 | + |
| 59 | + if self.files or self.form_is_multipart: |
| 60 | + ct = "multipart" |
| 61 | + else: |
| 62 | + ct = "urlencoded" |
| 63 | + repr = "structured" |
| 64 | + elif self.json is not None: |
| 65 | + data = self.json |
| 66 | + ct = "json" |
| 67 | + repr = "structured" |
| 68 | + elif self.raw_data: |
| 69 | + data = self.raw_data |
| 70 | + |
| 71 | + try: |
| 72 | + if isinstance(data, bytes): |
| 73 | + data = data.decode("utf-8") |
| 74 | + ct = "plain" |
| 75 | + repr = "other" |
| 76 | + except UnicodeDecodeError: |
| 77 | + ct = "bytes" |
| 78 | + repr = "base64" |
| 79 | + data = base64.b64encode(data).decode("ascii") |
| 80 | + else: |
| 81 | + return |
| 82 | + |
| 83 | + request_info["data"] = data |
| 84 | + request_info["data_info"] = {"ct": ct, "repr": repr} |
| 85 | + |
| 86 | + @property |
| 87 | + def url(self): |
| 88 | + raise NotImplementedError() |
| 89 | + |
| 90 | + @property |
| 91 | + def query_string(self): |
| 92 | + return self.env.get("QUERY_STRING") |
| 93 | + |
| 94 | + @property |
| 95 | + def method(self): |
| 96 | + return self.env.get("REQUEST_METHOD") |
| 97 | + |
| 98 | + @property |
| 99 | + def headers(self): |
| 100 | + return get_headers(self.env) |
| 101 | + |
| 102 | + @property |
| 103 | + def env(self): |
| 104 | + raise NotImplementedError() |
| 105 | + |
| 106 | + @property |
| 107 | + def cookies(self): |
| 108 | + raise NotImplementedError() |
| 109 | + |
| 110 | + @property |
| 111 | + def raw_data(self): |
| 112 | + raise NotImplementedError() |
| 113 | + |
| 114 | + @property |
| 115 | + def form(self): |
| 116 | + raise NotImplementedError() |
| 117 | + |
| 118 | + @property |
| 119 | + def form_is_multipart(self): |
| 120 | + return self.env.get("CONTENT_TYPE").startswith("multipart/form-data") |
| 121 | + |
| 122 | + @property |
| 123 | + def is_json(self): |
| 124 | + mt = (self.env.get("CONTENT_TYPE") or "").split(";", 1)[0] |
| 125 | + return ( |
| 126 | + mt == "application/json" |
| 127 | + or (mt.startswith("application/")) |
| 128 | + and mt.endswith("+json") |
| 129 | + ) |
| 130 | + |
| 131 | + @property |
| 132 | + def json(self): |
| 133 | + try: |
| 134 | + if self.is_json: |
| 135 | + return json.loads(self.raw_data.decode("utf-8")) |
| 136 | + except ValueError: |
| 137 | + pass |
| 138 | + |
| 139 | + @property |
| 140 | + def files(self): |
| 141 | + raise NotImplementedError() |
| 142 | + |
| 143 | + def size_of_file(self, file): |
| 144 | + raise NotImplementedError() |
0 commit comments