Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ public SystemWebRequestEventProcessor(IRequestPayloadExtractor payloadExtractor,
}
}

@event.ServerName = Environment.MachineName;
if (_options.AttachServerName)
{
@event.ServerName ??= Environment.MachineName;
}

if (dataCollection.IncomingRequestBody &&
PayloadExtractor.ExtractPayload(new SystemWebHttpRequest(context.Request)) is { } body)
Expand Down
9 changes: 3 additions & 6 deletions src/Sentry.AspNetCore/AspNetCoreEventProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using Microsoft.Extensions.Options;
using Sentry.Extensibility;
using Sentry.Internal;

namespace Sentry.AspNetCore;

internal class AspNetCoreEventProcessor : ISentryEventProcessor, ISdkProcessor
internal class AspNetCoreEventProcessor(IOptions<SentryAspNetCoreOptions> options) : ISentryEventProcessor, ISdkProcessor
{
public SentryEvent Process(SentryEvent @event)
{
// Not PII as this is running on a server
if (@event.ServerName == null)
if (@event.ServerName is null && options.Value.AttachServerName)
{
// ServerName from the environment machine name is set by the Sentry base package.
// That is guarded by the SendDefaultPii since the SDK can be used in desktop apps.
// For ASP.NET Core apps, we always set the machine name if not explicitly set by the user.
@event.ServerName = Environment.MachineName;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Sentry.AspNetCore/ScopeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ private static void SetEnv(Scope scope, HttpContext context, SentryAspNetCoreOpt
scope.Request.Env["REMOTE_ADDR"] = ipAddress;
}

scope.Request.Env["SERVER_NAME"] = Environment.MachineName;
if (options.AttachServerName)
{
scope.Request.Env["SERVER_NAME"] = Environment.MachineName;
}
scope.Request.Env["SERVER_PORT"] = context.Connection.LocalPort.ToString();

if (context.Response.Headers.TryGetValue("Server", out var server))
Expand Down
2 changes: 2 additions & 0 deletions src/Sentry/BindableSentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal partial class BindableSentryOptions
public bool? SendDefaultPii { get; set; }
public bool? IsEnvironmentUser { get; set; }
public string? ServerName { get; set; }
public bool? AttachServerName { get; set; }
public bool? AttachStacktrace { get; set; }
public int? MaxBreadcrumbs { get; set; }
public float? SampleRate { get; set; }
Expand Down Expand Up @@ -69,6 +70,7 @@ public void ApplyTo(SentryOptions options)
#pragma warning restore CS0618
options.IsEnvironmentUser = IsEnvironmentUser ?? options.IsEnvironmentUser;
options.ServerName = ServerName ?? options.ServerName;
options.AttachServerName = AttachServerName ?? options.AttachServerName;
options.AttachStacktrace = AttachStacktrace ?? options.AttachStacktrace;
options.MaxBreadcrumbs = MaxBreadcrumbs ?? options.MaxBreadcrumbs;
options.SampleRate = SampleRate ?? options.SampleRate;
Expand Down
7 changes: 7 additions & 0 deletions src/Sentry/Internal/DataCollectionResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ internal sealed class DataCollectionResolver(SentryOptions options)
/// </summary>
public bool RedactUrlCredentialsWithLegacyPiiGate => Configured is null && !SendDefaultPii;

/// <summary>
/// Temporary bridge for call sites that only sent the machine name with <c>SendDefaultPii</c>; remove with it, see
/// https://github.com/getsentry/sentry-dotnet/issues/5439
/// </summary>
public bool AttachServerNameWithLegacyPiiGate =>
options.AttachServerName && (Configured is not null || SendDefaultPii);

public bool IncomingRequestBody => CollectsBody(HttpBodyTypes.IncomingRequest, legacyFallback: true);

public bool OutgoingRequestBody => CollectsBody(HttpBodyTypes.OutgoingRequest, SendDefaultPii);
Expand Down
4 changes: 1 addition & 3 deletions src/Sentry/Internal/MainSentryEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ public SentryEvent Process(SentryEvent @event)
{
@event.ServerName = _options.ServerName;
}
#pragma warning disable CS0618
else if (_options.SendDefaultPii)
else if (_options.DataCollectionResolver.AttachServerNameWithLegacyPiiGate)
{
@event.ServerName = Environment.MachineName;
}
#pragma warning restore CS0618
}

@event.Level ??= SentryLevel.Error;
Expand Down
4 changes: 1 addition & 3 deletions src/Sentry/SentryLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,10 @@ internal void SetDefaultAttributes(SentryOptions options, Scope? scope, SdkVersi
{
SetAttribute("server.address", options.ServerName!);
}
#pragma warning disable CS0618
else if (options.SendDefaultPii)
else if (options.DataCollectionResolver.AttachServerNameWithLegacyPiiGate)
{
SetAttribute("server.address", Environment.MachineName);
}
#pragma warning restore CS0618

// User Attributes
if (scope?.User is { } user)
Expand Down
31 changes: 21 additions & 10 deletions src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ internal HttpRequestMessage CreateHttpRequest(HttpContent content)
/// dataCollection.GraphQL.Document = false;
/// dataCollection.GraphQL.Variables = false;
/// });
/// options.AttachServerName = false;
/// </code>
Comment thread
jamescrosswell marked this conversation as resolved.
Resolved
/// Leave out the last line in ASP.NET and ASP.NET Core apps, whose events include the machine name with
/// <c>SendDefaultPii = false</c> too.
/// This still differs from <c>SendDefaultPii = false</c> in four ways. Failed HTTP request events include
/// headers: <see cref="DataCollectionOptions.HttpHeaders"/> sets one behavior for those and for the headers of
/// incoming requests, and <c>SendDefaultPii = false</c> collected only the latter. They also include the URL
Expand All @@ -360,13 +363,9 @@ internal HttpRequestMessage CreateHttpRequest(HttpContent content)
/// it records.
/// </para>
/// <para>
/// Two behaviors still follow this option, whether or not <see cref="ConfigureDataCollection()"/> has been called:
/// <list type="bullet">
/// <item>The machine name is sent as <see cref="ServerName"/> only when this option is <c>true</c>. Set
/// <see cref="ServerName"/> explicitly to keep sending it without this option.</item>
/// <item><c>Sentry.OpenTelemetry</c>'s <c>AddSentry</c> sets the user from the HTTP request only when this option
/// is <c>true</c>.</item>
/// </list>
/// <c>Sentry.OpenTelemetry</c>'s <c>AddSentry</c> still follows this option after
/// <see cref="ConfigureDataCollection()"/> has been called: it sets the user from the HTTP request only when this
/// option is <c>true</c>.
/// </para>
/// </remarks>
[Obsolete(ObsoleteSendDefaultPii)]
Expand Down Expand Up @@ -437,12 +436,24 @@ public void ConfigureDataCollection(Action<DataCollectionOptions> configure)
/// Gets or sets the name of the server running the application.
/// </summary>
/// <remarks>
/// When <see cref="SendDefaultPii"/> is set to <c>true</c>, <see cref="System.Environment.MachineName"/> is
/// automatically set as ServerName. This property can serve as an override.
/// This is relevant only to server applications.
/// Takes precedence over the machine name sent by <see cref="AttachServerName"/>.
/// </remarks>
public string? ServerName { get; set; }

/// <summary>
/// Whether to send <see cref="System.Environment.MachineName"/> as the <see cref="ServerName"/> when that isn't set.
/// </summary>
/// <remarks>
/// The default is <c>false</c> on Android, iOS and Mac Catalyst, where the machine name is the name of the user's
/// device, and <c>true</c> elsewhere. Until <see cref="ConfigureDataCollection()"/> is called, events and logs only
/// include it when <see cref="SendDefaultPii"/> is also <c>true</c>, except for events in ASP.NET and ASP.NET Core.
/// </remarks>
#if __MOBILE__

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads-up for #6066: the macOS SDK doesn't define __MOBILE__, so MAUI on macOS (AppKit) would default to true while Mac Catalyst on the same Mac defaults to false. Worth settling in whichever of the two lands second.

public bool AttachServerName { get; set; }
#else
public bool AttachServerName { get; set; } = true;
#endif

/// <summary>
/// Whether to send the stack trace of a event captured without an exception.
/// As of version 3.22.0, the default is <c>true</c>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,37 @@ public void Process_DefaultFixture_ReadsMockBody()
Assert.Same(_fixture.MockBody, expected.Request.Data);
}

[Fact]
public void Process_DefaultOptions_ServerNameSetToMachineName()
{
var evt = new SentryEvent();

_fixture.GetSut().Process(evt);

Assert.Equal(Environment.MachineName, evt.ServerName);
}

[Fact]
public void Process_ServerNameOnEvent_NotOverwritten()
{
var evt = new SentryEvent { ServerName = "expected server name" };

_fixture.GetSut().Process(evt);

Assert.Equal("expected server name", evt.ServerName);
}

[Fact]
public void Process_AttachServerNameFalse_ServerNameNotSet()
{
_fixture.SentryOptions.AttachServerName = false;
var evt = new SentryEvent();

_fixture.GetSut().Process(evt);

Assert.Null(evt.ServerName);
}

[Fact]
public void Process_QueryStringWithSensitiveParameter_FiltersValueAndPreservesEncoding()
{
Expand Down
16 changes: 15 additions & 1 deletion test/Sentry.AspNetCore.Tests/AspNetCoreEventProcessorTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using Microsoft.Extensions.Options;

namespace Sentry.AspNetCore.Tests;

public class AspNetCoreEventProcessorTests
{
private readonly SentryAspNetCoreOptions _options = new();
private readonly AspNetCoreEventProcessor _sut;

public AspNetCoreEventProcessorTests()
{
_sut = new AspNetCoreEventProcessor();
_sut = new AspNetCoreEventProcessor(Options.Create(_options));
}

[Fact]
Expand All @@ -30,4 +33,15 @@ public void Process_ServerName_SetToEnvironmentMachineName()

Assert.Equal(Environment.MachineName, target.ServerName);
}

[Fact]
public void Process_AttachServerNameFalse_ServerNameNotSet()
{
_options.AttachServerName = false;
var target = new SentryEvent();

_ = _sut.Process(target);

Assert.Null(target.ServerName);
}
}
10 changes: 10 additions & 0 deletions test/Sentry.AspNetCore.Tests/ScopeExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,16 @@ public void Populate_MachineName_SetToEnv()
Assert.Equal(Environment.MachineName, _sut.Request.Env["SERVER_NAME"]);
}

[Fact]
public void Populate_AttachServerNameFalse_MachineNameNotSetToEnv()
{
SentryAspNetCoreOptions.AttachServerName = false;

_sut.Populate(_httpContext, SentryAspNetCoreOptions);

Assert.False(_sut.Request.Env.ContainsKey("SERVER_NAME"));
}

[Fact]
public void Populate_NoAspNetCoreOptions_NoPayloadExtractors_NoBodyRead()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ namespace Sentry
public SentryOptions() { }
[System.CLSCompliant(false)]
public System.Func<string, System.Reflection.PortableExecutable.PEReader?>? AssemblyReader { get; set; }
public bool AttachServerName { get; set; }
public bool AttachStacktrace { get; set; }
public bool AutoSessionTracking { get; set; }
public System.TimeSpan AutoSessionTrackingInterval { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ namespace Sentry
public SentryOptions() { }
[System.CLSCompliant(false)]
public System.Func<string, System.Reflection.PortableExecutable.PEReader?>? AssemblyReader { get; set; }
public bool AttachServerName { get; set; }
public bool AttachStacktrace { get; set; }
public bool AutoSessionTracking { get; set; }
public System.TimeSpan AutoSessionTrackingInterval { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ namespace Sentry
public SentryOptions() { }
[System.CLSCompliant(false)]
public System.Func<string, System.Reflection.PortableExecutable.PEReader?>? AssemblyReader { get; set; }
public bool AttachServerName { get; set; }
public bool AttachStacktrace { get; set; }
public bool AutoSessionTracking { get; set; }
public System.TimeSpan AutoSessionTrackingInterval { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ namespace Sentry
public SentryOptions() { }
[System.CLSCompliant(false)]
public System.Func<string, System.Reflection.PortableExecutable.PEReader?>? AssemblyReader { get; set; }
public bool AttachServerName { get; set; }
public bool AttachStacktrace { get; set; }
public bool AutoSessionTracking { get; set; }
public System.TimeSpan AutoSessionTrackingInterval { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ namespace Sentry
public SentryOptions() { }
[System.CLSCompliant(false)]
public System.Func<string, System.Reflection.PortableExecutable.PEReader?>? AssemblyReader { get; set; }
public bool AttachServerName { get; set; }
public bool AttachStacktrace { get; set; }
public bool AutoSessionTracking { get; set; }
public System.TimeSpan AutoSessionTrackingInterval { get; set; }
Expand Down
23 changes: 23 additions & 0 deletions test/Sentry.Tests/BindableSentryOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ public void ApplyTo_SetsOptionsFromConfig()
AssertContainsExpectedPropertyValues(actual);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void ApplyTo_AttachServerNameConfigured_OverridesDefault(bool configured)
{
// Arrange
var actual = new SentryOptions();
var bindable = new BindableSentryOptions();
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
["AttachServerName"] = configured.ToString()
})
.Build();

// Act
config.Bind(bindable);
bindable.ApplyTo(actual);

// Assert
actual.AttachServerName.Should().Be(configured);
}

[Fact]
public void ApplyTo_TraceIgnoreStatusCodesNotConfigured_KeepsDefault()
{
Expand Down
18 changes: 18 additions & 0 deletions test/Sentry.Tests/Internals/DataCollectionResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ public void RedactUrlCredentialsWithLegacyPiiGate_OnlyWithoutSendDefaultPiiOrDat
Resolver(options).RedactUrlCredentialsWithLegacyPiiGate.Should().Be(expected);
}

[Theory]
[InlineData(true, false, false, false)]
[InlineData(true, true, false, true)]
[InlineData(true, false, true, true)]
[InlineData(false, true, false, false)]
[InlineData(false, false, true, false)]
public void AttachServerNameWithLegacyPiiGate_RequiresSendDefaultPiiOrDataCollection(
bool attachServerName, bool sendDefaultPii, bool configureDataCollection, bool expected)
{
var options = new SentryOptions { AttachServerName = attachServerName, SendDefaultPii = sendDefaultPii };
if (configureDataCollection)
{
options.ConfigureDataCollection();
}

Resolver(options).AttachServerNameWithLegacyPiiGate.Should().Be(expected);
}

[Fact]
public void HeadersWithLegacyPiiGate_ConfiguredDataCollectionDefaults_AreDenyList()
{
Expand Down
41 changes: 41 additions & 0 deletions test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,53 @@ public void Process_SendDefaultPiiTrue_ServerNameSet()
var sut = _fixture.GetSut();
var evt = new SentryEvent();

_fixture.SentryOptions.AttachServerName = true;
_fixture.SentryOptions.SendDefaultPii = true;
_ = sut.Process(evt);

Assert.Equal(Environment.MachineName, evt.ServerName);
}

[Fact]
public void Process_DataCollectionConfigured_ServerNameSet()
{
_fixture.SentryOptions.AttachServerName = true;
_fixture.SentryOptions.ConfigureDataCollection();
var sut = _fixture.GetSut();
var evt = new SentryEvent();

_ = sut.Process(evt);

Assert.Equal(Environment.MachineName, evt.ServerName);
}

[Fact]
public void Process_AttachServerNameFalse_NoServerNameSet()
{
_fixture.SentryOptions.AttachServerName = false;
_fixture.SentryOptions.SendDefaultPii = true;
_fixture.SentryOptions.ConfigureDataCollection();
var sut = _fixture.GetSut();
var evt = new SentryEvent();

_ = sut.Process(evt);

Assert.Null(evt.ServerName);
}

[Fact]
public void Process_AttachServerNameFalseAndNameOnOption_ServerNameSetToOptionsValue()
{
_fixture.SentryOptions.AttachServerName = false;
_fixture.SentryOptions.ServerName = "expected server name";
var sut = _fixture.GetSut();
var evt = new SentryEvent();

_ = sut.Process(evt);

Assert.Equal("expected server name", evt.ServerName);
}

[Fact]
public void Process_SendDefaultPiiTrueNameOnOptionAndOnEvent_ServerNameNotOverwritten()
{
Expand Down
Loading
Loading