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

Client-Side Sentry Filtering #495

Merged
merged 8 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions DisCatSharp/Clients/BaseDiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>(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)
Expand Down
4 changes: 3 additions & 1 deletion DisCatSharp/DiscordConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,12 @@ public UdpClientFactoryDelegate UdpClientFactory
/// </summary>
public IServiceProvider ServiceProvider { internal get; init; } = new ServiceCollection().BuildServiceProvider(true);

// TODO: Add disclaimer and docs for sentry
/// <summary>
/// <para>Whether to report missing fields for discord object.</para>
/// <para>Whether to emable sentry.</para>
/// <para>This helps us to track missing data and library bugs better.</para>
/// <para>Defaults to <see langword="false"/>.</para>
/// <para><note type="note">TODO: Add disclaimer and docs.</note></para>
/// </summary>
public bool EnableSentry { internal get; set; } = false;

Expand Down
6 changes: 2 additions & 4 deletions DisCatSharp/Net/Rest/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
}

Expand Down
18 changes: 18 additions & 0 deletions DisCatSharp/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ static Utilities()
VersionHeader = $"DiscordBot (https://github.com/Aiko-IT-Systems/DisCatSharp, v{vs})";
}



/// <summary>
/// Removes discord-based tokens from a given string.
/// </summary>
/// <param name="str">The string to remove the tokens from.</param>
/// <returns>A new string with the tokens replaced with <c>{KEY_TOKEN}</c></returns>
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,})|((?<botid>[a-z0-9_-]{23,28})\.(?<creation>[a-z0-9_-]{6,7})\.(?<enc>[a-z0-9_-]{27,}))", "{BOT_OR_USER_TOKEN}");

return str;
}

/// <summary>
/// Adds the specified parameter to the Query String.
/// </summary>
Expand Down
Loading