Skip to content

Envelope exceeded size limits on file upload endpoints #2600

Closed

Description

@fifi98

Package

Sentry.AspNetCore

.NET Flavor

.NET Core

.NET Version

7.0.5

OS

Linux

SDK Version

3.38.0

Self-Hosted Sentry Version

No response

Steps to Reproduce

  1. have a file upload endpoint (either form-data or binary)
  2. inside the method that's executed on that endpoint capture the exception using the SentrySdk.CaptureException method
  3. upload a file of size larger than 1MB to that endpoint

Expected Result

The captured exception gets sent to Sentry and the event is visible in the dashboard.

Actual Result

The envelope fails to get sent to sentry. With debug mode enabled this error is shown in the console:

fail: Sentry.ISentryClient[0]
      Sentry rejected the envelope b028e13adc6b4344b34ec423354522ae. Status code: BadRequest. Error detail: envelope exceeded size limits (https://develop.sentry.dev/sdk/envelopes/#size-limits). Error causes: .

Looks like the uploaded file gets embedded in the envelope thus exceeding the envelope size limit.

Not sure if this is the normal behaviour but it would make sense to strip the uploaded file in that case so the envelope does not exceed the size limit. A workaround for this issue would be to set a method in SetBeforeSend that omits the Request.Data in case of file uploads.

Activity

  1. added
    Bug
    on Sep 7, 2023
  2. moved this to Waiting for: Product Owner in GitHub Issues with 👀on Sep 7, 2023
  3. bitsandfoxes commented on Sep 7, 2023

    @bitsandfoxes
    Contributor

    Fair point. If the SDK can already predict that the envelope will be rejected due to size it should not simply attempt to send it.
    I'm not sure where the attached file comes from? Are you capturing the exception manually?

  4. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀on Sep 7, 2023
  5. fifi98 commented on Sep 7, 2023

    @fifi98
    Author

    Correct - I am capturing the exception manually.

    Here is a minimal reproduction snippet of code:

    [HttpPost]
    public async Task<IActionResult> Upload([FromForm] IFormFileCollection file)
    {
        try
        {
            throw new Exception();
        }
        catch (Exception e)
        {
            SentrySdk.CaptureException(e);
        }
    
        return Ok();
    }
    
  6. moved this to Waiting for: Product Owner in GitHub Issues with 👀on Sep 7, 2023
  7. fobos531 commented on Sep 8, 2023

    @fobos531

    Correct - I am capturing the exception manually.

    Here is a minimal reproduction snippet of code:

    [HttpPost]
    public async Task<IActionResult> Upload([FromForm] IFormFileCollection file)
    {
        try
        {
            throw new Exception();
        }
        catch (Exception e)
        {
            SentrySdk.CaptureException(e);
        }
    
        return Ok();
    }
    

    @bitsandfoxes To add to this (I'm on the same team), we are utilizing the overload of the CaptureException method to configure the local scope on the fly and its where we are attaching the file, like so:

      // Create attachment from uploaded file
      var attachment = new Attachment(AttachmentType.Default, new StreamAttachmentContent(uploadedFileStream),
          file.FileName, file.ContentType);
    
      // Add attachment to Sentry
      scope.AddAttachment(attachment);
    

    We can work around the envelope exceeded limits by attaching a custom event processor as well, which strips the file from the request body, but if we do not do this, it might be useful for the SentrySdk to be a little more specific on the why the envelope was rejected, e.g. a multipart/form-data file was detected that exceeds the permitted file size limit.

    Additionally, the attachment flow I outlined above does not seem to work - even though we attached the attachment in what looks to be the correct manner, it is still not visible in Sentry on web. Any pointers?

  8. bitsandfoxes commented on Sep 8, 2023

    @bitsandfoxes
    Contributor

    This starts to sound like multiple issues.
    I'll bring "hitting size limits" up with the team. It seems like a recurring thing that we can definitely improve.

    A workaround for this issue would be to set a method in SetBeforeSend that omits the Request.Data in case of file uploads.

    You could rely on the hint object that gets passed to the SetBeforeSend callback instead of adding your own eventprocessor. The hint holds the attachments as a list and if you remove an attachment there it won't be sent with the event.

    Additionally, the attachment flow I outlined above does not seem to work - even though we attached the attachment in what looks to be the correct manner, it is still not visible in Sentry on web. Any pointers?

    Without having access to any logs it's a bit hard to tell. But you're right. It appears to be the correct manner. The only thing that comes to mind would be that since you're adding the attachment as a stream, during the upload that stream has been closed/disposed, causing the SDK to fail internally which would not be reported.

  9. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀on Sep 8, 2023
  10. 4 remaining items

  11. bitsandfoxes commented on Jan 12, 2024

    @bitsandfoxes
    Contributor

    Can we make that limit on the server queryable maybe (so the client could read it via an API at startup)?

    I don't think users that hit that problem are just slightly above the limit. So the users this should help are currently sending huge envelopes. This doesn't need to be this sophisticated. But filter obviously unreasonable envelopes (i.e. 3x? 5x? current max size) and revisit once we increase the accepted size.

  12. bruno-garcia commented on Jan 16, 2024

    @bruno-garcia
    Member

    I don't think users that hit that problem are just slightly above the limit.

    Any data to back this assumption? I think it's very reasonable to be slightly over

    So the users this should help are currently sending huge envelopes.

    Relay drops it so it's already dealt with

    But filter obviously unreasonable envelopes (i.e. 3x? 5x? current max size) and revisit once we increase the accepted size.

    For that you need to know the size. Often we get a stream all the way to the writing it to the network so we only know the size anyway too late. All memory already allocated (or not allocated all at once, if we're really streaming through). Not sure this is worth the effort.

  13. lightel commented on Feb 13, 2024

    @lightel

    I have faced the same issue and tried to deal with it in SetBeforeSend callback by checking the Content-Type of the request and if it is set to multipart/form-data then set sentryEvent.Request.Data object to null. It didn't help and the request still gets rejected by the server. Can you advise on what I am missing here?

    o.SetBeforeSend((sentryEvent, hint) =>
    {
        sentryEvent.Request.Data = null;
        if (sentryEvent.Request.Headers.TryGetValue("Content-Type", out var contentType) && 
            contentType.Contains("multipart/form-data"))
        {
            sentryEvent.Request.Data = null;
        }
        
        return sentryEvent;
    });
    
  14. moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Feb 13, 2024
  15. bruno-garcia commented on Feb 13, 2024

    @bruno-garcia
    Member

    Best way to debug this is to keep the envelope around to check out what's going on:

    const string persistLargeEnvelopePathEnvVar = "SENTRY_KEEP_LARGE_ENVELOPE_PATH";

    With SENTRY_KEEP_LARGE_ENVELOPE_PATH the SDK will write the file somewhere, so u can check offline (attach here so we can debug for example)

    Note the SDK needs to be in Debug mode (options.Debug = true)

    This can be found on our docs under 'troubleshooting': https://docs.sentry.io/platforms/dotnet/troubleshooting/#the-event-is-dropped-by-sentry-because-its-too-large

  16. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Feb 13, 2024
  17. jamescrosswell commented on Jul 1, 2025

    @jamescrosswell
    Collaborator

    From what I can tell, there's nothing for us to do in the SDK here so I'll close this for now.

    If you believe there is a problem with the SDK or that the behaviour could be improved, feel free to reopen to continue the discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugGood First Issue

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions