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: Validate Release string #389

Merged
merged 9 commits into from
Oct 29, 2021
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Fixes

- The sample C plugin does no longer get inlined ([#391](https://github.com/getsentry/sentry-unity/pull/391))
- Added missing release string validation ([#389](https://github.com/getsentry/sentry-unity/pull/389))
- Sentry internal logs no longer show up as breadcrumbs ([#377](https://github.com/getsentry/sentry-unity/pull/377))
- Fixed missing context data when initializing SDK programmatically ([#376](https://github.com/getsentry/sentry-unity/pull/376))
- Fixed CaptureInEditor flag when initializing SDK programmatically ([#370](https://github.com/getsentry/sentry-unity/pull/370))
Expand Down
19 changes: 14 additions & 5 deletions src/Sentry.Unity/SentryOptionsUtility.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq;
using System.Text.RegularExpressions;
using Sentry.Unity.Integrations;

namespace Sentry.Unity
Expand Down Expand Up @@ -65,11 +67,18 @@ public static void SetDefaults(ScriptableSentryUnityOptions scriptableOptions)
scriptableOptions.DiagnosticLevel = SentryLevel.Warning;
}

private static string Release(IApplication application) =>
application.ProductName is string productName
&& !string.IsNullOrWhiteSpace(productName)
? $"{productName}@{application.Version}"
: $"{application.Version}";
private static string Release(IApplication application)
{
if (application.ProductName is string productName
&& !string.IsNullOrWhiteSpace(productName)
&& productName.Any(c => c != '.')) // productName consisting solely of '.'
{
productName = Regex.Replace(productName, @"\n|\r|\t|\/|\\|\.{2}|@", "_");
return $"{productName}@{application.Version}";
}

return application.Version;
}

private static string Environment(IApplication application, bool isBuilding) => (application.IsEditor && !isBuilding) ? "editor" : "production";

Expand Down
32 changes: 32 additions & 0 deletions test/Sentry.Unity.Tests/SentryOptionsUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,38 @@ public void SetDefaults_Release_IsApplicationProductNameAtVersion()
Assert.AreEqual($"{productName}@{version}", options.Release);
}

[Test]
[TestCase("\n")]
[TestCase("\t")]
[TestCase("/")]
[TestCase("\\")]
[TestCase("..")]
[TestCase("@")]
public void SetDefaults_Release_DoesNotContainInvalidCharacters(string invalidString)
{
var options = new SentryUnityOptions();
var prefix = "test";
var suffix = "application";
var version = "0.1.0";
var application = new TestApplication(productName: $"{prefix}{invalidString}{suffix}", version: version);

SentryOptionsUtility.SetDefaults(options, application);

Assert.AreEqual($"{prefix}_{suffix}@{version}", options.Release);
}

[Test]
public void SetDefaults_Release_IgnoresDotOnlyProductNames()
{
var options = new SentryUnityOptions();
var version = "0.1.0";
var application = new TestApplication(productName: ".........", version: version);

SentryOptionsUtility.SetDefaults(options, application);

Assert.AreEqual($"{version}", options.Release);
}

[Test]
public void SetDefaults_Environment_IsEditorInEditor()
{
Expand Down