Skip to content

Commit 487913f

Browse files
committed
- 完善 DynamicFilter Custom 安全问题;
1 parent bbaf947 commit 487913f

File tree

5 files changed

+224
-2
lines changed

5 files changed

+224
-2
lines changed

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql.Tests/FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,6 +2543,7 @@ public enum ts_dyfilter_enum01_status { staring, stoped, finished }
25432543

25442544
public class DynamicFilterMyCustom
25452545
{
2546+
[DynamicFilterCustom]
25462547
public static string MyRawSql(string value) => value;
25472548

25482549
public static string TupleIn(string value)

FreeSql/FreeSql.xml

Lines changed: 189 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,10 @@ void ParseFilter(DynamicFilterLogic logic, DynamicFilterInfo fi, bool isend)
553553
if (string.IsNullOrWhiteSpace(fiValueCustomArray[0])) throw new ArgumentException("Custom {静态方法名}不能为空,格式:{静态方法名}{空格}{反射信息}");
554554
if (string.IsNullOrWhiteSpace(fiValueCustomArray[1])) throw new ArgumentException("Custom {反射信息}不能为空,格式:{静态方法名}{空格}{反射信息}");
555555
var fiValue1Type = Type.GetType(fiValueCustomArray[1]);
556-
if (fiValue1Type == null) throw new ArgumentException($"Custom 找到对应的{{反射信息}}:{fiValueCustomArray[1]}");
556+
if (fiValue1Type == null) throw new ArgumentException($"Custom 找不到对应的{{反射信息}}:{fiValueCustomArray[1]}");
557557
var fiValue0Method = fiValue1Type.GetMethod(fiValueCustomArray[0], new Type[] { typeof(string) });
558-
if (fiValue0Method == null) throw new ArgumentException($"Custom 找到对应的{{静态方法名}}:{fiValueCustomArray[0]}");
558+
if (fiValue0Method == null) throw new ArgumentException($"Custom 找不到对应的{{静态方法名}}:{fiValueCustomArray[0]}");
559+
if (MethodIsDynamicFilterCustomAttribute(fiValue0Method) == false) throw new ArgumentException($"Custom 对应的{{静态方法名}}:{fiValueCustomArray[0]} 未设置 [DynamicFilterCustomAttribute] 特性");
559560
var fiValue0MethodReturn = fiValue0Method?.Invoke(null, new object[] { fi.Value?.ToString() })?.ToString();
560561
exp = Expression.Call(typeof(SqlExt).GetMethod("InternalRawSql", BindingFlags.NonPublic | BindingFlags.Static), Expression.Constant(fiValue0MethodReturn, typeof(string)));
561562
break;
@@ -693,6 +694,21 @@ bool IsIgnoreFilter(DynamicFilterInfo testFilter)
693694
string.IsNullOrEmpty(testFilter.Value?.ToString());
694695
}
695696
}
697+
static ConcurrentDictionary<MethodInfo, bool> _dicMethodIsDynamicFilterCustomAttribute = new ConcurrentDictionary<MethodInfo, bool>();
698+
static bool MethodIsDynamicFilterCustomAttribute(MethodInfo method) => _dicMethodIsDynamicFilterCustomAttribute.GetOrAdd(method, m =>
699+
{
700+
object[] attrs = null;
701+
try
702+
{
703+
attrs = m.GetCustomAttributes(false).ToArray(); //.net core 反射存在版本冲突问题,导致该方法异常
704+
}
705+
catch { }
706+
707+
var dyattr = attrs?.Where(a => {
708+
return ((a as Attribute)?.TypeId as Type)?.Name == "DynamicFilterCustomAttribute";
709+
}).FirstOrDefault();
710+
return dyattr != null;
711+
});
696712

697713
public TSelect DisableGlobalFilter(params string[] name)
698714
{

FreeSql/Internal/Model/DynamicFilterInfo.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,17 @@ public enum DynamicFilterOperator
131131
/// {<para></para>
132132
/// public class DynamicFilterCustom<para></para>
133133
/// {<para></para>
134+
/// [DynamicFilterCustom]<para></para>
134135
/// public static string RawSql(string value) => value;<para></para>
135136
/// }<para></para>
136137
/// }<para></para>
137138
/// </summary>
138139
Custom
139140
}
141+
142+
/// <summary>
143+
/// 授权 DynamicFilter 支持 Custom 自定义解析
144+
/// </summary>
145+
[AttributeUsage(AttributeTargets.Method)]
146+
public class DynamicFilterCustomAttribute : Attribute { }
140147
}

0 commit comments

Comments
 (0)