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

Limit recursion depth in markdown parser to prevent stack overflow #1273

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions DiscordChatExporter.Core/Markdown/Parsing/AggregateMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

namespace DiscordChatExporter.Core.Markdown.Parsing;

internal class AggregateMatcher<T>(IReadOnlyList<IMatcher<T>> matchers) : IMatcher<T>
internal class AggregateMatcher<TContext, TValue>(
IReadOnlyList<IMatcher<TContext, TValue>> matchers
) : IMatcher<TContext, TValue>
{
public AggregateMatcher(params IMatcher<T>[] matchers)
: this((IReadOnlyList<IMatcher<T>>)matchers) { }
public AggregateMatcher(params IMatcher<TContext, TValue>[] matchers)
: this((IReadOnlyList<IMatcher<TContext, TValue>>)matchers) { }

public ParsedMatch<T>? TryMatch(StringSegment segment)
public ParsedMatch<TValue>? TryMatch(TContext context, StringSegment segment)
{
ParsedMatch<T>? earliestMatch = null;
ParsedMatch<TValue>? earliestMatch = null;

// Try to match the input with each matcher and get the match with the lowest start index
foreach (var matcher in matchers)
{
// Try to match
var match = matcher.TryMatch(segment);
var match = matcher.TryMatch(context, segment);

// If there's no match - continue
if (match is null)
Expand Down
21 changes: 13 additions & 8 deletions DiscordChatExporter.Core/Markdown/Parsing/IMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

namespace DiscordChatExporter.Core.Markdown.Parsing;

internal interface IMatcher<T>
internal interface IMatcher<in TContext, TValue>
{
ParsedMatch<T>? TryMatch(StringSegment segment);
ParsedMatch<TValue>? TryMatch(TContext context, StringSegment segment);
}

internal static class MatcherExtensions
{
public static IEnumerable<ParsedMatch<T>> MatchAll<T>(
this IMatcher<T> matcher,
public static IEnumerable<ParsedMatch<TValue>> MatchAll<TContext, TValue>(
this IMatcher<TContext, TValue> matcher,
TContext context,
StringSegment segment,
Func<StringSegment, T> transformFallback
Func<TContext, StringSegment, TValue> transformFallback
)
{
// Loop through segments divided by individual matches
Expand All @@ -22,6 +23,7 @@ Func<StringSegment, T> transformFallback
{
// Find a match within this segment
var match = matcher.TryMatch(
context,
segment.Relocate(currentIndex, segment.EndIndex - currentIndex)
);

Expand All @@ -36,9 +38,9 @@ Func<StringSegment, T> transformFallback
match.Segment.StartIndex - currentIndex
);

yield return new ParsedMatch<T>(
yield return new ParsedMatch<TValue>(
fallbackSegment,
transformFallback(fallbackSegment)
transformFallback(context, fallbackSegment)
);
}

Expand All @@ -53,7 +55,10 @@ Func<StringSegment, T> transformFallback
{
var fallbackSegment = segment.Relocate(currentIndex, segment.EndIndex - currentIndex);

yield return new ParsedMatch<T>(fallbackSegment, transformFallback(fallbackSegment));
yield return new ParsedMatch<TValue>(
fallbackSegment,
transformFallback(context, fallbackSegment)
);
}
}
}
3 changes: 3 additions & 0 deletions DiscordChatExporter.Core/Markdown/Parsing/MarkdownContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace DiscordChatExporter.Core.Markdown.Parsing;

internal readonly record struct MarkdownContext(int Depth = 0);
Loading