Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(django): Don't let RawPostDataException bubble up #3553

Merged
merged 9 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sentry_sdk/integrations/_wsgi_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ def json(self):
if not self.is_json():
return None

raw_data = self.raw_data()
try:
raw_data = self.raw_data()
except (RawPostDataException, ValueError):
# The body might have already been read, in which case this will
# fail
raw_data = None

if raw_data is None:
return None

Expand Down
28 changes: 27 additions & 1 deletion tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import re
import pytest
from functools import partial
from unittest.mock import patch

from werkzeug.test import Client

from django import VERSION as DJANGO_VERSION
from django.contrib.auth.models import User
from django.core.management import execute_from_command_line
from django.db.utils import OperationalError, ProgrammingError, DataError
from django.http.request import RawPostDataException

try:
from django.urls import reverse
Expand All @@ -20,7 +22,11 @@
from sentry_sdk._compat import PY310
from sentry_sdk import capture_message, capture_exception
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations.django import DjangoIntegration, _set_db_data
from sentry_sdk.integrations.django import (
DjangoIntegration,
DjangoRequestExtractor,
_set_db_data,
)
from sentry_sdk.integrations.django.signals_handlers import _get_receiver_name
from sentry_sdk.integrations.executing import ExecutingIntegration
from sentry_sdk.tracing import Span
Expand Down Expand Up @@ -740,6 +746,26 @@ def test_read_request(sentry_init, client, capture_events):
assert "data" not in event["request"]


def test_request_body_already_read(sentry_init, client, capture_events):
sentry_init(integrations=[DjangoIntegration()])

events = capture_events()

class MockExtractor(DjangoRequestExtractor):
def raw_data(self):
raise RawPostDataException

with patch("sentry_sdk.integrations.django.DjangoRequestExtractor", MockExtractor):
client.post(
reverse("post_echo"), data=b'{"hey": 42}', content_type="application/json"
)

(event,) = events

assert event["message"] == "hi"
assert "data" not in event["request"]


def test_template_tracing_meta(sentry_init, client, capture_events):
sentry_init(integrations=[DjangoIntegration()])
events = capture_events()
Expand Down
Loading