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

No request body for HTTP request is logged when enableCaptureFailedRequests=true #4332

Open
DataGreed opened this issue Sep 13, 2024 · 2 comments

Comments

@DataGreed
Copy link

Platform

iOS

Environment

Develop, Production, TestFlight

Installed

Swift Package Manager

Version

8.35.0

Xcode Version

15.4

Did it work on previous versions?

No response

Steps to Reproduce

  1. Set up SentrySDK with enableCaptureFailedRequests=true
  2. Trigger an HTTP error on POST request or any other request with a body

Expected Result

Error is logged is full captured with request headers and body

Actual Result

No request body is being logged making the debugging impossible

Are you willing to submit a PR?

No response

@brustolin
Copy link
Contributor

Thanks @DataGreed for reaching out.

We will discuss what we can do about it, usually body requests contains PII and we need to make sure we're not sending any user information in the event.

@kahest kahest changed the title Now request body for HTTP request is logged when enableCaptureFailedRequests=true No request body for HTTP request is logged when enableCaptureFailedRequests=true Sep 18, 2024
@kahest
Copy link
Member

kahest commented Sep 18, 2024

As pointed out above, this is currently by design. We're gonna follow up internally if we want to allow this as opt-in guarded by some kind of flag to ensure users know that this opens up potentials PII/sensitive data issues.

In the mean time, you can get some inspiration from the following implementation on how to implement this yourself:

SentryEvent *event = [[SentryEvent alloc] initWithLevel:kSentryLevelError];
SentryThreadInspector *threadInspector = SentrySDK.currentHub.getClient.threadInspector;
NSArray<SentryThread *> *threads = [threadInspector getCurrentThreads];
// sessionTask.error isn't used because it's not about network errors but rather
// requests that are considered failed depending on the HTTP status code
SentryException *sentryException = [[SentryException alloc] initWithValue:message
type:@"HTTPClientError"];
sentryException.mechanism = [[SentryMechanism alloc] initWithType:@"HTTPClientError"];
for (SentryThread *thread in threads) {
if ([thread.current boolValue]) {
SentryStacktrace *sentryStacktrace = [thread stacktrace];
sentryStacktrace.snapshot = @(YES);
sentryException.stacktrace = sentryStacktrace;
break;
}
}
SentryRequest *request = [[SentryRequest alloc] init];
UrlSanitized *url = [[UrlSanitized alloc] initWithURL:[[sessionTask currentRequest] URL]];
request.url = url.sanitizedUrl;
request.method = myRequest.HTTPMethod;
request.fragment = url.fragment;
request.queryString = url.query;
request.bodySize = [NSNumber numberWithLongLong:sessionTask.countOfBytesSent];
if (nil != myRequest.allHTTPHeaderFields) {
NSDictionary<NSString *, NSString *> *headers = myRequest.allHTTPHeaderFields.copy;
request.headers = [HTTPHeaderSanitizer sanitizeHeaders:headers];
}
event.exceptions = @[ sentryException ];
event.request = request;
NSMutableDictionary<NSString *, id> *context = [[NSMutableDictionary alloc] init];
NSMutableDictionary<NSString *, id> *response = [[NSMutableDictionary alloc] init];
[response setValue:responseStatusCode forKey:@"status_code"];
if (nil != myResponse.allHeaderFields) {
NSDictionary<NSString *, NSString *> *headers =
[HTTPHeaderSanitizer sanitizeHeaders:myResponse.allHeaderFields];
[response setValue:headers forKey:@"headers"];
}
if (sessionTask.countOfBytesReceived != 0) {
[response setValue:[NSNumber numberWithLongLong:sessionTask.countOfBytesReceived]
forKey:@"body_size"];
}
context[@"response"] = response;
if (self.isGraphQLOperationTrackingEnabled) {
context[@"graphql_operation_name"] =
[URLSessionTaskHelper getGraphQLOperationNameFrom:sessionTask];
}
event.context = context;
[SentrySDK captureEvent:event];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Status: Backlog
Development

No branches or pull requests

3 participants