diff --git a/DisCatSharp/Clients/BaseDiscordClient.cs b/DisCatSharp/Clients/BaseDiscordClient.cs index 25b00bfde0..d0f3525782 100644 --- a/DisCatSharp/Clients/BaseDiscordClient.cs +++ b/DisCatSharp/Clients/BaseDiscordClient.cs @@ -249,6 +249,23 @@ protected BaseDiscordClient(DiscordConfiguration config) EnableScopeSync = true, Debug = this.Configuration.SentryDebug }; + + options.SetBeforeBreadcrumb(b + => new Breadcrumb(Utilities.StripTokens(b.Message), + b.Type, + b.Data?.Select(x => new KeyValuePair(x.Key, Utilities.StripTokens(x.Value))) + .ToDictionary(x => x.Key, x => x.Value), + b.Category, + b.Level)); + + options.SetBeforeSendTransaction(tr => + { + if (tr.Request.Data is string str) + tr.Request.Data = Utilities.StripTokens(str); + + return tr; + }); + options.SetBeforeSend((e, _) => { if (!this.Configuration.DisableExceptionFilter) diff --git a/DisCatSharp/DiscordConfiguration.cs b/DisCatSharp/DiscordConfiguration.cs index 891128dd03..60d96f3551 100644 --- a/DisCatSharp/DiscordConfiguration.cs +++ b/DisCatSharp/DiscordConfiguration.cs @@ -239,10 +239,12 @@ public UdpClientFactoryDelegate UdpClientFactory /// public IServiceProvider ServiceProvider { internal get; init; } = new ServiceCollection().BuildServiceProvider(true); + // TODO: Add disclaimer and docs for sentry /// - /// Whether to report missing fields for discord object. + /// Whether to emable sentry. /// This helps us to track missing data and library bugs better. /// Defaults to . + /// TODO: Add disclaimer and docs. /// public bool EnableSentry { internal get; set; } = false; diff --git a/DisCatSharp/Net/Rest/RestClient.cs b/DisCatSharp/Net/Rest/RestClient.cs index 40bd26eb2b..1060194935 100644 --- a/DisCatSharp/Net/Rest/RestClient.cs +++ b/DisCatSharp/Net/Rest/RestClient.cs @@ -563,8 +563,7 @@ private async Task ExecuteRequestAsync(BaseRestRequest request, RateLimitBucket? case HttpStatusCode.BadRequest: case HttpStatusCode.MethodNotAllowed: ex = new BadRequestException(request, response); - // ex won't be added to avoid possible leaks - senex = new(ex.Message + "\nJson Response: " + ((ex as BadRequestException)?.JsonMessage ?? "null")); + senex = new(ex.Message + "\nJson Response: " + ((ex as BadRequestException)?.JsonMessage ?? "null"), ex); break; case HttpStatusCode.Unauthorized: @@ -629,8 +628,7 @@ private async Task ExecuteRequestAsync(BaseRestRequest request, RateLimitBucket? case HttpStatusCode.ServiceUnavailable: case HttpStatusCode.GatewayTimeout: ex = new ServerErrorException(request, response); - // ex won't be added to avoid possible leaks - senex = new(ex.Message + "\nJson Response: " + ((ex as ServerErrorException)!.JsonMessage ?? "null")); + senex = new(ex.Message + "\nJson Response: " + ((ex as ServerErrorException)!.JsonMessage ?? "null"), ex); break; } diff --git a/DisCatSharp/Utilities.cs b/DisCatSharp/Utilities.cs index 67f2a6bb64..1b4a243543 100644 --- a/DisCatSharp/Utilities.cs +++ b/DisCatSharp/Utilities.cs @@ -90,6 +90,24 @@ static Utilities() VersionHeader = $"DiscordBot (https://github.com/Aiko-IT-Systems/DisCatSharp, v{vs})"; } + + + /// + /// Removes discord-based tokens from a given string. + /// + /// The string to remove the tokens from. + /// A new string with the tokens replaced with {KEY_TOKEN} + public static string? StripTokens(string? str) + { + if (string.IsNullOrWhiteSpace(str)) + return str; + + str = Regex.Replace(str, @"([a-zA-Z0-9]{68,})", "{WEBHOOK_OR_INTERACTION_TOKEN}"); // Any alphanumeric string this long is likely to be sensitive information anyways + str = Regex.Replace(str, @"(mfa\.[a-z0-9_-]{20,})|((?[a-z0-9_-]{23,28})\.(?[a-z0-9_-]{6,7})\.(?[a-z0-9_-]{27,}))", "{BOT_OR_USER_TOKEN}"); + + return str; + } + /// /// Adds the specified parameter to the Query String. ///