implement wildcard lenght quantifiers, TreatAsRegex property and solve catastrpohic backtracking (#2528)

This commit is contained in:
Cenk Ergen
2022-12-14 17:06:57 +03:00
committed by GitHub
parent 25cfb8822f
commit 3b107c2d01
9 changed files with 89 additions and 7 deletions

View File

@@ -2,14 +2,13 @@ using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Discord.Interactions
{
internal class CommandMapNode<T> where T : class, ICommandInfo
{
private const string RegexWildCardExp = "(\\S+)?";
{
private readonly string _wildCardStr = "*";
private readonly ConcurrentDictionary<string, CommandMapNode<T>> _nodes;
private readonly ConcurrentDictionary<string, T> _commands;
@@ -35,10 +34,8 @@ namespace Discord.Interactions
{
if (keywords.Count == index + 1)
{
if (commandInfo.SupportsWildCards && commandInfo.Name.Contains(_wildCardStr))
if (commandInfo.SupportsWildCards && RegexUtils.TryBuildRegexPattern(commandInfo, _wildCardStr, out var patternStr))
{
var escapedStr = RegexUtils.EscapeExcluding(commandInfo.Name, _wildCardStr.ToArray());
var patternStr = "\\A" + escapedStr.Replace(_wildCardStr, RegexWildCardExp) + "\\Z";
var regex = new Regex(patternStr, RegexOptions.Singleline | RegexOptions.Compiled);
if (!_wildCardCommands.TryAdd(regex, commandInfo))