ColorCode-Universal icon indicating copy to clipboard operation
ColorCode-Universal copied to clipboard

Change Json regex to prevent timeout with partial json

Open ogchrislem opened this issue 1 year ago • 1 comments

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 avatar May 13 '24 10:05 ogchrislem

@dotnet-policy-service agree

ogchrislem avatar May 13 '24 10:05 ogchrislem