Skip to content

Commit e4deb10

Browse files
osamashtayatcodex
andauthored
fix(android): drop native events when BeforeSend conversion fails (#5868)
* fix(android): drop native events when BeforeSend conversion fails * fix(android): separate segfault suppression from BeforeSend filtering Co-Authored-By: Codex <noreply@openai.com> * test(android): cover suppression with a disabled BeforeSend filter --------- Co-authored-by: Codex <noreply@openai.com>
1 parent 837511d commit e4deb10

6 files changed

Lines changed: 206 additions & 119 deletions

File tree

‎src/Sentry/Platforms/Android/Callbacks/BeforeSendCallback.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public BeforeSendCallback(
3535
catch (Exception exception)
3636
{
3737
_options.LogError(exception, "Before Send Error");
38-
return e;
38+
return null;
3939
}
4040
}
4141
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Sentry.Android.Callbacks;
2+
3+
internal class SegfaultSuppressionCallback(JavaSdk.SentryOptions.IBeforeSendCallback? next)
4+
: JavaObject, JavaSdk.SentryOptions.IBeforeSendCallback
5+
{
6+
public JavaSdk.SentryEvent? Execute(JavaSdk.SentryEvent e, JavaSdk.Hint h) =>
7+
e.Exceptions?.FirstOrDefault() is { Type: "SIGSEGV", Value: "Segfault" }
8+
? null
9+
: next is null ? e : next.Execute(e, h);
10+
}

‎src/Sentry/Platforms/Android/SentrySdk.cs‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,7 @@ private static void InitSentryAndroidSdk(SentryOptions options)
129129
}
130130
}
131131

132-
if (options.Android.SuppressSegfaults || (options.Native.EnableBeforeSend && options.BeforeSendInternal != null))
133-
{
134-
o.BeforeSend = new BeforeSendCallback(BeforeSendWrapper(options), options, o);
135-
}
132+
o.BeforeSend = CreateBeforeSendCallback(options, o);
136133

137134
// These options are from SentryAndroidOptions
138135
o.AttachScreenshot = options.Native.AttachScreenshot;
@@ -226,25 +223,23 @@ private static void InitSentryAndroidSdk(SentryOptions options)
226223
// TODO: Pause/Resume
227224
}
228225

229-
internal static Func<SentryEvent, SentryHint, SentryEvent?> BeforeSendWrapper(SentryOptions options)
226+
internal static JavaSdk.SentryOptions.IBeforeSendCallback? CreateBeforeSendCallback(
227+
SentryOptions options, JavaSdk.SentryOptions javaOptions)
230228
{
231-
return (evt, hint) =>
229+
JavaSdk.SentryOptions.IBeforeSendCallback? callback = null;
230+
if (options.Native.EnableBeforeSend && options.BeforeSendInternal is { } beforeSend)
232231
{
233-
// Suppress SIGSEGV errors.
234-
// See: https://github.com/getsentry/sentry-dotnet/pull/3903
235-
if (options.Android.SuppressSegfaults
236-
&& evt.SentryExceptions?.FirstOrDefault() is { Type: "SIGSEGV", Value: "Segfault" })
237-
{
238-
options.LogDebug("Suppressing SIGSEGV (this will be thrown as a managed exception instead)");
239-
return null;
240-
}
232+
callback = new BeforeSendCallback(BeforeSendWrapper(options, beforeSend), options, javaOptions);
233+
}
241234

242-
// Call the user defined BeforeSend callback, if it's defined - otherwise return the event as-is
243-
if (!options.Native.EnableBeforeSend || options.BeforeSendInternal is not { } beforeSend)
244-
{
245-
return evt;
246-
}
235+
return options.Android.SuppressSegfaults ? new SegfaultSuppressionCallback(callback) : callback;
236+
}
247237

238+
private static Func<SentryEvent, SentryHint, SentryEvent?> BeforeSendWrapper(
239+
SentryOptions options, Func<SentryEvent, SentryHint, SentryEvent?> beforeSend)
240+
{
241+
return (evt, hint) =>
242+
{
248243
try
249244
{
250245
return beforeSend(evt, hint);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Sentry.Android.Callbacks;
2+
3+
namespace Sentry.Tests.Platforms.Android;
4+
5+
public class BeforeSendCallbackTests
6+
{
7+
[Fact]
8+
public void Execute_NativeEventCannotBeRead_DropsEventBeforeCallback()
9+
{
10+
var called = false;
11+
var logger = new InMemoryDiagnosticLogger();
12+
var options = new SentryOptions { Debug = true, DiagnosticLogger = logger };
13+
using var javaOptions = new JavaSdk.SentryOptions();
14+
using var callback = new BeforeSendCallback((evt, _) =>
15+
{
16+
called = true;
17+
return evt;
18+
}, options, javaOptions);
19+
using var native = new JavaSdk.SentryEvent
20+
{
21+
// Java accepts a 32-character ID; the managed SDK requires hexadecimal digits.
22+
EventId = new JavaSdk.Protocol.SentryId(new string('z', 32))
23+
};
24+
using var hint = new JavaSdk.Hint();
25+
26+
var result = callback.Execute(native, hint);
27+
28+
result.Should().BeNull();
29+
called.Should().BeFalse();
30+
logger.Entries.Should().ContainSingle(entry => entry.Exception is FormatException);
31+
}
32+
33+
[Fact]
34+
public void Execute_FilteredEventCannotBeWritten_DropsEventAfterCallback()
35+
{
36+
var exception = new InvalidOperationException("Cannot read fingerprint");
37+
var fingerprint = Substitute.For<IReadOnlyList<string>>();
38+
fingerprint.Count.Throws(exception);
39+
var logger = new InMemoryDiagnosticLogger();
40+
var options = new SentryOptions { Debug = true, DiagnosticLogger = logger };
41+
using var javaOptions = new JavaSdk.SentryOptions();
42+
using var callback = new BeforeSendCallback((evt, _) =>
43+
{
44+
evt.ServerName = null;
45+
evt.Fingerprint = fingerprint;
46+
return evt;
47+
}, options, javaOptions);
48+
using var native = new JavaSdk.SentryEvent { ServerName = "private-host" };
49+
using var hint = new JavaSdk.Hint();
50+
51+
var result = callback.Execute(native, hint);
52+
53+
result.Should().BeNull();
54+
logger.Entries.Should().ContainSingle(entry => entry.Exception == exception);
55+
}
56+
57+
[Fact]
58+
public void Execute_CallbackRedactsServerName_ReturnsFilteredEvent()
59+
{
60+
using var javaOptions = new JavaSdk.SentryOptions();
61+
using var callback = new BeforeSendCallback((evt, _) =>
62+
{
63+
evt.ServerName = null;
64+
return evt;
65+
}, new SentryOptions(), javaOptions);
66+
using var native = new JavaSdk.SentryEvent { ServerName = "private-host" };
67+
using var hint = new JavaSdk.Hint();
68+
69+
using var result = callback.Execute(native, hint);
70+
71+
result.Should().NotBeNull();
72+
result!.EventId!.ToString().Should().Be(native.EventId!.ToString());
73+
result.ServerName.Should().BeNull();
74+
}
75+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Sentry.Android.Callbacks;
2+
3+
namespace Sentry.Tests.Platforms.Android;
4+
5+
public class SegfaultSuppressionCallbackTests
6+
{
7+
[Theory]
8+
[InlineData("SIGSEGV", "Segfault", true)]
9+
[InlineData("SIGSEGV", "Other", false)]
10+
[InlineData("Other", "Segfault", false)]
11+
public void Execute_WithoutFilter_OnlyDropsMatchingSegfault(string type, string value, bool drop)
12+
{
13+
using var callback = new SegfaultSuppressionCallback(null);
14+
using var exception = new JavaSdk.Protocol.SentryException { Type = type, Value = value };
15+
using var evt = new JavaSdk.SentryEvent
16+
{
17+
Exceptions = new[] { exception },
18+
EventId = new JavaSdk.Protocol.SentryId(new string('z', 32))
19+
};
20+
using var hint = new JavaSdk.Hint();
21+
22+
var result = callback.Execute(evt, hint);
23+
24+
if (drop)
25+
{
26+
result.Should().BeNull();
27+
}
28+
else
29+
{
30+
result.Should().BeSameAs(evt);
31+
}
32+
}
33+
34+
[Theory]
35+
[InlineData(true, false)]
36+
[InlineData(false, false)]
37+
[InlineData(false, true)]
38+
public void Execute_WithFilter_SuppressesFirstAndPreservesFilterResult(bool segfault, bool filterDrops)
39+
{
40+
var options = new SentryOptions();
41+
options.Android.SuppressSegfaults = true;
42+
options.Native.EnableBeforeSend = true;
43+
var calls = 0;
44+
options.SetBeforeSend((evt, _) =>
45+
{
46+
calls++;
47+
evt.ServerName = "filtered";
48+
return filterDrops ? null : evt;
49+
});
50+
using var javaOptions = new JavaSdk.SentryOptions();
51+
using var callback = SentrySdk.CreateBeforeSendCallback(options, javaOptions);
52+
using var evt = new JavaSdk.SentryEvent();
53+
if (segfault)
54+
{
55+
evt.Exceptions = new[] { new JavaSdk.Protocol.SentryException { Type = "SIGSEGV", Value = "Segfault" } };
56+
}
57+
using var hint = new JavaSdk.Hint();
58+
59+
using var result = callback!.Execute(evt, hint);
60+
61+
calls.Should().Be(segfault ? 0 : 1);
62+
if (segfault || filterDrops)
63+
{
64+
result.Should().BeNull();
65+
}
66+
else
67+
{
68+
result!.ServerName.Should().Be("filtered");
69+
}
70+
}
71+
}

‎test/Sentry.Tests/Platforms/Android/SentrySdkTests.cs‎

Lines changed: 35 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -3,130 +3,66 @@ namespace Sentry.Tests.Platforms.Android;
33

44
public class SentrySdkTests
55
{
6-
[Fact]
7-
public void BeforeSendWrapper_Suppress_SIGSEGV_ReturnsNull()
8-
{
9-
// Arrange
10-
var options = new SentryOptions();
11-
options.Android.SuppressSegfaults = true;
12-
var evt = new SentryEvent
13-
{
14-
SentryExceptions = new[]
15-
{
16-
new SentryException
17-
{
18-
Type = "SIGSEGV",
19-
Value = "Segfault"
20-
}
21-
}
22-
};
23-
var hint = new SentryHint();
24-
25-
// Act
26-
var result = SentrySdk.BeforeSendWrapper(options).Invoke(evt, hint);
27-
28-
// Assert
29-
result.Should().BeNull();
30-
}
31-
32-
[Fact]
33-
public void BeforeSendWrapper_DontSupress_SIGSEGV_ReturnsEvent()
6+
[Theory]
7+
[InlineData(false, true)]
8+
[InlineData(true, false)]
9+
public void CreateBeforeSendCallback_NoEnabledFilter_ReturnsNull(bool enabled, bool hasFilter)
3410
{
35-
// Arrange
3611
var options = new SentryOptions();
3712
options.Android.SuppressSegfaults = false;
38-
var evt = new SentryEvent
13+
options.Native.EnableBeforeSend = enabled;
14+
if (hasFilter)
3915
{
40-
SentryExceptions = new[]
41-
{
42-
new SentryException
43-
{
44-
Type = "SIGSEGV",
45-
Value = "Segfault"
46-
}
47-
}
48-
};
49-
var hint = new SentryHint();
50-
51-
// Act
52-
var result = SentrySdk.BeforeSendWrapper(options).Invoke(evt, hint);
53-
54-
// Assert
55-
result.Should().Be(evt);
56-
}
57-
58-
[Fact]
59-
public void BeforeSendWrapper_BeforeSendCallbackDefined_CallsBeforeSend()
60-
{
61-
// Arrange
62-
var beforeSend = Substitute.For<Func<SentryEvent, SentryHint, SentryEvent>>();
63-
beforeSend.Invoke(Arg.Any<SentryEvent>(), Arg.Any<SentryHint>()).Returns(callInfo => callInfo.Arg<SentryEvent>());
16+
options.SetBeforeSend((evt, _) => evt);
17+
}
18+
using var javaOptions = new JavaSdk.SentryOptions();
6419

65-
var options = new SentryOptions();
66-
options.Native.EnableBeforeSend = true;
67-
options.SetBeforeSend(beforeSend);
68-
var evt = new SentryEvent();
69-
var hint = new SentryHint();
70-
71-
// Act
72-
var result = SentrySdk.BeforeSendWrapper(options).Invoke(evt, hint);
73-
74-
// Assert
75-
beforeSend.Received(1).Invoke(Arg.Any<SentryEvent>(), Arg.Any<SentryHint>());
76-
result.Should().Be(evt);
77-
}
78-
79-
[Fact]
80-
public void BeforeSendWrapper_NoBeforeSendCallback_ReturnsEvent()
81-
{
82-
// Arrange
83-
var options = new SentryOptions();
84-
options.Native.EnableBeforeSend = true;
85-
var evt = new SentryEvent();
86-
var hint = new SentryHint();
87-
88-
// Act
89-
var result = SentrySdk.BeforeSendWrapper(options).Invoke(evt, hint);
90-
91-
// Assert
92-
result.Should().Be(evt);
20+
SentrySdk.CreateBeforeSendCallback(options, javaOptions).Should().BeNull();
9321
}
9422

9523
[Fact]
96-
public void BeforeSendWrapper_NativeBeforeSendDisabled_ReturnsEvent()
24+
public void CreateBeforeSendCallback_SuppressionWithDisabledFilter_ReturnsUnconvertedEventWithoutCallingFilter()
9725
{
98-
// Arrange
99-
var beforeSend = Substitute.For<Func<SentryEvent, SentryHint, SentryEvent>>();
100-
beforeSend.Invoke(Arg.Any<SentryEvent>(), Arg.Any<SentryHint>()).Returns(_ => null);
101-
10226
var options = new SentryOptions();
103-
options.SetBeforeSend(beforeSend);
27+
options.Android.SuppressSegfaults = true;
10428
options.Native.EnableBeforeSend = false;
105-
var evt = new SentryEvent();
106-
var hint = new SentryHint();
29+
var called = false;
30+
options.SetBeforeSend((evt, _) =>
31+
{
32+
called = true;
33+
return evt;
34+
});
35+
using var javaOptions = new JavaSdk.SentryOptions();
36+
using var callback = SentrySdk.CreateBeforeSendCallback(options, javaOptions);
37+
using var evt = new JavaSdk.SentryEvent
38+
{
39+
EventId = new JavaSdk.Protocol.SentryId(new string('z', 32))
40+
};
41+
using var hint = new JavaSdk.Hint();
10742

108-
// Act
109-
var result = SentrySdk.BeforeSendWrapper(options).Invoke(evt, hint);
43+
var result = callback!.Execute(evt, hint);
11044

111-
// Assert
112-
beforeSend.DidNotReceive().Invoke(Arg.Any<SentryEvent>(), Arg.Any<SentryHint>());
113-
result.Should().Be(evt);
45+
called.Should().BeFalse();
46+
result.Should().BeSameAs(evt);
11447
}
11548

11649
[Fact]
117-
public void BeforeSendWrapper_BeforeSendThrows_ReturnsNullAndLogsError()
50+
public void CreateBeforeSendCallback_BeforeSendThrows_ReturnsNullAndLogsError()
11851
{
11952
// Arrange
12053
var exception = new InvalidOperationException("callback failed");
12154
var logger = new InMemoryDiagnosticLogger();
12255
var options = new SentryOptions { Debug = true, DiagnosticLogger = logger };
12356
options.Native.EnableBeforeSend = true;
12457
options.SetBeforeSend((SentryEvent _, SentryHint _) => throw exception);
125-
var evt = new SentryEvent();
126-
var hint = new SentryHint();
58+
options.Android.SuppressSegfaults = false;
59+
using var javaOptions = new JavaSdk.SentryOptions();
60+
using var callback = SentrySdk.CreateBeforeSendCallback(options, javaOptions);
61+
using var evt = new JavaSdk.SentryEvent();
62+
using var hint = new JavaSdk.Hint();
12763

12864
// Act
129-
var result = SentrySdk.BeforeSendWrapper(options).Invoke(evt, hint);
65+
var result = callback!.Execute(evt, hint);
13066

13167
// Assert
13268
result.Should().BeNull();

0 commit comments

Comments
 (0)