@@ -61,7 +61,7 @@ protected void Translate(Expression? node, bool isSearchCondition = false)
6161 return ;
6262
6363 case QueryParameterExpression { Name : var name , Value : var value } :
64- this . TranslateQueryParameter ( name , value ) ;
64+ this . TranslateQueryParameter ( value ) ;
6565 return ;
6666
6767 case MemberExpression member :
@@ -154,8 +154,10 @@ protected virtual void TranslateConstant(object? value)
154154 this . _sql . Append ( l ) ;
155155 return ;
156156
157- case string s :
158- this . _sql . Append ( '\' ' ) . Append ( s . Replace ( "'" , "''" ) ) . Append ( '\' ' ) ;
157+ case string untrustedInput :
158+ // This is the only place where we allow untrusted input to be passed in, so we need to quote and escape it.
159+ // Luckily for us, values are escaped in the same way for every provider that we support so far.
160+ this . _sql . Append ( '\' ' ) . Append ( untrustedInput . Replace ( "'" , "''" ) ) . Append ( '\' ' ) ;
159161 return ;
160162 case bool b :
161163 this . _sql . Append ( b ? "TRUE" : "FALSE" ) ;
@@ -182,25 +184,26 @@ private void TranslateMember(MemberExpression memberExpression, bool isSearchCon
182184 {
183185 if ( this . TryBindProperty ( memberExpression , out var property ) )
184186 {
185- this . GenerateColumn ( property . StorageName , isSearchCondition ) ;
187+ this . GenerateColumn ( property , isSearchCondition ) ;
186188 return ;
187189 }
188190
189191 throw new NotSupportedException ( $ "Member access for '{ memberExpression . Member . Name } ' is unsupported - only member access over the filter parameter are supported") ;
190192 }
191193
192- protected virtual void GenerateColumn ( string column , bool isSearchCondition = false )
193- => this . _sql . Append ( '"' ) . Append ( column . Replace ( "\" " , "\" \" " ) ) . Append ( '"' ) ;
194+ protected virtual void GenerateColumn ( PropertyModel property , bool isSearchCondition = false )
195+ // StorageName is considered to be a safe input, we quote and escape it mostly to produce valid SQL.
196+ => this . _sql . Append ( '"' ) . Append ( property . StorageName . Replace ( "\" " , "\" \" " ) ) . Append ( '"' ) ;
194197
195- protected abstract void TranslateQueryParameter ( string name , object ? value ) ;
198+ protected abstract void TranslateQueryParameter ( object ? value ) ;
196199
197200 private void TranslateMethodCall ( MethodCallExpression methodCall , bool isSearchCondition = false )
198201 {
199202 switch ( methodCall )
200203 {
201204 // Dictionary access for dynamic mapping (r => r["SomeString"] == "foo")
202205 case MethodCallExpression when this . TryBindProperty ( methodCall , out var property ) :
203- this . GenerateColumn ( property . StorageName , isSearchCondition ) ;
206+ this . GenerateColumn ( property , isSearchCondition ) ;
204207 return ;
205208
206209 // Enumerable.Contains()
@@ -302,7 +305,7 @@ private void TranslateUnary(UnaryExpression unary, bool isSearchCondition)
302305
303306 // Handle convert over member access, for dynamic dictionary access (r => (int)r["SomeInt"] == 8)
304307 case ExpressionType . Convert when this . TryBindProperty ( unary . Operand , out var property ) && unary . Type == property . Type :
305- this . GenerateColumn ( property . StorageName , isSearchCondition ) ;
308+ this . GenerateColumn ( property , isSearchCondition ) ;
306309 return ;
307310
308311 default :
0 commit comments