diff --git a/docs/index.rst b/docs/index.rst index 1cf8a482..b8447e88 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -30,7 +30,7 @@ Instantiate the client with your DSN: .. sourcecode:: csharp - var ravenClient = new RavenClient("___DSN___"); + var ravenClient = new RavenClient("___PUBLIC_DSN___"); Capturing Exceptions -------------------- @@ -106,7 +106,7 @@ The only thing you have to do is provide a DSN, either by registering an instanc protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) { - container.Register(new Dsn("http://public:secret@example.com/project-id")); + container.Register(new Dsn("___PUBLIC_DSN___")); } or through configuration: @@ -118,7 +118,7 @@ or through configuration:
- + diff --git a/src/app/SharpRaven/Dsn.cs b/src/app/SharpRaven/Dsn.cs index dacbca6f..e8380db3 100644 --- a/src/app/SharpRaven/Dsn.cs +++ b/src/app/SharpRaven/Dsn.cs @@ -61,7 +61,7 @@ public Dsn(string dsn) { this.uri = new Uri(dsn); this.privateKey = GetPrivateKey(this.uri); - this.publicKey = GetPublicKey(this.uri); + this.publicKey = GetPublicKey(this.uri) ?? throw new ArgumentException("A publicKey is required.", nameof(dsn)); this.port = this.uri.Port; this.projectID = GetProjectID(this.uri); this.path = GetPath(this.uri); @@ -176,13 +176,14 @@ private static string GetPath(Uri uri) /// - /// Get a private key from a Dsn uri. + /// Get a private key or null if no private key defined. /// /// /// private static string GetPrivateKey(Uri uri) { - return uri.UserInfo.Split(':')[1]; + var parts = uri.UserInfo.Split(':'); + return parts.Length == 2 ? parts[1] : null; } @@ -205,7 +206,8 @@ private static string GetProjectID(Uri uri) /// private static string GetPublicKey(Uri uri) { - return uri.UserInfo.Split(':')[0]; + var publicKey = uri.UserInfo.Split(':')[0]; + return publicKey != string.Empty ? publicKey : null; } } } \ No newline at end of file diff --git a/src/app/SharpRaven/Utilities/PacketBuilder.cs b/src/app/SharpRaven/Utilities/PacketBuilder.cs index 3c2f9b73..63ac9baf 100644 --- a/src/app/SharpRaven/Utilities/PacketBuilder.cs +++ b/src/app/SharpRaven/Utilities/PacketBuilder.cs @@ -78,12 +78,12 @@ public static string CreateAuthenticationHeader(Dsn dsn) + ", sentry_client={1}" + ", sentry_timestamp={2}" + ", sentry_key={3}" - + ", sentry_secret={4}", + + "{4}", SentryVersion, UserAgent, (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds, dsn.PublicKey, - dsn.PrivateKey); + dsn.PrivateKey != null ? ", sentry_secret=" + dsn.PrivateKey : null); } } } \ No newline at end of file diff --git a/src/tests/SharpRaven.UnitTests/DsnTests.cs b/src/tests/SharpRaven.UnitTests/DsnTests.cs index 2db251ca..fd50b29d 100644 --- a/src/tests/SharpRaven.UnitTests/DsnTests.cs +++ b/src/tests/SharpRaven.UnitTests/DsnTests.cs @@ -73,6 +73,24 @@ public void Constructor_ValidHttpsUri_SentryUriHasHttpsScheme() } + [Test] + public void Constructor_ValidDsnWithoutPrivateKey_PrivateKeyGetterReturnsNull() + { + var dsn = new Dsn(TestHelper.DsnUriWithoutPrivateKey); + + Assert.That(dsn.PrivateKey, Is.Null); + } + + + [Test] + public void Constructor_ValidDsnWithoutPrivateKey_PublicKeyIsParsed() + { + var dsn = new Dsn(TestHelper.DsnUriWithoutPrivateKey); + + Assert.AreEqual("7d6466e66155431495bdb4036ba9a04b", dsn.PublicKey); + } + + [Test] public void Constructor_ValidHttpsUri_UriIsEqualToDsn() { @@ -83,6 +101,16 @@ public void Constructor_ValidHttpsUri_UriIsEqualToDsn() } + [Test] + public void Constructor_ValidDsnWithoutPrivateKey_UriIsEqualToDsn() + { + var dsn = new Dsn(TestHelper.DsnUriWithoutPrivateKey); + + Assert.That(dsn.Uri, Is.Not.Null); + Assert.That(dsn.Uri.ToString(), Is.EqualTo(TestHelper.DsnUriWithoutPrivateKey)); + } + + [Test] public void Constructor_ValidHttpUri_SentryUriHasHttpScheme() { diff --git a/src/tests/SharpRaven.UnitTests/Utilities/PacketBuilderTests.cs b/src/tests/SharpRaven.UnitTests/Utilities/PacketBuilderTests.cs index 31844c15..c76e6c23 100644 --- a/src/tests/SharpRaven.UnitTests/Utilities/PacketBuilderTests.cs +++ b/src/tests/SharpRaven.UnitTests/Utilities/PacketBuilderTests.cs @@ -38,7 +38,7 @@ namespace SharpRaven.UnitTests.Utilities public class PacketBuilderTests { [Test] - public void CreateAuthenticationHeader_ReturnsCorrectAuthenticationHeader() + public void CreateAuthenticationHeader_DsnWithSecret_ReturnsCorrectAuthenticationHeader() { const string expected = @"^Sentry sentry_version=[\d], sentry_client=SharpRaven/[\d\.]+, sentry_timestamp=\d+, sentry_key=7d6466e66155431495bdb4036ba9a04b, sentry_secret=4c1cfeab7ebd4c1cb9e18008173a3630$"; @@ -50,5 +50,19 @@ public void CreateAuthenticationHeader_ReturnsCorrectAuthenticationHeader() Assert.That(authenticationHeader, Is.StringMatching(expected)); } + + [Test] + public void CreateAuthenticationHeader_DsnWithoutSecret_ReturnsCorrectAuthenticationHeader() + { + const string expected = + @"^Sentry sentry_version=[\d], sentry_client=SharpRaven/[\d\.]+, sentry_timestamp=\d+, sentry_key=7d6466e66155431495bdb4036ba9a04b$"; + + var dsn = new Dsn( + "https://7d6466e66155431495bdb4036ba9a04b@app.getsentry.com/3739"); + + var authenticationHeader = PacketBuilder.CreateAuthenticationHeader(dsn); + + Assert.That(authenticationHeader, Is.StringMatching(expected)); + } } } \ No newline at end of file diff --git a/src/tests/SharpRaven.UnitTests/Utilities/TestHelper.cs b/src/tests/SharpRaven.UnitTests/Utilities/TestHelper.cs index 82b0e4ac..5684f784 100644 --- a/src/tests/SharpRaven.UnitTests/Utilities/TestHelper.cs +++ b/src/tests/SharpRaven.UnitTests/Utilities/TestHelper.cs @@ -39,6 +39,9 @@ public static class TestHelper public const string DsnUri = "https://7d6466e66155431495bdb4036ba9a04b:4c1cfeab7ebd4c1cb9e18008173a3630@app.getsentry.com/3739"; + public const string DsnUriWithoutPrivateKey = + "https://7d6466e66155431495bdb4036ba9a04b@app.getsentry.com/3739"; + public static Exception GetException() {