Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated sql query not sanitizing alias names #518

Closed
kichalla opened this issue Aug 12, 2014 · 1 comment
Closed

Generated sql query not sanitizing alias names #518

kichalla opened this issue Aug 12, 2014 · 1 comment
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-unknown
Milestone

Comments

@kichalla
Copy link
Contributor

In my user administration pages, I was trying to sort users by registered date and I was doing the following:

[HttpGet]
public IActionResult Index()
{
    var model = _userManager.Users.OrderByDescending(user => user.RegisteredDate);

    return View(model);
}

The above resulted in a sql query like (from SQL profiler):

SELECT user.[About], user.[AccessFailedCount], user.[Email], user.[EmailConfirmed], user.[Id], user.[LockoutEnabled], user.[LockoutEnd], user.[Name], user.[NormalizedUserName], user.[PasswordHash], user.[PhoneNumber], user.[PhoneNumberConfirmed], user.[RegisteredDate], user.[SecurityStamp], user.[TwoFactorEnabled], user.[UserName]
FROM [AspNetUsers] AS user
ORDER BY user.[RegisteredDate] DESC

Since user is a reserved word, this resulted in the following exception..after later changing user to something like appUser, it started working fine:

Microsoft.Data.Entity.Storage.DataStoreException: An error occured while running a data store operation. See InnerException for details. ---> System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'user'.
 at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
 at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
 at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
 at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
 at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
 at System.Data.SqlClient.SqlDataReader.get_MetaData()
 at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
 at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
 at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
 at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
 at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
 at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
 at System.Data.Common.DbCommand.ExecuteReader()
 at Microsoft.Data.Entity.Relational.Query.QueryMethodProvider.Enumerable`1.Enumerator.MoveNext()
 at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
 at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
 at Microsoft.Data.Entity.Query.EntityQueryExecutor.EnumerableExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
 --- End of inner exception stack trace ---
 at Microsoft.Data.Entity.Query.EntityQueryExecutor.EnumerableExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()

Should we always sanitize the alias names that we generate..like in this case say [user]?

@divega divega added this to the 1.0.0-beta1 milestone Aug 13, 2014
@maumar
Copy link
Contributor

maumar commented Aug 20, 2014

Fixed in 5a48b4c

@maumar maumar closed this as completed Aug 20, 2014
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
@ajcvickers ajcvickers modified the milestones: 1.0.0-beta1, 1.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-unknown
Projects
None yet
Development

No branches or pull requests

4 participants