CommunityToolkit / ColorCode-Universal

This is a port of ColorCode to .NET Standard. The original Html only formatter has been separated from the Logic, so now it can produce Syntax Highlighted code for any output. This Project can currently produce HTML, and Render to UWP RichTextBlocks.
Other
221 stars 41 forks source link

Change Json regex to prevent timeout with partial json #43

Open ogchrislem opened 5 months ago

ogchrislem commented 5 months ago

Json parsing can fail with incomplete json, timing out with no exception thrown. Adding the word boundary match to the regex prevents this. Unsure of any further implications but complete json is still parsed and coloured successfully after the change

Example failing code before change using ColorCode via Markdig

using Markdown.ColorCode;
using ColorCode.Styling;
using System;

public class Program
{
    public static void Main()
    {

        var markdownPipeline = new Markdig.MarkdownPipelineBuilder()            
            .UseColorCode(styleDictionary: StyleDictionary.DefaultDark)         
            .Build();

        var result = Markdig.Markdown.ToHtml("```json\n[\n    {\n        \"jsonfield1\": \"value1\",\n        \"jsonfield2\": \"value2\"\n    },\n    {\n        \"jsonfield1\": \"value1 value1 value1 value1 value1 value1 value1 ",markdownPipeline);

        Console.WriteLine(result); 
    }
}
ogchrislem commented 5 months ago

@dotnet-policy-service agree