Skip to content

Commit e7facf1

Browse files
committed
more fixes
1 parent 6a812bd commit e7facf1

4 files changed

Lines changed: 83 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ The `DefaultDynamicLinqCustomTypeProvider` has been changed to only return types
6868
If it's not possible to add that attribute, you need to implement a custom [CustomTypeProvider](https://dynamic-linq.net/advanced-configuration#customtypeprovider) and set this to the `ParsingConfig` and provide that config to all dynamic calls.
6969
Or provide a list of additional types in the [DefaultDynamicLinqCustomTypeProvider.cs](https://github.com/zzzprojects/System.Linq.Dynamic.Core/blob/master/src/System.Linq.Dynamic.Core/CustomTypeProviders/DefaultDynamicLinqCustomTypeProvider.cs).
7070

71+
### v1.7.5
72+
73+
#### Dictionary methods
74+
Dictionary method calls are restricted to recognized framework dictionary read methods.
75+
Custom dictionary methods, including methods that hide or overload those names, are not permitted through this path.
76+
Dictionary interface calls and other virtual calls can still dispatch to application-provided implementations, and enumerable operations can execute application-provided enumerators.
77+
Use trusted implementations when evaluating untrusted expressions.
78+
7179
---
7280

7381
## Useful links

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,12 +2323,14 @@ private bool TryParseEnumerable(Expression instance, Type enumerableType, string
23232323

23242324
if (type != null && TypeHelper.IsDictionary(type) && _methodFinder.ContainsMethod(type, methodName, false))
23252325
{
2326-
var dictionaryMethod = type.GetMethod(methodName)!;
2327-
if (!PredefinedMethodsHelper.IsPredefinedDictionaryMethod(dictionaryMethod))
2326+
var declaredMethods = type.GetMethods().Where(m =>
2327+
m.DeclaringType == type && m.IsPublic && !m.IsStatic && m.Name.Equals(methodName, StringComparison.OrdinalIgnoreCase));
2328+
if (declaredMethods.Any(m => !PredefinedMethodsHelper.IsPredefinedDictionaryMethod(m)))
23282329
{
2329-
throw ParseError(tokenPos, Res.MethodIsInaccessible, methodName, TypeHelper.GetTypeName(dictionaryMethod.DeclaringType!));
2330+
throw ParseError(tokenPos, Res.MethodIsInaccessible, methodName, TypeHelper.GetTypeName(type));
23302331
}
23312332

2333+
var dictionaryMethod = type.GetMethod(methodName)!;
23322334
expression = Expression.Call(instance, dictionaryMethod, args);
23332335
return true;
23342336
}

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Concurrent;
2+
using System.Collections.Generic;
3+
#if !(NET35 || NET40)
4+
using System.Collections.ObjectModel;
5+
#endif
26
using System.Linq.Dynamic.Core.Validation;
37
using System.Reflection;
48

@@ -11,6 +15,13 @@ internal static class PredefinedMethodsHelper
1115
internal static readonly MethodInfo ObjectStaticEquals = typeof(object).GetMethod(nameof(Equals), BindingFlags.Static | BindingFlags.Public, null, [typeof(object), typeof(object)], null)!;
1216
internal static readonly MethodInfo ObjectStaticReferenceEquals = typeof(object).GetMethod(nameof(ReferenceEquals), BindingFlags.Static | BindingFlags.Public, null, [typeof(object), typeof(object)], null)!;
1317

18+
private static readonly HashSet<string> AllowedDictionaryMethodNames =
19+
[
20+
"ContainsKey",
21+
"ContainsValue",
22+
"TryGetValue",
23+
"get_Item"
24+
];
1425

1526
private static readonly HashSet<MemberInfo> ObjectToStringAndObjectEquals =
1627
[
@@ -20,13 +31,25 @@ internal static class PredefinedMethodsHelper
2031
ObjectStaticReferenceEquals
2132
];
2233

23-
private static readonly HashSet<string> AllowedDictionaryMethodNames = ["ContainsKey", "ContainsValue", "TryGetValue", "get_Item"];
24-
2534
public static bool IsPredefinedDictionaryMethod(MethodInfo method)
2635
{
2736
Check.NotNull(method);
2837

29-
return AllowedDictionaryMethodNames.Contains(method.Name);
38+
var declaringType = method.DeclaringType;
39+
if (!AllowedDictionaryMethodNames.Contains(method.Name) || declaringType == null || !declaringType.GetTypeInfo().IsGenericType)
40+
{
41+
return false;
42+
}
43+
44+
var genericType = declaringType.GetGenericTypeDefinition();
45+
return genericType == typeof(Dictionary<,>) ||
46+
genericType == typeof(ConcurrentDictionary<,>) ||
47+
genericType == typeof(IDictionary<,>)
48+
#if !(NET35 || NET40)
49+
|| genericType == typeof(IReadOnlyDictionary<,>) ||
50+
genericType == typeof(ReadOnlyDictionary<,>)
51+
#endif
52+
;
3053
}
3154

3255
public static bool IsPredefinedMethod(ParsingConfig config, MemberInfo member)

‎test/System.Linq.Dynamic.Core.Tests/QueryableTests.Where.cs‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Concurrent;
2+
using System.Collections.Generic;
23
using System.Diagnostics.CodeAnalysis;
34
using System.Linq.Dynamic.Core.Exceptions;
45
using System.Linq.Dynamic.Core.Tests.Helpers;
@@ -182,6 +183,26 @@ public void Reset(string key)
182183
CallCount++;
183184
this[key] = "reset";
184185
}
186+
187+
public new bool ContainsKey(string key)
188+
{
189+
CallCount++;
190+
this[key] = "changed";
191+
return true;
192+
}
193+
194+
public bool ContainsValue(int value)
195+
{
196+
CallCount++;
197+
return true;
198+
}
199+
200+
public new bool TryGetValue(string key, out string value)
201+
{
202+
CallCount++;
203+
value = "changed";
204+
return true;
205+
}
185206
}
186207

187208
public class DictionaryValidationItem
@@ -194,7 +215,7 @@ public class DictionaryValidationItem
194215

195216
public Dictionary<string, string> Meta { get; set; } = new() { { "k", "v" } };
196217

197-
public System.Collections.Concurrent.ConcurrentDictionary<string, string> C { get; set; } = new();
218+
public ConcurrentDictionary<string, string> C { get; set; } = new();
198219
}
199220

200221
[Theory]
@@ -205,6 +226,9 @@ public class DictionaryValidationItem
205226
[InlineData("C.TryUpdate(\"x\",\"z\",\"y\")")]
206227
[InlineData("Custom.Danger()")]
207228
[InlineData("Custom.Reset(\"k\")")]
229+
[InlineData("Custom.ContainsKey(\"k\")")]
230+
[InlineData("Custom.ContainsValue(1)")]
231+
[InlineData("Custom.TryGetValue(\"k\", out _)")]
208232
public void Where_Dynamic_DictionaryMethods_ShouldThrowParseException_WhenMethodIsNotAccessible(string query)
209233
{
210234
// Arrange
@@ -225,6 +249,23 @@ public void Where_Dynamic_DictionaryMethods_ShouldThrowParseException_WhenMethod
225249
items.Select(i => i.Custom["k"]).Should().OnlyContain(value => value == "v");
226250
}
227251

252+
[Theory]
253+
[InlineData("Meta.ContainsKey(\"k\")")]
254+
[InlineData("Meta.ContainsValue(\"v\")")]
255+
[InlineData("C.ContainsKey(\"k\")")]
256+
public void Where_Dynamic_DictionaryMethods_AllowsStandardReadOperations(string query)
257+
{
258+
// Arrange
259+
var item = new DictionaryValidationItem();
260+
item.C.TryAdd("k", "v");
261+
var items = new[] { item }.AsQueryable();
262+
263+
// Act & Assert
264+
Assert.Same(item, Assert.Single(items.Where(query).ToList()));
265+
Assert.Equal(new KeyValuePair<string, string>("k", "v"), Assert.Single(item.Meta));
266+
Assert.Equal(new KeyValuePair<string, string>("k", "v"), Assert.Single(item.C));
267+
}
268+
228269
[Fact]
229270
public void Where_Dynamic_StringQuoted()
230271
{

0 commit comments

Comments
 (0)