Zimmergren/LogAnalytics.Client

Non-ASCII characters are not handled correctly

GunnarHakansson opened this issue · 3 comments

I noticed that if a string value contains a character outside ASCII the POST fails with 403.
I tried copying the code to my own project, and found the cause. This line, specifically serializedJsonObject.Length, only works when the string only contains ASCII characters.

string stringToSign = $"POST\n{serializedJsonObject.Length}\napplication/json\nx-ms-date:{dateString}\n/api/logs";

If you replace it with Encoding.UTF8.GetBytes(serializedJsonObject).Length, or something similar, it should work for more strings.

Note, that this simple solution would not have the best performance, but that might be a lesser concern.

Hey @GunnarHakansson ,

Thank you for filing the issue, along with a fix.
I could integrate some tests into the full E2E tests for this.

Do you possibly have an example of a working vs. non-working object to pass into the code, then I could quickly integrate a few tests for that and get a new release done.

Sure. Here is a minimal program to reproduce it.

        static async Task Main(string[] args)
        {
            var client = new LogAnalyticsClient(args[0], args[1]);

            var working = new ToLog() {Message = "A"};

            var nonWorking = new ToLog() { Message = "Å" };

            await client.SendLogEntry(working, "TempTest");

            await client.SendLogEntry(nonWorking, "TempTest");
        }

        public class ToLog
        {
            public string Message { get; set; }
        }

By the way, there are some feature improvements I would like to make. Should I just send a Pull Request, or open a new issue for discussion first?

I can integrate the above examples into the E2E tests and verify that they work all the way, and that the fix works with the existing scenarios.

Meanwhile, do what you think make most sense - I'm open for both discussions and PRs. All help is appreciated.