From 550b95938aa716c2f01bc53bd7e4c660ca8bacb0 Mon Sep 17 00:00:00 2001 From: erri120 Date: Tue, 17 Dec 2024 11:19:54 +0100 Subject: [PATCH 1/2] Fix URL encoding for events --- src/NexusMods.Telemetry/EventDefinition.cs | 14 +++++++++++++- src/NexusMods.Telemetry/EventMetadata.cs | 10 ++++++++++ src/NexusMods.Telemetry/EventSender.cs | 21 ++++++++++++++++++--- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/NexusMods.Telemetry/EventDefinition.cs b/src/NexusMods.Telemetry/EventDefinition.cs index fed9432a7..56c879fcf 100644 --- a/src/NexusMods.Telemetry/EventDefinition.cs +++ b/src/NexusMods.Telemetry/EventDefinition.cs @@ -1,3 +1,5 @@ +using System.Net; +using System.Text; using JetBrains.Annotations; namespace NexusMods.Telemetry; @@ -8,4 +10,14 @@ namespace NexusMods.Telemetry; /// The event category /// The event action [PublicAPI] -public record EventDefinition(string Category, string Action); +public record EventDefinition(string Category, string Action) +{ + internal byte[] SafeCategory { get; } = Encode(Category); + internal byte[] SafeAction { get; } = Encode(Action); + + private static byte[] Encode(string value) + { + var bytes = Encoding.UTF8.GetBytes(value); + return WebUtility.UrlEncodeToBytes(bytes, offset: 0, count: bytes.Length); + } +}; diff --git a/src/NexusMods.Telemetry/EventMetadata.cs b/src/NexusMods.Telemetry/EventMetadata.cs index c680d1dd4..ec2246dab 100644 --- a/src/NexusMods.Telemetry/EventMetadata.cs +++ b/src/NexusMods.Telemetry/EventMetadata.cs @@ -1,4 +1,6 @@ using System.Diagnostics; +using System.Net; +using System.Text; using JetBrains.Annotations; namespace NexusMods.Telemetry; @@ -41,4 +43,12 @@ public EventMetadata(string? name, TimeProvider? timeProvider = null) /// Checks whether the struct wasn't default initialized. /// public bool IsValid() => Name is not null || CurrentTime != default(TimeOnly); + + internal byte[] SafeName => Name is null ? [] : Encode(Name); + + private static byte[] Encode(string value) + { + var bytes = Encoding.UTF8.GetBytes(value); + return WebUtility.UrlEncodeToBytes(bytes, offset: 0, count: bytes.Length); + } } diff --git a/src/NexusMods.Telemetry/EventSender.cs b/src/NexusMods.Telemetry/EventSender.cs index a29bd12a5..3ffbf1065 100644 --- a/src/NexusMods.Telemetry/EventSender.cs +++ b/src/NexusMods.Telemetry/EventSender.cs @@ -163,14 +163,29 @@ private static void SerializeEvent(IBufferWriter writer, ValueTuple