-
Notifications
You must be signed in to change notification settings - Fork 664
fix(aws-lambda): Attach user info when streaming spans #7429
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
Changes from all commits
c25ea4f
ffd1721
abad60f
24dccdc
99dda7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,27 @@ | |
| MILLIS_TO_SECONDS = 1000.0 | ||
|
|
||
|
|
||
| def _get_user_from_event(aws_event: "dict[str, Any]") -> "dict[str, Any]": | ||
| if not isinstance(aws_event, dict): | ||
| return {} | ||
|
|
||
| identity = aws_event.get("requestContext", {}).get("identity") | ||
| if identity is None: | ||
| return {} | ||
|
|
||
| user_info: "dict[str, Any]" = {} | ||
|
|
||
| user_arn = identity.get("userArn") | ||
| if user_arn is not None: | ||
| user_info["id"] = user_arn | ||
|
|
||
| ip = identity.get("sourceIp") | ||
| if ip is not None: | ||
| user_info["ip_address"] = ip | ||
|
|
||
| return user_info | ||
|
Check warning on line 66 in sentry_sdk/integrations/aws_lambda.py
|
||
|
|
||
|
|
||
| def _wrap_init_error(init_error: "F") -> "F": | ||
| @ensure_integration_enabled(AwsLambdaIntegration, init_error) | ||
| def sentry_init_error(*args: "Any", **kwargs: "Any") -> "Any": | ||
|
|
@@ -181,6 +202,17 @@ | |
| elif should_send_default_pii(): | ||
| additional_attributes["url.query"] = urlencode(qs) | ||
|
|
||
| if not scope._user: | ||
| if has_data_collection_enabled(client.options): | ||
| if client.options["data_collection"]["user_info"]: | ||
| user_info = _get_user_from_event(request_data) | ||
| if user_info: | ||
| scope.set_user(user_info) | ||
| elif should_send_default_pii(): | ||
| user_info = _get_user_from_event(request_data) | ||
| if user_info: | ||
| scope.set_user(user_info) | ||
|
cursor[bot] marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. User extraction can crash handlerMedium Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit 99dda7e. Configure here. |
||
|
|
||
| sampling_context = { | ||
| "aws_event": aws_event, | ||
| "aws_context": aws_context, | ||
|
|
@@ -440,38 +472,22 @@ | |
| client_options = sentry_sdk.get_client().options | ||
| if has_data_collection_enabled(client_options): | ||
| if client_options["data_collection"]["user_info"]: | ||
| user_info = sentry_event.setdefault("user", {}) | ||
|
|
||
| identity = aws_event.get("requestContext", {}).get("identity") | ||
| if identity is None: | ||
| identity = {} | ||
|
|
||
| id = identity.get("userArn") | ||
| if id is not None: | ||
| user_info.setdefault("id", id) | ||
|
|
||
| ip = identity.get("sourceIp") | ||
| if ip is not None: | ||
| user_info.setdefault("ip_address", ip) | ||
| extracted_user = _get_user_from_event(aws_event) | ||
| if extracted_user: | ||
| user_info = sentry_event.setdefault("user", {}) | ||
| for key, value in extracted_user.items(): | ||
| user_info.setdefault(key, value) | ||
|
|
||
| if "incoming_request" in client_options["data_collection"]["http_bodies"]: | ||
| if "body" in aws_event: | ||
| request["data"] = aws_event.get("body", "") | ||
|
|
||
| elif should_send_default_pii(): | ||
| user_info = sentry_event.setdefault("user", {}) | ||
|
|
||
| identity = aws_event.get("requestContext", {}).get("identity") | ||
| if identity is None: | ||
| identity = {} | ||
|
|
||
| id = identity.get("userArn") | ||
| if id is not None: | ||
| user_info.setdefault("id", id) | ||
|
|
||
| ip = identity.get("sourceIp") | ||
| if ip is not None: | ||
| user_info.setdefault("ip_address", ip) | ||
| extracted_user = _get_user_from_event(aws_event) | ||
| if extracted_user: | ||
| user_info = sentry_event.setdefault("user", {}) | ||
| for key, value in extracted_user.items(): | ||
| user_info.setdefault(key, value) | ||
|
|
||
| if "body" in aws_event: | ||
| request["data"] = aws_event.get("body", "") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import os | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration | ||
|
|
||
| sentry_sdk.init( | ||
| dsn=os.environ.get("SENTRY_DSN"), | ||
| traces_sample_rate=1.0, | ||
| integrations=[AwsLambdaIntegration()], | ||
| trace_lifecycle="stream", | ||
| _experiments={ | ||
| "data_collection": { | ||
| "user_info": False, | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def handler(event, context): | ||
| return {"event": event} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import os | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration | ||
|
|
||
| sentry_sdk.init( | ||
| dsn=os.environ.get("SENTRY_DSN"), | ||
| traces_sample_rate=1.0, | ||
| integrations=[AwsLambdaIntegration()], | ||
| trace_lifecycle="stream", | ||
| _experiments={ | ||
| "data_collection": { | ||
| "user_info": True, | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def handler(event, context): | ||
| return {"event": event} |


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_get_user_from_event crashes when requestContext is None or non-dict
Guard
requestContextandidentitywithisinstance(..., dict)before calling.get();dict.get("requestContext", {})still returnsNonewhen the key is present, and this helper now runs on the request path outsidecapture_internal_exceptions.Evidence
_get_user_from_event()doesaws_event.get("requestContext", {}).get("identity"), which raisesAttributeErrorifrequestContextis explicitlyNoneor otherwise non-dict.identity is Nonecheck, it callsidentity.get(...)with no dict check, so a non-dict identity also raises.scope.set_user(...)outside the nearbycapture_internal_exceptions()block, so the exception can fail the Lambda invocation.headersfor this reason, but user extraction does not.Identified by Warden · code-review · QYJ-NUN
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix attempt detected (commit 99dda7e)
The change adds an aws_event type guard but still calls .get() on requestContext and identity without checking that they are dictionaries, so the reported crashes persist.
The original issue appears unresolved. Please review and try again.
Evaluated by Warden