Skip to content

Commit f195ed7

Browse files
committed
replace max_request_body_size with include_request_bodies
1 parent 0d46bd4 commit f195ed7

File tree

13 files changed

+30
-133
lines changed

13 files changed

+30
-133
lines changed

sentry_sdk/_types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ def removed_because_raw_data(cls) -> AnnotatedValue:
5757
@classmethod
5858
def removed_because_over_size_limit(cls, value: Any = "") -> AnnotatedValue:
5959
"""
60-
The actual value was removed because the size of the field exceeded the configured maximum size,
61-
for example specified with the max_request_body_size sdk option.
60+
The actual value was removed because the size of the field exceeded the configured maximum size.
6261
"""
6362
return AnnotatedValue(
6463
value=value,

sentry_sdk/client.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,6 @@ def _capture_envelope(envelope: Envelope) -> None:
329329

330330
self.log_batcher = LogBatcher(capture_func=_capture_envelope)
331331

332-
max_request_body_size = ("always", "never", "small", "medium")
333-
if self.options["max_request_body_size"] not in max_request_body_size:
334-
raise ValueError(
335-
"Invalid value for max_request_body_size. Must be one of {}".format(
336-
max_request_body_size
337-
)
338-
)
339-
340332
self.integrations = setup_integrations(
341333
self.options["integrations"],
342334
with_defaults=self.options["default_integrations"],
@@ -547,7 +539,6 @@ def _prepare_event(
547539
if event is not None:
548540
event: Event = serialize( # type: ignore[no-redef]
549541
event,
550-
max_request_body_size=self.options.get("max_request_body_size"),
551542
max_value_length=self.options.get("max_value_length"),
552543
custom_repr=self.options.get("custom_repr"),
553544
)

sentry_sdk/consts.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ def __init__(
812812
http_proxy: Optional[str] = None,
813813
https_proxy: Optional[str] = None,
814814
ignore_errors: Sequence[Union[type, str]] = [], # noqa: B006
815-
max_request_body_size: str = "medium",
815+
inclue_request_bodies: Optional[bool] = True,
816816
socket_options: Optional[List[Tuple[int, int, int | bytes]]] = None,
817817
keep_alive: Optional[bool] = None,
818818
before_send: Optional[EventProcessor] = None,
@@ -1006,18 +1006,8 @@ def __init__(
10061006
10071007
This option can be overridden using `in_app_include`.
10081008
1009-
:param max_request_body_size: This parameter controls whether integrations should capture HTTP request bodies.
1010-
It can be set to one of the following values:
1011-
1012-
- `never`: Request bodies are never sent.
1013-
- `small`: Only small request bodies will be captured. The cutoff for small depends on the SDK (typically
1014-
4KB).
1015-
- `medium`: Medium and small requests will be captured (typically 10KB).
1016-
- `always`: The SDK will always capture the request body as long as Sentry can make sense of it.
1017-
1018-
Please note that the Sentry server [limits HTTP request body size](https://develop.sentry.dev/sdk/
1019-
expected-features/data-handling/#variable-size). The server always enforces its size limit, regardless of
1020-
how you configure this option.
1009+
:param include_request_bodies: This parameter controls whether integrations should capture HTTP request bodies.
1010+
If set to False, request bodies will not be captured. By default, request bodies will be included.
10211011
10221012
:param max_value_length: The number of characters after which the values containing text in the event payload
10231013
will be truncated.

sentry_sdk/integrations/_wsgi_common.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,13 @@
5151
)
5252

5353

54-
def request_body_within_bounds(
54+
def should_include_request_body(
5555
client: Optional[sentry_sdk.client.BaseClient], content_length: int
5656
) -> bool:
5757
if client is None:
5858
return False
5959

60-
bodies = client.options["max_request_body_size"]
61-
return not (
62-
bodies == "never"
63-
or (bodies == "small" and content_length > 10**3)
64-
or (bodies == "medium" and content_length > 10**4)
65-
)
60+
return client.options.get("include_request_bodies") in (True, None)
6661

6762

6863
class RequestExtractor:
@@ -91,7 +86,7 @@ def extract_into_event(self, event: Event) -> None:
9186
if should_send_default_pii():
9287
request_info["cookies"] = dict(self.cookies())
9388

94-
if not request_body_within_bounds(client, content_length):
89+
if not should_include_request_body(client, content_length):
9590
data = AnnotatedValue.removed_because_over_size_limit()
9691
else:
9792
# First read the raw body data

sentry_sdk/integrations/aiohttp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from sentry_sdk.integrations._wsgi_common import (
2424
_filter_headers,
2525
_request_headers_to_span_attributes,
26-
request_body_within_bounds,
26+
should_include_request_body,
2727
)
2828
from sentry_sdk.tracing_utils import should_propagate_trace
2929
from sentry_sdk.utils import (
@@ -366,7 +366,7 @@ def get_aiohttp_request_data(request: Request) -> Union[Optional[str], Annotated
366366

367367
if bytes_body is not None:
368368
# we have body to show
369-
if not request_body_within_bounds(sentry_sdk.get_client(), len(bytes_body)):
369+
if not should_include_request_body(sentry_sdk.get_client(), len(bytes_body)):
370370
return AnnotatedValue.removed_because_over_size_limit()
371371

372372
encoding = request.charset or "utf-8"

sentry_sdk/integrations/ariadne.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from sentry_sdk import get_client, capture_event
66
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
77
from sentry_sdk.integrations.logging import ignore_logger
8-
from sentry_sdk.integrations._wsgi_common import request_body_within_bounds
8+
from sentry_sdk.integrations._wsgi_common import should_include_request_body
99
from sentry_sdk.scope import should_send_default_pii
1010
from sentry_sdk.utils import (
1111
capture_internal_exceptions,
@@ -128,7 +128,7 @@ def inner(event: Event, hint: dict[str, Any]) -> Event:
128128
except (TypeError, ValueError):
129129
return event
130130

131-
if should_send_default_pii() and request_body_within_bounds(
131+
if should_send_default_pii() and should_include_request_body(
132132
get_client(), content_length
133133
):
134134
request_info = event.setdefault("request", {})

sentry_sdk/integrations/dramatiq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import sentry_sdk
55
from sentry_sdk.integrations import Integration
6-
from sentry_sdk.integrations._wsgi_common import request_body_within_bounds
6+
from sentry_sdk.integrations._wsgi_common import should_include_request_body
77
from sentry_sdk.utils import (
88
AnnotatedValue,
99
capture_internal_exceptions,
@@ -160,7 +160,7 @@ def extract_into_event(self, event: Event) -> None:
160160
request_info["type"] = "dramatiq"
161161

162162
data: Optional[Union[AnnotatedValue, Dict[str, Any]]] = None
163-
if not request_body_within_bounds(client, self.content_length()):
163+
if not should_include_request_body(client, self.content_length()):
164164
data = AnnotatedValue.removed_because_over_size_limit()
165165
else:
166166
data = self.message_data

sentry_sdk/integrations/starlette.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from sentry_sdk.integrations._wsgi_common import (
1616
DEFAULT_HTTP_METHODS_TO_CAPTURE,
1717
_is_json_content_type,
18-
request_body_within_bounds,
18+
should_include_request_body,
1919
)
2020
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
2121
from sentry_sdk.scope import should_send_default_pii
@@ -598,7 +598,7 @@ async def extract_request_info(
598598
return request_info
599599

600600
# Add annotation if body is too big
601-
if content_length and not request_body_within_bounds(
601+
if content_length and not should_include_request_body(
602602
client, content_length
603603
):
604604
request_info["data"] = AnnotatedValue.removed_because_over_size_limit()

sentry_sdk/serializer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def serialize(event: Union[Dict[str, Any], Event], **kwargs: Any) -> Dict[str, A
9797
* Calling safe_repr() on objects appropriately to keep them informative and readable in the final payload.
9898
* Annotating the payload with the _meta field whenever trimming happens.
9999
100-
:param max_request_body_size: If set to "always", will never trim request bodies.
101100
:param max_value_length: The max length to strip strings to, defaults to sentry_sdk.consts.DEFAULT_MAX_VALUE_LENGTH
102101
:param is_vars: If we're serializing vars early, we want to repr() things that are JSON-serializable to make their type more apparent. For example, it's useful to see the difference between a unicode-string and a bytestring when viewing a stacktrace.
103102
:param custom_repr: A custom repr function that runs before safe_repr on the object to be serialized. If it returns None or throws internally, we will fallback to safe_repr.

tests/integrations/bottle/test_bottle.py

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def index():
122122

123123

124124
def test_large_json_request(sentry_init, capture_events, app, get_client):
125-
sentry_init(integrations=[BottleIntegration()], max_request_body_size="always")
125+
sentry_init(integrations=[BottleIntegration()])
126126

127127
data = {"foo": {"bar": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10)}}
128128

@@ -180,7 +180,7 @@ def index():
180180

181181

182182
def test_medium_formdata_request(sentry_init, capture_events, app, get_client):
183-
sentry_init(integrations=[BottleIntegration()], max_request_body_size="always")
183+
sentry_init(integrations=[BottleIntegration()])
184184

185185
data = {"foo": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10)}
186186

@@ -210,39 +210,8 @@ def index():
210210
assert len(event["request"]["data"]["foo"]) == DEFAULT_MAX_VALUE_LENGTH
211211

212212

213-
@pytest.mark.parametrize("input_char", ["a", b"a"])
214-
def test_too_large_raw_request(
215-
sentry_init, input_char, capture_events, app, get_client
216-
):
217-
sentry_init(integrations=[BottleIntegration()], max_request_body_size="small")
218-
219-
data = input_char * 2000
220-
221-
@app.route("/", method="POST")
222-
def index():
223-
import bottle
224-
225-
if isinstance(data, bytes):
226-
assert bottle.request.body.read() == data
227-
else:
228-
assert bottle.request.body.read() == data.encode("ascii")
229-
assert not bottle.request.json
230-
capture_message("hi")
231-
return "ok"
232-
233-
events = capture_events()
234-
235-
client = get_client()
236-
response = client.post("/", data=data)
237-
assert response[1] == "200 OK"
238-
239-
(event,) = events
240-
assert event["_meta"]["request"]["data"] == {"": {"rem": [["!config", "x"]]}}
241-
assert not event["request"]["data"]
242-
243-
244213
def test_files_and_form(sentry_init, capture_events, app, get_client):
245-
sentry_init(integrations=[BottleIntegration()], max_request_body_size="always")
214+
sentry_init(integrations=[BottleIntegration()])
246215

247216
data = {
248217
"foo": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10),
@@ -284,10 +253,8 @@ def index():
284253
assert not event["request"]["data"]["file"]
285254

286255

287-
def test_json_not_truncated_if_max_request_body_size_is_always(
288-
sentry_init, capture_events, app, get_client
289-
):
290-
sentry_init(integrations=[BottleIntegration()], max_request_body_size="always")
256+
def test_json_not_truncated(sentry_init, capture_events, app, get_client):
257+
sentry_init(integrations=[BottleIntegration()])
291258

292259
data = {
293260
"key{}".format(i): "value{}".format(i) for i in range(MAX_DATABAG_BREADTH + 10)

0 commit comments

Comments
 (0)