Skip to content
Draft
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
16 changes: 2 additions & 14 deletions sentry_sdk/integrations/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
RequestExtractor,
)
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.traces import SOURCE_FOR_STYLE
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
event_from_exception,
has_data_collection_enabled,
package_version,
)

Expand Down Expand Up @@ -169,13 +167,7 @@ def _request_started(app: "Flask", **kwargs: "Any") -> None:

scope = sentry_sdk.get_isolation_scope()

if has_data_collection_enabled(client.options):
if client.options["data_collection"]["user_info"]:
with capture_internal_exceptions():
user_properties = _get_flask_user_properties()
if user_properties:
scope.set_user(user_properties)
elif should_send_default_pii():
if client.options["data_collection"]["user_info"]:
with capture_internal_exceptions():
user_properties = _get_flask_user_properties()
if user_properties:
Expand Down Expand Up @@ -228,11 +220,7 @@ def inner(event: "Event", hint: "dict[str, Any]") -> "Event":
FlaskRequestExtractor(request).extract_into_event(event)

client_options = sentry_sdk.get_client().options
if has_data_collection_enabled(client_options):
if client_options["data_collection"]["user_info"]:
with capture_internal_exceptions():
_add_user_to_event(event)
elif should_send_default_pii():
if client_options["data_collection"]["user_info"]:
with capture_internal_exceptions():
_add_user_to_event(event)

Expand Down
98 changes: 47 additions & 51 deletions tests/integrations/flask/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,17 @@ def test_flask_login_partially_configured(
assert event.get("user", {}).get("id") is None


@pytest.mark.parametrize("send_default_pii", [True, False])
@pytest.mark.parametrize(
"init_kwargs, expect_user",
[
pytest.param({"data_collection": {}}, True, id="data_collection_default"),
*DATA_COLLECTION_USER_INFO_CASES,
],
)
@pytest.mark.parametrize("user_id", [None, "42", 3])
def test_flask_login_configured(
send_default_pii,
init_kwargs,
expect_user,
sentry_init,
app,
user_id,
Expand All @@ -249,8 +256,8 @@ def test_flask_login_configured(
):
sentry_init(
integrations=[flask_sentry.FlaskIntegration()],
send_default_pii=send_default_pii,
traces_sample_rate=1.0,
**init_kwargs,
)

class User:
Expand Down Expand Up @@ -284,7 +291,7 @@ def login():
spans = [i.payload for i in items if i.type == "span"]
segment = next(s for s in spans if s["name"] == "hi")

if send_default_pii and user_id is not None:
if expect_user and user_id is not None:
assert segment["attributes"]["user.id"] == str(user_id)
assert segment["attributes"]["user.email"] == "[email protected]"
assert segment["attributes"]["user.name"] == "testuser"
Expand All @@ -293,12 +300,16 @@ def login():


@pytest.mark.parametrize("max_value_length", [1024, None])
def test_flask_large_json_request(sentry_init, capture_events, app, max_value_length):
def test_flask_large_json_request(
sentry_init, capture_events, app, monkeypatch, max_value_length
):
sentry_init(
integrations=[flask_sentry.FlaskIntegration()],
max_request_body_size="always",
max_value_length=max_value_length,
data_collection={},
)
monkeypatch.setattr(flask_sentry, "flask_login", None)

data = {"foo": {"bar": "a" * (1034)}}

Expand Down Expand Up @@ -355,7 +366,7 @@ def index():
except ZeroDivisionError:
pass

sentry_sdk.get_client().flush()
sentry_sdk.flush()

(first_event, error_event, session) = envelopes
first_event = first_event.get_event()
Expand All @@ -373,8 +384,12 @@ def index():


@pytest.mark.parametrize("data", [{}, []], ids=["empty-dict", "empty-list"])
def test_flask_empty_json_request(sentry_init, capture_events, app, data):
sentry_init(integrations=[flask_sentry.FlaskIntegration()])
def test_flask_empty_json_request(sentry_init, capture_events, app, monkeypatch, data):
sentry_init(
integrations=[flask_sentry.FlaskIntegration()],
data_collection={},
)
monkeypatch.setattr(flask_sentry, "flask_login", None)

@app.route("/", methods=["POST"])
def index():
Expand All @@ -396,13 +411,15 @@ def index():

@pytest.mark.parametrize("max_value_length", [1024, None])
def test_flask_medium_formdata_request(
sentry_init, capture_events, app, max_value_length
sentry_init, capture_events, app, monkeypatch, max_value_length
):
sentry_init(
integrations=[flask_sentry.FlaskIntegration()],
max_request_body_size="always",
max_value_length=max_value_length,
data_collection={},
)
monkeypatch.setattr(flask_sentry, "flask_login", None)

data = {"foo": "a" * (1034)}

Expand Down Expand Up @@ -438,10 +455,15 @@ def index():


@pytest.mark.parametrize("input_char", ["a", b"a"])
def test_flask_too_large_raw_request(sentry_init, input_char, capture_events, app):
def test_flask_too_large_raw_request(
sentry_init, input_char, capture_events, app, monkeypatch
):
sentry_init(
integrations=[flask_sentry.FlaskIntegration()], max_request_body_size="small"
integrations=[flask_sentry.FlaskIntegration()],
max_request_body_size="small",
data_collection={},
)
monkeypatch.setattr(flask_sentry, "flask_login", None)

data = input_char * 2000

Expand Down Expand Up @@ -472,12 +494,16 @@ def index():


@pytest.mark.parametrize("max_value_length", [1024, None])
def test_flask_files_and_form(sentry_init, capture_events, app, max_value_length):
def test_flask_files_and_form(
sentry_init, capture_events, app, monkeypatch, max_value_length
):
sentry_init(
integrations=[flask_sentry.FlaskIntegration()],
max_request_body_size="always",
max_value_length=max_value_length,
data_collection={},
)
monkeypatch.setattr(flask_sentry, "flask_login", None)

data = {
"foo": "a" * (1034),
Expand Down Expand Up @@ -519,11 +545,14 @@ def index():


def test_json_not_truncated_if_max_request_body_size_is_always(
sentry_init, capture_events, app
sentry_init, capture_events, app, monkeypatch
):
sentry_init(
integrations=[flask_sentry.FlaskIntegration()], max_request_body_size="always"
integrations=[flask_sentry.FlaskIntegration()],
max_request_body_size="always",
data_collection={},
)
monkeypatch.setattr(flask_sentry, "flask_login", None)

data = {
"key{}".format(i): "value{}".format(i) for i in range(MAX_DATABAG_BREADTH + 10)
Expand Down Expand Up @@ -949,7 +978,7 @@ def test_request_not_modified_by_reference(sentry_init, capture_events, app):
integrations=[
flask_sentry.FlaskIntegration(),
LoggingIntegration(event_level=logging.ERROR),
]
],
)

@app.route("/", methods=["POST"])
Expand Down Expand Up @@ -1082,16 +1111,6 @@ def test_segment_http_method_custom(
@pytest.mark.parametrize(
"init_kwargs, expected_query_string",
[
pytest.param(
{"send_default_pii": True},
"toy=tennisball&color=red&auth=secret",
id="legacy_send_default_pii_true",
),
pytest.param(
{"send_default_pii": False},
"toy=tennisball&color=red&auth=secret",
id="legacy_send_default_pii_false",
),
pytest.param(
{"data_collection": {}},
"toy=tennisball&color=red&auth=%5BFiltered%5D",
Expand Down Expand Up @@ -1124,7 +1143,7 @@ def test_query_string_data_collection(
sentry_init(integrations=[flask_sentry.FlaskIntegration()], **init_kwargs)
# This test is about query-string filtering, not user data. Disable
# flask_login so the module-level login manager (which has no user_loader)
# does not raise when send_default_pii is on.
# does not raise while user info collection is enabled.
monkeypatch.setattr(flask_sentry, "flask_login", None)
events = capture_events()

Expand All @@ -1142,16 +1161,6 @@ def test_query_string_data_collection(
@pytest.mark.parametrize(
"init_kwargs, expected_query",
[
pytest.param(
{"send_default_pii": True},
"toy=tennisball&color=red&auth=secret",
id="legacy_send_default_pii_true",
),
pytest.param(
{"send_default_pii": False},
None,
id="legacy_send_default_pii_false",
),
pytest.param(
{"data_collection": {}},
"toy=tennisball&color=red&auth=%5BFiltered%5D",
Expand Down Expand Up @@ -1204,27 +1213,14 @@ def test_span_http_query_data_collection(
assert segment["attributes"][SPANDATA.HTTP_QUERY] == expected_query


def test_query_string_empty_legacy_emits_empty_string(
sentry_init, app, capture_events, monkeypatch
):
sentry_init(integrations=[flask_sentry.FlaskIntegration()], send_default_pii=True)
monkeypatch.setattr(flask_sentry, "flask_login", None)
events = capture_events()

client = app.test_client()
client.get("/message")

(event,) = events
assert event["request"]["query_string"] == ""


def test_empty_query_string_is_dropped_with_data_collection(
sentry_init, app, capture_events
sentry_init, app, capture_events, monkeypatch
):
sentry_init(
integrations=[flask_sentry.FlaskIntegration()],
data_collection={},
)
monkeypatch.setattr(flask_sentry, "flask_login", None)
events = capture_events()

client = app.test_client()
Expand Down
Loading