Skip to content

Commit 03416df

Browse files
StefHCopilot
andcommitted
Validate dictionary method calls using PredefinedMethodsHelper
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 59a8cb2 commit 03416df

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,11 @@ private bool TryParseEnumerable(Expression instance, Type enumerableType, string
23242324
if (type != null && TypeHelper.IsDictionary(type) && _methodFinder.ContainsMethod(type, methodName, false))
23252325
{
23262326
var dictionaryMethod = type.GetMethod(methodName)!;
2327+
if (!PredefinedMethodsHelper.IsPredefinedDictionaryMethod(dictionaryMethod))
2328+
{
2329+
throw ParseError(tokenPos, Res.MethodIsInaccessible, methodName, TypeHelper.GetTypeName(dictionaryMethod.DeclaringType!));
2330+
}
2331+
23272332
expression = Expression.Call(instance, dictionaryMethod, args);
23282333
return true;
23292334
}

‎src/System.Linq.Dynamic.Core/Parser/PredefinedMethodsHelper.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ internal static class PredefinedMethodsHelper
2020
ObjectStaticReferenceEquals
2121
];
2222

23+
private static readonly HashSet<string> AllowedDictionaryMethodNames = ["ContainsKey", "ContainsValue", "TryGetValue", "get_Item"];
24+
25+
public static bool IsPredefinedDictionaryMethod(MethodInfo method)
26+
{
27+
Check.NotNull(method);
28+
29+
return AllowedDictionaryMethodNames.Contains(method.Name);
30+
}
31+
2332
public static bool IsPredefinedMethod(ParsingConfig config, MemberInfo member)
2433
{
2534
Check.NotNull(config);

0 commit comments

Comments
 (0)