From bf6d500a7e92c16a47a7fa7594af2c8ecaebb2fc Mon Sep 17 00:00:00 2001 From: Richard Elms Date: Mon, 3 Jun 2024 16:01:10 +0200 Subject: [PATCH 1/6] initial commit --- src/BugsnagUnity/Configuration.cs | 46 +++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/BugsnagUnity/Configuration.cs b/src/BugsnagUnity/Configuration.cs index 63f3a4921..25ddbaaf2 100644 --- a/src/BugsnagUnity/Configuration.cs +++ b/src/BugsnagUnity/Configuration.cs @@ -4,6 +4,7 @@ using System.Linq; using BugsnagUnity.Payload; using UnityEngine; +using System.Text.RegularExpressions; namespace BugsnagUnity { @@ -32,7 +33,7 @@ public class Configuration : IMetadataEditor, IFeatureFlagStore public string BundleVersion; - public string[] RedactedKeys = new string[] { "password" }; + public int VersionCode = -1; @@ -54,15 +55,25 @@ public class Configuration : IMetadataEditor, IFeatureFlagStore internal OrderedDictionary FeatureFlags = new OrderedDictionary(); + private List _redactedKeysRegex; + public string[] RedactedKeys = new string[] { "password" }; public bool KeyIsRedacted(string key) { if (RedactedKeys == null || RedactedKeys.Length == 0) { return false; } - foreach (var redactedKey in RedactedKeys) + if (_redactedKeysRegex == null) + { + _redactedKeysRegex = new List(); + foreach (var redactedKey in RedactedKeys) + { + _redactedKeysRegex.Add(new Regex(redactedKey)); + } + } + foreach (var regex in _redactedKeysRegex) { - if (key.ToLower() == redactedKey.ToLower()) + if (regex.IsMatch(key)) { return true; } @@ -188,17 +199,40 @@ public ulong AppHangThresholdMillis } } - public string[] DiscardClasses; - public int MaxPersistedEvents = 32; public int MaxPersistedSessions = 128; public int MaxStringValueLength = 10000; + public string[] DiscardClasses; + + private List _discardClassesRegex; internal bool ErrorClassIsDiscarded(string className) { - return DiscardClasses != null && DiscardClasses.Contains(className); + if(DiscardClasses == null || DiscardClasses.Length == 0) + { + return false; + } + if(_discardClassesRegex == null) + { + _discardClassesRegex = new List(); + if(DiscardClasses != null) + { + foreach (var discardClass in DiscardClasses) + { + _discardClassesRegex.Add(new Regex(discardClass)); + } + } + } + foreach (var regex in _discardClassesRegex) + { + if (regex.IsMatch(className)) + { + return true; + } + } + return false; } private bool IsRunningInEditor() From 289062e0679c561c0e56e35949f97b706c50584f Mon Sep 17 00:00:00 2001 From: Richard Elms Date: Mon, 3 Jun 2024 16:29:26 +0200 Subject: [PATCH 2/6] added unit tests --- CHANGELOG.md | 2 + src/BugsnagUnity/Configuration.cs | 60 +++++++++---------- .../BugsnagUnity.Tests/ConfigurationTests.cs | 53 ++++++++++++++++ 3 files changed, 84 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cabf9c77b..f23b0de66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - Added the `Bugsnag-Integrity` header to outgoing Bugsnag requests. [#797](https://github.com/bugsnag/bugsnag-unity/pull/797) +- Changed proccessing of `Configuration.DiscardClasses` and `Configuration.RedactedKeys`. They remain sting collections in the config object, but are now converted to Regex objects when in use. [#807](https://github.com/bugsnag/bugsnag-unity/pull/807) + ### Bug Fixes - Added a null check to the Bugsnag client to prevent crashes when accessing the client before it has been initialised [#788](https://github.com/bugsnag/bugsnag-unity/pull/788) diff --git a/src/BugsnagUnity/Configuration.cs b/src/BugsnagUnity/Configuration.cs index 25ddbaaf2..ce53f6785 100644 --- a/src/BugsnagUnity/Configuration.cs +++ b/src/BugsnagUnity/Configuration.cs @@ -63,7 +63,7 @@ public bool KeyIsRedacted(string key) { return false; } - if (_redactedKeysRegex == null) + if (_redactedKeysRegex == null || _redactedKeysRegex.Count != RedactedKeys.Length) { _redactedKeysRegex = new List(); foreach (var redactedKey in RedactedKeys) @@ -81,6 +81,34 @@ public bool KeyIsRedacted(string key) return false; } + public string[] DiscardClasses; + + private List _discardClassesRegex; + + internal bool ErrorClassIsDiscarded(string className) + { + if (DiscardClasses == null || DiscardClasses.Length == 0) + { + return false; + } + if (_discardClassesRegex == null || _discardClassesRegex.Count != DiscardClasses.Length) + { + _discardClassesRegex = new List(); + foreach (var discardClass in DiscardClasses) + { + _discardClassesRegex.Add(new Regex(discardClass)); + } + } + foreach (var regex in _discardClassesRegex) + { + if (regex.IsMatch(className)) + { + return true; + } + } + return false; + } + public Configuration(string apiKey) { ApiKey = apiKey; @@ -204,36 +232,6 @@ public ulong AppHangThresholdMillis public int MaxPersistedSessions = 128; public int MaxStringValueLength = 10000; - public string[] DiscardClasses; - - private List _discardClassesRegex; - - internal bool ErrorClassIsDiscarded(string className) - { - if(DiscardClasses == null || DiscardClasses.Length == 0) - { - return false; - } - if(_discardClassesRegex == null) - { - _discardClassesRegex = new List(); - if(DiscardClasses != null) - { - foreach (var discardClass in DiscardClasses) - { - _discardClassesRegex.Add(new Regex(discardClass)); - } - } - } - foreach (var regex in _discardClassesRegex) - { - if (regex.IsMatch(className)) - { - return true; - } - } - return false; - } private bool IsRunningInEditor() { diff --git a/tests/BugsnagUnity.Tests/ConfigurationTests.cs b/tests/BugsnagUnity.Tests/ConfigurationTests.cs index 3551fa9be..202f8c598 100644 --- a/tests/BugsnagUnity.Tests/ConfigurationTests.cs +++ b/tests/BugsnagUnity.Tests/ConfigurationTests.cs @@ -82,5 +82,58 @@ public void EndpointValidation() Assert.IsFalse(config.Endpoints.IsValid); } + + [Test] + public void RedactedKeysTest() + { + var config = new Configuration("foo"); + + // // Default redacted keys + Assert.IsTrue(config.KeyIsRedacted("password")); + Assert.IsFalse(config.KeyIsRedacted("username")); + + var config2 = new Configuration("foo"); + + // Custom redacted keys + config2.RedactedKeys = new string[] { "secret", "token" }; + Assert.IsTrue(config2.KeyIsRedacted("secret")); + Assert.IsTrue(config2.KeyIsRedacted("token")); + Assert.IsFalse(config2.KeyIsRedacted("password")); + + var config3 = new Configuration("foo"); + + // Regex pattern keys + config3.RedactedKeys = new string[] { ".*_key$", "^token_.*" }; + Assert.IsTrue(config3.KeyIsRedacted("api_key")); + Assert.IsTrue(config3.KeyIsRedacted("token_value")); + Assert.IsFalse(config3.KeyIsRedacted("password")); + } + + [Test] + public void DiscardedClassesTest() + { + var config = new Configuration("foo"); + + // No discard classes by default + Assert.IsFalse(config.ErrorClassIsDiscarded("System.Exception")); + + + var config2 = new Configuration("foo"); + + // Adding discard classes + config2.DiscardClasses = new string[] { "System.Exception", "System.NullReferenceException" }; + Assert.IsTrue(config2.ErrorClassIsDiscarded("System.Exception")); + Assert.IsTrue(config2.ErrorClassIsDiscarded("System.NullReferenceException")); + Assert.IsFalse(config2.ErrorClassIsDiscarded("System.ArgumentException")); + + var config3 = new Configuration("foo"); + + // // Regex pattern discard classes + config3.DiscardClasses = new string[] { "^System\\..*Exception$", ".*ReferenceException" }; + Assert.IsTrue(config3.ErrorClassIsDiscarded("System.Exception")); + Assert.IsTrue(config3.ErrorClassIsDiscarded("System.NullReferenceException")); + Assert.IsTrue(config3.ErrorClassIsDiscarded("CustomReferenceException")); + Assert.IsFalse(config3.ErrorClassIsDiscarded("ArgumentError")); + } } } From 6a384c846e09aca66190c7e158d52c015c61823b Mon Sep 17 00:00:00 2001 From: Richard Elms Date: Mon, 3 Jun 2024 16:32:01 +0200 Subject: [PATCH 3/6] typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f23b0de66..72eb17938 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ - Added the `Bugsnag-Integrity` header to outgoing Bugsnag requests. [#797](https://github.com/bugsnag/bugsnag-unity/pull/797) -- Changed proccessing of `Configuration.DiscardClasses` and `Configuration.RedactedKeys`. They remain sting collections in the config object, but are now converted to Regex objects when in use. [#807](https://github.com/bugsnag/bugsnag-unity/pull/807) +- Changed processing of `Configuration.DiscardClasses` and `Configuration.RedactedKeys`. They remain sting collections in the config object, but are now converted to Regex objects when in use. [#807](https://github.com/bugsnag/bugsnag-unity/pull/807) ### Bug Fixes From b54f23b3a6b4d50f60fc7dc3921f568ea000dddf Mon Sep 17 00:00:00 2001 From: Richard Elms Date: Wed, 5 Jun 2024 10:22:36 +0200 Subject: [PATCH 4/6] switch to explicit regex collections and convert in the settings window --- .../Bugsnag/Scripts/BugsnagSettingsObject.cs | 25 +++++++++++++-- src/BugsnagUnity/Configuration.cs | 32 ++++--------------- .../Native/Android/NativeInterface.cs | 18 ++++++++--- src/BugsnagUnity/Native/Cocoa/NativeClient.cs | 18 ++++++++--- .../BugsnagUnity.Tests/ConfigurationTests.cs | 24 ++++++++------ 5 files changed, 71 insertions(+), 46 deletions(-) diff --git a/src/Assets/Bugsnag/Scripts/BugsnagSettingsObject.cs b/src/Assets/Bugsnag/Scripts/BugsnagSettingsObject.cs index 6d53316de..25eaa1121 100644 --- a/src/Assets/Bugsnag/Scripts/BugsnagSettingsObject.cs +++ b/src/Assets/Bugsnag/Scripts/BugsnagSettingsObject.cs @@ -34,7 +34,7 @@ public class BugsnagSettingsObject : ScriptableObject public bool PersistUser = true; public string SessionEndpoint = "https://sessions.bugsnag.com"; public ThreadSendPolicy SendThreads = ThreadSendPolicy.UnhandledOnly; - public string[] RedactedKeys = new string[] { "password" }; + public string[] RedactedKeys = new string[] { ".*password.*" }; public string ReleaseStage; public bool ReportExceptionLogsAsHandled = true; public bool SendLaunchCrashesSynchronously = true; @@ -79,7 +79,16 @@ public Configuration GetConfig() config.BreadcrumbLogLevel = GetLogTypeFromLogLevel( BreadcrumbLogLevel ); config.Context = Context; - config.DiscardClasses = DiscardClasses; + foreach(string discardedClass in DiscardClasses){ + try + { + config.DiscardClasses.Add(new System.Text.RegularExpressions.Regex(discardedClass)); + } + catch (Exception e) + { + Debug.LogError("Regex error adding discard class: " + e.Message); + } + } if (EnabledReleaseStages != null && EnabledReleaseStages.Length > 0) { config.EnabledReleaseStages = EnabledReleaseStages; @@ -98,7 +107,17 @@ public Configuration GetConfig() { config.Endpoints = new EndpointConfiguration(NotifyEndpoint, SessionEndpoint); } - config.RedactedKeys = RedactedKeys; + foreach(string key in RedactedKeys) + { + try + { + config.RedactedKeys.Add(new System.Text.RegularExpressions.Regex(key)); + } + catch (Exception e) + { + Debug.LogError("Regex error adding redacted key: " + e.Message); + } + } if (string.IsNullOrEmpty(ReleaseStage)) { config.ReleaseStage = Debug.isDebugBuild ? "development" : "production"; diff --git a/src/BugsnagUnity/Configuration.cs b/src/BugsnagUnity/Configuration.cs index ce53f6785..d888da28f 100644 --- a/src/BugsnagUnity/Configuration.cs +++ b/src/BugsnagUnity/Configuration.cs @@ -55,23 +55,15 @@ public class Configuration : IMetadataEditor, IFeatureFlagStore internal OrderedDictionary FeatureFlags = new OrderedDictionary(); - private List _redactedKeysRegex; - public string[] RedactedKeys = new string[] { "password" }; + + public List RedactedKeys = new List{new Regex(".*password.*",RegexOptions.IgnoreCase)}; public bool KeyIsRedacted(string key) { - if (RedactedKeys == null || RedactedKeys.Length == 0) + if (RedactedKeys == null || RedactedKeys.Count == 0) { return false; } - if (_redactedKeysRegex == null || _redactedKeysRegex.Count != RedactedKeys.Length) - { - _redactedKeysRegex = new List(); - foreach (var redactedKey in RedactedKeys) - { - _redactedKeysRegex.Add(new Regex(redactedKey)); - } - } - foreach (var regex in _redactedKeysRegex) + foreach (var regex in RedactedKeys) { if (regex.IsMatch(key)) { @@ -81,25 +73,15 @@ public bool KeyIsRedacted(string key) return false; } - public string[] DiscardClasses; - - private List _discardClassesRegex; + public List DiscardClasses = new List(); internal bool ErrorClassIsDiscarded(string className) { - if (DiscardClasses == null || DiscardClasses.Length == 0) + if (DiscardClasses == null || DiscardClasses.Count == 0) { return false; } - if (_discardClassesRegex == null || _discardClassesRegex.Count != DiscardClasses.Length) - { - _discardClassesRegex = new List(); - foreach (var discardClass in DiscardClasses) - { - _discardClassesRegex.Add(new Regex(discardClass)); - } - } - foreach (var regex in _discardClassesRegex) + foreach (var regex in DiscardClasses) { if (regex.IsMatch(className)) { diff --git a/src/BugsnagUnity/Native/Android/NativeInterface.cs b/src/BugsnagUnity/Native/Android/NativeInterface.cs index ef0f8cda7..56c1f907a 100644 --- a/src/BugsnagUnity/Native/Android/NativeInterface.cs +++ b/src/BugsnagUnity/Native/Android/NativeInterface.cs @@ -454,9 +454,14 @@ AndroidJavaObject CreateNativeConfig(Configuration config) } // set DiscardedClasses - if (config.DiscardClasses != null && config.DiscardClasses.Length > 0) + if (config.DiscardClasses != null && config.DiscardClasses.Count > 0) { - obj.Call("setDiscardClasses", GetAndroidRegexPatternSetFromArray(config.DiscardClasses)); + var patternsAsStrings = new string[config.DiscardClasses.Count]; + foreach (var pattern in config.DiscardClasses) + { + patternsAsStrings[config.DiscardClasses.IndexOf(pattern)] = pattern.ToString(); + } + obj.Call("setDiscardClasses", GetAndroidRegexPatternSetFromArray(patternsAsStrings)); } // set ProjectPackages @@ -466,9 +471,14 @@ AndroidJavaObject CreateNativeConfig(Configuration config) } // set redacted keys - if (config.RedactedKeys != null && config.RedactedKeys.Length > 0) + if (config.RedactedKeys != null && config.RedactedKeys.Count > 0) { - obj.Call("setRedactedKeys", GetAndroidRegexPatternSetFromArray(config.RedactedKeys)); + var patternsAsStrings = new string[config.RedactedKeys.Count]; + foreach (var key in config.RedactedKeys) + { + patternsAsStrings[config.RedactedKeys.IndexOf(key)] = key.ToString(); + } + obj.Call("setRedactedKeys", GetAndroidRegexPatternSetFromArray(patternsAsStrings)); } // add unity event callback diff --git a/src/BugsnagUnity/Native/Cocoa/NativeClient.cs b/src/BugsnagUnity/Native/Cocoa/NativeClient.cs index eb5e1b55a..7651db39c 100644 --- a/src/BugsnagUnity/Native/Cocoa/NativeClient.cs +++ b/src/BugsnagUnity/Native/Cocoa/NativeClient.cs @@ -73,13 +73,23 @@ IntPtr CreateNativeConfig(Configuration config) var user = config.GetUser(); NativeCode.bugsnag_setUserInConfig(obj, user.Id, user.Email, user.Name); } - if (config.DiscardClasses != null && config.DiscardClasses.Length > 0) + if (config.DiscardClasses != null && config.DiscardClasses.Count > 0) { - NativeCode.bugsnag_setDiscardClasses(obj, config.DiscardClasses, config.DiscardClasses.Length); + var patternsAsStrings = new string[config.DiscardClasses.Count]; + foreach (var key in config.DiscardClasses) + { + patternsAsStrings[config.DiscardClasses.IndexOf(key)] = key.ToString(); + } + NativeCode.bugsnag_setDiscardClasses(obj, patternsAsStrings, patternsAsStrings.Length); } - if (config.RedactedKeys != null && config.RedactedKeys.Length > 0) + if (config.RedactedKeys != null && config.RedactedKeys.Count > 0) { - NativeCode.bugsnag_setRedactedKeys(obj, config.RedactedKeys, config.RedactedKeys.Length); + var patternsAsStrings = new string[config.RedactedKeys.Count]; + foreach (var key in config.RedactedKeys) + { + patternsAsStrings[config.RedactedKeys.IndexOf(key)] = key.ToString(); + } + NativeCode.bugsnag_setRedactedKeys(obj, patternsAsStrings, patternsAsStrings.Length); } SetEnabledTelemetryTypes(obj,config); diff --git a/tests/BugsnagUnity.Tests/ConfigurationTests.cs b/tests/BugsnagUnity.Tests/ConfigurationTests.cs index 202f8c598..83dbeaa83 100644 --- a/tests/BugsnagUnity.Tests/ConfigurationTests.cs +++ b/tests/BugsnagUnity.Tests/ConfigurationTests.cs @@ -1,5 +1,6 @@ using NUnit.Framework; using System.Linq; +using System.Text.RegularExpressions; using System.Threading; using UnityEngine; @@ -87,26 +88,28 @@ public void EndpointValidation() public void RedactedKeysTest() { var config = new Configuration("foo"); - - // // Default redacted keys + + // Default redacted keys Assert.IsTrue(config.KeyIsRedacted("password")); Assert.IsFalse(config.KeyIsRedacted("username")); var config2 = new Configuration("foo"); // Custom redacted keys - config2.RedactedKeys = new string[] { "secret", "token" }; + config2.RedactedKeys.Add(new Regex(".*secret.*", RegexOptions.IgnoreCase)); + config2.RedactedKeys.Add(new Regex(".*token.*", RegexOptions.IgnoreCase)); Assert.IsTrue(config2.KeyIsRedacted("secret")); Assert.IsTrue(config2.KeyIsRedacted("token")); - Assert.IsFalse(config2.KeyIsRedacted("password")); + Assert.IsTrue(config2.KeyIsRedacted("password")); var config3 = new Configuration("foo"); // Regex pattern keys - config3.RedactedKeys = new string[] { ".*_key$", "^token_.*" }; + config3.RedactedKeys.Add(new Regex(".*api_key.*", RegexOptions.IgnoreCase)); + config3.RedactedKeys.Add(new Regex(".*token_value.*", RegexOptions.IgnoreCase)); Assert.IsTrue(config3.KeyIsRedacted("api_key")); Assert.IsTrue(config3.KeyIsRedacted("token_value")); - Assert.IsFalse(config3.KeyIsRedacted("password")); + Assert.IsTrue(config3.KeyIsRedacted("password")); } [Test] @@ -117,19 +120,20 @@ public void DiscardedClassesTest() // No discard classes by default Assert.IsFalse(config.ErrorClassIsDiscarded("System.Exception")); - var config2 = new Configuration("foo"); // Adding discard classes - config2.DiscardClasses = new string[] { "System.Exception", "System.NullReferenceException" }; + config2.DiscardClasses.Add(new Regex("^System\\.Exception$", RegexOptions.IgnoreCase)); + config2.DiscardClasses.Add(new Regex("^System\\.NullReferenceException$", RegexOptions.IgnoreCase)); Assert.IsTrue(config2.ErrorClassIsDiscarded("System.Exception")); Assert.IsTrue(config2.ErrorClassIsDiscarded("System.NullReferenceException")); Assert.IsFalse(config2.ErrorClassIsDiscarded("System.ArgumentException")); var config3 = new Configuration("foo"); - // // Regex pattern discard classes - config3.DiscardClasses = new string[] { "^System\\..*Exception$", ".*ReferenceException" }; + // Regex pattern discard classes + config3.DiscardClasses.Add(new Regex("^System\\..*Exception$", RegexOptions.IgnoreCase)); + config3.DiscardClasses.Add(new Regex(".*ReferenceException", RegexOptions.IgnoreCase)); Assert.IsTrue(config3.ErrorClassIsDiscarded("System.Exception")); Assert.IsTrue(config3.ErrorClassIsDiscarded("System.NullReferenceException")); Assert.IsTrue(config3.ErrorClassIsDiscarded("CustomReferenceException")); From ae03f242663659cb0b95d9bf5b0574852d8e058a Mon Sep 17 00:00:00 2001 From: Richard Elms Date: Wed, 5 Jun 2024 11:36:31 +0200 Subject: [PATCH 5/6] fixture fix --- .../Scripts/Scenarios/Csharp/Config/DiscardErrorClass.cs | 6 ++++-- .../Assets/Scripts/Scenarios/Csharp/Config/RedactedKeys.cs | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/features/fixtures/maze_runner/Assets/Scripts/Scenarios/Csharp/Config/DiscardErrorClass.cs b/features/fixtures/maze_runner/Assets/Scripts/Scenarios/Csharp/Config/DiscardErrorClass.cs index 6202bcb8d..8f9a291ec 100644 --- a/features/fixtures/maze_runner/Assets/Scripts/Scenarios/Csharp/Config/DiscardErrorClass.cs +++ b/features/fixtures/maze_runner/Assets/Scripts/Scenarios/Csharp/Config/DiscardErrorClass.cs @@ -1,11 +1,13 @@ -using BugsnagUnity; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using BugsnagUnity; public class DiscardErrorClass : Scenario { public override void PrepareConfig(string apiKey, string host) { base.PrepareConfig(apiKey, host); - Configuration.DiscardClasses = new string[] { "IndexOutOfRangeException" }; + Configuration.DiscardClasses = new List { new Regex("IndexOutOfRangeException") }; } public override void Run() diff --git a/features/fixtures/maze_runner/Assets/Scripts/Scenarios/Csharp/Config/RedactedKeys.cs b/features/fixtures/maze_runner/Assets/Scripts/Scenarios/Csharp/Config/RedactedKeys.cs index 8d7198446..354ad9c1b 100644 --- a/features/fixtures/maze_runner/Assets/Scripts/Scenarios/Csharp/Config/RedactedKeys.cs +++ b/features/fixtures/maze_runner/Assets/Scripts/Scenarios/Csharp/Config/RedactedKeys.cs @@ -1,9 +1,12 @@ -public class RedactedKeys : Scenario +using System.Collections.Generic; +using System.Text.RegularExpressions; + +public class RedactedKeys : Scenario { public override void PrepareConfig(string apiKey, string host) { base.PrepareConfig(apiKey, host); - Configuration.RedactedKeys = new string[] { "testKey" }; + Configuration.RedactedKeys = new List { new Regex("testKey") }; Configuration.AddMetadata("testSection","testKey","testValue"); } From b61755457ef04655a37ccafe2ad87fdd3883e4ea Mon Sep 17 00:00:00 2001 From: Richard Elms Date: Wed, 5 Jun 2024 16:09:17 +0200 Subject: [PATCH 6/6] Review fixes --- .../Bugsnag/Scripts/BugsnagSettingsObject.cs | 8 +++---- src/BugsnagUnity/Configuration.cs | 4 ++-- .../Native/Android/NativeInterface.cs | 12 ++++++++-- .../BugsnagUnity.Tests/ConfigurationTests.cs | 22 ++----------------- 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/Assets/Bugsnag/Scripts/BugsnagSettingsObject.cs b/src/Assets/Bugsnag/Scripts/BugsnagSettingsObject.cs index 25eaa1121..e66f5db26 100644 --- a/src/Assets/Bugsnag/Scripts/BugsnagSettingsObject.cs +++ b/src/Assets/Bugsnag/Scripts/BugsnagSettingsObject.cs @@ -84,9 +84,9 @@ public Configuration GetConfig() { config.DiscardClasses.Add(new System.Text.RegularExpressions.Regex(discardedClass)); } - catch (Exception e) + catch (ArgumentException e) { - Debug.LogError("Regex error adding discard class: " + e.Message); + Debug.LogError("Invalid Regex pattern for discard class: " + e.Message); } } if (EnabledReleaseStages != null && EnabledReleaseStages.Length > 0) @@ -113,9 +113,9 @@ public Configuration GetConfig() { config.RedactedKeys.Add(new System.Text.RegularExpressions.Regex(key)); } - catch (Exception e) + catch (ArgumentException e) { - Debug.LogError("Regex error adding redacted key: " + e.Message); + Debug.LogError("Invalid Regex pattern for redacted key: " + e.Message); } } if (string.IsNullOrEmpty(ReleaseStage)) diff --git a/src/BugsnagUnity/Configuration.cs b/src/BugsnagUnity/Configuration.cs index d888da28f..550142de2 100644 --- a/src/BugsnagUnity/Configuration.cs +++ b/src/BugsnagUnity/Configuration.cs @@ -59,7 +59,7 @@ public class Configuration : IMetadataEditor, IFeatureFlagStore public List RedactedKeys = new List{new Regex(".*password.*",RegexOptions.IgnoreCase)}; public bool KeyIsRedacted(string key) { - if (RedactedKeys == null || RedactedKeys.Count == 0) + if (RedactedKeys == null) { return false; } @@ -77,7 +77,7 @@ public bool KeyIsRedacted(string key) internal bool ErrorClassIsDiscarded(string className) { - if (DiscardClasses == null || DiscardClasses.Count == 0) + if (DiscardClasses == null) { return false; } diff --git a/src/BugsnagUnity/Native/Android/NativeInterface.cs b/src/BugsnagUnity/Native/Android/NativeInterface.cs index 56c1f907a..da0c558e7 100644 --- a/src/BugsnagUnity/Native/Android/NativeInterface.cs +++ b/src/BugsnagUnity/Native/Android/NativeInterface.cs @@ -461,6 +461,7 @@ AndroidJavaObject CreateNativeConfig(Configuration config) { patternsAsStrings[config.DiscardClasses.IndexOf(pattern)] = pattern.ToString(); } + obj.Call("setDiscardClasses", GetAndroidRegexPatternSetFromArray(patternsAsStrings)); } @@ -525,8 +526,15 @@ private AndroidJavaObject GetAndroidRegexPatternSetFromArray(string[] array) foreach (var item in array) { - AndroidJavaObject pattern = patternClass.CallStatic("compile", item); - set.Call("add", pattern); + try + { + AndroidJavaObject pattern = patternClass.CallStatic("compile", item); + set.Call("add", pattern); + } + catch (AndroidJavaException e) + { + Debug.LogWarning("Failed to compile regex pattern: " + item + " " + e.Message); + } } return set; diff --git a/tests/BugsnagUnity.Tests/ConfigurationTests.cs b/tests/BugsnagUnity.Tests/ConfigurationTests.cs index 83dbeaa83..2218c9e43 100644 --- a/tests/BugsnagUnity.Tests/ConfigurationTests.cs +++ b/tests/BugsnagUnity.Tests/ConfigurationTests.cs @@ -90,7 +90,7 @@ public void RedactedKeysTest() var config = new Configuration("foo"); // Default redacted keys - Assert.IsTrue(config.KeyIsRedacted("password")); + Assert.IsTrue(config.KeyIsRedacted("user-password")); Assert.IsFalse(config.KeyIsRedacted("username")); var config2 = new Configuration("foo"); @@ -101,15 +101,7 @@ public void RedactedKeysTest() Assert.IsTrue(config2.KeyIsRedacted("secret")); Assert.IsTrue(config2.KeyIsRedacted("token")); Assert.IsTrue(config2.KeyIsRedacted("password")); - - var config3 = new Configuration("foo"); - - // Regex pattern keys - config3.RedactedKeys.Add(new Regex(".*api_key.*", RegexOptions.IgnoreCase)); - config3.RedactedKeys.Add(new Regex(".*token_value.*", RegexOptions.IgnoreCase)); - Assert.IsTrue(config3.KeyIsRedacted("api_key")); - Assert.IsTrue(config3.KeyIsRedacted("token_value")); - Assert.IsTrue(config3.KeyIsRedacted("password")); + Assert.IsFalse(config2.KeyIsRedacted("app_id")); } [Test] @@ -128,16 +120,6 @@ public void DiscardedClassesTest() Assert.IsTrue(config2.ErrorClassIsDiscarded("System.Exception")); Assert.IsTrue(config2.ErrorClassIsDiscarded("System.NullReferenceException")); Assert.IsFalse(config2.ErrorClassIsDiscarded("System.ArgumentException")); - - var config3 = new Configuration("foo"); - - // Regex pattern discard classes - config3.DiscardClasses.Add(new Regex("^System\\..*Exception$", RegexOptions.IgnoreCase)); - config3.DiscardClasses.Add(new Regex(".*ReferenceException", RegexOptions.IgnoreCase)); - Assert.IsTrue(config3.ErrorClassIsDiscarded("System.Exception")); - Assert.IsTrue(config3.ErrorClassIsDiscarded("System.NullReferenceException")); - Assert.IsTrue(config3.ErrorClassIsDiscarded("CustomReferenceException")); - Assert.IsFalse(config3.ErrorClassIsDiscarded("ArgumentError")); } } }