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: Skip attachment if stream is empty #1854

Merged
merged 6 commits into from
Aug 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

- Fix logging loop with NLog sentry ([#1824](https://github.com/getsentry/sentry-dotnet/pull/1824))
- Fix logging loop with Serilog sentry ([#1828](https://github.com/getsentry/sentry-dotnet/pull/1828))
- Skip attachment if stream is empty ([#1854](https://github.com/getsentry/sentry-dotnet/pull/1854))

## 3.20.1

Expand Down
13 changes: 12 additions & 1 deletion src/Sentry/Envelopes/Envelope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,18 @@ internal void Serialize(Stream stream, IDiagnosticLogger? logger, ISystemClock c
{
try
{
items.Add(EnvelopeItem.FromAttachment(attachment));
// We pull the stream out here so we can length check
// to avoid adding an invalid attachment
var stream = attachment.Content.GetStream();
if (stream.TryGetLength() != 0)
{
items.Add(EnvelopeItem.FromAttachment(attachment, stream));
}
else
{
logger?.LogWarning("Did not add '{0}' to envelope because the stream was empty.",
mattjohnsonpint marked this conversation as resolved.
Show resolved Hide resolved
attachment.FileName);
}
}
catch (Exception exception)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Sentry/Envelopes/EnvelopeItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ public static EnvelopeItem FromSession(SessionUpdate sessionUpdate)
public static EnvelopeItem FromAttachment(Attachment attachment)
{
var stream = attachment.Content.GetStream();
return FromAttachment(attachment, stream);
}

internal static EnvelopeItem FromAttachment(Attachment attachment, Stream stream)
{
var attachmentType = attachment.Type switch
{
AttachmentType.Minidump => "event.minidump",
Expand Down
25 changes: 23 additions & 2 deletions test/Sentry.Tests/Protocol/Envelopes/EnvelopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,11 @@ public async Task Roundtrip_WithEvent_WithAttachment_Success()
Sdk = new SdkVersion { Name = "SDK-test", Version = "1.0.0" }
};

using var attachmentStream = new MemoryStream(new byte[] {1, 2, 3});

var attachment = new Attachment(
AttachmentType.Default,
new StreamAttachmentContent(Stream.Null),
new StreamAttachmentContent(attachmentStream),
"file.txt",
null);

Expand Down Expand Up @@ -573,9 +575,11 @@ public async Task Roundtrip_WithEvent_WithSession_Success()
Sdk = new SdkVersion { Name = "SDK-test", Version = "1.0.0" }
};

using var attachmentStream = new MemoryStream(new byte[] {1, 2, 3});

var attachment = new Attachment(
AttachmentType.Default,
new StreamAttachmentContent(Stream.Null),
new StreamAttachmentContent(attachmentStream),
"file.txt",
null);

Expand Down Expand Up @@ -699,6 +703,23 @@ public void FromEvent_Header_IncludesSdkInformation()
}).Should().BeTrue();
}

[Fact]
public void FromEvent_EmptyAttachmentStream_DoesNotIncludeAttachment()
{
// Arrange
var attachment = new Attachment(
default,
new StreamAttachmentContent(Stream.Null),
"Screenshot.jpg",
"image/jpg");

// Act
var envelope = Envelope.FromEvent(new SentryEvent(), attachments: new List<Attachment> { attachment });

// Assert
envelope.Items.Should().HaveCount(1);
}

[Fact]
public async Task Serialization_RoundTrip_ReplacesSentAtHeader()
{
Expand Down