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

Change Json regex to prevent timeout with partial json #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ogchrislem
Copy link

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); 
	}
}

Json parsing can fail with incomplete json, and just timeout 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 successfully after the change
@ogchrislem
Copy link
Author

@dotnet-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants