Skip to content

Commit

Permalink
Log Warning when secret is detected in DSN (#1749)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jun 28, 2022
1 parent 1e7a298 commit c5d84bf
Show file tree
Hide file tree
Showing 31 changed files with 213 additions and 210 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Android Scope Sync ([#1737](https://github.com/getsentry/sentry-dotnet/pull/1737))
- Enable logging in MAUI ([#1738](https://github.com/getsentry/sentry-dotnet/pull/1738))
- Support IntPtr and UIntPtr serialization ([#1746](https://github.com/getsentry/sentry-dotnet/pull/1746))
- Log Warning when secret is detected in DSN ([#1749](https://github.com/getsentry/sentry-dotnet/pull/1749))
- Catch permission exceptions on Android ([#1750](https://github.com/getsentry/sentry-dotnet/pull/1750))
- Enable offline caching in MAUI ([#1753](https://github.com/getsentry/sentry-dotnet/pull/1753))

Expand Down
10 changes: 7 additions & 3 deletions src/Sentry/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ internal static IHub InitHub(SentryOptions options)

// If DSN is null (i.e. not explicitly disabled, just unset), then
// try to resolve the value from environment.
var dsn = options.Dsn ??= DsnLocator.FindDsnStringOrDisable();
var dsnString = options.Dsn ??= DsnLocator.FindDsnStringOrDisable();

// If it's either explicitly disabled or we couldn't resolve the DSN
// from anywhere else, return a disabled hub.
if (Dsn.IsDisabled(dsn))
if (Dsn.IsDisabled(dsnString))
{
options.LogWarning("Init was called but no DSN was provided nor located. Sentry SDK will be disabled.");
return DisabledHub.Instance;
}

// Validate DSN for an early exception in case it's malformed
_ = Dsn.Parse(dsn);
var dsn = Dsn.Parse(dsnString);
if (dsn.SecretKey != null)
{
options.LogWarning("The provided DSN that contains a secret key. This is not required and will be ignored.");
}

return new Hub(options);
}
Expand Down
1 change: 1 addition & 0 deletions test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Using Include="NSubstitute"/>
<Using Include="NSubstitute.Core"/>
<Using Include="NSubstitute.ReturnsExtensions"/>
<Using Include="Sentry.DsnSamples" Static="True" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="NSubstitute" Version="4.3.0" />
<PackageReference Include="FluentAssertions" Version="6.7.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override void ConfigureBuilder(WebHostBuilder builder)
sentryBuilder.AddGrpc();
sentryBuilder.AddSentryOptions(options =>
{
options.Dsn = DsnSamples.ValidDsnWithSecret;
options.Dsn = ValidDsn;
options.SentryHttpClientFactory = new DelegateHttpClientFactory(_ => sentryHttpClient);
Configure?.Invoke(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class SentryWebHostBuilderExtensionsIntegrationTests : AspNetSentrySdkTes
[Fact]
public void UseSentry_ValidDsnString_EnablesSdk()
{
_ = _webHostBuilder.UseSentry(DsnSamples.ValidDsnWithoutSecret)
_ = _webHostBuilder.UseSentry(ValidDsn)
.Build();

try
Expand Down
2 changes: 1 addition & 1 deletion test/Sentry.AspNetCore.Tests/AspNetSentrySdkTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected override void ConfigureBuilder(WebHostBuilder builder)
var sentryHttpClient = sentry.CreateClient();
_ = builder.UseSentry(options =>
{
options.Dsn = DsnSamples.ValidDsnWithSecret;
options.Dsn = ValidDsn;
options.SentryHttpClientFactory = new DelegateHttpClientFactory(_ => sentryHttpClient);
Configure?.Invoke(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public IntegrationMockedBackgroundWorker()
{
_ = builder.UseSentry(options =>
{
options.Dsn = DsnSamples.ValidDsnWithSecret;
options.Dsn = ValidDsn;
options.BackgroundWorker = Worker;
Configure?.Invoke(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Fixture()
Client.When(client => client.CaptureEvent(Arg.Any<SentryEvent>(), Arg.Any<Scope>()))
.Do(callback => callback.Arg<Scope>().Evaluate());

var hub = new Hub(new SentryOptions { Dsn = DsnSamples.ValidDsnWithSecret });
var hub = new Hub(new SentryOptions { Dsn = ValidDsn });
hub.BindClient(Client);
Hub = hub;
var provider = new SentryLoggerProvider(hub, Clock, loggingOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task Generated_client_sends_Sentry_trace_header_automatically()

var hub = new Internal.Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret
Dsn = ValidDsn
});

var server = new TestServer(new WebHostBuilder()
Expand Down
16 changes: 8 additions & 8 deletions test/Sentry.AspNetCore.Tests/SentryTracingMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task Transactions_are_grouped_by_route()
// Arrange
var sentryClient = Substitute.For<ISentryClient>();

var hub = new Internal.Hub(new SentryOptions { Dsn = DsnSamples.ValidDsnWithoutSecret, TracesSampleRate = 1 }, sentryClient);
var hub = new Internal.Hub(new SentryOptions { Dsn = ValidDsn, TracesSampleRate = 1 }, sentryClient);

var server = new TestServer(new WebHostBuilder()
.UseDefaultServiceProvider(di => di.EnableValidation())
Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task Transaction_is_bound_on_the_scope_automatically()

var sentryClient = Substitute.For<ISentryClient>();

var hub = new Internal.Hub(new SentryOptions { Dsn = DsnSamples.ValidDsnWithoutSecret }, sentryClient);
var hub = new Internal.Hub(new SentryOptions { Dsn = ValidDsn }, sentryClient);

var server = new TestServer(new WebHostBuilder()
.UseDefaultServiceProvider(di => di.EnableValidation())
Expand Down Expand Up @@ -108,7 +108,7 @@ public async Task Transaction_is_started_automatically_from_incoming_trace_heade
// Arrange
var sentryClient = Substitute.For<ISentryClient>();

var hub = new Internal.Hub(new SentryOptions { Dsn = DsnSamples.ValidDsnWithoutSecret, TracesSampleRate = 1 }, sentryClient);
var hub = new Internal.Hub(new SentryOptions { Dsn = ValidDsn, TracesSampleRate = 1 }, sentryClient);

var server = new TestServer(new WebHostBuilder()
.UseDefaultServiceProvider(di => di.EnableValidation())
Expand Down Expand Up @@ -152,7 +152,7 @@ public async Task Transaction_is_automatically_populated_with_request_data()

var sentryClient = Substitute.For<ISentryClient>();

var hub = new Internal.Hub(new SentryOptions { Dsn = DsnSamples.ValidDsnWithoutSecret, TracesSampleRate = 1 }, sentryClient);
var hub = new Internal.Hub(new SentryOptions { Dsn = ValidDsn, TracesSampleRate = 1 }, sentryClient);

var server = new TestServer(new WebHostBuilder()
.UseDefaultServiceProvider(di => di.EnableValidation())
Expand Down Expand Up @@ -204,7 +204,7 @@ public async Task Transaction_sampling_context_contains_HTTP_context_data()

var hub = new Internal.Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret,
Dsn = ValidDsn,
TracesSampler = ctx =>
{
samplingContext = ctx;
Expand Down Expand Up @@ -257,7 +257,7 @@ public async Task Transaction_binds_exception_thrown()

var hub = new Internal.Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret,
Dsn = ValidDsn,
TracesSampler = ctx =>
{
samplingContext = ctx;
Expand Down Expand Up @@ -318,7 +318,7 @@ public async Task Transaction_TransactionNameProviderSetSet_TransactionNameSet()
.Do(callback => transaction = callback.Arg<Transaction>());
var options = new SentryAspNetCoreOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret,
Dsn = ValidDsn,
TracesSampleRate = 1
};

Expand Down Expand Up @@ -359,7 +359,7 @@ public async Task Transaction_TransactionNameProviderSetUnset_UnknownTransaction
.Do(callback => transaction = callback.Arg<Transaction>());
var options = new SentryAspNetCoreOptions
{
Dsn = DsnSamples.ValidDsnWithoutSecret,
Dsn = ValidDsn,
TracesSampleRate = 1
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public SentryWebHostBuilderExtensionsTests()
[Theory, MemberData(nameof(ExpectedServices))]
public void UseSentry_ValidDsnString_ServicesRegistered(Action<IServiceCollection> assert)
{
_ = WebHostBuilder.UseSentry(DsnSamples.ValidDsnWithoutSecret);
_ = WebHostBuilder.UseSentry(ValidDsn);
assert(Services);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task RecordsSql()
{
TracesSampleRate = 1,
Transport = transport,
Dsn = DsnSamples.ValidDsnWithoutSecret,
Dsn = ValidDsn,
DiagnosticLevel = SentryLevel.Debug
};

Expand Down Expand Up @@ -62,7 +62,7 @@ public async Task RecordsEf()
{
TracesSampleRate = 1,
Transport = transport,
Dsn = DsnSamples.ValidDsnWithoutSecret,
Dsn = ValidDsn,
DiagnosticLevel = SentryLevel.Debug
};

Expand Down
2 changes: 1 addition & 1 deletion test/Sentry.EntityFramework.Tests/ErrorProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Fixture()
SentryClient = new SentryClient(new SentryOptions
{
BeforeSend = _beforeSend,
Dsn = DsnSamples.ValidDsnWithoutSecret,
Dsn = ValidDsn,
}.AddEntityFramework());
}
}
Expand Down
24 changes: 12 additions & 12 deletions test/Sentry.Maui.Tests/SentryMauiAppBuilderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void CanUseSentry_WithConfigurationOnly()
var builder = _fixture.Builder;
builder.Services.Configure<SentryMauiOptions>(options =>
{
options.Dsn = DsnSamples.ValidDsnWithoutSecret;
options.Dsn = ValidDsn;
});

// Act
Expand All @@ -38,7 +38,7 @@ public void CanUseSentry_WithConfigurationOnly()
// Assert
Assert.Same(builder, chainedBuilder);
Assert.True(SentrySdk.IsEnabled);
Assert.Equal(DsnSamples.ValidDsnWithoutSecret, options.Dsn);
Assert.Equal(ValidDsn, options.Dsn);
Assert.False(options.Debug);
}

Expand All @@ -49,15 +49,15 @@ public void CanUseSentry_WithDsnStringOnly()
var builder = _fixture.Builder;

// Act
var chainedBuilder = builder.UseSentry(DsnSamples.ValidDsnWithoutSecret);
var chainedBuilder = builder.UseSentry(ValidDsn);

using var app = builder.Build();
var options = app.Services.GetRequiredService<IOptions<SentryMauiOptions>>().Value;

// Assert
Assert.Same(builder, chainedBuilder);
Assert.True(SentrySdk.IsEnabled);
Assert.Equal(DsnSamples.ValidDsnWithoutSecret, options.Dsn);
Assert.Equal(ValidDsn, options.Dsn);
Assert.False(options.Debug);
}

Expand All @@ -70,7 +70,7 @@ public void CanUseSentry_WithOptionsOnly()
// Act
var chainedBuilder = builder.UseSentry(options =>
{
options.Dsn = DsnSamples.ValidDsnWithoutSecret;
options.Dsn = ValidDsn;
});

using var app = builder.Build();
Expand All @@ -79,7 +79,7 @@ public void CanUseSentry_WithOptionsOnly()
// Assert
Assert.Same(builder, chainedBuilder);
Assert.True(SentrySdk.IsEnabled);
Assert.Equal(DsnSamples.ValidDsnWithoutSecret, options.Dsn);
Assert.Equal(ValidDsn, options.Dsn);
Assert.False(options.Debug);
}

Expand All @@ -90,7 +90,7 @@ public void CanUseSentry_WithConfigurationAndOptions()
var builder = _fixture.Builder;
builder.Services.Configure<SentryMauiOptions>(options =>
{
options.Dsn = DsnSamples.ValidDsnWithoutSecret;
options.Dsn = ValidDsn;
});

// Act
Expand All @@ -105,7 +105,7 @@ public void CanUseSentry_WithConfigurationAndOptions()
// Assert
Assert.Same(builder, chainedBuilder);
Assert.True(SentrySdk.IsEnabled);
Assert.Equal(DsnSamples.ValidDsnWithoutSecret, options.Dsn);
Assert.Equal(ValidDsn, options.Dsn);
Assert.True(options.Debug);
}

Expand All @@ -116,7 +116,7 @@ public void UseSentry_EnablesGlobalMode()
var builder = _fixture.Builder;

// Act
_ = builder.UseSentry(DsnSamples.ValidDsnWithoutSecret);
_ = builder.UseSentry(ValidDsn);

using var app = builder.Build();
var options = app.Services.GetRequiredService<IOptions<SentryMauiOptions>>().Value;
Expand All @@ -133,7 +133,7 @@ public void UseSentry_SetsMauiSdkNameAndVersion()
var builder = _fixture.Builder
.UseSentry(options =>
{
options.Dsn = DsnSamples.ValidDsnWithoutSecret;
options.Dsn = ValidDsn;
options.BeforeSend = e =>
{
// capture the event
Expand All @@ -160,7 +160,7 @@ public void UseSentry_EnablesHub()
{
// Arrange
var builder = _fixture.Builder
.UseSentry(DsnSamples.ValidDsnWithoutSecret);
.UseSentry(ValidDsn);

// Act
using var app = builder.Build();
Expand All @@ -179,7 +179,7 @@ public void UseSentry_AppDispose_DisposesHub()

// Arrange
var builder = _fixture.Builder
.UseSentry(DsnSamples.ValidDsnWithoutSecret);
.UseSentry(ValidDsn);

// Act
IHub hub;
Expand Down
12 changes: 5 additions & 7 deletions test/Sentry.NLog.Tests/SentryTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@

namespace Sentry.NLog.Tests;

using static DsnSamples;

public class SentryTargetTests
{
private const string DefaultMessage = "This is a logged message";

private class Fixture
{
public SentryNLogOptions Options { get; set; } = new() { Dsn = ValidDsnWithSecret };
public SentryNLogOptions Options { get; set; } = new() { Dsn = ValidDsn };

public IHub Hub { get; set; } = Substitute.For<IHub>();

Expand Down Expand Up @@ -86,7 +84,7 @@ public void Can_configure_from_xml_file()
<add type='{typeof(SentryTarget).AssemblyQualifiedName}' />
</extensions>
<targets>
<target type='Sentry' name='sentry' dsn='{ValidDsnWithoutSecret}' release='1.2.3' environment='test'>
<target type='Sentry' name='sentry' dsn='{ValidDsn}' release='1.2.3' environment='test'>
<options>
<attachStacktrace>True</attachStacktrace>
</options>
Expand All @@ -103,7 +101,7 @@ public void Can_configure_from_xml_file()
Assert.NotNull(t);
if (t.Options.Dsn != null)
{
Assert.Equal(ValidDsnWithoutSecret, t.Options.Dsn);
Assert.Equal(ValidDsn, t.Options.Dsn);
}

Assert.Equal("test", t.Options.Environment);
Expand All @@ -120,7 +118,7 @@ public void Can_configure_user_from_xml_file()
<add type='{typeof(SentryTarget).AssemblyQualifiedName}' />
</extensions>
<targets>
<target type='Sentry' name='sentry' dsn='{ValidDsnWithoutSecret}'>
<target type='Sentry' name='sentry' dsn='{ValidDsn}'>
<user username=""myUser"">
<other name='mood' layout='joyous'/>
</user>
Expand All @@ -135,7 +133,7 @@ public void Can_configure_user_from_xml_file()

var t = logFactory.Configuration.FindTargetByName("sentry") as SentryTarget;
Assert.NotNull(t);
Assert.Equal(ValidDsnWithoutSecret, t.Options.Dsn);
Assert.Equal(ValidDsn, t.Options.Dsn);
Assert.Equal("'myUser'", t.User.Username.ToString());
Assert.NotEmpty(t.User.Other);
Assert.Equal("mood", t.User.Other[0].Name);
Expand Down
2 changes: 1 addition & 1 deletion test/Sentry.Serilog.Tests/AspNetSentrySdkTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected override void ConfigureBuilder(WebHostBuilder builder)
var sentryHttpClient = sentry.CreateClient();
_ = builder.UseSentry(options =>
{
options.Dsn = DsnSamples.ValidDsnWithSecret;
options.Dsn = ValidDsn;
options.SentryHttpClientFactory = new DelegateHttpClientFactory(_ => sentryHttpClient);
Configure?.Invoke(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private class Fixture
public float SampleRate { get; } = 0.4f;
public string Release { get; } = nameof(ConfigureSentrySerilogOptions_WithAllParameters_MakesAppropriateChangesToObject);
public string Environment { get; } = nameof(ConfigureSentrySerilogOptions_WithAllParameters_MakesAppropriateChangesToObject);
public string Dsn { get; } = DsnSamples.ValidDsnWithSecret;
public string Dsn { get; } = ValidDsn;
public int MaxQueueItems { get; } = 17;
public TimeSpan ShutdownTimeout { get; } = TimeSpan.FromDays(1.3);
public DecompressionMethods DecompressionMethods { get; } = DecompressionMethods.Deflate & DecompressionMethods.GZip;
Expand Down
6 changes: 1 addition & 5 deletions test/Sentry.Testing/DsnSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ public static class DsnSamples
/// <summary>
/// Sentry has dropped the use of secrets
/// </summary>
public const string ValidDsnWithoutSecret = "https://d4d82fc1c2c4032a83f3a29aa3a3aff@fake-sentry.io:65535/2147483647";
/// <summary>
/// Legacy includes secret
/// </summary>
public const string ValidDsnWithSecret = "https://d4d82fc1c2c4032a83f3a29aa3a3aff:ed0a8589a0bb4d4793ac4c70375f3d65@fake-sentry.io:65535/2147483647";
public const string ValidDsn = "https://d4d82fc1c2c4032a83f3a29aa3a3aff@fake-sentry.io:65535/2147483647";
/// <summary>
/// Missing ProjectId
/// </summary>
Expand Down
Loading

0 comments on commit c5d84bf

Please sign in to comment.